-
Notifications
You must be signed in to change notification settings - Fork 0
/
tormodbot.py
488 lines (418 loc) · 16.4 KB
/
tormodbot.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import weechat
# stdlib imports
import os
import sys
# stuff that comes with tormodbot itself
from config import conf as CONF
import tmb_mod.liberaham
# other modules/packages
import tmb_util.cmdqueue as cmd_q
import help as tmb_help
from tmb_util import chanserv
from tmb_util import userlist
from tmb_util.msg import notice, join, mode, reconnect, oper_w_eval, close,\
disconnect
from tmb_util.lcsv import lcsv
from tmb_util.userstr import UserStr
w = weechat
SCRIPT_NAME = 'tormodbot'
SCRIPT_AUTHOR = 'Matt Traudt <[email protected]>'
SCRIPT_VERSION = '0.1.0'
SCRIPT_LICENSE = 'MIT'
SCRIPT_DESC = 'Help Tor Project moderate their many channels'
CONNECTED = False
#: Weechat timer hook on our event for delayed on-connect actions
CONNECTED_TIMER_HOOK = None
#: How long to wait, in seconds, before doing delayed on-connect actions
CONNECTED_DELAY_SECS = 5
#: All modules, even those that are disabled
MODULES = []
def log(s, *a, **kw):
# log to core window
w.prnt('', w.prefix('error') + s.format(*a, **kw))
# log to log channel
if not log_chan():
return
notice(log_chan(), s, *a, **kw)
def serv():
''' Returns the configured server '''
return CONF['serv']
def my_nick():
''' Returns my current nick on the configured server '''
return w.info_get('irc_nick', serv())
def cmd_chan():
s = CONF['cmd_chan']
return s.lower() if s else None
def masters():
''' Returns the list of my currently configured masters. Nicks are
normalized to lowercase '''
return lcsv(CONF['masters'].lower())
def ignores():
''' Returns the list of nicks which we ignore all PRIVMSG and NOTICE. Nicks
are normalized to lowercase. '''
return lcsv(CONF['ignores'].lower())
def mod_chans():
''' Returns the list of my currently configured channels to moderate. Chans
are normalized to lowercase. '''
return lcsv(CONF['mod_chans'].lower())
def log_chan():
''' Return the currently configured logging channel, or None if not
configured. Chan is normalized to lowercase '''
s = CONF['log_chan']
return s.lower() if s else None
def nickserv_user():
''' Returns UserStr of the configured nickserv '''
return UserStr(CONF['nickserv_userstr'])
def chanserv_user():
''' Returns UserStr of the configured chanserv '''
# return UserStr('[email protected]')
return UserStr(CONF['chanserv_userstr'])
def _homedir():
''' Returns weechat's home directory '''
return w.info_get('weechat_dir', '')
def datadir():
''' Returns tormodbot's data directory '''
return os.path.join(_homedir(), 'tmb_data')
def codedir():
''' Returns the directory in which this file resides '''
return os.path.abspath(os.path.dirname(__file__))
def code_url():
return CONF['code_url']
def liberaham_url():
return CONF['liberaham_url']
def timer_cb(data, remaining_calls):
''' Whenever a timer expires, this function should be called. If data is
set, then it was that module that set a timer that expired, so we should
hand control off to it. Otherwise it was us.
'''
if data == 'cmd_q':
return cmd_q.timer_cb()
elif data == 'userlist':
return userlist.timer_cb()
elif data == 'chanserv':
return chanserv.timer_cb()
elif data == 'connected':
return delayed_connect_cb()
log(
'timer_cb called with empty or unrecognized data arg "{}", so don\'t '
'know who to tell about this.', data)
return w.WEECHAT_RC_OK
def delayed_connect_cb():
# oper up so we can force ourselves to be chanop in all our chans
log(
'Trying to oper up with secure username "oper_user" and secure '
'password "oper_pw". Make sure these are set with /secure.')
oper_w_eval('${sec.data.oper_user}', '${sec.data.oper_pw}')
# set god mode so we can definitely +o ourselves in all our mod chans
mode(my_nick(), '+S')
# make sure we're in all the chans for modding, and for logging
for c in mod_chans():
join(c)
if log_chan():
join(log_chan())
if cmd_chan():
join(cmd_chan())
# make sure we're op in all the mod chans, and force the mode to +Mz
for c in mod_chans():
mode(c, '+o', my_nick())
mode(c, '+Mz')
# unset god mode
mode(my_nick(), '-S')
# make sure we know about all users in all chans
userlist.connect_cb()
return w.WEECHAT_RC_OK
def connected_cb(data, signal, signal_data):
''' Callback for when we have (dis)connected to a server '''
# data: empty
# signal: "irc_server_connected" or "irc_server_disconnected"
# signal_data: "oftc"
global CONNECTED
global CONNECTED_TIMER_HOOK
CONNECTED = signal == "irc_server_connected"
log('We are {}connected to {}', '' if CONNECTED else 'not ', signal_data)
# If we have just connected, wait a little bit before doing anything to
# hopefully win the identify-to-nickserv race. Yes this race still exists
# on OFTC with CertFP.
if CONNECTED:
if CONNECTED_TIMER_HOOK:
w.unhook(CONNECTED_TIMER_HOOK)
CONNECTED_TIMER_HOOK = None
log(
'Sleeping {} seconds before doing delayed on-connect actions',
CONNECTED_DELAY_SECS)
CONNECTED_TIMER_HOOK = w.hook_timer(
int(CONNECTED_DELAY_SECS * 1000), # interval, num ms
0, # align_second, don't care
1, # call once, we'll schedule ourselves again
# Function to call. Since this is tormodbot.py, it is actually the
# timer_cb in this file. We still need to specify callback_data,
# however.
'timer_cb',
'connected') # callback_data
return w.WEECHAT_RC_OK
def join_cb(data, signal, signal_data):
''' Callback for when we see a JOIN '''
# signal is for example: "freenode,irc_in2_join"
# signal_data is IRC message, for example: ":nick!user@host JOIN :#channel"
data = w.info_get_hashtable('irc_message_parse', {'message': signal_data})
user, chan = UserStr(data['host']), data['channel']
userlist.join_cb(user, chan)
# Tell all da modules
global MODULES
for mod in [m for m in MODULES if m.enabled()]:
if mod.enabled():
mod.join_cb(user, chan)
return w.WEECHAT_RC_OK
def part_cb(data, signal, signal_data):
''' Callback for when we see a PART '''
# signal is for example: "freenode,irc_in2_part"
# signal_data is IRC message, for example: ":nick!user@host PART :#channel"
data = w.info_get_hashtable('irc_message_parse', {'message': signal_data})
user, chan = UserStr(data['host']), data['channel']
userlist.part_cb(user, chan)
return w.WEECHAT_RC_OK
def _handle_command_mod(dest, chan, who):
''' Handle the 'mod' command from masters, sending any response messages to
dest. '''
if not chan:
notice(dest, lcsv(mod_chans()))
return
chans = set(mod_chans())
chans.add(chan)
w.config_set_plugin('mod_chans', lcsv(chans))
notice(dest, 'Okay. mod_chans={}', lcsv(chans))
log('{} told me to start modding {}', who, chan)
mode(my_nick(), '+S')
join(chan)
mode(chan, '+o', my_nick())
mode(chan, '+Mz')
mode(my_nick(), '-S')
def _handle_command_unmod(dest, chan, who):
chans = set(mod_chans())
if chan not in chans:
notice(dest, 'Not modding {}', chan)
return
chans.remove(chan)
w.config_set_plugin('mod_chans', lcsv(chans))
notice(dest, 'Okay. mod_chans={}', lcsv(chans))
log('{} told me to stop modding {}', who, chan)
close(chan)
def handle_command(user, where, message):
''' UserStr *user* sent us str *message* that maybe should be treated as a
command. The caller DID verified this user has permission to command us
and that they sent us the message in a proper place. The caller does NOT
verify that the message is a valid command. The str *where* indicates the
place where we we got it: either '#channel' if the cmd channel, or our own
nick. '''
# If it came in as a PM: *where* is our own nick and any response should go
# to the user's nick. If it came in via the command channel: any response
# should go to the command channel
dest = user.nick if where != cmd_chan() else cmd_chan()
words = message.split()
if not len(words):
return w.WEECHAT_RC_OK
if words[0].lower() == 'ping':
notice(
dest, 'pong' if where == my_nick() else user.nick + ': pong')
return w.WEECHAT_RC_OK
elif words[0].lower() == 'reconnect':
notice(dest, 'Okay. Be right back!')
reconnect(serv())
return w.WEECHAT_RC_OK
elif words[0].lower() == 'help':
return tmb_help.handle_command(user, where, message)
elif words[0].lower() == 'mod':
chan = words[1].lower() if len(words) == 2 else None
_handle_command_mod(dest, chan, user.nick)
return w.WEECHAT_RC_OK
elif words[0].lower() == 'unmod':
if len(words) != 2:
notice(dest, 'Provide one channel name')
return w.WEECHAT_RC_OK
chan = words[1].lower()
_handle_command_unmod(dest, chan, user.nick)
return w.WEECHAT_RC_OK
elif words[0].lower() == 'die':
notice(dest, 'Okay. I won\'t be back. Sorry if I was bad :\'(')
disconnect(serv())
return w.WEECHAT_RC_OK
# This function should NOT assume that the given message contains a valid
# command.
return w.WEECHAT_RC_OK
def privmsg_cb(data, signal, signal_data):
''' Callback for when we see a PRIVMSG '''
# signal is for example: "oftc,irc_raw_in2_PRIVMSG"
# signal_data is for example:
# ":nick!~user@host PRIVMSG #chan :the message" (if sent to a channel)
# ":nick!~user@host PRIVMSG mynick :the message" (if sent to us)
#############
# Parse data
#############
# remove leading ':'
assert signal_data.startswith(':')
signal_data = signal_data[1:]
# parse out user that sent this message
user, signal_data = signal_data.split(' ', 1)
user = UserStr(user)
# trim cruft
assert signal_data.startswith('PRIVMSG ')
signal_data = signal_data[len('PRIVMSG '):]
# get the place to which the user sent this message
dest, signal_data = signal_data.split(' ', 1)
# @#channel is a statusmsg to chanops in #channel, called 'opmod' by OFTC's
# ircd (pre-solanum, aka "hybrid"ish). Remove the leading '@' if it exists
# and remember that this is an opmod message
if dest.startswith('@#'):
dest = dest[1:]
is_opmod = True
else:
is_opmod = False
# trim leading ':'
assert signal_data.startswith(':')
signal_data = signal_data[1:]
# the message that was sent
message = signal_data.strip()
#######################
# Determine what to do
#######################
# If it is a user to ignore, ignore them
if user.nick in ignores():
# log('Ignore PRIVMSG from {} in {}', user.nick, dest)
return w.WEECHAT_RC_OK
# Try handling the message as a command if it's from a master in a PM or in
# the cmd_chan, or respond with the canned auto response if non-master and
# PM. Non-masters in our cmd_chan should be ignored.
#
# A master's PM may not be a command, so it is wrong to return early here.
# At least, that's what I wrote before, but now we're doign it. Lol fuck
# me.
if dest == my_nick() or dest == cmd_chan():
# handle commands from masters
if user.nick in masters():
handle_command(user, dest, message)
# it's a non-master, if a PM, then do canned response
elif dest == my_nick():
notice(
user.nick, 'I am a bot operated by OFTC netops (mostly '
'pastly) that blocks the "libera hamradio" spam before '
'channels see it. For more information, see {} or ask about '
'me in #oftc.', liberaham_url())
return w.WEECHAT_RC_OK
# If it came in on something other than a moderated channel (e.g. cmd_chan
# or PM), ignore it
if dest not in mod_chans() + [my_nick(), cmd_chan()]:
return w.WEECHAT_RC_OK
# Tell our modules about this message
global MODULES
for mod in [m for m in MODULES if m.enabled()]:
mod.privmsg_cb(user, dest, message, is_opmod)
return w.WEECHAT_RC_OK
def notice_cb(data, signal, signal_data):
''' Callback for when we see a NOTICE '''
# signal is for example: "oftc,irc_raw_in2_NOTICE"
# signal_data is for example:
# ":dacia.oftc.net NOTICE pastly :Activating Cloak: example.com ->
# foo.oftc.net for foo"
# ":nick!user@host NOTICE #channel :some messge"
#############
# Parse data
#############
# remove leading ':'
assert signal_data.startswith(':')
signal_data = signal_data[1:]
# parse out who sent this message. It could be a 'n!u@h' str, but it could
# also be an IRC server if we are an op
sender, signal_data = signal_data.split(' ', 1)
sender = sender.lower()
# trim cruft
assert signal_data.startswith('NOTICE ')
signal_data = signal_data[len('NOTICE '):]
# get the place to which the user sent this message (will always be us?)
receiver, signal_data = signal_data.split(' ', 1)
receiver = receiver.lower()
# trim leading ':'
assert signal_data.startswith(':')
signal_data = signal_data[1:]
# the message that was sent
message = signal_data.strip()
#######################
# Determine what to do
#######################
# If it is a user to ignore, ignore them.
if '!' in sender and sender[:sender.index('!')].lower() in ignores():
log('Ignore NOTICE from {}', sender)
return w.WEECHAT_RC_OK
global MODULES
for mod in [m for m in MODULES if m.enabled()]:
if mod.enabled():
mod.notice_cb(sender, receiver, message)
return w.WEECHAT_RC_OK
def config_cb(data, option, value):
''' Called whenever the user changes some script options '''
# set the new value
prefix = 'plugins.var.python.' + SCRIPT_NAME + '.'
option = option[len(prefix):]
CONF[option] = value
# make sure we're in all the right chans for modding
for c in mod_chans():
join(c)
return w.WEECHAT_RC_OK
def infolist_len(ilist):
''' Takes an infolist that has a cursor already at the beginning. Count the
number of items in it. Return cursor to beginning. Return number of items
in infolist. '''
count = 0
while w.infolist_next(ilist):
# log('%s' % (w.infolist_fields(ilist),))
n = w.infolist_string(ilist, 'name')
h = w.infolist_string(ilist, 'host')
a = w.infolist_string(ilist, 'prefixes')
log('{n}@{h} "{a}"', n=n, h=h, a=a)
count += 1
w.infolist_reset_item_cursor(ilist)
return count
if __name__ == '__main__':
if not w.register(
SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
SCRIPT_DESC, '', ''):
exit(1)
# ensure files/dirs exist
w.mkdir(datadir(), 0o755)
# set default options
for opt, def_val in CONF.items():
if not w.config_is_set_plugin(opt):
w.config_set_plugin(opt, def_val)
else:
CONF[opt] = w.config_get_plugin(opt)
# (re)init systems
cmd_q.initialize(int(CONF['msg_burst']), float(CONF['msg_rate'])/1000)
# tmb_mod.faq.initialize()
# tmb_mod.hello.initialize()
chanserv.initialize()
userlist.initialize()
# create modules
if not len(MODULES):
MODULES = [
tmb_mod.liberaham.LiberaHamModule(),
]
for mod in [m for m in MODULES if m.enabled()]:
mod.initialize()
w.hook_signal('irc_server_connected', 'connected_cb', '')
w.hook_signal('irc_server_disconnected', 'connected_cb', '')
w.hook_signal('*,irc_raw_in2_JOIN', 'join_cb', '')
w.hook_signal('*,irc_raw_in2_PART', 'part_cb', '')
w.hook_signal('*,irc_raw_in2_PRIVMSG', 'privmsg_cb', '')
w.hook_signal('*,irc_raw_in2_NOTICE', 'notice_cb', '')
w.hook_config('plugins.var.python.' + SCRIPT_NAME + '.*', 'config_cb', '')
# count = 0
# ilist = w.infolist_get('irc_nick', '', '%s,%s' % (log_serv, log_chan))
# count = infolist_len(ilist)
# log('%d nicks' % (count,))
# w.infolist_free(ilist)
s = '{} v{} (re)loaded'.format(SCRIPT_NAME, SCRIPT_VERSION)
log(s)
s = 'Using: Python {}'.format(sys.version.split('\n')[0])
log(s)
# for opt, def_val in CONF.items():
# log('{} => {}', opt, CONF[opt])
# vim: ts=4 sw=4 et