-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin.py
259 lines (209 loc) · 7.21 KB
/
admin.py
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
#!/usr/bin/python
import ast
import json
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
def get_channel_arg(channel, args):
if args and args[0].startswith(('#', '&', '+', '!')):
channel = args[0]
args.pop(0)
return channel
adminActions = {}
def Action(name, helpmsg):
class ActionClass(object):
__slots__ = ('name', 'helpmsg', 'cmd')
def __init__(self, f):
self.name = name
self.helpmsg = helpmsg
self.cmd = f
adminActions[name] = self;
def __call__(self, *args, **kwargs):
return self.cmd(*args, **kwargs)
return ActionClass
@Action("help", "List command help")
def Action_help(bot, user, args):
if args:
for arg in args:
a = adminActions.get(arg)
if a:
bot.msg(user, "{0}:\t{1}".format(a.name, a.helpmsg))
else:
bot.msg(
user,
" ".join(a for a in adminActions))
@Action("verify", "Confirm admin access")
def Action_verify(bot, user, args):
bot.msg(user, "Authentication valid")
@Action("admin", "Adjust user admin access")
def Action_admin(bot, user, args):
if len(args) < 2 and args[0] != "list":
bot.msg(user, "Admin change failed: too few arguments")
return
cmd = args[0]
args = args[1:]
if cmd == "add":
bot.cfg["admins"] = sorted(set(bot.cfg["admins"]) | set(args))
elif cmd in ("remove", "rm"):
bot.cfg["admins"] = sorted(set(bot.cfg["admins"]) - set(args))
elif cmd == "list":
bot.msg(user, str(list(bot.cfg["admins"])))
else:
bot.msg(user, "Admin change failed: unknown action")
bot.rebuild_wildcards()
@Action("ignore", "Adjust ignore list")
def Action_ignore(bot, user, args):
if len(args) < 1:
bot.msg(user, "Ignore change failed: too few arguments")
return
cmd = args[0]
args = args[1:]
if cmd == "add":
bot.cfg["ignore"] = sorted(set(bot.cfg["ignore"]) | set(args))
elif cmd in ("remove", "rm"):
bot.cfg["ignore"] = sorted(set(bot.cfg["ignore"]) - set(args))
elif cmd == "list":
bot.msg(user, str(list(bot.cfg["ignore"])))
else:
bot.msg(user, "Ignore change failed: unknown action")
bot.rebuild_wildcards()
@Action("cfg", "Control a config setting")
def Action_cfg(bot, user, args):
if len(args) == 1:
bot.msg(user, "{0} = {1}".format(args[0], bot.cfg.get(args[0])))
elif len(args) == 2:
newval = ast.literal_eval(args[1])
if type(newval) == type(bot.cfg.get(args[0])):
bot.cfg[args[0]] = newval
# I know, a sad little hack for now.
if args[0] == "savespeed":
bot.saver.stop()
bot.saver.start(newval)
elif args[0] == "report" and len(args) == 3:
newval = ast.literal_eval(args[2])
if type(newval) == type(bot.cfg["report"].get(args[1])):
bot.cfg["report"][args[1]] = newval
else:
bot.msg(user, "Invalid config setting change")
@Action("dump", "Dump database to a file")
def Action_dump(bot, user, args):
bot.reps.dump()
bot.log("Rep file dumped")
@Action("save", "Save all bot information")
def Action_save(bot, user, args):
bot.save()
@Action("load", "Load database from a file")
def Action_load(bot, user, args):
bot.reps.load()
bot.log("Rep file loaded")
@Action("filter", "Remove unused entries")
def Action_filter(bot, user, args):
bot.reps.filter()
bot.log("Filtered zeroed entries")
@Action("clear", "Remove the given names from the system")
def Action_clear(bot, user, args):
if len(args) == 1 and args[0] == "all":
bot.reps.reps = {}
else:
for name in args:
bot.reps.clear(name)
@Action("tell", "Tell a channel rep information for users")
def Action_tell(bot, user, args):
user = get_channel_arg(user, args)
for name in args:
bot.msg(user, bot.reps.tell(name))
@Action("all", "Get all reputations")
def Action_all(bot, user, args):
user = get_channel_arg(user, args)
bot.msg(user, bot.reps.all())
@Action("limit", "Adjust limits")
def Action_limit(bot, user, args):
if len(args) < 2:
bot.msg(user, "Limit change failed: too few arguments")
return
cmd = args[0]
args = args[1:]
if cmd == "rep":
if args:
bot.cfg["replimit"] = int(args[0])
else:
bot.msg(user, "Rep limit: {0}".format(bot.cfg["replimit"]))
elif cmd == "time":
if args:
bot.cfg["timelimit"] = int(args[0])
else:
bot.msg(user, "Time limit: {0}".format(bot.cfg["timelimit"]))
else:
bot.msg(user, "Limit change failed: unknown limit")
@Action("set", "Manually set a user's rep value")
def Action_set(bot, user, args):
if len(args) != 2:
bot.msg(user, "Set failed: incorrect number of arguments")
else:
bot.reps.set(args[0], args[1])
@Action("allow", "Clear rep timeout restrictions for all given users")
def Action_allow(bot, user, args):
for name in args:
bot.users[name] = []
@Action("apply", "Apply the Python dictionary provided to the rep database")
def Action_apply(bot, user, args):
bot.reps.update(json.loads(" ".join(args)))
@Action("term", "Safely terminate RepBot")
def Action_term(bot, user, args):
bot.save()
for chan in bot.cfg["channels"]:
bot.leave(chan, " ".join(args))
bot.quit(" ".join(args))
reactor.stop()
@Action("join", "Join a channel")
def Action_join(bot, user, args):
for chan in args:
bot.join(chan)
bot.cfg["channels"].append(chan)
bot.cfg["channels"] = sorted(set(bot.cfg["channels"]))
@Action("part", "Leave a channel")
def Action_part(bot, user, args):
for chan in args:
bot.leave(chan)
bot.cfg["channels"].remove(chan)
@Action("autoreport", "Automatically report to a channel")
def Action_autoreport(bot, user, args):
channels = bot.cfg["report"]["channels"]
for chan in args:
if chan in bot.loops:
bot.loops.pop(chan).stop()
channels.remove(chan)
else:
bot.loops[chan] = LoopingCall(lambda:bot.report(chan))
bot.loops[chan].start(bot.cfg["report"]["delay"])
channels.append(chan)
@Action("report", "Generate a report")
def Action_report(bot, user, args):
user = get_channel_arg(user, args)
forceFlag = False
if args == ["force"]:
forceFlag = True
elif args:
bot.msg(user, "Report failed: Too many arguments")
return
bot.msg(user, bot.reps.report(forceFlag))
@Action("as", "Spoof a message as a user")
def Action_as(bot, user, args):
if len(args)<2:
bot.msg(user, "as failed: Not enough information")
return
bot.privmsg(args[0],args[0]," ".join(args[1:]))
@Action("say", "Say a message")
def Action_say(bot, user, args):
if len(args) < 2:
bot.msg(user, "Not enough arguments")
bot.msg(args[0], " ".join(args[1:]))
def admin(bot, user, msg):
if not msg.strip():
return
command = msg.split()[0].lower()
args = msg.split()[1:]
action = adminActions.get(command)
if action:
action(bot, user, args)
else:
print "Invalid command {0}".format(command)