source: code/trunk/bridge.go@ 468

Last change on this file since 468 was 405, checked in by hubert, 5 years ago

Forward RPL_TOPICWHOTIME to downstreams

File size: 2.2 KB
Line 
1package soju
2
3import (
4 "strconv"
5 "strings"
6
7 "gopkg.in/irc.v3"
8)
9
10func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
11 if !ch.complete {
12 panic("Tried to forward a partial channel")
13 }
14
15 sendTopic(dc, ch)
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 if ch.TopicWho != nil {
29 topicWho := dc.marshalUserPrefix(ch.conn.network, ch.TopicWho)
30 topicTime := strconv.FormatInt(ch.TopicTime.Unix(), 10)
31 dc.SendMessage(&irc.Message{
32 Prefix: dc.srv.prefix(),
33 Command: rpl_topicwhotime,
34 Params: []string{dc.nick, downstreamName, topicWho.String(), topicTime},
35 })
36 }
37 } else {
38 dc.SendMessage(&irc.Message{
39 Prefix: dc.srv.prefix(),
40 Command: irc.RPL_NOTOPIC,
41 Params: []string{dc.nick, downstreamName, "No topic is set"},
42 })
43 }
44}
45
46func sendNames(dc *downstreamConn, ch *upstreamChannel) {
47 downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
48
49 emptyNameReply := &irc.Message{
50 Prefix: dc.srv.prefix(),
51 Command: irc.RPL_NAMREPLY,
52 Params: []string{dc.nick, string(ch.Status), downstreamName, ""},
53 }
54 maxLength := maxMessageLength - len(emptyNameReply.String())
55
56 var buf strings.Builder
57 for nick, memberships := range ch.Members {
58 s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
59
60 n := buf.Len() + 1 + len(s)
61 if buf.Len() != 0 && n > maxLength {
62 // There's not enough space for the next space + nick.
63 dc.SendMessage(&irc.Message{
64 Prefix: dc.srv.prefix(),
65 Command: irc.RPL_NAMREPLY,
66 Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
67 })
68 buf.Reset()
69 }
70
71 if buf.Len() != 0 {
72 buf.WriteByte(' ')
73 }
74 buf.WriteString(s)
75 }
76
77 if buf.Len() != 0 {
78 dc.SendMessage(&irc.Message{
79 Prefix: dc.srv.prefix(),
80 Command: irc.RPL_NAMREPLY,
81 Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
82 })
83 }
84
85 dc.SendMessage(&irc.Message{
86 Prefix: dc.srv.prefix(),
87 Command: irc.RPL_ENDOFNAMES,
88 Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
89 })
90}
Note: See TracBrowser for help on using the repository browser.