From c3a6d9c0abf6ac9ffbc130a79a1661997877ba9e Mon Sep 17 00:00:00 2001 From: delthas Date: Fri, 19 Aug 2022 22:39:17 +0200 Subject: Highlight only on word boundaries Preivously, we highlighted the user for any message that contained the nick, even in the middle of a word. Now, we only send a highlight if the user was mentioned in a single word, by detecting word boundaries around the mention of the user. For example, considering user "foo": Previously, we would highlight on "foo" and "foolish". Now, we highlight only on "foo". This was inspired/copied from soju's handling [1]. [1]: https://git.sr.ht/~emersion/soju/tree/5e56cc30c538eb88c21cce732e2e357e80098164/item/irc.go#L473 Fixes: https://todo.sr.ht/~taiite/senpai/97 --- app.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index 35cc3b8..6b5614e 100644 --- a/app.go +++ b/app.go @@ -11,6 +11,7 @@ import ( "sync" "time" "unicode" + "unicode/utf8" "golang.org/x/net/proxy" @@ -1006,14 +1007,40 @@ func isBlackListed(command string) bool { return false } +func isWordBoundary(r rune) bool { + switch r { + case '-', '_', '|': // inspired from weechat.look.highlight_regex + return false + default: + return !unicode.IsLetter(r) && !unicode.IsNumber(r) + } +} + +func isHighlight(text, nick string) bool { + for { + i := strings.Index(text, nick) + if i < 0 { + return false + } + + left, _ := utf8.DecodeLastRuneInString(text[:i]) + right, _ := utf8.DecodeRuneInString(text[i+len(nick):]) + if isWordBoundary(left) && isWordBoundary(right) { + return true + } + + text = text[i+len(nick):] + } +} + // isHighlight reports whether the given message content is a highlight. func (app *App) isHighlight(s *irc.Session, content string) bool { contentCf := s.Casemap(content) if app.highlights == nil { - return strings.Contains(contentCf, s.NickCf()) + return isHighlight(contentCf, s.NickCf()) } for _, h := range app.highlights { - if strings.Contains(contentCf, s.Casemap(h)) { + if isHighlight(contentCf, s.Casemap(h)) { return true } } -- cgit v1.2.3