1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "strconv"
|
---|
5 | "strings"
|
---|
6 |
|
---|
7 | "gopkg.in/irc.v3"
|
---|
8 | )
|
---|
9 |
|
---|
10 | func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
|
---|
11 | if !ch.complete {
|
---|
12 | panic("Tried to forward a partial channel")
|
---|
13 | }
|
---|
14 |
|
---|
15 | // RPL_NOTOPIC shouldn't be sent on JOIN
|
---|
16 | if ch.Topic != "" {
|
---|
17 | sendTopic(dc, ch)
|
---|
18 | }
|
---|
19 | sendNames(dc, ch)
|
---|
20 | }
|
---|
21 |
|
---|
22 | func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
|
---|
23 | downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
|
---|
24 |
|
---|
25 | if ch.Topic != "" {
|
---|
26 | dc.SendMessage(&irc.Message{
|
---|
27 | Prefix: dc.srv.prefix(),
|
---|
28 | Command: irc.RPL_TOPIC,
|
---|
29 | Params: []string{dc.nick, downstreamName, ch.Topic},
|
---|
30 | })
|
---|
31 | if ch.TopicWho != nil {
|
---|
32 | topicWho := dc.marshalUserPrefix(ch.conn.network, ch.TopicWho)
|
---|
33 | topicTime := strconv.FormatInt(ch.TopicTime.Unix(), 10)
|
---|
34 | dc.SendMessage(&irc.Message{
|
---|
35 | Prefix: dc.srv.prefix(),
|
---|
36 | Command: rpl_topicwhotime,
|
---|
37 | Params: []string{dc.nick, downstreamName, topicWho.String(), topicTime},
|
---|
38 | })
|
---|
39 | }
|
---|
40 | } else {
|
---|
41 | dc.SendMessage(&irc.Message{
|
---|
42 | Prefix: dc.srv.prefix(),
|
---|
43 | Command: irc.RPL_NOTOPIC,
|
---|
44 | Params: []string{dc.nick, downstreamName, "No topic is set"},
|
---|
45 | })
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | func sendNames(dc *downstreamConn, ch *upstreamChannel) {
|
---|
50 | downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
|
---|
51 |
|
---|
52 | emptyNameReply := &irc.Message{
|
---|
53 | Prefix: dc.srv.prefix(),
|
---|
54 | Command: irc.RPL_NAMREPLY,
|
---|
55 | Params: []string{dc.nick, string(ch.Status), downstreamName, ""},
|
---|
56 | }
|
---|
57 | maxLength := maxMessageLength - len(emptyNameReply.String())
|
---|
58 |
|
---|
59 | var buf strings.Builder
|
---|
60 | for _, entry := range ch.Members.innerMap {
|
---|
61 | nick := entry.originalKey
|
---|
62 | memberships := entry.value.(*memberships)
|
---|
63 | s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
|
---|
64 |
|
---|
65 | n := buf.Len() + 1 + len(s)
|
---|
66 | if buf.Len() != 0 && n > maxLength {
|
---|
67 | // There's not enough space for the next space + nick.
|
---|
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 | buf.Reset()
|
---|
74 | }
|
---|
75 |
|
---|
76 | if buf.Len() != 0 {
|
---|
77 | buf.WriteByte(' ')
|
---|
78 | }
|
---|
79 | buf.WriteString(s)
|
---|
80 | }
|
---|
81 |
|
---|
82 | if buf.Len() != 0 {
|
---|
83 | dc.SendMessage(&irc.Message{
|
---|
84 | Prefix: dc.srv.prefix(),
|
---|
85 | Command: irc.RPL_NAMREPLY,
|
---|
86 | Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
|
---|
87 | })
|
---|
88 | }
|
---|
89 |
|
---|
90 | dc.SendMessage(&irc.Message{
|
---|
91 | Prefix: dc.srv.prefix(),
|
---|
92 | Command: irc.RPL_ENDOFNAMES,
|
---|
93 | Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
|
---|
94 | })
|
---|
95 | }
|
---|