]>
Commit | Line | Data |
---|---|---|
1 | # Erebus IRC bot - Author: John Runyon | |
2 | # vim: fileencoding=utf-8 | |
3 | # module helper functions, see modules/modtest.py for usage | |
4 | # This file is released into the public domain; see http://unlicense.org/ | |
5 | ||
6 | import abc | |
7 | import sys | |
8 | import socket | |
9 | from functools import wraps | |
10 | ||
11 | if sys.version_info.major < 3: | |
12 | stringbase = basestring | |
13 | else: | |
14 | stringbase = str | |
15 | ||
16 | """Used to return an error to the bot core.""" | |
17 | class error(object): | |
18 | def __init__(self, desc): | |
19 | self.errormsg = desc | |
20 | def __nonzero__(self): | |
21 | return False #object will test to False | |
22 | __bool__ = __nonzero__ #py3 compat | |
23 | def __repr__(self): | |
24 | return '<modlib.error %r>' % self.errormsg | |
25 | def __str__(self): | |
26 | return str(self.errormsg) | |
27 | ||
28 | class modlib(object): | |
29 | # default (global) access levels | |
30 | OWNER = 100 | |
31 | MANAGER = 99 | |
32 | ADMIN = 75 | |
33 | STAFF = 50 | |
34 | KNOWN = 1 | |
35 | AUTHED = 0 # Users which have are known to be authed | |
36 | ANYONE = -1 # non-authed users have glevel set to -1 | |
37 | IGNORED = -2 # If the user is IGNORED, no hooks or chanhooks will fire for their messages. numhooks can still be fired. | |
38 | glevs = { | |
39 | 'OWNER': OWNER, | |
40 | 'MANAGER': MANAGER, | |
41 | 'ADMIN': ADMIN, | |
42 | 'STAFF': STAFF, | |
43 | 'KNOWN': KNOWN, | |
44 | 'AUTHED': AUTHED, | |
45 | 'ANYONE': ANYONE, | |
46 | 'IGNORED': IGNORED, | |
47 | } | |
48 | ||
49 | # (channel) access levels | |
50 | COWNER = 5 | |
51 | MASTER = 4 | |
52 | OP = 3 | |
53 | VOICE = 2 | |
54 | FRIEND = 1 | |
55 | PUBLIC = 0 # Anyone (use glevel to control whether auth is needed) | |
56 | BANNED = -1 # The default reqclevel is PUBLIC, so any commands which needchan will be ignored from BANNED users unless the command reqclevel=-1 | |
57 | # [ 0 1 2 3 4 5 -1] | |
58 | clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', 'Banned'] | |
59 | ||
60 | # messages | |
61 | WRONGARGS = "Wrong number of arguments." | |
62 | ||
63 | def __init__(self, name): | |
64 | self.Socketlike = Socketlike | |
65 | self.hooks = {} # {command:handler} | |
66 | self.chanhooks = {} # {channel:handler} | |
67 | self.exceptionhooks = [] # [(exception,handler)] | |
68 | self.numhooks = {} # {numeric:handler} | |
69 | self.sockhooks = [] # [(af,ty,address,handler_class)] | |
70 | self.sockets = [] # [(sock,obj)] | |
71 | self.helps = [] | |
72 | self.parent = None | |
73 | ||
74 | self.name = (name.split("."))[-1] | |
75 | ||
76 | def modstart(self, parent): | |
77 | #modstart can return a few things... | |
78 | # None: unspecified success | |
79 | # False: unspecified error | |
80 | # modlib.error (or anything else False-y): specified error | |
81 | # True: unspecified success | |
82 | # non-empty string (or anything else True-y): specified success | |
83 | #"specified" values will be printed. unspecified values will result in "OK" or "failed" | |
84 | self.parent = parent | |
85 | for cmd, func in self.hooks.items(): | |
86 | parent.hook(cmd, func) | |
87 | parent.hook("%s.%s" % (self.name, cmd), func) | |
88 | for chan, func in self.chanhooks.items(): | |
89 | parent.hookchan(chan, func) | |
90 | for exc, func in self.exceptionhooks: | |
91 | parent.hookexception(exc, func) | |
92 | for num, func in self.numhooks.items(): | |
93 | parent.hooknum(num, func) | |
94 | for hookdata in self.sockhooks: | |
95 | self._create_socket(*hookdata) | |
96 | ||
97 | for func, args, kwargs in self.helps: | |
98 | try: | |
99 | self.mod('help').reghelp(func, *args, **kwargs) | |
100 | except: | |
101 | pass | |
102 | return True | |
103 | def modstop(self, parent): | |
104 | for cmd, func in self.hooks.items(): | |
105 | parent.unhook(cmd, func) | |
106 | parent.unhook("%s.%s" % (self.name, cmd), func) | |
107 | for chan, func in self.chanhooks.items(): | |
108 | parent.unhookchan(chan, func) | |
109 | for exc, func in self.exceptionhooks: | |
110 | parent.unhookexception(exc, func) | |
111 | for num, func in self.numhooks.items(): | |
112 | parent.unhooknum(num, func) | |
113 | for sock, obj in self.sockets: | |
114 | self._destroy_socket(sock, obj) | |
115 | ||
116 | for func, args, kwargs in self.helps: | |
117 | try: | |
118 | self.mod('help').dereghelp(func, *args, **kwargs) | |
119 | except: | |
120 | pass | |
121 | return True | |
122 | ||
123 | def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None): | |
124 | if wantchan is None: wantchan = needchan | |
125 | _cmd = cmd #save this since it gets wiped out... | |
126 | def realhook(func): | |
127 | cmd = _cmd #...and restore it | |
128 | if cmd is None: | |
129 | cmd = func.__name__ # default to function name | |
130 | if isinstance(cmd, stringbase): | |
131 | cmd = (cmd,) | |
132 | ||
133 | if clevel > self.PUBLIC and not needchan: | |
134 | raise Exception('clevel must be left at default if needchan is False') | |
135 | ||
136 | func.needchan = needchan | |
137 | func.wantchan = wantchan | |
138 | func.reqglevel = glevel | |
139 | func.reqclevel = clevel | |
140 | func.cmd = cmd | |
141 | func.module = func.__module__.split('.')[1] | |
142 | ||
143 | for c in cmd: | |
144 | self.hooks[c] = func | |
145 | if self.parent is not None: | |
146 | self.parent.hook(c, func) | |
147 | self.parent.hook("%s.%s" % (self.name, c), func) | |
148 | return func | |
149 | return realhook | |
150 | ||
151 | def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC): | |
152 | def realhook(func): | |
153 | self.chanhooks[chan] = func | |
154 | if self.parent is not None: | |
155 | self.parent.hookchan(chan, func) | |
156 | return func | |
157 | return realhook | |
158 | ||
159 | def hookexception(self, exc): | |
160 | def realhook(func): | |
161 | self.exceptionhooks.append((exc, func)) | |
162 | if self.parent is not None: | |
163 | self.parent.hookexception(exc, func) | |
164 | return func | |
165 | return realhook | |
166 | ||
167 | def hooknum(self, num): | |
168 | def realhook(func): | |
169 | self.numhooks[str(num)] = func | |
170 | if self.parent is not None: | |
171 | self.parent.hooknum(str(num), func) | |
172 | return func | |
173 | return realhook | |
174 | ||
175 | def bind(self, bindto, data=None): | |
176 | """Used as a decorator on a class which implements getdata and parse methods. | |
177 | See modules/sockets.py for an example. | |
178 | Takes an arg like: | |
179 | [unix:]/foo/bar | |
180 | [udp|tcp:][ip:]port | |
181 | """ | |
182 | if len(bindto) == 0: | |
183 | raise Exception('bindto must have a value') | |
184 | if bindto[0] == '/': | |
185 | return self._hooksocket(socket.AF_UNIX, socket.SOCK_STREAM, bindto) | |
186 | if len(bindto) > 5 and bindto[0:5] == 'unix:': | |
187 | return self._hooksocket(socket.AF_UNIX, socket.SOCK_STREAM, bindto[5:]) | |
188 | af = socket.AF_INET | |
189 | ty = socket.SOCK_STREAM | |
190 | host = '0.0.0.0' | |
191 | if len(bindto) > 4 and bindto[0:4] == 'udp:': | |
192 | ty = socket.SOCK_DGRAM | |
193 | bindto = bindto[4:] | |
194 | if len(bindto) > 4 and bindto[0:4] == 'tcp:': | |
195 | bindto = bindto[4:] | |
196 | if ':' in bindto: | |
197 | pieces = bindto.rsplit(':', 1) | |
198 | host = pieces[0] | |
199 | bindto = pieces[1] | |
200 | port = int(bindto) | |
201 | return self._hooksocket(af, ty, (host, port), data) | |
202 | ||
203 | def bind_tcp(self, host, port, data=None): | |
204 | return self._hooksocket(socket.AF_INET, socket.SOCK_STREAM, (host, port), data) | |
205 | def bind_udp(self, host, port, data=None): | |
206 | return self._hooksocket(socket.AF_INET, socket.SOCK_DGRAM, (host, port), data) | |
207 | def bind_unix(self, path, data=None): | |
208 | return self._hooksocket(socket.AF_UNIX, socket.SOCK_STREAM, path, data) | |
209 | def _hooksocket(self, af, ty, address, data): | |
210 | def realhook(cls): | |
211 | if not issubclass(cls, Socketlike): | |
212 | raise Exception('Attempted to hook a socket without a class to process data') | |
213 | self.sockhooks.append((af, ty, address, cls, data)) | |
214 | if self.parent is not None: | |
215 | self._create_socket(af, ty, address, cls, data) | |
216 | return cls | |
217 | return realhook | |
218 | def _create_socket(self, af, ty, address, cls, data): | |
219 | ty = ty | socket.SOCK_NONBLOCK | |
220 | sock = socket.socket(af, ty) | |
221 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
222 | sock.bind(address) | |
223 | obj = _ListenSocket(self, sock, cls, data) | |
224 | self.sockets.append((sock,obj)) | |
225 | sock.listen(5) | |
226 | self.parent.newfd(obj, sock.fileno()) | |
227 | self.parent.log(repr(obj), '?', 'Socket ready to accept new connections (%r, %r, %r, %r)' % (af, ty, address, cls)) | |
228 | def _destroy_socket(self, sock, obj): | |
229 | obj.close() | |
230 | ||
231 | def mod(self, modname): | |
232 | if self.parent is not None: | |
233 | return self.parent.module(modname) | |
234 | else: | |
235 | return error('unknown parent') | |
236 | ||
237 | ||
238 | def flags(self, *flags): | |
239 | """Parses out "flags" to a command, like `MODUNLOAD -AUTOLOAD somemodule` | |
240 | @lib.hook() | |
241 | @lib.flags('autoload', 'force') | |
242 | def baz(bot, user, chan, realtarget, flags, *args) | |
243 | ||
244 | Note the added `flags` argument, which will be a dict - in this case `{'autounload':true,'force':false}`.""" | |
245 | def realhook(func): | |
246 | func.flags = [f.lower() for f in flags] | |
247 | ||
248 | @wraps(func) | |
249 | def parseargs(bot, user, chan, realtarget, *_args): | |
250 | args = list(_args) # we need a copy, also need a list, iterate over _args-tuple, mutate args-list | |
251 | found_flags = {f: False for f in flags} | |
252 | for arg in _args: | |
253 | if arg[0] == "-" and len(arg) > 1: | |
254 | found_prefix = None | |
255 | possible_flag = arg[1:].lower() | |
256 | for f in flags: | |
257 | if possible_flag == f: # Exact match? | |
258 | found_flags[possible_flag] = True | |
259 | args.remove(arg) | |
260 | found_prefix = None | |
261 | break | |
262 | elif f.find(possible_flag) == 0: # Is the current word a prefix of a flag? | |
263 | if found_prefix is not None: # Is it also a prefix of another flag? | |
264 | return 'Error: %s is a prefix of multiple flags (%s, %s).' % (possible_flag, found_prefix[1], f) | |
265 | else: | |
266 | found_prefix = (arg,f) | |
267 | if found_prefix is not None: # found (only one) prefix | |
268 | found_flags[found_prefix[1]] = True | |
269 | args.remove(found_prefix[0]) | |
270 | return func(bot, user, chan, realtarget, found_flags, *args) | |
271 | ||
272 | return parseargs | |
273 | return realhook | |
274 | ||
275 | ||
276 | def argsEQ(self, num): | |
277 | def realhook(func): | |
278 | @wraps(func) | |
279 | def checkargs(bot, user, chan, realtarget, *args): | |
280 | adjuster = 0 | |
281 | if hasattr(checkargs, 'flags'): | |
282 | adjuster = 1 | |
283 | if len(args)-adjuster == num: | |
284 | return func(bot, user, chan, realtarget, *args) | |
285 | else: | |
286 | bot.msg(user, self.WRONGARGS) | |
287 | return checkargs | |
288 | return realhook | |
289 | ||
290 | def argsGE(self, num): | |
291 | def realhook(func): | |
292 | @wraps(func) | |
293 | def checkargs(bot, user, chan, realtarget, *args): | |
294 | adjuster = 0 | |
295 | if hasattr(checkargs, 'flags'): | |
296 | adjuster = 1 | |
297 | if len(args)-adjuster >= num: | |
298 | return func(bot, user, chan, realtarget, *args) | |
299 | else: | |
300 | bot.msg(user, self.WRONGARGS) | |
301 | return checkargs | |
302 | return realhook | |
303 | ||
304 | def help(self, *args, **kwargs): | |
305 | """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?) | |
306 | Example: | |
307 | help("<user> <pass>", "login") | |
308 | ^ Help will only be one line. Command name determined based on function name. | |
309 | help("<user> <level>", "add a user", cmd=("adduser", "useradd")) | |
310 | ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser" | |
311 | help(None, "do stuff", "This command is really complicated.") | |
312 | ^ Command takes no args. Short description (in overall HELP listing) is "do stuff". | |
313 | Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated." | |
314 | """ | |
315 | def realhook(func): | |
316 | if self.parent is not None: | |
317 | try: | |
318 | self.mod('help').reghelp(func, *args, **kwargs) | |
319 | except: | |
320 | pass | |
321 | self.helps.append((func, args, kwargs)) | |
322 | return func | |
323 | return realhook | |
324 | ||
325 | class Socketlike(abc.ABC): | |
326 | def __init__(self, sock, data): | |
327 | """This default method saves the socket in self.sock and creates self.buffer for getdata(). The data is discarded.""" | |
328 | self.sock = sock | |
329 | self.buffer = b'' | |
330 | ||
331 | def getdata(self): | |
332 | """This default method gets LF or CRLF separated lines from the socket and returns an array of completely-seen lines to the core. | |
333 | This should work well for most line-based protocols (like IRC).""" | |
334 | recvd = self.sock.recv(8192) | |
335 | if recvd == b"": # EOF | |
336 | if len(self.buffer) != 0: | |
337 | # Process what's left in the buffer. We'll get called again after. | |
338 | remaining_buf = self.buffer.decode('utf-8', 'backslashreplace') | |
339 | self.buffer = b"" | |
340 | return [remaining_buf] | |
341 | else: | |
342 | # Nothing left in the buffer. Return None to signal the core to close this socket. | |
343 | return None | |
344 | self.buffer += recvd | |
345 | lines = [] | |
346 | ||
347 | while b"\n" in self.buffer: | |
348 | pieces = self.buffer.split(b"\n", 1) | |
349 | s = pieces[0].decode('utf-8', 'backslashreplace').rstrip("\r") | |
350 | lines.append(pieces[0].decode('utf-8', 'backslashreplace')) | |
351 | self.buffer = pieces[1] | |
352 | ||
353 | return lines | |
354 | ||
355 | def __str__(self): | |
356 | return '%s#%d' % (self.__class__.__name__, self.sock.fileno()) | |
357 | def __repr__(self): | |
358 | return '<%s.%s #%d %s:%d>' % ((self.__class__.__module__, self.__class__.__name__, self.sock.fileno())+self.sock.getpeername()) | |
359 | ||
360 | @abc.abstractmethod | |
361 | def parse(self, chunk): pass | |
362 | ||
363 | @classmethod | |
364 | def __subclasshook__(cls, C): | |
365 | if cls is Socketlike: | |
366 | if any('parse' in B.__dict__ and 'getdata' in B.__dict__ for B in C.__mro__): | |
367 | return True | |
368 | return NotImplemented | |
369 | ||
370 | class _ListenSocket(Socketlike): | |
371 | def __init__(self, lib, sock, cls, data): | |
372 | self.clients = [] | |
373 | self.lib = lib | |
374 | self.sock = sock | |
375 | self.cls = cls | |
376 | self.data = data | |
377 | ||
378 | def _make_closer(self, obj, client): | |
379 | def close(): | |
380 | self.lib.parent.log(repr(self), '?', 'Closing child socket #%d' % (client.fileno())) | |
381 | try: | |
382 | obj.closing() | |
383 | except AttributeError: | |
384 | pass | |
385 | self.lib.parent.delfd(client.fileno()) | |
386 | client.shutdown(socket.SHUT_RDWR) | |
387 | client.close() | |
388 | self.clients.remove((client,obj)) | |
389 | return close | |
390 | ||
391 | def parse(self): | |
392 | # getdata will never return a non-empty array, so parse will never be called; but Socketlike requires this method | |
393 | pass | |
394 | ||
395 | def getdata(self): | |
396 | client, addr = self.sock.accept() | |
397 | obj = self.cls(client, self.data) | |
398 | obj.close = self._make_closer(obj, client) | |
399 | self.lib.parent.log(repr(self), '?', 'New connection #%d from %s' % (client.fileno(), addr)) | |
400 | self.clients.append((client,obj)) | |
401 | self.lib.parent.newfd(obj, client.fileno()) | |
402 | return [] | |
403 | ||
404 | def close(self): | |
405 | self.lib.parent.log(repr(self), '?', 'Socket closing') | |
406 | if self.sock.fileno() != -1: | |
407 | self.lib.parent.delfd(self.sock.fileno()) | |
408 | self.sock.shutdown(socket.SHUT_RDWR) | |
409 | self.sock.close() | |
410 | for client, obj in self.clients: | |
411 | obj.close() | |
412 | ||
413 | def __repr__(self): return '<_ListenSocket #%d>' % (self.sock.fileno()) |