| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package pushrules |
| 8 |
|
| 9 |
import ( |
| 10 |
"regexp" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
"unicode" |
| 14 |
|
| 15 |
"maunium.net/go/mautrix/event" |
| 16 |
"maunium.net/go/mautrix/pushrules/glob" |
| 17 |
) |
| 18 |
|
| 19 |
// Room is an interface with the functions that are needed for processing room-specific push conditions |
| 20 |
type Room interface { |
| 21 |
GetOwnDisplayname() string |
| 22 |
GetMemberCount() int |
| 23 |
} |
| 24 |
|
| 25 |
// PushCondKind is the type of a push condition. |
| 26 |
type PushCondKind string |
| 27 |
|
| 28 |
// The allowed push condition kinds as specified in https://spec.matrix.org/v1.2/client-server-api/#conditions-1 |
| 29 |
const ( |
| 30 |
KindEventMatch PushCondKind = "event_match" |
| 31 |
KindContainsDisplayName PushCondKind = "contains_display_name" |
| 32 |
KindRoomMemberCount PushCondKind = "room_member_count" |
| 33 |
) |
| 34 |
|
| 35 |
// PushCondition wraps a condition that is required for a specific PushRule to be used. |
| 36 |
type PushCondition struct { |
| 37 |
// The type of the condition. |
| 38 |
Kind PushCondKind `json:"kind"` |
| 39 |
// The dot-separated field of the event to match. Only applicable if kind is EventMatch. |
| 40 |
Key string `json:"key,omitempty"` |
| 41 |
// The glob-style pattern to match the field against. Only applicable if kind is EventMatch. |
| 42 |
Pattern string `json:"pattern,omitempty"` |
| 43 |
// The condition that needs to be fulfilled for RoomMemberCount-type conditions. |
| 44 |
// A decimal integer optionally prefixed by ==, <, >, >= or <=. Prefix "==" is assumed if no prefix found. |
| 45 |
MemberCountCondition string `json:"is,omitempty"` |
| 46 |
} |
| 47 |
|
| 48 |
// MemberCountFilterRegex is the regular expression to parse the MemberCountCondition of PushConditions. |
| 49 |
var MemberCountFilterRegex = regexp.MustCompile("^(==|[<>]=?)?([0-9]+)$") |
| 50 |
|
| 51 |
// Match checks if this condition is fulfilled for the given event in the given room. |
| 52 |
func (cond *PushCondition) Match(room Room, evt *event.Event) bool { |
| 53 |
switch cond.Kind { |
| 54 |
case KindEventMatch: |
| 55 |
return cond.matchValue(room, evt) |
| 56 |
case KindContainsDisplayName: |
| 57 |
return cond.matchDisplayName(room, evt) |
| 58 |
case KindRoomMemberCount: |
| 59 |
return cond.matchMemberCount(room) |
| 60 |
default: |
| 61 |
return false |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func (cond *PushCondition) matchValue(room Room, evt *event.Event) bool { |
| 66 |
index := strings.IndexRune(cond.Key, '.') |
| 67 |
key := cond.Key |
| 68 |
subkey := "" |
| 69 |
if index > 0 { |
| 70 |
subkey = key[index+1:] |
| 71 |
key = key[0:index] |
| 72 |
} |
| 73 |
|
| 74 |
pattern, err := glob.Compile(cond.Pattern) |
| 75 |
if err != nil { |
| 76 |
return false |
| 77 |
} |
| 78 |
|
| 79 |
switch key { |
| 80 |
case "type": |
| 81 |
return pattern.MatchString(evt.Type.String()) |
| 82 |
case "sender": |
| 83 |
return pattern.MatchString(string(evt.Sender)) |
| 84 |
case "room_id": |
| 85 |
return pattern.MatchString(string(evt.RoomID)) |
| 86 |
case "state_key": |
| 87 |
if evt.StateKey == nil { |
| 88 |
return cond.Pattern == "" |
| 89 |
} |
| 90 |
return pattern.MatchString(*evt.StateKey) |
| 91 |
case "content": |
| 92 |
val, _ := evt.Content.Raw[subkey].(string) |
| 93 |
return pattern.MatchString(val) |
| 94 |
default: |
| 95 |
return false |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func (cond *PushCondition) matchDisplayName(room Room, evt *event.Event) bool { |
| 100 |
displayname := room.GetOwnDisplayname() |
| 101 |
if len(displayname) == 0 { |
| 102 |
return false |
| 103 |
} |
| 104 |
|
| 105 |
msg, ok := evt.Content.Raw["body"].(string) |
| 106 |
if !ok { |
| 107 |
return false |
| 108 |
} |
| 109 |
|
| 110 |
isAcceptable := func(r uint8) bool { |
| 111 |
return unicode.IsSpace(rune(r)) || unicode.IsPunct(rune(r)) |
| 112 |
} |
| 113 |
length := len(displayname) |
| 114 |
for index := strings.Index(msg, displayname); index != -1; index = strings.Index(msg, displayname) { |
| 115 |
if (index <= 0 || isAcceptable(msg[index-1])) && (index+length >= len(msg) || isAcceptable(msg[index+length])) { |
| 116 |
return true |
| 117 |
} |
| 118 |
msg = msg[index+len(displayname):] |
| 119 |
} |
| 120 |
return false |
| 121 |
} |
| 122 |
|
| 123 |
func (cond *PushCondition) matchMemberCount(room Room) bool { |
| 124 |
group := MemberCountFilterRegex.FindStringSubmatch(cond.MemberCountCondition) |
| 125 |
if len(group) != 3 { |
| 126 |
return false |
| 127 |
} |
| 128 |
|
| 129 |
operator := group[1] |
| 130 |
wantedMemberCount, _ := strconv.Atoi(group[2]) |
| 131 |
|
| 132 |
memberCount := room.GetMemberCount() |
| 133 |
|
| 134 |
switch operator { |
| 135 |
case "==", "": |
| 136 |
return memberCount == wantedMemberCount |
| 137 |
case ">": |
| 138 |
return memberCount > wantedMemberCount |
| 139 |
case ">=": |
| 140 |
return memberCount >= wantedMemberCount |
| 141 |
case "<": |
| 142 |
return memberCount < wantedMemberCount |
| 143 |
case "<=": |
| 144 |
return memberCount <= wantedMemberCount |
| 145 |
default: |
| 146 |
// Should be impossible due to regex. |
| 147 |
return false |
| 148 |
} |
| 149 |
} |