]>
jfr.im git - erebus.git/blob - modlib.py
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/
7 from functools
import wraps
9 if sys
.version_info
.major
< 3:
10 stringbase
= basestring
14 """Used to return an error to the bot core."""
16 def __init__(self
, desc
):
18 def __nonzero__(self
):
19 return False #object will test to False
20 __bool__
= __nonzero__
#py3 compat
22 return '<modlib.error %r>' % self
.errormsg
24 return str(self
.errormsg
)
27 # default (global) access levels
32 AUTHED
= 0 # Users which have are known to be authed
33 ANYONE
= -1 # non-authed users have glevel set to -1
34 IGNORED
= -2 # The default reqglevel is ANYONE, so any commands will be ignored from IGNORED users unless the command reglevel=-2
36 # (channel) access levels
42 PUBLIC
= 0 # Anyone (use glevel to control whether auth is needed)
43 BANNED
= -1 # The default reqclevel is PUBLIC, so any commands which needchan will be ignored from BANNED users unless the command reqclevel=-1
45 clevs
= [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
48 WRONGARGS
= "Wrong number of arguments."
50 def __init__(self
, name
):
57 self
.name
= (name
.split("."))[-1]
59 def modstart(self
, parent
):
60 #modstart can return a few things...
61 # None: unspecified success
62 # False: unspecified error
63 # modlib.error (or anything else False-y): specified error
64 # True: unspecified success
65 # non-empty string (or anything else True-y): specified success
66 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
68 for cmd
, func
in self
.hooks
.items():
69 self
.parent
.hook(cmd
, func
)
70 self
.parent
.hook("%s.%s" % (self
.name
, cmd
), func
)
71 for num
, func
in self
.numhooks
.items():
72 self
.parent
.hooknum(num
, func
)
73 for chan
, func
in self
.chanhooks
.items():
74 self
.parent
.hookchan(chan
, func
)
76 for func
, args
, kwargs
in self
.helps
:
78 self
.mod('help').reghelp(func
, *args
, **kwargs
)
82 def modstop(self
, parent
):
83 for cmd
, func
in self
.hooks
.items():
84 parent
.unhook(cmd
, func
)
85 parent
.unhook("%s.%s" % (self
.name
, cmd
), func
)
86 for num
, func
in self
.numhooks
.items():
87 parent
.unhooknum(num
, func
)
88 for chan
, func
in self
.chanhooks
.items():
89 parent
.unhookchan(chan
, func
)
91 for func
, args
, kwargs
in self
.helps
:
93 self
.mod('help').dereghelp(func
, *args
, **kwargs
)
98 def hooknum(self
, num
):
100 self
.numhooks
[str(num
)] = func
101 if self
.parent
is not None:
102 self
.parent
.hooknum(str(num
), func
)
106 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
108 self
.chanhooks
[chan
] = func
109 if self
.parent
is not None:
110 self
.parent
.hookchan(chan
, func
)
114 def hook(self
, cmd
=None, needchan
=True, glevel
=ANYONE
, clevel
=PUBLIC
, wantchan
=None):
115 if wantchan
is None: wantchan
= needchan
116 _cmd
= cmd
#save this since it gets wiped out...
118 cmd
= _cmd
#...and restore it
120 cmd
= func
.__name
__ # default to function name
121 if isinstance(cmd
, stringbase
):
124 if clevel
> self
.PUBLIC
and not needchan
:
125 raise Exception('clevel must be left at default if needchan is False')
127 func
.needchan
= needchan
128 func
.wantchan
= wantchan
129 func
.reqglevel
= glevel
130 func
.reqclevel
= clevel
132 func
.module
= func
.__module
__.split('.')[1]
136 if self
.parent
is not None:
137 self
.parent
.hook(c
, func
)
138 self
.parent
.hook("%s.%s" % (self
.name
, c
), func
)
142 def mod(self
, modname
):
143 if self
.parent
is not None:
144 return self
.parent
.module(modname
)
146 return error('unknown parent')
148 def argsEQ(self
, num
):
151 def checkargs(bot
, user
, chan
, realtarget
, *args
):
153 return func(bot
, user
, chan
, realtarget
, *args
)
155 bot
.msg(user
, self
.WRONGARGS
)
159 def argsGE(self
, num
):
162 def checkargs(bot
, user
, chan
, realtarget
, *args
):
164 return func(bot
, user
, chan
, realtarget
, *args
)
166 bot
.msg(user
, self
.WRONGARGS
)
170 def help(self
, *args
, **kwargs
):
171 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
173 help("<user> <pass>", "login")
174 ^ Help will only be one line. Command name determined based on function name.
175 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
176 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
177 help(None, "do stuff", "This command is really complicated.")
178 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
179 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
182 if self
.parent
is not None:
184 self
.mod('help').reghelp(func
, *args
, **kwargs
)
187 self
.helps
.append((func
, args
, kwargs
))