]> jfr.im git - erebus.git/blob - modlib.py
add reporting of bad questions
[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 str(self.errormsg)
14
15 class modlib(object):
16 # default (global) access levels
17 OWNER = 100
18 MANAGER = 99
19 ADMIN = 75
20 STAFF = 50
21 AUTHED = 0
22 ANYONE = -1
23
24 # (channel) access levels
25 OWNER = 5
26 MASTER = 4
27 OP = 3
28 VOICE = 2
29 KNOWN = 1
30 PUBLIC = 0 #anyone (use glevel to control auth-needed)
31
32 # messages
33 WRONGARGS = "Wrong number of arguments."
34
35 def __init__(self, name):
36 self.hooks = {}
37 self.numhooks = {}
38 self.chanhooks = {}
39 self.parent = None
40
41 self.name = name
42
43 def modstart(self, parent):
44 self.parent = parent
45 for cmd, func in self.hooks.iteritems():
46 self.parent.hook(cmd, func)
47 for num, func in self.numhooks.iteritems():
48 self.parent.hooknum(num, func)
49 for chan, func in self.chanhooks.iteritems():
50 self.parent.hookchan(chan, func)
51 return True
52 def modstop(self, parent):
53 for cmd, func in self.hooks.iteritems():
54 self.parent.unhook(cmd, func)
55 for num, func in self.numhooks.iteritems():
56 self.parent.unhooknum(num, func)
57 for chan, func in self.chanhooks.iteritems():
58 self.parent.unhookchan(chan, func)
59 return True
60
61 def hooknum(self, num):
62 def realhook(func):
63 self.numhooks[str(num)] = func
64 if self.parent is not None:
65 self.parent.hooknum(str(num), func)
66 return func
67 return realhook
68
69 def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC):
70 def realhook(func):
71 self.chanhooks[chan] = func
72 if self.parent is not None:
73 self.parent.hookchan(chan, func)
74 return func
75 return realhook
76
77 def hook(self, cmd, needchan=True, glevel=ANYONE, clevel=PUBLIC):
78 def realhook(func):
79 func.needchan = needchan
80 func.reqglevel = glevel
81 func.reqclevel = clevel
82
83 self.hooks[cmd] = func
84 if self.parent is not None:
85 self.parent.hook(cmd, func)
86 return func
87 return realhook
88
89 def mod(self, modname):
90 if self.parent is not None:
91 return self.parent.module(modname)
92 else:
93 return error('unknown parent')
94
95 def argsEQ(self, num):
96 def realhook(func):
97 def checkargs(bot, user, chan, realtarget, *args):
98 if len(args) == num:
99 return func(bot, user, chan, realtarget, *args)
100 else:
101 bot.msg(user, self.WRONGARGS)
102 return checkargs
103 return realhook
104
105 def argsGE(self, num):
106 def realhook(func):
107 def checkargs(bot, user, chan, realtarget, *args):
108 if len(args) >= num:
109 return func(bot, user, chan, realtarget, *args)
110 else:
111 bot.msg(user, self.WRONGARGS)
112 return checkargs
113 return realhook