From 754f8e493df64d6532ace194f3e75c9ccf5bfd95 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 16 Jun 2019 21:25:41 +0300 Subject: Only ping if displayname has spaces/punctuation characters around it. Fixes #96 --- matrix/pushrules/condition.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/matrix/pushrules/condition.go b/matrix/pushrules/condition.go index 12fd3f3..0b7776a 100644 --- a/matrix/pushrules/condition.go +++ b/matrix/pushrules/condition.go @@ -20,6 +20,7 @@ import ( "regexp" "strconv" "strings" + "unicode" "maunium.net/go/mautrix" @@ -116,7 +117,19 @@ func (cond *PushCondition) matchDisplayName(room Room, event *mautrix.Event) boo if member == nil { return false } - return strings.Contains(event.Content.Body, member.Displayname) + + msg := event.Content.Body + isAcceptable := func(r uint8) bool { + return unicode.IsSpace(rune(r)) || unicode.IsPunct(rune(r)) + } + length := len(member.Displayname) + for index := strings.Index(msg, member.Displayname); index != -1; index = strings.Index(msg, member.Displayname) { + if (index <= 0 || isAcceptable(msg[index-1])) && (index + length >= len(msg) || isAcceptable(msg[index+length])) { + return true + } + msg = msg[index+len(member.Displayname):] + } + return false } func (cond *PushCondition) matchMemberCount(room Room, event *mautrix.Event) bool { -- cgit v1.2.3