]>
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 |
69071d33 | 25 | OWNER = 5 |
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) | |
61b67f0f | 47 | for num, func in self.numhooks.iteritems(): |
48 | self.parent.hooknum(num, func) | |
2a1a69a6 | 49 | for chan, func in self.chanhooks.iteritems(): |
50 | self.parent.hookchan(chan, func) | |
d431e543 | 51 | return True |
db50981b | 52 | def modstop(self, parent): |
53 | for cmd, func in self.hooks.iteritems(): | |
e4a4c762 | 54 | self.parent.unhook(cmd, func) |
61b67f0f | 55 | for num, func in self.numhooks.iteritems(): |
56 | self.parent.unhooknum(num, func) | |
2a1a69a6 | 57 | for chan, func in self.chanhooks.iteritems(): |
9557ee54 | 58 | self.parent.unhookchan(chan, func) |
d431e543 | 59 | return True |
6c70d82c | 60 | |
61b67f0f | 61 | def hooknum(self, num): |
62 | def realhook(func): | |
36411de9 | 63 | self.numhooks[str(num)] = func |
61b67f0f | 64 | if self.parent is not None: |
36411de9 | 65 | self.parent.hooknum(str(num), func) |
61b67f0f | 66 | return func |
67 | return realhook | |
68 | ||
2a1a69a6 | 69 | def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC): |
70 | def realhook(func): | |
71 | self.chanhooks[chan] = func | |
72 | if self.parent is not None: | |
73 | self.parent.hookchan(chan, func) | |
74 | return func | |
75 | return realhook | |
76 | ||
3a8b7b5f | 77 | def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC): |
78 | _cmd = cmd #save this since it gets wiped out... | |
6c70d82c | 79 | def realhook(func): |
3a8b7b5f | 80 | cmd = _cmd #...and restore it |
81 | if cmd is None: | |
82 | cmd = func.__name__ # default to function name | |
83 | ||
839d2b35 | 84 | func.needchan = needchan |
85 | func.reqglevel = glevel | |
86 | func.reqclevel = clevel | |
87 | ||
6c70d82c | 88 | self.hooks[cmd] = func |
89 | if self.parent is not None: | |
90 | self.parent.hook(cmd, func) | |
91 | return func | |
92 | return realhook | |
d431e543 | 93 | |
36411de9 | 94 | def mod(self, modname): |
95 | if self.parent is not None: | |
96 | return self.parent.module(modname) | |
97 | else: | |
98 | return error('unknown parent') | |
99 | ||
d431e543 | 100 | def argsEQ(self, num): |
101 | def realhook(func): | |
102 | def checkargs(bot, user, chan, realtarget, *args): | |
103 | if len(args) == num: | |
104 | return func(bot, user, chan, realtarget, *args) | |
105 | else: | |
106 | bot.msg(user, self.WRONGARGS) | |
3a8b7b5f | 107 | checkargs.__name__ = func.__name__ |
d431e543 | 108 | return checkargs |
109 | return realhook | |
110 | ||
111 | def argsGE(self, num): | |
112 | def realhook(func): | |
113 | def checkargs(bot, user, chan, realtarget, *args): | |
114 | if len(args) >= num: | |
115 | return func(bot, user, chan, realtarget, *args) | |
116 | else: | |
117 | bot.msg(user, self.WRONGARGS) | |
3a8b7b5f | 118 | checkargs.__name__ = func.__name__ |
d431e543 | 119 | return checkargs |
120 | return realhook | |
b670c2f4 | 121 | |
122 | def help(*args, **kwargs): | |
123 | """help(args, shorthelp, longhelp, more lines longhelp, cmd=...?) | |
124 | Example: | |
125 | help("<user> <pass>", "login") | |
126 | ^ Help will only be one line. Command name determined based on function name. | |
127 | help("<user> <level>", "add a user", cmd=("adduser", "useradd")) | |
128 | ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser" | |
129 | help(None, "do stuff", "This command is really complicated.") | |
130 | ^ Command takes no args. Short description (in overall HELP listing) is "do stuff". | |
131 | Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated." | |
132 | """ | |
133 | try: | |
134 | self.mod('help').reghelp(*args, **kwargs) | |
135 | except: | |
136 | pass |