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 | sendTopic(dc, ch)
|
---|
16 | sendNames(dc, ch)
|
---|
17 | }
|
---|
18 |
|
---|
19 | func 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 |
|
---|
46 | func 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 _, entry := range ch.Members.innerMap {
|
---|
58 | nick := entry.originalKey
|
---|
59 | memberships := entry.value.(*memberships)
|
---|
60 | s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
|
---|
61 |
|
---|
62 | n := buf.Len() + 1 + len(s)
|
---|
63 | if buf.Len() != 0 && n > maxLength {
|
---|
64 | // There's not enough space for the next space + nick.
|
---|
65 | dc.SendMessage(&irc.Message{
|
---|
66 | Prefix: dc.srv.prefix(),
|
---|
67 | Command: irc.RPL_NAMREPLY,
|
---|
68 | Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
|
---|
69 | })
|
---|
70 | buf.Reset()
|
---|
71 | }
|
---|
72 |
|
---|
73 | if buf.Len() != 0 {
|
---|
74 | buf.WriteByte(' ')
|
---|
75 | }
|
---|
76 | buf.WriteString(s)
|
---|
77 | }
|
---|
78 |
|
---|
79 | if buf.Len() != 0 {
|
---|
80 | dc.SendMessage(&irc.Message{
|
---|
81 | Prefix: dc.srv.prefix(),
|
---|
82 | Command: irc.RPL_NAMREPLY,
|
---|
83 | Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
|
---|
84 | })
|
---|
85 | }
|
---|
86 |
|
---|
87 | dc.SendMessage(&irc.Message{
|
---|
88 | Prefix: dc.srv.prefix(),
|
---|
89 | Command: irc.RPL_ENDOFNAMES,
|
---|
90 | Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
|
---|
91 | })
|
---|
92 | }
|
---|