From ee8aae7a968bfd4dd46a50ac88030bfca3b8c646 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 3 Apr 2020 21:00:19 +0200 Subject: [PATCH] Add NOTICE messages to ring buffer References: https://todo.sr.ht/~emersion/soju/33 --- downstream.go | 4 ++-- upstream.go | 35 +++++++++++------------------------ 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/downstream.go b/downstream.go index 7a0b248..0a40507 100644 --- a/downstream.go +++ b/downstream.go @@ -720,11 +720,11 @@ func (dc *downstreamConn) runNetwork(net *network, loadHistory bool) { msg = msg.Copy() switch msg.Command { - case "PRIVMSG": + case "PRIVMSG", "NOTICE": msg.Prefix = dc.marshalUserPrefix(uc, msg.Prefix) msg.Params[0] = dc.marshalEntity(uc, msg.Params[0]) default: - panic("expected to consume a PRIVMSG message") + panic(fmt.Sprintf("unexpected %q message", msg.Command)) } dc.SendMessage(msg) diff --git a/upstream.go b/upstream.go index d9f358b..2d4e9bb 100644 --- a/upstream.go +++ b/upstream.go @@ -257,34 +257,21 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { }) return nil case "NOTICE": - uc.logger.Print(msg) - if msg.Prefix.User == "" && msg.Prefix.Host == "" { // server message - uc.forEachDownstream(func(dc *downstreamConn) { - dc.SendMessage(&irc.Message{ - Command: "NOTICE", - Params: msg.Params, - }) - }) + uc.network.ring.Produce(msg) } else { // regular user NOTICE - var nick, text string - if err := parseMessageParams(msg, &nick, &text); err != nil { + var entity, text string + if err := parseMessageParams(msg, &entity, &text); err != nil { return err } - target := nick - if nick == uc.nick { + target := entity + if target == uc.nick { target = msg.Prefix.Name } uc.appendLog(target, msg) - uc.forEachDownstream(func(dc *downstreamConn) { - dc.SendMessage(&irc.Message{ - Prefix: dc.marshalUserPrefix(uc, msg.Prefix), - Command: "NOTICE", - Params: []string{dc.marshalEntity(uc, nick), text}, - }) - }) + uc.network.ring.Produce(msg) } case "CAP": var subCmd string @@ -1139,8 +1126,8 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { return fmt.Errorf("expected a prefix") } - var nick, text string - if err := parseMessageParams(msg, &nick, &text); err != nil { + var entity, text string + if err := parseMessageParams(msg, &entity, &text); err != nil { return err } @@ -1148,13 +1135,13 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { uc.logger.Printf("skipping PRIVMSG from soju's service: %v", msg) break } - if nick == serviceNick { + if entity == serviceNick { uc.logger.Printf("skipping PRIVMSG to soju's service: %v", msg) break } - target := nick - if nick == uc.nick { + target := entity + if target == uc.nick { target = msg.Prefix.Name } uc.appendLog(target, msg)