Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
455 lines (389 loc) · 14.6 KB
/
Copy pathbot.py
File metadata and controls
455 lines (389 loc) · 14.6 KB
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
#!/usr/bin/env python3
"""
bot.py - Phenny IRC Bot
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import asyncio
import collections
import dataclasses
import functools
import importlib
import logging
import os
import re
import sys
import threading
import time
import traceback
import tools
from tools import GrumbleError, decorate, rephrase_errors
logger = logging.getLogger('phenny')
home = os.getcwd()
def module_control(phenny, module, func):
if not hasattr(module, func):
return True
try:
rephrase_errors(getattr(module, func), phenny)
return True
except GrumbleError as e:
desc = str(e)
except Exception as e:
desc = traceback.format_exc()
name = os.path.basename(module.__file__)
logger.error("Error during %s of %s module:\n%s" % (func, name, desc))
return False
@dataclasses.dataclass
class Message:
line: str
source: str = ''
body: str = ''
nick: str = ''
user: str = ''
host: str = ''
text: str = ''
args: tuple = ()
sender: str = ''
command: str = ''
sub_command: str = ''
command_args: str = ''
command_point: str = ''
command_args_list: list = None
source_pattern = re.compile(r'([^!]*)!?([^@]*)@?(.*)')
def __post_init__(self):
if self.line.startswith(':'):
self.source, self.body = self.line[1:].split(' ', 1)
m = self.source_pattern.match(self.source)
self.nick, self.user, self.host = m.groups()
else:
self.body = self.line
if ' :' in self.body:
self.body, self.text = self.body.split(' :', 1)
self.text = self.text.rstrip()
self.args = tuple(self.body.split())
if len(self.args) > 1:
self.sender = self.args[1]
COMMANDS = {}
KEYWORDS = []
EVENTS = collections.defaultdict(list)
def return_msg(ping=True):
def decorator(fn):
@functools.wraps(fn)
def wrapper(bot, msg):
val = fn(bot, msg)
if val:
target = msg.command_point or msg.nick
if not ping or not target:
target = None
bot.msg(msg.sender, val, target=target)
return wrapper
return decorator
def command(commands, admin=False, owner=False, private=False, ping=True,
priority='medium', example=None, thread=False):
def decorator(fn):
@functools.wraps(fn)
@return_msg(ping=ping)
def wrapper(bot, msg):
if private and msg.sender.startswith('#'):
return
if owner and msg.nick != bot.config.owner:
return
if admin and msg.nick not in bot.config.admins and msg.nick != bot.config.owner:
return
return fn(bot, msg)
global COMMANDS
cmd = [commands] if isinstance(commands, str) else commands
for c in cmd:
COMMANDS[c] = wrapper
if example:
wrapper.example = example
if thread:
wrapper.thread = True
return wrapper
return decorator
def keyword(pattern, ping=True, priority='medium'):
def decorator(fn):
@functools.wraps(fn)
@return_msg(ping=ping)
def wrapper(bot, msg):
return fn(bot, msg)
global KEYWORDS
pat = pattern
if isinstance(pattern, str):
pat = re.compile(pat)
KEYWORDS.append((pat, wrapper))
return wrapper
return decorator
def event(etype, admin=False, thread=False):
def decorator(fn):
@functools.wraps(fn)
def wrapper(bot, msg):
if admin and msg.nick not in bot.config.admins and msg.nick != bot.config.owner:
return
return fn(bot, msg)
global EVENTS
evt = [etype] if isinstance(etype, str) else etype
for e in evt:
EVENTS[e].append(wrapper)
if thread:
wrapper.thread = True
return wrapper
return decorator
def every_message(fn):
@functools.wraps(fn)
@event('*')
def wrapper(bot, msg):
if 'PRIVMSG' not in msg.args:
return
fn(bot, msg)
return wrapper
class Phenny:
def __init__(self, config):
self.config = config
self.doc = {}
self.stats = collections.Counter()
self.nick = config.nick
self.channels = config.channels or []
self.setup()
self.sending = threading.RLock()
self.stack = []
np = re.escape(self.nick.lower())
self.command_pattern = re.compile(r'(?i)^(?:'+np+r':\s+|\.|(?P<point1>\w+)[:,]\s+\.)(?P<cmd>\w+)(?:\.(?P<subcmd>\w+))?\s*(?P<args>.*)(?:\s*(?:->|→)\s*(?P<point2>\w+))?$')
def setup(self):
self.variables = {}
filenames = []
if not hasattr(self.config, 'enable'):
for fn in os.listdir(os.path.join(home, 'modules')):
if fn.endswith('.py') and not fn.startswith('_'):
filenames.append(os.path.join(home, 'modules', fn))
else:
for fn in self.config.enable:
filenames.append(os.path.join(home, 'modules', fn + '.py'))
if hasattr(self.config, 'extra'):
for fn in self.config.extra:
if os.path.isfile(fn):
filenames.append(fn)
elif os.path.isdir(fn):
for n in os.listdir(fn):
if n.endswith('.py') and not n.startswith('_'):
filenames.append(os.path.join(fn, n))
tools.setup(self)
modules = {}
excluded_modules = getattr(self.config, 'exclude', [])
for filename in filenames:
name = os.path.basename(filename)[:-3]
if name in excluded_modules: continue
try:
spec = importlib.util.spec_from_file_location(name, filename)
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
except Exception as e:
trace = traceback.format_exc()
logger.error("Error loading %s module:\n%s" % (name, trace))
continue
if module_control(self, module, 'setup'):
self.register(module)
modules[name] = module
self.modules = modules
if modules:
logger.info('Registered modules: ' + ', '.join(sorted(modules.keys())))
else:
logger.warning("Couldn't find any modules")
def run(self, host, port=6667, ssl=False, ipv6=False, ca_certs=None,
ssl_context=None):
asyncio.run(self.main_loop(host, port, ssl))
def prepare_outgoing_message(self, args, text=None):
def safe(arg):
if isinstance(arg, str):
arg = arg.encode('utf-8')
return re.sub(b' ?[\r\n]+', b' ', arg)
line = b' '.join([safe(arg) for arg in args])
if text:
line += b' :' + safe(text)
return line
def write(self, args, text=None):
line = self.prepare_outgoing_message(args, text)
self.sending.acquire()
self.writer.write(line + b'\r\n')
self.sending.release()
def msg(self, recipient, text, target=None):
if target:
text = target + ': ' + text
# Cf. http://swhack.com/logs/2006-03-01#T19-43-25
if isinstance(text, str):
try: text = text.encode('utf-8')
except UnicodeEncodeError as error:
logger.error(str(error))
text = error.__class__ + ': ' + str(error)
if isinstance(recipient, str):
try: recipient = recipient.encode('utf-8')
except UnicodeEncodeError as error:
logger.error(str(error))
return
# Split long messages
if len(text) > tools.max_message_length:
for message in tools.break_up(text, max_count=3):
self.msg(recipient, message)
return
# No messages within the last 3 seconds? Go ahead!
# Otherwise, wait so it's been at least 0.8 seconds + penalty
if self.stack:
elapsed = time.time() - self.stack[-1][0]
if elapsed < 3:
penalty = float(max(0, len(text) - 50)) / 70
wait = 0.8 + penalty
if elapsed < wait:
time.sleep(wait - elapsed)
# Loop detection
messages = [m[1:3] for m in self.stack[-10:]]
if messages.count((recipient, text)) >= 3:
text = '...'
if messages.count((recipient, text)) >= 3:
self.sending.release()
return
self.write(('PRIVMSG', recipient), text)
self.stack.append((time.time(), recipient, text))
self.stack = self.stack[-10:]
def msg_queued(self, recipient, text, queue):
# hook in case we ever need to re-implement flood control
self.msg(recipient, text)
def handle_line(self, line):
try:
line = line.decode('utf-8')
except UnicodeDecodeError:
line = line.decode('iso-8859-1')
msg = Message(line)
self.dispatch(msg)
if msg.args[0] == 'PING':
self.write(('PONG', msg.args[-1]))
def error(self, report):
logger.error(str(traceback.format_exc()))
async def main_loop(self, host, port, ssl):
self.reader, self.writer = await asyncio.open_connection(
host, port, ssl=ssl)
self.write(('NICK', self.config.nick))
self.write(('USER', self.config.nick, '+iw', self.config.name))
if hasattr(self.config, 'password'):
self.write(('PRIVMSG', 'NickServ'),
'IDENTIFY %s' % self.config.password)
for channel in self.config.channels:
self.write(('JOIN', channel))
while True:
line = await self.reader.readline()
if not line:
break
self.handle_line(line)
self.writer.close()
await self.writer.wait_closed()
logger.info('Closed!')
def register(self, module):
# This is used by reload.py, hence it being methodised
if module.__name__ not in self.variables:
self.variables[module.__name__] = {}
for name, obj in vars(module).items():
if hasattr(obj, 'commands') or hasattr(obj, 'rule'):
self.variables[module.__name__][name] = obj
def wrapped(self, message):
sender = message.sender or message.text
if sender == self.nick:
sender = message.nick
delegate = {
'reply': lambda msg, target=message.nick: self.msg(sender, msg, target=target),
'say': lambda msg, target=None: self.msg(sender, msg, target=target),
'say_queued': lambda msg, target=None: self.msg_queued(sender, msg, target=target),
'do': lambda msg: self.action(sender, msg),
}
return decorate(self, delegate)
def call(self, func, msg):
if msg.sender.startswith('#') and hasattr(self.config, 'limit'):
limits = self.config.limit.get(msg.sender)
if limits and func.__module__ not in limits:
return
if hasattr(func, 'thread') and func.thread:
t = threading.Thread(target=self.call_unthreaded,
args=(func, msg), name=func.__name__)
t.start()
else:
self.call_unthreaded(func, msg)
self.stats[(func.__name__, msg.sender)] += 1
self.stats[(func.__name__, msg.nick)] += 1
def call_unthreaded(self, func, msg):
phenny = self.wrapped(msg)
def report(*lines, verbose=True):
for admin in self.config.admins:
if verbose:
self.msg(admin, "Error in '{}/{}' with input '{}'"
.format(func.__module__, func.__name__, input.bytes))
for line in lines:
self.msg(admin, line)
try:
rephrase_errors(func, phenny, msg)
except GrumbleError as e:
report(str(e), verbose=False)
except Exception as e:
self.error(report)
def dispatch(self, msg):
if msg.nick in self.config.ignore:
return
for fn in EVENTS['*']:
self.call(fn, msg)
if msg.args and msg.args[0] in EVENTS:
for fn in EVENTS[msg.args[0]]:
self.call(fn, msg)
if msg.args and msg.args[0] != 'PRIVMSG':
return
m = self.command_pattern.fullmatch(msg.text)
if m:
cmd = m.group('cmd').lower()
if cmd in COMMANDS:
msg.command = cmd
msg.sub_command = m.group('subcmd')
msg.command_args = m.group('args')
msg.command_args_list = msg.command_args.split()
msg.command_point = m.group('point1') or m.group('point2') or None
self.call(COMMANDS[cmd], msg)
return
for pat, fn in KEYWORDS:
if pat.fullmatch(msg.text):
self.call(fn, msg)
def join(self, channels, keys=None):
if isinstance(channels, list):
self.join(','.join(channels), keys)
return
if keys:
if isinstance(keys, list):
self.join(channels, ','.join(keys))
return
self.write(('JOIN', channels, keys))
else:
self.write(('JOIN', channels))
def nick(self, nickname):
self.write(('NICK', nickname))
def notice(self, msgtarget, message):
self.write(('NOTICE', msgtarget), message)
def part(self, channels, message=None):
if isinstance(channels, list):
self.part(','.join(channels), keys)
return
self.write(('PART', channels), message)
def pass_(self, password):
self.write(('PASS', password))
def ping(self, server1, server2=None):
self.write(('PING', server1), server2)
def pong(self, server1, server2=None):
self.write(('PONG', server1), server2)
def privmsg(self, msgtarget, message):
self.write(('PRIVMSG', msgtarget), message)
def quit(self, message=None):
self.write(('QUIT'), message)
def user(self, user, mode, realname):
self.write(('USER', user, mode, '_'), realname)
def action(self, recipient, text):
text = "\x01ACTION {0}\x01".format(text)
return self.msg(recipient, text)
def is_admin(self, nick):
return nick in self.config.admins or nick == self.config.owner
if __name__ == '__main__':
print(__doc__)