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