]> jfr.im git - erebus.git/blob - modlib.py
added some coloring to trivia, a few temporary aids for testing
[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 = 5
25 MASTER = 4
26 OP = 3
27 VOICE = 2
28 KNOWN = 1
29 PUBLIC = 0 #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.chanhooks = {}
38 self.parent = None
39
40 self.name = name
41
42 def modstart(self, parent):
43 self.parent = parent
44 for cmd, func in self.hooks.iteritems():
45 self.parent.hook(cmd, func)
46 for num, func in self.numhooks.iteritems():
47 self.parent.hooknum(num, func)
48 for chan, func in self.chanhooks.iteritems():
49 self.parent.hookchan(chan, func)
50 return True
51 def modstop(self, parent):
52 for cmd, func in self.hooks.iteritems():
53 self.parent.unhook(cmd, func)
54 for num, func in self.numhooks.iteritems():
55 self.parent.unhooknum(num, func)
56 for chan, func in self.chanhooks.iteritems():
57 self.parent.unhookchan(chan, func)
58 return True
59
60 def hooknum(self, num):
61 def realhook(func):
62 self.numhooks[num] = func
63 if self.parent is not None:
64 self.parent.hooknum(num, func)
65 return func
66 return realhook
67
68 def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC):
69 def realhook(func):
70 self.chanhooks[chan] = func
71 if self.parent is not None:
72 self.parent.hookchan(chan, func)
73 return func
74 return realhook
75
76 def hook(self, cmd, needchan=True, glevel=ANYONE, clevel=PUBLIC):
77 def realhook(func):
78 func.needchan = needchan
79 func.reqglevel = glevel
80 func.reqclevel = clevel
81
82 self.hooks[cmd] = func
83 if self.parent is not None:
84 self.parent.hook(cmd, func)
85 return func
86 return realhook
87
88 def argsEQ(self, num):
89 def realhook(func):
90 def checkargs(bot, user, chan, realtarget, *args):
91 if len(args) == num:
92 return func(bot, user, chan, realtarget, *args)
93 else:
94 bot.msg(user, self.WRONGARGS)
95 return checkargs
96 return realhook
97
98 def argsGE(self, num):
99 def realhook(func):
100 def checkargs(bot, user, chan, realtarget, *args):
101 if len(args) >= num:
102 return func(bot, user, chan, realtarget, *args)
103 else:
104 bot.msg(user, self.WRONGARGS)
105 return checkargs
106 return realhook