From 4c22cf316933613215e83f70a6bb2c556d42e02c Mon Sep 17 00:00:00 2001 From: eta Date: Mon, 15 Jun 2020 17:59:22 +0100 Subject: feat(clbot): insert Unicode ZWS to avoid pinging users Change-Id: I732bf6daa01f5b2099c4f4cbdd21a2ceedc79cb1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/385 Reviewed-by: q3k Reviewed-by: lukegb --- fun/clbot/clbot.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'fun') diff --git a/fun/clbot/clbot.go b/fun/clbot/clbot.go index b7bfa0d961..222ce07788 100644 --- a/fun/clbot/clbot.go +++ b/fun/clbot/clbot.go @@ -63,7 +63,9 @@ func callOnShutdown(f func()) { shutdownFuncs = append(shutdownFuncs, f) } -const ignoreBotChar = "\xe2\x80\x8b" +// Unicode U+200B zero-width-space, to avoid triggering other bots +// or highlighting people on IRC. +const zeroWidthSpace = "\u200b" func runIRC(ctx context.Context, ircCfg irc.ClientConfig, sendMsg <-chan string) { bo := backoffutil.NewDefaultBackOff() @@ -100,7 +102,7 @@ func runIRC(ctx context.Context, ircCfg irc.ClientConfig, sendMsg <-chan string) return case msg := <-sendMsg: log.Infof("sending message %q to %v", msg, *ircChannel) - ircClient.Writef("PRIVMSG %s :%s%s", *ircChannel, ignoreBotChar, msg) + ircClient.Writef("PRIVMSG %s :%s%s", *ircChannel, zeroWidthSpace, msg) } } }() @@ -132,6 +134,12 @@ func username(p gerritevents.PatchSet) string { return "UNKNOWN USER" } +// noping inserts a Unicode zero-width space between the first and rest characters of `user` +// in an effort to avoid pinging that user on IRC. +func noping(user string) string { + return fmt.Sprintf("%s%s%s", user[0], zeroWidthSpace, user[1:]) +} + func patchSetURL(c gerritevents.Change, p gerritevents.PatchSet) string { return fmt.Sprintf("https://cl.tvl.fyi/%d", c.Number) } @@ -190,12 +198,12 @@ func main() { if e.Change.Project != "depot" || e.Change.Branch != "master" || e.PatchSet.Number != 1 { continue } - parsedMsg = fmt.Sprintf("CL/%d: %q proposed by %s - %s", e.Change.Number, e.Change.Subject, username(e.PatchSet), patchSetURL(e.Change, e.PatchSet)) + parsedMsg = fmt.Sprintf("CL/%d: %q proposed by %s - %s", e.Change.Number, e.Change.Subject, noping(username(e.PatchSet)), patchSetURL(e.Change, e.PatchSet)) case *gerritevents.ChangeMerged: if e.Change.Project != "depot" || e.Change.Branch != "master" { continue } - parsedMsg = fmt.Sprintf("CL/%d: %q submitted by %s - %s", e.Change.Number, e.Change.Subject, username(e.PatchSet), patchSetURL(e.Change, e.PatchSet)) + parsedMsg = fmt.Sprintf("CL/%d: %q submitted by %s - %s", e.Change.Number, e.Change.Subject, noping(username(e.PatchSet)), patchSetURL(e.Change, e.PatchSet)) } if parsedMsg != "" { sendMsgChan <- parsedMsg -- cgit 1.4.1