Forward RPL_TOPICWHOTIME to downstreams

This commit is contained in:
Hubert Hirtz 2020-08-20 10:39:23 +02:00 committed by Simon Ser
parent e740d952ad
commit 81c7e80e0f
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 36 additions and 5 deletions

View File

@ -1,8 +1,10 @@
package soju package soju
import ( import (
"gopkg.in/irc.v3" "strconv"
"strings" "strings"
"gopkg.in/irc.v3"
) )
func forwardChannel(dc *downstreamConn, ch *upstreamChannel) { func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
@ -11,8 +13,6 @@ func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
} }
sendTopic(dc, ch) sendTopic(dc, ch)
// TODO: rpl_topicwhotime
sendNames(dc, ch) sendNames(dc, ch)
} }
@ -25,6 +25,15 @@ func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
Command: irc.RPL_TOPIC, Command: irc.RPL_TOPIC,
Params: []string{dc.nick, downstreamName, ch.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},
})
}
} else { } else {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),

View File

@ -42,7 +42,7 @@ type upstreamChannel struct {
Name string Name string
conn *upstreamConn conn *upstreamConn
Topic string Topic string
TopicWho string TopicWho *irc.Prefix
TopicTime time.Time TopicTime time.Time
Status channelStatus Status channelStatus
modes channelModes modes channelModes
@ -842,6 +842,10 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
ch.Topic = "" ch.Topic = ""
} }
case "TOPIC": case "TOPIC":
if msg.Prefix == nil {
return fmt.Errorf("expected a prefix")
}
var name string var name string
if err := parseMessageParams(msg, &name); err != nil { if err := parseMessageParams(msg, &name); err != nil {
return err return err
@ -852,6 +856,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
} }
if len(msg.Params) > 1 { if len(msg.Params) > 1 {
ch.Topic = msg.Params[1] ch.Topic = msg.Params[1]
ch.TopicWho = msg.Prefix.Copy()
ch.TopicTime = time.Now() // TODO use msg.Tags["time"]
} else { } else {
ch.Topic = "" ch.Topic = ""
} }
@ -983,12 +989,28 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
if err != nil { if err != nil {
return err return err
} }
ch.TopicWho = who firstTopicWhoTime := ch.TopicWho == nil
ch.TopicWho = irc.ParsePrefix(who)
sec, err := strconv.ParseInt(timeStr, 10, 64) sec, err := strconv.ParseInt(timeStr, 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("failed to parse topic time: %v", err) return fmt.Errorf("failed to parse topic time: %v", err)
} }
ch.TopicTime = time.Unix(sec, 0) ch.TopicTime = time.Unix(sec, 0)
if firstTopicWhoTime {
uc.forEachDownstream(func(dc *downstreamConn) {
topicWho := dc.marshalUserPrefix(uc.network, ch.TopicWho)
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: rpl_topicwhotime,
Params: []string{
dc.nick,
dc.marshalEntity(uc.network, ch.Name),
topicWho.String(),
timeStr,
},
})
})
}
case irc.RPL_LIST: case irc.RPL_LIST:
var channel, clients, topic string var channel, clients, topic string
if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil { if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil {