]> jfr.im git - erebus.git/blame_incremental - modlib.py
update to use new config features
[erebus.git] / modlib.py
... / ...
CommitLineData
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
5class 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
15class 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):
104 _cmd = cmd #save this since it gets wiped out...
105 def realhook(func):
106 cmd = _cmd #...and restore it
107 if cmd is None:
108 cmd = func.__name__ # default to function name
109 if isinstance(cmd, basestring):
110 cmd = (cmd,)
111
112 func.needchan = needchan
113 func.reqglevel = glevel
114 func.reqclevel = clevel
115 func.cmd = cmd
116 func.module = func.__module__.split('.')[1]
117
118 for c in cmd:
119 self.hooks[c] = func
120 if self.parent is not None:
121 self.parent.hook(c, func)
122 self.parent.hook("%s.%s" % (self.name, c), func)
123 return func
124 return realhook
125
126 def mod(self, modname):
127 if self.parent is not None:
128 return self.parent.module(modname)
129 else:
130 return error('unknown parent')
131
132 def argsEQ(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 argsGE(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 help(self, *args, **kwargs):
157 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
158 Example:
159 help("<user> <pass>", "login")
160 ^ Help will only be one line. Command name determined based on function name.
161 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
162 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
163 help(None, "do stuff", "This command is really complicated.")
164 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
165 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
166 """
167 def realhook(func):
168 if self.parent is not None:
169 try:
170 self.mod('help').reghelp(func, *args, **kwargs)
171 except:
172 pass
173 self.helps.append((func,args,kwargs))
174 return func
175 return realhook