-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
254 lines (216 loc) · 6.45 KB
/
commands.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
// 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"
"log"
"strings"
"github.com/gobwas/glob"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
const (
// Message to be sent when fallacy does not have sufficient permissions.
permsMessage = "fallacy does not have sufficient permission to perform that action"
// Usage message to be sent when asking for help.
usage = "Hey, check out the usage guide at https://github.com/qua3k/fallacy/blob/main/USAGE.md"
)
var (
errNoPerms = errors.New(permsMessage)
)
type Callback struct {
Function func(command []string, event event.Event)
Min int
}
// Register registers a command with a keyword.
func Register(keyword string, callback Callback) {
lock.Lock()
defer lock.Unlock()
keyword = strings.ToLower(keyword)
if _, ok := handles[keyword]; !ok {
handles[keyword] = []Callback{}
}
handles[keyword] = append(handles[keyword], callback)
}
// notifyListeners notifies listeners of incoming events.
func notifyListeners(command []string, ev event.Event) {
if len(command) < 2 || strings.EqualFold(command[1], "help") {
if _, err := sendReply(ev, usage); err != nil {
log.Println("could not send reply into room, failed with:", err)
}
return
}
if !isAdmin(ev.RoomID, ev.Sender) {
if _, err := sendReply(ev, "shut up ur not admin"); err != nil {
log.Println("could not send reply into room, failed with:", err)
}
return
}
if c, ok := handles[strings.ToLower(command[1])]; ok {
for i := range c {
args := command[2:]
if len(args) < c[i].Min {
sendNotice(ev.RoomID, "not enough arguments!")
continue
}
go c[i].Function(args, ev)
}
return
}
sendNotice(ev.RoomID, command[1]+" is not a valid command!")
}
// sendNotice is a wrapper around Client.SendNotice that logs when sending a
// notice fails.
func sendNotice(roomID id.RoomID, text ...string) (resp *mautrix.RespSendEvent) {
<-limit
resp, err := Client.SendNotice(roomID, strings.Join(text, " "))
if err != nil {
log.Println("could not send notice into room", roomID, "failed with error:", err)
}
return
}
// sendReply sends a message as a reply to another message.
func sendReply(ev event.Event, s string) (*mautrix.RespSendEvent, error) {
<-limit
return Client.SendMessageEvent(ev.RoomID, event.EventMessage, &event.MessageEventContent{
MsgType: event.MsgText,
Body: s,
RelatesTo: &event.RelatesTo{
Type: event.RelReply,
EventID: ev.ID,
},
})
}
func adminLevel(pl *event.PowerLevelsEventContent) (lvl int) {
lvl = pl.Ban()
k, r := pl.Kick(), pl.Redact()
if k < lvl {
lvl = k
}
if r < lvl {
lvl = r
}
return
}
// isAdmin returns whether the user is a room admin by checking ban/kick/redact
// power levels.
func isAdmin(roomID id.RoomID, userID id.UserID) bool {
pl, err := powerLevels(roomID)
if err != nil {
log.Println("fetching power levels event failed!")
return false
}
return pl.GetUserLevel(userID) >= adminLevel(pl)
}
// hasPerms checks whether the fallacy bot has perms.
func hasPerms(roomID id.RoomID, event event.Type) bool {
pl, err := powerLevels(roomID)
if err != nil {
log.Println("fetching power levels event failed with error", err)
return false
}
if pl.GetEventLevel(event) > pl.GetUserLevel(Client.UserID) {
return false
}
return true
}
// BanServer bans a server by adding it to the room ACL.
func BanServer(roomID id.RoomID, homeserver string) (err error) {
if !hasPerms(roomID, event.StateServerACL) {
sendNotice(roomID, permsMessage)
return
}
glb, err := glob.Compile(homeserver)
if err != nil {
sendNotice(roomID, "not a valid glob pattern!")
return
}
_, hs, _ := Client.UserID.Parse()
if !glb.Match(hs) {
sendNotice(roomID, "Refusing to ban own homeserver...")
return
}
acls, err := acls(roomID)
if err != nil {
return
}
for _, s := range acls.Allow {
if s == homeserver {
return
}
}
for _, server := range acls.Deny {
if server == homeserver {
return
}
}
acls.Deny = append(acls.Deny, homeserver)
_, err = Client.SendStateEvent(roomID, event.StateServerACL, "", &acls)
return
}
// MuteUser mutes a target user in a specified room by utilizing power levels.
func MuteUser(body []string, ev event.Event) {
pl, err := powerLevels(ev.RoomID)
if err != nil {
log.Println(err)
return
}
targetID := id.UserID(body[0])
level := pl.GetEventLevel(event.EventMessage)
if pl.GetUserLevel(targetID) < level {
sendNotice(ev.RoomID, "cannot mute a user that is already muted")
return
}
pl.SetUserLevel(targetID, level)
if _, err := Client.SendStateEvent(ev.RoomID, event.StatePowerLevels, "", &pl); err != nil {
sendNotice(ev.RoomID, "could not mute user! failed with: "+err.Error())
return
}
msg := strings.Join([]string{body[0], "was muted by", ev.Sender.String(), "in", ev.RoomID.String()}, " ")
sendNotice(ev.RoomID, msg)
}
// UnmuteUser unmutes a target user in a specified room by utilizing power levels.
func UnmuteUser(body []string, ev event.Event) {
pl, err := powerLevels(ev.RoomID)
if err != nil {
log.Println(err)
return
}
targetID := id.UserID(body[0])
level := pl.GetEventLevel(event.EventMessage)
if pl.GetUserLevel(targetID) >= level {
sendNotice(ev.RoomID, "cannot unmute a user that is not muted")
return
}
pl.SetUserLevel(targetID, level)
if _, err := Client.SendStateEvent(ev.RoomID, event.StatePowerLevels, "", &pl); err != nil {
sendNotice(ev.RoomID, "could not unmute user! failed with: "+err.Error())
return
}
msg := strings.Join([]string{body[0], "was unmuted by", ev.Sender.String(), "in", ev.RoomID.String()}, " ")
sendNotice(ev.RoomID, msg)
}
// PinMessage pins the replied-to event.
func PinMessage(body []string, ev event.Event) {
if !hasPerms(ev.RoomID, event.StatePinnedEvents) {
sendNotice(ev.RoomID, permsMessage)
return
}
relatesTo := ev.Content.AsMessage().RelatesTo
if relatesTo == nil {
sendNotice(ev.RoomID, "Reply to the message you want to pin!")
return
}
p := event.PinnedEventsEventContent{}
// Avoid handling this error. The pinned event may not exist.
Client.StateEvent(ev.RoomID, event.StatePinnedEvents, "", &p)
p.Pinned = append(p.Pinned, relatesTo.EventID)
Client.SendStateEvent(ev.RoomID, event.StatePinnedEvents, "", &p)
}
// SayMessage sends a message into the chat.
func SayMessage(body []string, ev event.Event) {
msg := strings.Join(body, " ")
sendNotice(ev.RoomID, msg)
}