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