| 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 |
"encoding/gob" |
| 11 |
|
| 12 |
"maunium.net/go/mautrix/event" |
| 13 |
"maunium.net/go/mautrix/id" |
| 14 |
"maunium.net/go/mautrix/pushrules/glob" |
| 15 |
) |
| 16 |
|
| 17 |
func init() { |
| 18 |
gob.Register(PushRuleArray{}) |
| 19 |
gob.Register(PushRuleMap{}) |
| 20 |
} |
| 21 |
|
| 22 |
type PushRuleCollection interface { |
| 23 |
GetActions(room Room, evt *event.Event) PushActionArray |
| 24 |
} |
| 25 |
|
| 26 |
type PushRuleArray []*PushRule |
| 27 |
|
| 28 |
func (rules PushRuleArray) SetType(typ PushRuleType) PushRuleArray { |
| 29 |
for _, rule := range rules { |
| 30 |
rule.Type = typ |
| 31 |
} |
| 32 |
return rules |
| 33 |
} |
| 34 |
|
| 35 |
func (rules PushRuleArray) GetActions(room Room, evt *event.Event) PushActionArray { |
| 36 |
for _, rule := range rules { |
| 37 |
if !rule.Match(room, evt) { |
| 38 |
continue |
| 39 |
} |
| 40 |
return rule.Actions |
| 41 |
} |
| 42 |
return nil |
| 43 |
} |
| 44 |
|
| 45 |
type PushRuleMap struct { |
| 46 |
Map map[string]*PushRule |
| 47 |
Type PushRuleType |
| 48 |
} |
| 49 |
|
| 50 |
func (rules PushRuleArray) SetTypeAndMap(typ PushRuleType) PushRuleMap { |
| 51 |
data := PushRuleMap{ |
| 52 |
Map: make(map[string]*PushRule), |
| 53 |
Type: typ, |
| 54 |
} |
| 55 |
for _, rule := range rules { |
| 56 |
rule.Type = typ |
| 57 |
data.Map[rule.RuleID] = rule |
| 58 |
} |
| 59 |
return data |
| 60 |
} |
| 61 |
|
| 62 |
func (ruleMap PushRuleMap) GetActions(room Room, evt *event.Event) PushActionArray { |
| 63 |
var rule *PushRule |
| 64 |
var found bool |
| 65 |
switch ruleMap.Type { |
| 66 |
case RoomRule: |
| 67 |
rule, found = ruleMap.Map[string(evt.RoomID)] |
| 68 |
case SenderRule: |
| 69 |
rule, found = ruleMap.Map[string(evt.Sender)] |
| 70 |
} |
| 71 |
if found && rule.Match(room, evt) { |
| 72 |
return rule.Actions |
| 73 |
} |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
func (ruleMap PushRuleMap) Unmap() PushRuleArray { |
| 78 |
array := make(PushRuleArray, len(ruleMap.Map)) |
| 79 |
index := 0 |
| 80 |
for _, rule := range ruleMap.Map { |
| 81 |
array[index] = rule |
| 82 |
index++ |
| 83 |
} |
| 84 |
return array |
| 85 |
} |
| 86 |
|
| 87 |
type PushRuleType string |
| 88 |
|
| 89 |
const ( |
| 90 |
OverrideRule PushRuleType = "override" |
| 91 |
ContentRule PushRuleType = "content" |
| 92 |
RoomRule PushRuleType = "room" |
| 93 |
SenderRule PushRuleType = "sender" |
| 94 |
UnderrideRule PushRuleType = "underride" |
| 95 |
) |
| 96 |
|
| 97 |
type PushRule struct { |
| 98 |
// The type of this rule. |
| 99 |
Type PushRuleType `json:"-"` |
| 100 |
// The ID of this rule. |
| 101 |
// For room-specific rules and user-specific rules, this is the room or user ID (respectively) |
| 102 |
// For other types of rules, this doesn't affect anything. |
| 103 |
RuleID string `json:"rule_id"` |
| 104 |
// The actions this rule should trigger when matched. |
| 105 |
Actions PushActionArray `json:"actions"` |
| 106 |
// Whether this is a default rule, or has been set explicitly. |
| 107 |
Default bool `json:"default"` |
| 108 |
// Whether or not this push rule is enabled. |
| 109 |
Enabled bool `json:"enabled"` |
| 110 |
// The conditions to match in order to trigger this rule. |
| 111 |
// Only applicable to generic underride/override rules. |
| 112 |
Conditions []*PushCondition `json:"conditions,omitempty"` |
| 113 |
// Pattern for content-specific push rules |
| 114 |
Pattern string `json:"pattern,omitempty"` |
| 115 |
} |
| 116 |
|
| 117 |
func (rule *PushRule) Match(room Room, evt *event.Event) bool { |
| 118 |
if !rule.Enabled { |
| 119 |
return false |
| 120 |
} |
| 121 |
switch rule.Type { |
| 122 |
case OverrideRule, UnderrideRule: |
| 123 |
return rule.matchConditions(room, evt) |
| 124 |
case ContentRule: |
| 125 |
return rule.matchPattern(room, evt) |
| 126 |
case RoomRule: |
| 127 |
return id.RoomID(rule.RuleID) == evt.RoomID |
| 128 |
case SenderRule: |
| 129 |
return id.UserID(rule.RuleID) == evt.Sender |
| 130 |
default: |
| 131 |
return false |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func (rule *PushRule) matchConditions(room Room, evt *event.Event) bool { |
| 136 |
for _, cond := range rule.Conditions { |
| 137 |
if !cond.Match(room, evt) { |
| 138 |
return false |
| 139 |
} |
| 140 |
} |
| 141 |
return true |
| 142 |
} |
| 143 |
|
| 144 |
func (rule *PushRule) matchPattern(room Room, evt *event.Event) bool { |
| 145 |
pattern, err := glob.Compile(rule.Pattern) |
| 146 |
if err != nil { |
| 147 |
return false |
| 148 |
} |
| 149 |
msg, ok := evt.Content.Raw["body"].(string) |
| 150 |
if !ok { |
| 151 |
return false |
| 152 |
} |
| 153 |
return pattern.MatchString(msg) |
| 154 |
} |