soju/bridge.go

91 lines
2.2 KiB
Go
Raw Normal View History

2020-03-13 17:13:03 +00:00
package soju
2020-02-06 21:45:54 +00:00
import (
"strconv"
"strings"
"gopkg.in/irc.v3"
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)
sendNames(dc, ch)
}
func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
2020-02-06 21:45:54 +00:00
if ch.Topic != "" {
dc.SendMessage(&irc.Message{
2020-02-06 21:45:54 +00:00
Prefix: dc.srv.prefix(),
Command: irc.RPL_TOPIC,
Params: []string{dc.nick, downstreamName, ch.Topic},
})
if ch.TopicWho != nil {
topicWho := dc.marshalUserPrefix(ch.conn.network, ch.TopicWho)
topicTime := strconv.FormatInt(ch.TopicTime.Unix(), 10)
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: rpl_topicwhotime,
Params: []string{dc.nick, downstreamName, topicWho.String(), topicTime},
})
}
2020-02-06 21:45:54 +00:00
} else {
dc.SendMessage(&irc.Message{
2020-02-06 21:45:54 +00:00
Prefix: dc.srv.prefix(),
Command: irc.RPL_NOTOPIC,
Params: []string{dc.nick, downstreamName, "No topic is set"},
})
2020-02-06 21:45:54 +00:00
}
}
2020-02-06 21:45:54 +00:00
func sendNames(dc *downstreamConn, ch *upstreamChannel) {
downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
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
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-07-06 09:59:34 +01:00
n := buf.Len() + 1 + len(s)
if buf.Len() != 0 && n > maxLength {
// 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 {
dc.SendMessage(&irc.Message{
2020-02-06 21:45:54 +00:00
Prefix: dc.srv.prefix(),
Command: irc.RPL_NAMREPLY,
Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
})
2020-02-06 21:45:54 +00:00
}
dc.SendMessage(&irc.Message{
2020-02-06 21:45:54 +00:00
Prefix: dc.srv.prefix(),
Command: irc.RPL_ENDOFNAMES,
Params: []string{dc.nick, downstreamName, "End of /NAMES list"},
})
2020-02-06 21:45:54 +00:00
}