]> jfr.im git - erebus.git/blob - modlib.py
Added hooknum in modlib
[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.numhooks = {}
37 self.parent = None
38
39 self.name = name
40
41 def modstart(self, parent):
42 self.parent = parent
43 for cmd, func in self.hooks.iteritems():
44 self.parent.hook(cmd, func)
45 for num, func in self.numhooks.iteritems():
46 self.parent.hooknum(num, func)
47 return True
48 def modstop(self, parent):
49 for cmd, func in self.hooks.iteritems():
50 self.parent.unhook(cmd, func)
51 for num, func in self.numhooks.iteritems():
52 self.parent.unhooknum(num, func)
53 return True
54
55 def hooknum(self, num):
56 def realhook(func):
57 self.numhooks[num] = func
58 if self.parent is not None:
59 self.parent.hooknum(num, func)
60 return func
61 return realhook
62
63 def hook(self, cmd, needchan=True, glevel=ANYONE, clevel=PUBLIC):
64 def realhook(func):
65 func.needchan = needchan
66 func.reqglevel = glevel
67 func.reqclevel = clevel
68
69 self.hooks[cmd] = func
70 if self.parent is not None:
71 self.parent.hook(cmd, func)
72 return func
73 return realhook
74
75 def argsEQ(self, num):
76 def realhook(func):
77 def checkargs(bot, user, chan, realtarget, *args):
78 if len(args) == num:
79 return func(bot, user, chan, realtarget, *args)
80 else:
81 bot.msg(user, self.WRONGARGS)
82 return checkargs
83 return realhook
84
85 def argsGE(self, num):
86 def realhook(func):
87 def checkargs(bot, user, chan, realtarget, *args):
88 if len(args) >= num:
89 return func(bot, user, chan, realtarget, *args)
90 else:
91 bot.msg(user, self.WRONGARGS)
92 return checkargs
93 return realhook