]>
Commit | Line | Data |
---|---|---|
931c88a4 | 1 | # Erebus IRC bot - Author: John Runyon |
4477123d | 2 | # vim: fileencoding=utf-8 |
931c88a4 | 3 | # module helper functions, see modules/modtest.py for usage |
4 | # This file is released into the public domain; see http://unlicense.org/ | |
5 | ||
a28e2ae9 | 6 | import sys |
4f8abd95 | 7 | from functools import wraps |
a28e2ae9 | 8 | |
9 | if sys.version_info.major < 3: | |
10 | stringbase = basestring | |
11 | else: | |
12 | stringbase = str | |
13 | ||
43151ead | 14 | """Used to return an error to the bot core.""" |
e4255e70 | 15 | class error(object): |
16 | def __init__(self, desc): | |
17 | self.errormsg = desc | |
18 | def __nonzero__(self): | |
19 | return False #object will test to False | |
71ef8273 | 20 | __bool__ = __nonzero__ #py3 compat |
e4255e70 | 21 | def __repr__(self): |
22 | return '<modlib.error %r>' % self.errormsg | |
23 | def __str__(self): | |
e3878612 | 24 | return str(self.errormsg) |
e4255e70 | 25 | |
6c70d82c | 26 | class modlib(object): |
839d2b35 | 27 | # default (global) access levels |
69071d33 | 28 | OWNER = 100 |
29 | MANAGER = 99 | |
30 | ADMIN = 75 | |
31 | STAFF = 50 | |
25bf8fc5 | 32 | KNOWN = 1 |
43151ead JR |
33 | AUTHED = 0 # Users which have are known to be authed |
34 | ANYONE = -1 # non-authed users have glevel set to -1 | |
35 | IGNORED = -2 # The default reqglevel is ANYONE, so any commands will be ignored from IGNORED users unless the command reglevel=-2 | |
25bf8fc5 JR |
36 | glevs = { |
37 | 'OWNER': OWNER, | |
38 | 'MANAGER': MANAGER, | |
39 | 'ADMIN': ADMIN, | |
40 | 'STAFF': STAFF, | |
41 | 'KNOWN': KNOWN, | |
42 | 'AUTHED': AUTHED, | |
43 | 'ANYONE': ANYONE, | |
44 | 'IGNORED': IGNORED, | |
45 | } | |
931c88a4 | 46 | |
839d2b35 | 47 | # (channel) access levels |
a290635a | 48 | COWNER = 5 |
69071d33 | 49 | MASTER = 4 |
50 | OP = 3 | |
51 | VOICE = 2 | |
25bf8fc5 | 52 | #KNOWN = 1 is set above by glevels |
43151ead JR |
53 | PUBLIC = 0 # Anyone (use glevel to control whether auth is needed) |
54 | BANNED = -1 # The default reqclevel is PUBLIC, so any commands which needchan will be ignored from BANNED users unless the command reqclevel=-1 | |
f164fd1c | 55 | # [ 0 1 2 3 4 5 -1] |
56 | clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None] | |
839d2b35 | 57 | |
d431e543 | 58 | # messages |
59 | WRONGARGS = "Wrong number of arguments." | |
60 | ||
6c70d82c | 61 | def __init__(self, name): |
db50981b | 62 | self.hooks = {} |
61b67f0f | 63 | self.numhooks = {} |
2a1a69a6 | 64 | self.chanhooks = {} |
e8885384 | 65 | self.exceptionhooks = [] |
0f8352dd | 66 | self.helps = [] |
db50981b | 67 | self.parent = None |
68 | ||
a8553c45 | 69 | self.name = (name.split("."))[-1] |
6c70d82c | 70 | |
71 | def modstart(self, parent): | |
a62d0d18 | 72 | #modstart can return a few things... |
73 | # None: unspecified success | |
74 | # False: unspecified error | |
75 | # modlib.error (or anything else False-y): specified error | |
76 | # True: unspecified success | |
77 | # non-empty string (or anything else True-y): specified success | |
78 | #"specified" values will be printed. unspecified values will result in "OK" or "failed" | |
6c70d82c | 79 | self.parent = parent |
a28e2ae9 | 80 | for cmd, func in self.hooks.items(): |
e8885384 JR |
81 | parent.hook(cmd, func) |
82 | parent.hook("%s.%s" % (self.name, cmd), func) | |
a28e2ae9 | 83 | for num, func in self.numhooks.items(): |
e8885384 | 84 | parent.hooknum(num, func) |
a28e2ae9 | 85 | for chan, func in self.chanhooks.items(): |
e8885384 JR |
86 | parent.hookchan(chan, func) |
87 | for exc, func in self.exceptionhooks: | |
88 | parent.hookexception(exc, func) | |
0f8352dd | 89 | |
90 | for func, args, kwargs in self.helps: | |
91 | try: | |
92 | self.mod('help').reghelp(func, *args, **kwargs) | |
93 | except: | |
94 | pass | |
d431e543 | 95 | return True |
db50981b | 96 | def modstop(self, parent): |
a28e2ae9 | 97 | for cmd, func in self.hooks.items(): |
1bdecfcd | 98 | parent.unhook(cmd, func) |
99 | parent.unhook("%s.%s" % (self.name, cmd), func) | |
a28e2ae9 | 100 | for num, func in self.numhooks.items(): |
1bdecfcd | 101 | parent.unhooknum(num, func) |
a28e2ae9 | 102 | for chan, func in self.chanhooks.items(): |
1bdecfcd | 103 | parent.unhookchan(chan, func) |
e8885384 JR |
104 | for exc, func in self.exceptionhooks: |
105 | parent.unhookexception(exc, func) | |
0f8352dd | 106 | |
107 | for func, args, kwargs in self.helps: | |
108 | try: | |
109 | self.mod('help').dereghelp(func, *args, **kwargs) | |
110 | except: | |
111 | pass | |
d431e543 | 112 | return True |
6c70d82c | 113 | |
e8885384 JR |
114 | def hookexception(self, exc): |
115 | def realhook(func): | |
116 | self.exceptionhooks.append((exc, func)) | |
117 | if self.parent is not None: | |
118 | self.parent.hookexception(exc, func) | |
119 | return func | |
120 | return realhook | |
121 | ||
61b67f0f | 122 | def hooknum(self, num): |
123 | def realhook(func): | |
36411de9 | 124 | self.numhooks[str(num)] = func |
61b67f0f | 125 | if self.parent is not None: |
36411de9 | 126 | self.parent.hooknum(str(num), func) |
61b67f0f | 127 | return func |
128 | return realhook | |
129 | ||
2a1a69a6 | 130 | def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC): |
131 | def realhook(func): | |
132 | self.chanhooks[chan] = func | |
133 | if self.parent is not None: | |
134 | self.parent.hookchan(chan, func) | |
135 | return func | |
136 | return realhook | |
137 | ||
827ec8f0 | 138 | def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None): |
139 | if wantchan is None: wantchan = needchan | |
3a8b7b5f | 140 | _cmd = cmd #save this since it gets wiped out... |
6c70d82c | 141 | def realhook(func): |
3a8b7b5f | 142 | cmd = _cmd #...and restore it |
143 | if cmd is None: | |
144 | cmd = func.__name__ # default to function name | |
a28e2ae9 | 145 | if isinstance(cmd, stringbase): |
0f8352dd | 146 | cmd = (cmd,) |
3a8b7b5f | 147 | |
68dff4aa JR |
148 | if clevel > self.PUBLIC and not needchan: |
149 | raise Exception('clevel must be left at default if needchan is False') | |
150 | ||
839d2b35 | 151 | func.needchan = needchan |
827ec8f0 | 152 | func.wantchan = wantchan |
839d2b35 | 153 | func.reqglevel = glevel |
154 | func.reqclevel = clevel | |
0f8352dd | 155 | func.cmd = cmd |
179c06a9 | 156 | func.module = func.__module__.split('.')[1] |
839d2b35 | 157 | |
9ea2be43 | 158 | for c in cmd: |
159 | self.hooks[c] = func | |
160 | if self.parent is not None: | |
161 | self.parent.hook(c, func) | |
a290635a | 162 | self.parent.hook("%s.%s" % (self.name, c), func) |
6c70d82c | 163 | return func |
164 | return realhook | |
d431e543 | 165 | |
36411de9 | 166 | def mod(self, modname): |
167 | if self.parent is not None: | |
168 | return self.parent.module(modname) | |
169 | else: | |
170 | return error('unknown parent') | |
171 | ||
d431e543 | 172 | def argsEQ(self, num): |
173 | def realhook(func): | |
4f8abd95 | 174 | @wraps(func) |
d431e543 | 175 | def checkargs(bot, user, chan, realtarget, *args): |
176 | if len(args) == num: | |
177 | return func(bot, user, chan, realtarget, *args) | |
178 | else: | |
179 | bot.msg(user, self.WRONGARGS) | |
180 | return checkargs | |
181 | return realhook | |
182 | ||
183 | def argsGE(self, num): | |
184 | def realhook(func): | |
4f8abd95 | 185 | @wraps(func) |
d431e543 | 186 | def checkargs(bot, user, chan, realtarget, *args): |
187 | if len(args) >= num: | |
188 | return func(bot, user, chan, realtarget, *args) | |
189 | else: | |
190 | bot.msg(user, self.WRONGARGS) | |
191 | return checkargs | |
192 | return realhook | |
b670c2f4 | 193 | |
984fc310 | 194 | def help(self, *args, **kwargs): |
0f8352dd | 195 | """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?) |
b670c2f4 | 196 | Example: |
197 | help("<user> <pass>", "login") | |
198 | ^ Help will only be one line. Command name determined based on function name. | |
199 | help("<user> <level>", "add a user", cmd=("adduser", "useradd")) | |
200 | ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser" | |
201 | help(None, "do stuff", "This command is really complicated.") | |
202 | ^ Command takes no args. Short description (in overall HELP listing) is "do stuff". | |
203 | Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated." | |
204 | """ | |
0f8352dd | 205 | def realhook(func): |
206 | if self.parent is not None: | |
207 | try: | |
208 | self.mod('help').reghelp(func, *args, **kwargs) | |
209 | except: | |
210 | pass | |
71ef8273 | 211 | self.helps.append((func, args, kwargs)) |
0f8352dd | 212 | return func |
213 | return realhook |