From 19854b7ec79b2b0039ed88dc39e135a033746588 Mon Sep 17 00:00:00 2001 From: delthas Date: Tue, 21 Apr 2020 22:54:45 +0200 Subject: [PATCH] Unmarshal nicks in texts of PRIVMSG and NOTICE from downstreams When writing a PRIVMSG or NOTICE on a channel, it is very common to use autocompletion to mention other users on that channel. When using soju in multi-network mode, all users will have their nicked suffixed by `/network`. This suffix should be removed before sending it upstream. This adds support for removing all `/network` suffixes in messages sent to a channel of that network. --- downstream.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/downstream.go b/downstream.go index 318461b..745db51 100644 --- a/downstream.go +++ b/downstream.go @@ -199,6 +199,14 @@ func (dc *downstreamConn) unmarshalEntity(name string) (*upstreamConn, string, e return conn, name, nil } +func (dc *downstreamConn) unmarshalText(uc *upstreamConn, text string) string { + if dc.upstream() != nil { + return text + } + // TODO: smarter parsing that ignores URLs + return strings.ReplaceAll(text, "/"+uc.network.GetName(), "") +} + func (dc *downstreamConn) readMessages(ch chan<- event) error { for { msg, err := dc.ReadMessage() @@ -1233,9 +1241,13 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error { dc.handleNickServPRIVMSG(uc, text) } + unmarshaledText := text + if uc.isChannel(upstreamName) { + unmarshaledText = dc.unmarshalText(uc, text) + } uc.SendMessage(&irc.Message{ Command: "PRIVMSG", - Params: []string{upstreamName, text}, + Params: []string{upstreamName, unmarshaledText}, }) echoMsg := &irc.Message{ @@ -1261,9 +1273,13 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error { return err } + unmarshaledText := text + if uc.isChannel(upstreamName) { + unmarshaledText = dc.unmarshalText(uc, text) + } uc.SendMessage(&irc.Message{ Command: "NOTICE", - Params: []string{upstreamName, text}, + Params: []string{upstreamName, unmarshaledText}, }) } case "INVITE":