2020-03-13 17:13:03 +00:00
|
|
|
package soju
|
2020-02-06 21:45:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/irc.v3"
|
2020-06-30 07:11:30 +00:00
|
|
|
"strings"
|
2020-02-06 21:45:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
|
|
|
|
if !ch.complete {
|
|
|
|
panic("Tried to forward a partial channel")
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:19:45 +00:00
|
|
|
sendTopic(dc, ch)
|
|
|
|
|
|
|
|
// TODO: rpl_topicwhotime
|
|
|
|
sendNames(dc, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
|
2020-04-16 15:19:00 +00:00
|
|
|
downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
|
2020-02-19 17:25:19 +00:00
|
|
|
|
2020-02-06 21:45:54 +00:00
|
|
|
if ch.Topic != "" {
|
2020-02-17 11:27:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
2020-02-06 21:45:54 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_TOPIC,
|
2020-02-19 17:25:19 +00:00
|
|
|
Params: []string{dc.nick, downstreamName, ch.Topic},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-06 21:45:54 +00:00
|
|
|
} else {
|
2020-02-17 11:27:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
2020-02-06 21:45:54 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_NOTOPIC,
|
2020-02-19 17:25:19 +00:00
|
|
|
Params: []string{dc.nick, downstreamName, "No topic is set"},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-06 21:45:54 +00:00
|
|
|
}
|
2020-03-21 00:24:29 +00:00
|
|
|
}
|
2020-02-06 21:45:54 +00:00
|
|
|
|
2020-03-21 00:24:29 +00:00
|
|
|
func sendNames(dc *downstreamConn, ch *upstreamChannel) {
|
2020-04-16 15:19:00 +00:00
|
|
|
downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
|
2020-03-21 00:24:29 +00:00
|
|
|
|
2020-06-30 07:11:30 +00:00
|
|
|
emptyNameReply := &irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_NAMREPLY,
|
|
|
|
Params: []string{dc.nick, string(ch.Status), downstreamName, ""},
|
|
|
|
}
|
|
|
|
maxLength := maxMessageLength - len(emptyNameReply.String())
|
|
|
|
|
|
|
|
var buf strings.Builder
|
2020-04-30 21:39:59 +00:00
|
|
|
for nick, memberships := range ch.Members {
|
|
|
|
s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
|
2020-02-06 21:45:54 +00:00
|
|
|
|
2020-06-30 07:11:30 +00:00
|
|
|
if buf.Len() != 0 && maxLength < buf.Len()+1+len(s) {
|
|
|
|
// There's not enough space for the next space + nick.
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_NAMREPLY,
|
|
|
|
Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
|
|
|
|
})
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.Len() != 0 {
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
buf.WriteString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.Len() != 0 {
|
2020-02-17 11:27:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
2020-02-06 21:45:54 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_NAMREPLY,
|
2020-06-30 07:11:30 +00:00
|
|
|
Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-06 21:45:54 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:27:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
2020-02-06 21:45:54 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFNAMES,
|
2020-02-19 17:25:19 +00:00
|
|
|
Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-06 21:45:54 +00:00
|
|
|
}
|