]>
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/
8 if sys
.version_info
.major
< 3:
9 stringbase
= basestring
14 """Used to return an error to the bot core."""
15 def __init__(self
, desc
):
17 def __nonzero__(self
):
18 return False #object will test to False
19 __bool__
= __nonzero__
#py3 compat
21 return '<modlib.error %r>' % self
.errormsg
23 return str(self
.errormsg
)
26 # default (global) access levels
35 # (channel) access levels
41 PUBLIC
= 0 #anyone (use glevel to control auth-needed)
44 clevs
= [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
47 WRONGARGS
= "Wrong number of arguments."
49 def __init__(self
, name
):
56 self
.name
= (name
.split("."))[-1]
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"
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
)
75 for func
, args
, kwargs
in self
.helps
:
77 self
.mod('help').reghelp(func
, *args
, **kwargs
)
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
)
90 for func
, args
, kwargs
in self
.helps
:
92 self
.mod('help').dereghelp(func
, *args
, **kwargs
)
97 def hooknum(self
, num
):
99 self
.numhooks
[str(num
)] = func
100 if self
.parent
is not None:
101 self
.parent
.hooknum(str(num
), func
)
105 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
107 self
.chanhooks
[chan
] = func
108 if self
.parent
is not None:
109 self
.parent
.hookchan(chan
, func
)
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...
117 cmd
= _cmd
#...and restore it
119 cmd
= func
.__name
__ # default to function name
120 if isinstance(cmd
, stringbase
):
123 func
.needchan
= needchan
124 func
.wantchan
= wantchan
125 func
.reqglevel
= glevel
126 func
.reqclevel
= clevel
128 func
.module
= func
.__module
__.split('.')[1]
132 if self
.parent
is not None:
133 self
.parent
.hook(c
, func
)
134 self
.parent
.hook("%s.%s" % (self
.name
, c
), func
)
138 def mod(self
, modname
):
139 if self
.parent
is not None:
140 return self
.parent
.module(modname
)
142 return error('unknown parent')
144 def argsEQ(self
, num
):
146 def checkargs(bot
, user
, chan
, realtarget
, *args
):
148 return func(bot
, user
, chan
, realtarget
, *args
)
150 bot
.msg(user
, self
.WRONGARGS
)
151 checkargs
.__name
__ = func
.__name
__
152 checkargs
.__module
__ = func
.__module
__
156 def argsGE(self
, num
):
158 def checkargs(bot
, user
, chan
, realtarget
, *args
):
160 return func(bot
, user
, chan
, realtarget
, *args
)
162 bot
.msg(user
, self
.WRONGARGS
)
163 checkargs
.__name
__ = func
.__name
__
164 checkargs
.__module
__ = func
.__module
__
168 def help(self
, *args
, **kwargs
):
169 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
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."
180 if self
.parent
is not None:
182 self
.mod('help').reghelp(func
, *args
, **kwargs
)
185 self
.helps
.append((func
, args
, kwargs
))