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