-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.go
275 lines (241 loc) · 6.3 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2021 The fallacy Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package fallacy
import (
"errors"
"strings"
"github.com/gobwas/glob"
"golang.org/x/sync/errgroup"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
// the options struct for banning people
type options[T modReq, U modResp] struct {
userID string
// the glob compiled from userID
glb glob.Glob
roomID id.RoomID
members *mautrix.RespJoinedMembers
power *event.PowerLevelsEventContent
// action to take if a joined member matches the userID
action func(id.RoomID, *T) (*U, error)
}
// init ensures that options struct has power levels and joined_members,
// otherwise error out
func (o *options[T, U]) init() error {
if o.members == nil {
m, err := Client.JoinedMembers(o.roomID)
if err != nil {
return err
}
o.members = m
}
if o.power == nil {
p, err := powerLevels(o.roomID)
if err != nil {
return err
}
o.power = p
}
return nil
}
type (
modReq interface {
mautrix.ReqKickUser | mautrix.ReqBanUser
}
modResp interface {
mautrix.RespKickUser | mautrix.RespBanUser
}
)
// globMatch is a generic function to kick or ban joined users matching the glob
// from the room. It returns an error on the first non-nil error.
func (o options[T, U]) globMatch() error {
if err := o.init(); err != nil {
return err
}
lvl := adminLevel(o.power)
var g errgroup.Group
for user := range o.members.Joined {
u := user
if !o.glb.Match(string(u)) || o.power.GetUserLevel(u) >= lvl {
continue
}
g.Go(func() error {
_, err := o.action(o.roomID, &T{
Reason: "u just got globbed",
UserID: u,
})
return err
})
}
return g.Wait()
}
// Interactively action on a user based on whether they are a glob or a MXID.
//
// The glob matching is able to do matching with a literal but structuring it
// this way allows users to preemptively ban problematic users.
func (o options[T, U]) dispatchAction() error {
switch {
case strings.Contains(o.userID, "*"), strings.Contains(o.userID, "?"):
glb, err := glob.Compile(o.userID)
if err != nil {
return err
}
o.glb = glb
return o.globMatch()
case o.userID[0] == '@':
_, err := o.action(o.roomID, &T{UserID: id.UserID(o.userID)})
return err
}
return errNotUser
}
func moderateUser[T modReq, U modResp](roomID id.RoomID, userID string,
f func(id.RoomID, *T) (*U, error)) error {
pl, err := powerLevels(roomID)
if err != nil {
return err
}
if pl.Ban() > pl.GetUserLevel(Client.UserID) {
return errNoPerms
}
jm, err := Client.JoinedMembers(roomID)
if err != nil {
return err
}
opt := options[T, U]{
userID: userID,
roomID: roomID,
power: pl,
members: jm,
action: f,
}
return opt.dispatchAction()
}
func BanUser(body []string, ev event.Event) {
if err := moderateUser(ev.RoomID, body[0], Client.BanUser); err != nil {
sendNotice(ev.RoomID, "banning user failed with", err.Error())
}
}
func KickUser(body []string, ev event.Event) {
if err := moderateUser(ev.RoomID, body[0], Client.KickUser); err != nil {
sendNotice(ev.RoomID, "kicking user failed with", err.Error())
}
}
func (o options[T, U]) processBans(evs map[string]*event.Event) error {
for key, ev := range evs {
r, ok := ev.Content.Raw["recommendation"].(string)
if !ok {
continue
}
e, ok := ev.Content.Raw["entity"].(string)
if !ok {
continue
}
o.userID = e
switch r {
case "m.ban", "org.matrix.mjolnir.ban": // TODO: remove legacy mjolnir ban
_, err := Client.SendStateEvent(o.roomID, event.StatePolicyUser, key, &ev.Content)
if err != nil {
return err
}
if o.dispatchAction() != nil {
return err
}
}
}
return nil
}
func resolveRoom(roomID string) (id.RoomID, error) {
switch roomID[0] {
case '#':
r, err := Client.ResolveAlias(id.RoomAlias(roomID))
if err != nil {
return "", err
}
return r.RoomID, nil
case '!':
return id.RoomID(roomID), nil
}
return "", errInvalidRoom
}
// ImportList imports a banlist from another room.
func ImportList(body []string, ev event.Event) {
pl, err := powerLevels(ev.RoomID)
if err != nil {
sendNotice(ev.RoomID, err.Error())
return
}
lvl := pl.GetEventLevel(event.StatePolicyUser)
if b := pl.Ban(); b > lvl {
lvl = b
}
if lvl > pl.GetUserLevel(Client.UserID) {
sendNotice(ev.RoomID, permsMessage)
return
}
roomID, err := resolveRoom(body[0])
if err != nil {
sendNotice(ev.RoomID, err.Error())
return
}
if roomID == ev.RoomID {
sendNotice(ev.RoomID, "Refusing to import events from this room!")
return
}
_, hs, _ := ev.Sender.ParseAndDecode()
if _, err := Client.JoinRoom(string(roomID), hs, nil); err != nil {
sendNotice(ev.RoomID, "could not join room", roomID.String(), "failed with:", err.Error())
return
}
s, err := Client.State(roomID)
if err != nil {
sendNotice(ev.RoomID, "could not import state from", roomID.String(), "failed with:", err.Error())
return
}
jm, err := Client.JoinedMembers(ev.RoomID)
if err != nil {
sendNotice(ev.RoomID, errMembers.Error(), "failed with", err.Error())
return
}
opt := options[mautrix.ReqBanUser, mautrix.RespBanUser]{
roomID: ev.RoomID,
members: jm,
power: pl,
action: Client.BanUser,
}
if err = opt.processBans(s[event.StatePolicyUser]); err != nil {
sendNotice(ev.RoomID, "processing bans failed with", err.Error())
return
}
opt.processBans(s[event.NewEventType("m.room.rule.user")])
sendNotice(ev.RoomID, "Finished importing list from", body[0])
}
func createBanList(sender id.UserID, room string) (id.RoomID, error) {
display := string(sender)
r, err := Client.GetDisplayName(sender)
if err == nil {
display = r.DisplayName
}
resp, err := Client.CreateRoom(&mautrix.ReqCreateRoom{
Invite: []id.UserID{sender},
Name: room,
PowerLevelOverride: &event.PowerLevelsEventContent{
EventsDefault: 50,
Users: map[id.UserID]int{
sender: 100,
Client.UserID: 50,
},
},
Preset: "trusted_private_chat",
Topic: "ban list created by " + display,
})
return resp.RoomID, err
}
var (
errInvalidRoom = errors.New("not a valid room ID")
errMembers = errors.New("could not fetch joined members")
errNotUser = errors.New("could not ban user, not a valid glob or user id")
errPowerLevels = errors.New("could not fetch power levels")
)