source: code/trunk/bridge.go@ 360

Last change on this file since 360 was 349, checked in by contact, 5 years ago

Make length check clearer in sendNames

File size: 1.9 KB
RevLine 
[98]1package soju
[32]2
3import (
4 "gopkg.in/irc.v3"
[346]5 "strings"
[32]6)
7
8func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
9 if !ch.complete {
10 panic("Tried to forward a partial channel")
11 }
12
[160]13 sendTopic(dc, ch)
14
15 // TODO: rpl_topicwhotime
16 sendNames(dc, ch)
17}
18
19func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
[260]20 downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
[69]21
[32]22 if ch.Topic != "" {
[54]23 dc.SendMessage(&irc.Message{
[32]24 Prefix: dc.srv.prefix(),
25 Command: irc.RPL_TOPIC,
[69]26 Params: []string{dc.nick, downstreamName, ch.Topic},
[54]27 })
[32]28 } else {
[54]29 dc.SendMessage(&irc.Message{
[32]30 Prefix: dc.srv.prefix(),
31 Command: irc.RPL_NOTOPIC,
[69]32 Params: []string{dc.nick, downstreamName, "No topic is set"},
[54]33 })
[32]34 }
[140]35}
[32]36
[140]37func sendNames(dc *downstreamConn, ch *upstreamChannel) {
[260]38 downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
[140]39
[346]40 emptyNameReply := &irc.Message{
41 Prefix: dc.srv.prefix(),
42 Command: irc.RPL_NAMREPLY,
43 Params: []string{dc.nick, string(ch.Status), downstreamName, ""},
44 }
45 maxLength := maxMessageLength - len(emptyNameReply.String())
46
47 var buf strings.Builder
[292]48 for nick, memberships := range ch.Members {
49 s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
[32]50
[349]51 n := buf.Len() + 1 + len(s)
52 if buf.Len() != 0 && n > maxLength {
[346]53 // There's not enough space for the next space + nick.
54 dc.SendMessage(&irc.Message{
55 Prefix: dc.srv.prefix(),
56 Command: irc.RPL_NAMREPLY,
57 Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
58 })
59 buf.Reset()
60 }
61
62 if buf.Len() != 0 {
63 buf.WriteByte(' ')
64 }
65 buf.WriteString(s)
66 }
67
68 if buf.Len() != 0 {
[54]69 dc.SendMessage(&irc.Message{
[32]70 Prefix: dc.srv.prefix(),
71 Command: irc.RPL_NAMREPLY,
[346]72 Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
[54]73 })
[32]74 }
75
[54]76 dc.SendMessage(&irc.Message{
[32]77 Prefix: dc.srv.prefix(),
78 Command: irc.RPL_ENDOFNAMES,
[69]79 Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
[54]80 })
[32]81}
Note: See TracBrowser for help on using the repository browser.