]> jfr.im git - erebus.git/blob - modlib.py
Added numeric handlers.
[erebus.git] / modlib.py
1 # Erebus IRC bot - Author: John Runyon
2 # module helper functions, see modules/modtest.py for usage
3 # This file is released into the public domain; see http://unlicense.org/
4
5 class error(object):
6 def __init__(self, desc):
7 self.errormsg = desc
8 def __nonzero__(self):
9 return False #object will test to False
10 def __repr__(self):
11 return '<modlib.error %r>' % self.errormsg
12 def __str__(self):
13 return self.errormsg
14
15 class modlib(object):
16 # default (global) access levels
17 MANAGER = 100
18 ADMIN = 75
19 STAFF = 50
20 AUTHED = 0
21 ANYONE = -1
22
23 # (channel) access levels
24 OWNER = -10
25 MASTER = -8 #master is {-8,-9}
26 OP = -5 #op is {-5,-6,-7}
27 VOICE = -4
28 KNOWN = -3
29 PUBLIC = -2 #anyone (use glevel to control auth-needed)
30
31 # messages
32 WRONGARGS = "Wrong number of arguments."
33
34 def __init__(self, name):
35 self.hooks = {}
36 self.parent = None
37
38 self.name = name
39
40 def modstart(self, parent):
41 self.parent = parent
42 for cmd, func in self.hooks.iteritems():
43 self.parent.hook(cmd, func)
44 return True
45 def modstop(self, parent):
46 for cmd, func in self.hooks.iteritems():
47 self.parent.unhook(cmd, func)
48 return True
49
50 def hook(self, cmd, needchan=True, glevel=ANYONE, clevel=PUBLIC):
51 def realhook(func):
52 func.needchan = needchan
53 func.reqglevel = glevel
54 func.reqclevel = clevel
55
56 self.hooks[cmd] = func
57 if self.parent is not None:
58 self.parent.hook(cmd, func)
59 return func
60 return realhook
61
62 def argsEQ(self, num):
63 def realhook(func):
64 def checkargs(bot, user, chan, realtarget, *args):
65 if len(args) == num:
66 return func(bot, user, chan, realtarget, *args)
67 else:
68 bot.msg(user, self.WRONGARGS)
69 return checkargs
70 return realhook
71
72 def argsGE(self, num):
73 def realhook(func):
74 def checkargs(bot, user, chan, realtarget, *args):
75 if len(args) >= num:
76 return func(bot, user, chan, realtarget, *args)
77 else:
78 bot.msg(user, self.WRONGARGS)
79 return checkargs
80 return realhook