forked from mutantmonkey/phenny
-
Notifications
You must be signed in to change notification settings - Fork 42
/
bot.py
299 lines (242 loc) · 10 KB
/
bot.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
#!/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 importlib
import irc
import logging
import os
import re
import sys
import threading
import traceback
import tools
from tools import GrumbleError, decorate, rephrase_errors
logger = logging.getLogger('phenny')
home = os.getcwd()
def decode(bytes):
if type(bytes) == str:
return bytes
try:
text = bytes.decode('utf-8')
except UnicodeDecodeError:
try:
text = bytes.decode('iso-8859-1')
except UnicodeDecodeError:
text = bytes.decode('cp1252')
except AttributeError:
return bytes
return text
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
class Phenny(irc.Bot):
def __init__(self, config):
args = (config.nick, config.name, config.channels, config.password)
irc.Bot.__init__(self, *args)
self.config = config
self.doc = {}
self.stats = {}
self.setup()
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:
module_loader = importlib.machinery.SourceFileLoader(name, filename)
module = module_loader.load_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")
self.bind_commands()
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 bind(self, module, name, func, regexp):
# register documentation
if not hasattr(func, 'name'):
func.name = func.__name__
if func.__doc__:
if hasattr(func, 'example'):
example = func.example
example = example.replace('$nickname', self.nick)
else: example = None
self.doc[func.name] = (func.__doc__, example)
commands = self.commands[func.priority]
keys = []
if func.point:
keys.append('(?!.*(?:->|→))' + regexp + '()')
keys.append(regexp + '\s(?:->|→)\s(\S*)')
else:
keys.append(regexp)
for key in keys:
key = re.compile(key)
commands.setdefault(key, []).append(func)
def bind_command(self, module, name, func):
logger.debug("Binding module '{:}' command '{:}'".format(module, name))
defaults = {
'priority': 'medium',
'thread': True,
'point': False,
'event': 'PRIVMSG',
}
for key, value in defaults.items():
if not hasattr(func, key):
setattr(func, key, value)
func.event = func.event.upper()
def sub(pattern, self=self):
# These replacements have significant order
pattern = pattern.replace('$nickname', re.escape(self.nick))
return pattern.replace('$nick', r'%s[,:] +' % re.escape(self.nick))
if hasattr(func, 'rule'):
if isinstance(func.rule, str):
pattern = sub(func.rule)
regexp = pattern
self.bind(module, name, func, regexp)
if isinstance(func.rule, tuple):
# 1) e.g. ('$nick', '(.*)')
if len(func.rule) == 2 and isinstance(func.rule[0], str):
prefix, pattern = func.rule
prefix = sub(prefix)
regexp = prefix + pattern
self.bind(module, name, func, regexp)
# 2) e.g. (['p', 'q'], '(.*)')
elif len(func.rule) == 2 and isinstance(func.rule[0], list):
prefix = self.config.prefix
commands, pattern = func.rule
command = r'(?:%s)(?: +(%s))?' % ('|'.join(commands), pattern)
regexp = prefix + command
self.bind(module, name, func, regexp)
# 3) e.g. ('$nick', ['p', 'q'], '(.*)')
elif len(func.rule) == 3:
prefix, commands, pattern = func.rule
prefix = sub(prefix)
command = r'(?:%s) +' % '|'.join(commands)
regexp = prefix + command + pattern
self.bind(module, name, func, regexp)
if hasattr(func, 'commands'):
template = r'(?:%s)(?: +(.+))?'
pattern = template % '|'.join(func.commands)
regexp = self.config.prefix + pattern
self.bind(module, name, func, regexp)
def bind_commands(self):
self.commands = {'high': {}, 'medium': {}, 'low': {}}
for module, functions in self.variables.items():
for name, func in functions.items():
self.bind_command(module, name, func)
def wrapped(self, origin, text, match):
sender = origin.sender or text
delegate = {
'reply': lambda msg, target=origin.nick: self.msg(sender, msg, target=target),
'say': lambda msg, target=None: self.msg(sender, msg, target=target),
'do': lambda msg: self.action(sender, msg),
}
return decorate(self, delegate)
def input(self, origin, text, match, args):
class CommandInput(str):
def __new__(cls, text, origin, match, args):
s = str.__new__(cls, text)
s.sender = decode(origin.sender)
s.nick = decode(origin.nick)
s.bytes = text
s.group = match.group
s.groups = match.groups
s.args = args
s.owner = s.nick == self.config.owner
s.admin = (s.nick in self.config.admins) or s.owner
return s
return CommandInput(text, origin, match, args)
def call(self, func, origin, phenny, input):
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, input)
except GrumbleError as e:
report(str(e), verbose=False)
except Exception as e:
self.error(report)
def limit(self, origin, func):
if origin.sender and origin.sender.startswith('#'):
if hasattr(self.config, 'limit'):
limits = self.config.limit.get(origin.sender)
if limits and (func.__module__ not in limits):
return True
return False
def dispatch(self, origin, args, text):
event = args[0]
if origin.nick in self.config.ignore:
return
for priority in ('high', 'medium', 'low'):
items = list(self.commands[priority].items())
for regexp, funcs in items:
for func in funcs:
if event != func.event and func.event != '*': continue
match = regexp.fullmatch(text)
if not match: continue
if self.limit(origin, func): continue
phenny = self.wrapped(origin, text, match)
input = self.input(origin, text, match, args)
if func.thread:
targs = (func, origin, phenny, input)
t = threading.Thread(target=self.call, args=targs, name=func.name)
t.start()
else:
self.call(func, origin, phenny, input)
for source in [decode(origin.sender), decode(origin.nick)]:
try:
self.stats[(func.name, source)] += 1
except KeyError:
self.stats[(func.name, source)] = 1
if __name__ == '__main__':
print(__doc__)