]>
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 | |
931c88a4 | 23 | |
839d2b35 | 24 | # (channel) access levels |
a290635a | 25 | COWNER = 5 |
69071d33 | 26 | MASTER = 4 |
27 | OP = 3 | |
28 | VOICE = 2 | |
29 | KNOWN = 1 | |
30 | PUBLIC = 0 #anyone (use glevel to control auth-needed) | |
839d2b35 | 31 | |
d431e543 | 32 | # messages |
33 | WRONGARGS = "Wrong number of arguments." | |
34 | ||
6c70d82c | 35 | def __init__(self, name): |
db50981b | 36 | self.hooks = {} |
61b67f0f | 37 | self.numhooks = {} |
2a1a69a6 | 38 | self.chanhooks = {} |
db50981b | 39 | self.parent = None |
40 | ||
6c70d82c | 41 | self.name = name |
42 | ||
43 | def modstart(self, parent): | |
44 | self.parent = parent | |
45 | for cmd, func in self.hooks.iteritems(): | |
46 | self.parent.hook(cmd, func) | |
a290635a | 47 | self.parent.hook("%s.%s" % (self.name, cmd), func) |
61b67f0f | 48 | for num, func in self.numhooks.iteritems(): |
49 | self.parent.hooknum(num, func) | |
2a1a69a6 | 50 | for chan, func in self.chanhooks.iteritems(): |
51 | self.parent.hookchan(chan, func) | |
d431e543 | 52 | return True |
db50981b | 53 | def modstop(self, parent): |
54 | for cmd, func in self.hooks.iteritems(): | |
e4a4c762 | 55 | self.parent.unhook(cmd, func) |
a290635a | 56 | self.parent.unhook("%s.%s" % (self.name, cmd), func) |
61b67f0f | 57 | for num, func in self.numhooks.iteritems(): |
58 | self.parent.unhooknum(num, func) | |
2a1a69a6 | 59 | for chan, func in self.chanhooks.iteritems(): |
9557ee54 | 60 | self.parent.unhookchan(chan, func) |
d431e543 | 61 | return True |
6c70d82c | 62 | |
61b67f0f | 63 | def hooknum(self, num): |
64 | def realhook(func): | |
36411de9 | 65 | self.numhooks[str(num)] = func |
61b67f0f | 66 | if self.parent is not None: |
36411de9 | 67 | self.parent.hooknum(str(num), func) |
61b67f0f | 68 | return func |
69 | return realhook | |
70 | ||
2a1a69a6 | 71 | def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC): |
72 | def realhook(func): | |
73 | self.chanhooks[chan] = func | |
74 | if self.parent is not None: | |
75 | self.parent.hookchan(chan, func) | |
76 | return func | |
77 | return realhook | |
78 | ||
3a8b7b5f | 79 | def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC): |
80 | _cmd = cmd #save this since it gets wiped out... | |
6c70d82c | 81 | def realhook(func): |
3a8b7b5f | 82 | cmd = _cmd #...and restore it |
83 | if cmd is None: | |
84 | cmd = func.__name__ # default to function name | |
85 | ||
839d2b35 | 86 | func.needchan = needchan |
87 | func.reqglevel = glevel | |
88 | func.reqclevel = clevel | |
89 | ||
9ea2be43 | 90 | if isinstance(cmd, basestring): |
91 | cmd = (cmd,) | |
92 | for c in cmd: | |
93 | self.hooks[c] = func | |
94 | if self.parent is not None: | |
95 | self.parent.hook(c, func) | |
a290635a | 96 | self.parent.hook("%s.%s" % (self.name, c), func) |
6c70d82c | 97 | return func |
98 | return realhook | |
d431e543 | 99 | |
36411de9 | 100 | def mod(self, modname): |
101 | if self.parent is not None: | |
102 | return self.parent.module(modname) | |
103 | else: | |
104 | return error('unknown parent') | |
105 | ||
d431e543 | 106 | def argsEQ(self, num): |
107 | def realhook(func): | |
108 | def checkargs(bot, user, chan, realtarget, *args): | |
109 | if len(args) == num: | |
110 | return func(bot, user, chan, realtarget, *args) | |
111 | else: | |
112 | bot.msg(user, self.WRONGARGS) | |
3a8b7b5f | 113 | checkargs.__name__ = func.__name__ |
d431e543 | 114 | return checkargs |
115 | return realhook | |
116 | ||
117 | def argsGE(self, num): | |
118 | def realhook(func): | |
119 | def checkargs(bot, user, chan, realtarget, *args): | |
120 | if len(args) >= num: | |
121 | return func(bot, user, chan, realtarget, *args) | |
122 | else: | |
123 | bot.msg(user, self.WRONGARGS) | |
3a8b7b5f | 124 | checkargs.__name__ = func.__name__ |
d431e543 | 125 | return checkargs |
126 | return realhook | |
b670c2f4 | 127 | |
984fc310 | 128 | def help(self, *args, **kwargs): |
129 | """help(syntax, shorthelp, longhelp, more lines longhelp, cmd=...?) | |
b670c2f4 | 130 | Example: |
131 | help("<user> <pass>", "login") | |
132 | ^ Help will only be one line. Command name determined based on function name. | |
133 | help("<user> <level>", "add a user", cmd=("adduser", "useradd")) | |
134 | ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser" | |
135 | help(None, "do stuff", "This command is really complicated.") | |
136 | ^ Command takes no args. Short description (in overall HELP listing) is "do stuff". | |
137 | Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated." | |
138 | """ | |
139 | try: | |
140 | self.mod('help').reghelp(*args, **kwargs) | |
141 | except: | |
142 | pass |