From 9513c28208e9f93262fa57ae43d4f247dbece38a Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 5 Apr 2022 10:06:31 +0200 Subject: [PATCH] downstream: unify PRIVMSG/NOTICE and TAGMSG codepaths They are nearly identical, the only difference is that TAGMSG has no text parameter. Inspired from a patch from delthas: https://lists.sr.ht/~emersion/soju-dev/patches/30651 --- downstream.go | 96 +++++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 61 deletions(-) diff --git a/downstream.go b/downstream.go index 843ee93..c825667 100644 --- a/downstream.go +++ b/downstream.go @@ -2424,14 +2424,26 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Command: "WHOIS", Params: params, }) - case "PRIVMSG", "NOTICE": + case "PRIVMSG", "NOTICE", "TAGMSG": var targetsStr, text string - if err := parseMessageParams(msg, &targetsStr, &text); err != nil { - return err + if msg.Command != "TAGMSG" { + if err := parseMessageParams(msg, &targetsStr, &text); err != nil { + return err + } + } else { + if err := parseMessageParams(msg, &targetsStr); err != nil { + return err + } } + tags := copyClientTags(msg.Tags) for _, name := range strings.Split(targetsStr, ",") { + params := []string{name} + if msg.Command != "TAGMSG" { + params = append(params, text) + } + if name == "$"+dc.srv.Config().Hostname || (name == "$*" && dc.network == nil) { // "$" means a server mask follows. If it's the bouncer's // hostname, broadcast the message to all bouncer users. @@ -2451,7 +2463,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Tags: broadcastTags, Prefix: servicePrefix, Command: msg.Command, - Params: []string{name, text}, + Params: params, } dc.srv.forEachUser(func(u *user) { u.events <- eventBroadcast{broadcastMsg} @@ -2464,12 +2476,12 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Tags: msg.Tags.Copy(), Prefix: dc.prefix(), Command: msg.Command, - Params: []string{name, text}, + Params: params, }) continue } - if msg.Command == "PRIVMSG" && casemapASCII(name) == serviceNickCM { + if casemapASCII(name) == serviceNickCM { if dc.caps.IsEnabled("echo-message") { echoTags := tags.Copy() echoTags["time"] = irc.TagValue(formatServerTime(time.Now())) @@ -2477,10 +2489,12 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Tags: echoTags, Prefix: dc.prefix(), Command: msg.Command, - Params: []string{name, text}, + Params: params, }) } - handleServicePRIVMSG(ctx, dc, text) + if msg.Command == "PRIVMSG" { + handleServicePRIVMSG(ctx, dc, text) + } continue } @@ -2497,12 +2511,23 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. if uc.isChannel(upstreamName) { unmarshaledText = dc.unmarshalText(uc, text) } + + upstreamParams := []string{upstreamName} + if msg.Command != "TAGMSG" { + upstreamParams = append(upstreamParams, unmarshaledText) + } + uc.SendMessageLabeled(ctx, dc.id, &irc.Message{ Tags: tags, Command: msg.Command, - Params: []string{upstreamName, unmarshaledText}, + Params: upstreamParams, }) + echoParams := []string{upstreamName} + if msg.Command != "TAGMSG" { + echoParams = append(echoParams, text) + } + echoTags := tags.Copy() echoTags["time"] = irc.TagValue(formatServerTime(time.Now())) if uc.account != "" { @@ -2512,58 +2537,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Tags: echoTags, Prefix: &irc.Prefix{Name: uc.nick}, Command: msg.Command, - Params: []string{upstreamName, text}, - } - uc.produce(upstreamName, echoMsg, dc) - - uc.updateChannelAutoDetach(upstreamName) - } - case "TAGMSG": - var targetsStr string - if err := parseMessageParams(msg, &targetsStr); err != nil { - return err - } - tags := copyClientTags(msg.Tags) - - for _, name := range strings.Split(targetsStr, ",") { - if dc.network == nil && casemapASCII(name) == dc.nickCM { - dc.SendMessage(&irc.Message{ - Tags: msg.Tags.Copy(), - Prefix: dc.prefix(), - Command: "TAGMSG", - Params: []string{name}, - }) - continue - } - - if casemapASCII(name) == serviceNickCM { - continue - } - - uc, upstreamName, err := dc.unmarshalEntity(name) - if err != nil { - return err - } - if !uc.caps.IsEnabled("message-tags") { - continue - } - - uc.SendMessageLabeled(ctx, dc.id, &irc.Message{ - Tags: tags, - Command: "TAGMSG", - Params: []string{upstreamName}, - }) - - echoTags := tags.Copy() - echoTags["time"] = irc.TagValue(formatServerTime(time.Now())) - if uc.account != "" { - echoTags["account"] = irc.TagValue(uc.account) - } - echoMsg := &irc.Message{ - Tags: echoTags, - Prefix: &irc.Prefix{Name: uc.nick}, - Command: "TAGMSG", - Params: []string{upstreamName}, + Params: echoParams, } uc.produce(upstreamName, echoMsg, dc)