source: code/trunk/bridge.go@ 347

Last change on this file since 347 was 346, checked in by hubert, 5 years ago

Send compact channel name lists

This commit resolves sendNames' TODO.

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 if buf.Len() != 0 && maxLength < buf.Len()+1+len(s) {
52 // There's not enough space for the next space + nick.
53 dc.SendMessage(&irc.Message{
54 Prefix: dc.srv.prefix(),
55 Command: irc.RPL_NAMREPLY,
56 Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
57 })
58 buf.Reset()
59 }
60
61 if buf.Len() != 0 {
62 buf.WriteByte(' ')
63 }
64 buf.WriteString(s)
65 }
66
67 if buf.Len() != 0 {
68 dc.SendMessage(&irc.Message{
69 Prefix: dc.srv.prefix(),
70 Command: irc.RPL_NAMREPLY,
71 Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
72 })
73 }
74
75 dc.SendMessage(&irc.Message{
76 Prefix: dc.srv.prefix(),
77 Command: irc.RPL_ENDOFNAMES,
78 Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
79 })
80}
Note: See TracBrowser for help on using the repository browser.