source: code/trunk/bridge.go@ 365

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

Make length check clearer in sendNames

File size: 1.9 KB
Line 
1package soju
2
3import (
4 "gopkg.in/irc.v3"
5 "strings"
6)
7
8func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
9 if !ch.complete {
10 panic("Tried to forward a partial channel")
11 }
12
13 sendTopic(dc, ch)
14
15 // TODO: rpl_topicwhotime
16 sendNames(dc, ch)
17}
18
19func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
20 downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
21
22 if ch.Topic != "" {
23 dc.SendMessage(&irc.Message{
24 Prefix: dc.srv.prefix(),
25 Command: irc.RPL_TOPIC,
26 Params: []string{dc.nick, downstreamName, ch.Topic},
27 })
28 } else {
29 dc.SendMessage(&irc.Message{
30 Prefix: dc.srv.prefix(),
31 Command: irc.RPL_NOTOPIC,
32 Params: []string{dc.nick, downstreamName, "No topic is set"},
33 })
34 }
35}
36
37func sendNames(dc *downstreamConn, ch *upstreamChannel) {
38 downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
39
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
48 for nick, memberships := range ch.Members {
49 s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
50
51 n := buf.Len() + 1 + len(s)
52 if buf.Len() != 0 && n > maxLength {
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 {
69 dc.SendMessage(&irc.Message{
70 Prefix: dc.srv.prefix(),
71 Command: irc.RPL_NAMREPLY,
72 Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
73 })
74 }
75
76 dc.SendMessage(&irc.Message{
77 Prefix: dc.srv.prefix(),
78 Command: irc.RPL_ENDOFNAMES,
79 Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
80 })
81}
Note: See TracBrowser for help on using the repository browser.