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