]>
Commit | Line | Data |
---|---|---|
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 | ||
a28e2ae9 | 5 | import sys |
6 | ||
7 | if sys.version_info.major < 3: | |
8 | stringbase = basestring | |
9 | else: | |
10 | stringbase = str | |
11 | ||
e4255e70 | 12 | class error(object): |
13 | def __init__(self, desc): | |
14 | self.errormsg = desc | |
15 | def __nonzero__(self): | |
16 | return False #object will test to False | |
17 | def __repr__(self): | |
18 | return '<modlib.error %r>' % self.errormsg | |
19 | def __str__(self): | |
e3878612 | 20 | return str(self.errormsg) |
e4255e70 | 21 | |
6c70d82c | 22 | class modlib(object): |
839d2b35 | 23 | # default (global) access levels |
69071d33 | 24 | OWNER = 100 |
25 | MANAGER = 99 | |
26 | ADMIN = 75 | |
27 | STAFF = 50 | |
28 | AUTHED = 0 | |
29 | ANYONE = -1 | |
8b8ff79e | 30 | IGNORED = -2 |
931c88a4 | 31 | |
839d2b35 | 32 | # (channel) access levels |
a290635a | 33 | COWNER = 5 |
69071d33 | 34 | MASTER = 4 |
35 | OP = 3 | |
36 | VOICE = 2 | |
37 | KNOWN = 1 | |
38 | PUBLIC = 0 #anyone (use glevel to control auth-needed) | |
8b8ff79e | 39 | BANNED = -1 |
f164fd1c | 40 | # [ 0 1 2 3 4 5 -1] |
41 | clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None] | |
839d2b35 | 42 | |
d431e543 | 43 | # messages |
44 | WRONGARGS = "Wrong number of arguments." | |
45 | ||
6c70d82c | 46 | def __init__(self, name): |
db50981b | 47 | self.hooks = {} |
61b67f0f | 48 | self.numhooks = {} |
2a1a69a6 | 49 | self.chanhooks = {} |
0f8352dd | 50 | self.helps = [] |
db50981b | 51 | self.parent = None |
52 | ||
a8553c45 | 53 | self.name = (name.split("."))[-1] |
6c70d82c | 54 | |
55 | def modstart(self, parent): | |
a62d0d18 | 56 | #modstart can return a few things... |
57 | # None: unspecified success | |
58 | # False: unspecified error | |
59 | # modlib.error (or anything else False-y): specified error | |
60 | # True: unspecified success | |
61 | # non-empty string (or anything else True-y): specified success | |
62 | #"specified" values will be printed. unspecified values will result in "OK" or "failed" | |
6c70d82c | 63 | self.parent = parent |
a28e2ae9 | 64 | for cmd, func in self.hooks.items(): |
6c70d82c | 65 | self.parent.hook(cmd, func) |
a290635a | 66 | self.parent.hook("%s.%s" % (self.name, cmd), func) |
a28e2ae9 | 67 | for num, func in self.numhooks.items(): |
61b67f0f | 68 | self.parent.hooknum(num, func) |
a28e2ae9 | 69 | for chan, func in self.chanhooks.items(): |
2a1a69a6 | 70 | self.parent.hookchan(chan, func) |
0f8352dd | 71 | |
72 | for func, args, kwargs in self.helps: | |
73 | try: | |
74 | self.mod('help').reghelp(func, *args, **kwargs) | |
75 | except: | |
76 | pass | |
d431e543 | 77 | return True |
db50981b | 78 | def modstop(self, parent): |
a28e2ae9 | 79 | for cmd, func in self.hooks.items(): |
1bdecfcd | 80 | parent.unhook(cmd, func) |
81 | parent.unhook("%s.%s" % (self.name, cmd), func) | |
a28e2ae9 | 82 | for num, func in self.numhooks.items(): |
1bdecfcd | 83 | parent.unhooknum(num, func) |
a28e2ae9 | 84 | for chan, func in self.chanhooks.items(): |
1bdecfcd | 85 | parent.unhookchan(chan, func) |
0f8352dd | 86 | |
87 | for func, args, kwargs in self.helps: | |
88 | try: | |
89 | self.mod('help').dereghelp(func, *args, **kwargs) | |
90 | except: | |
91 | pass | |
d431e543 | 92 | return True |
6c70d82c | 93 | |
61b67f0f | 94 | def hooknum(self, num): |
95 | def realhook(func): | |
36411de9 | 96 | self.numhooks[str(num)] = func |
61b67f0f | 97 | if self.parent is not None: |
36411de9 | 98 | self.parent.hooknum(str(num), func) |
61b67f0f | 99 | return func |
100 | return realhook | |
101 | ||
2a1a69a6 | 102 | def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC): |
103 | def realhook(func): | |
104 | self.chanhooks[chan] = func | |
105 | if self.parent is not None: | |
106 | self.parent.hookchan(chan, func) | |
107 | return func | |
108 | return realhook | |
109 | ||
827ec8f0 | 110 | def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None): |
111 | if wantchan is None: wantchan = needchan | |
3a8b7b5f | 112 | _cmd = cmd #save this since it gets wiped out... |
6c70d82c | 113 | def realhook(func): |
3a8b7b5f | 114 | cmd = _cmd #...and restore it |
115 | if cmd is None: | |
116 | cmd = func.__name__ # default to function name | |
a28e2ae9 | 117 | if isinstance(cmd, stringbase): |
0f8352dd | 118 | cmd = (cmd,) |
3a8b7b5f | 119 | |
839d2b35 | 120 | func.needchan = needchan |
827ec8f0 | 121 | func.wantchan = wantchan |
839d2b35 | 122 | func.reqglevel = glevel |
123 | func.reqclevel = clevel | |
0f8352dd | 124 | func.cmd = cmd |
179c06a9 | 125 | func.module = func.__module__.split('.')[1] |
839d2b35 | 126 | |
9ea2be43 | 127 | for c in cmd: |
128 | self.hooks[c] = func | |
129 | if self.parent is not None: | |
130 | self.parent.hook(c, func) | |
a290635a | 131 | self.parent.hook("%s.%s" % (self.name, c), func) |
6c70d82c | 132 | return func |
133 | return realhook | |
d431e543 | 134 | |
36411de9 | 135 | def mod(self, modname): |
136 | if self.parent is not None: | |
137 | return self.parent.module(modname) | |
138 | else: | |
139 | return error('unknown parent') | |
140 | ||
d431e543 | 141 | def argsEQ(self, num): |
142 | def realhook(func): | |
143 | def checkargs(bot, user, chan, realtarget, *args): | |
144 | if len(args) == num: | |
145 | return func(bot, user, chan, realtarget, *args) | |
146 | else: | |
147 | bot.msg(user, self.WRONGARGS) | |
3a8b7b5f | 148 | checkargs.__name__ = func.__name__ |
11ef6fed | 149 | checkargs.__module__ = func.__module__ |
d431e543 | 150 | return checkargs |
151 | return realhook | |
152 | ||
153 | def argsGE(self, num): | |
154 | def realhook(func): | |
155 | def checkargs(bot, user, chan, realtarget, *args): | |
156 | if len(args) >= num: | |
157 | return func(bot, user, chan, realtarget, *args) | |
158 | else: | |
159 | bot.msg(user, self.WRONGARGS) | |
3a8b7b5f | 160 | checkargs.__name__ = func.__name__ |
11ef6fed | 161 | checkargs.__module__ = func.__module__ |
d431e543 | 162 | return checkargs |
163 | return realhook | |
b670c2f4 | 164 | |
984fc310 | 165 | def help(self, *args, **kwargs): |
0f8352dd | 166 | """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?) |
b670c2f4 | 167 | Example: |
168 | help("<user> <pass>", "login") | |
169 | ^ Help will only be one line. Command name determined based on function name. | |
170 | help("<user> <level>", "add a user", cmd=("adduser", "useradd")) | |
171 | ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser" | |
172 | help(None, "do stuff", "This command is really complicated.") | |
173 | ^ Command takes no args. Short description (in overall HELP listing) is "do stuff". | |
174 | Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated." | |
175 | """ | |
0f8352dd | 176 | def realhook(func): |
177 | if self.parent is not None: | |
178 | try: | |
179 | self.mod('help').reghelp(func, *args, **kwargs) | |
180 | except: | |
181 | pass | |
182 | self.helps.append((func,args,kwargs)) | |
183 | return func | |
184 | return realhook |