]> jfr.im git - erebus.git/blame - modlib.py
fixing bug where TriviaState used global variable instead of self
[erebus.git] / modlib.py
CommitLineData
931c88a4 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
e4255e70 5class 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):
e3878612 13 return str(self.errormsg)
e4255e70 14
6c70d82c 15class modlib(object):
839d2b35 16 # default (global) access levels
b2a896c8 17 MANAGER = 100
d431e543 18 ADMIN = 75
19 STAFF = 50
931c88a4 20 AUTHED = 0
21 ANYONE = -1
22
839d2b35 23 # (channel) access levels
586997a7 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)
839d2b35 30
d431e543 31 # messages
32 WRONGARGS = "Wrong number of arguments."
33
6c70d82c 34 def __init__(self, name):
db50981b 35 self.hooks = {}
61b67f0f 36 self.numhooks = {}
2a1a69a6 37 self.chanhooks = {}
db50981b 38 self.parent = None
39
6c70d82c 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)
61b67f0f 46 for num, func in self.numhooks.iteritems():
47 self.parent.hooknum(num, func)
2a1a69a6 48 for chan, func in self.chanhooks.iteritems():
49 self.parent.hookchan(chan, func)
d431e543 50 return True
db50981b 51 def modstop(self, parent):
52 for cmd, func in self.hooks.iteritems():
e4a4c762 53 self.parent.unhook(cmd, func)
61b67f0f 54 for num, func in self.numhooks.iteritems():
55 self.parent.unhooknum(num, func)
2a1a69a6 56 for chan, func in self.chanhooks.iteritems():
9557ee54 57 self.parent.unhookchan(chan, func)
d431e543 58 return True
6c70d82c 59
61b67f0f 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
2a1a69a6 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
839d2b35 76 def hook(self, cmd, needchan=True, glevel=ANYONE, clevel=PUBLIC):
6c70d82c 77 def realhook(func):
839d2b35 78 func.needchan = needchan
79 func.reqglevel = glevel
80 func.reqclevel = clevel
81
6c70d82c 82 self.hooks[cmd] = func
83 if self.parent is not None:
84 self.parent.hook(cmd, func)
85 return func
86 return realhook
d431e543 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