]>
jfr.im git - erebus.git/blob - 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/
7 if sys
.version_info
.major
< 3:
8 stringbase
= basestring
13 def __init__(self
, desc
):
15 def __nonzero__(self
):
16 return False #object will test to False
18 return '<modlib.error %r>' % self
.errormsg
20 return str(self
.errormsg
)
23 # default (global) access levels
32 # (channel) access levels
38 PUBLIC
= 0 #anyone (use glevel to control auth-needed)
41 clevs
= [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
44 WRONGARGS
= "Wrong number of arguments."
46 def __init__(self
, name
):
53 self
.name
= (name
.split("."))[-1]
55 def modstart(self
, parent
):
56 #modstart can return a few things...
57 # None: unspecified success
58 # False: unspecified error
59 # modlib.error (or anything else False-y): specified error
60 # True: unspecified success
61 # non-empty string (or anything else True-y): specified success
62 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
64 for cmd
, func
in self
.hooks
.items():
65 self
.parent
.hook(cmd
, func
)
66 self
.parent
.hook("%s.%s" % (self
.name
, cmd
), func
)
67 for num
, func
in self
.numhooks
.items():
68 self
.parent
.hooknum(num
, func
)
69 for chan
, func
in self
.chanhooks
.items():
70 self
.parent
.hookchan(chan
, func
)
72 for func
, args
, kwargs
in self
.helps
:
74 self
.mod('help').reghelp(func
, *args
, **kwargs
)
78 def modstop(self
, parent
):
79 for cmd
, func
in self
.hooks
.items():
80 parent
.unhook(cmd
, func
)
81 parent
.unhook("%s.%s" % (self
.name
, cmd
), func
)
82 for num
, func
in self
.numhooks
.items():
83 parent
.unhooknum(num
, func
)
84 for chan
, func
in self
.chanhooks
.items():
85 parent
.unhookchan(chan
, func
)
87 for func
, args
, kwargs
in self
.helps
:
89 self
.mod('help').dereghelp(func
, *args
, **kwargs
)
94 def hooknum(self
, num
):
96 self
.numhooks
[str(num
)] = func
97 if self
.parent
is not None:
98 self
.parent
.hooknum(str(num
), func
)
102 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
104 self
.chanhooks
[chan
] = func
105 if self
.parent
is not None:
106 self
.parent
.hookchan(chan
, func
)
110 def hook(self
, cmd
=None, needchan
=True, glevel
=ANYONE
, clevel
=PUBLIC
, wantchan
=None):
111 if wantchan
is None: wantchan
= needchan
112 _cmd
= cmd
#save this since it gets wiped out...
114 cmd
= _cmd
#...and restore it
116 cmd
= func
.__name
__ # default to function name
117 if isinstance(cmd
, stringbase
):
120 func
.needchan
= needchan
121 func
.wantchan
= wantchan
122 func
.reqglevel
= glevel
123 func
.reqclevel
= clevel
125 func
.module
= func
.__module
__.split('.')[1]
129 if self
.parent
is not None:
130 self
.parent
.hook(c
, func
)
131 self
.parent
.hook("%s.%s" % (self
.name
, c
), func
)
135 def mod(self
, modname
):
136 if self
.parent
is not None:
137 return self
.parent
.module(modname
)
139 return error('unknown parent')
141 def argsEQ(self
, num
):
143 def checkargs(bot
, user
, chan
, realtarget
, *args
):
145 return func(bot
, user
, chan
, realtarget
, *args
)
147 bot
.msg(user
, self
.WRONGARGS
)
148 checkargs
.__name
__ = func
.__name
__
149 checkargs
.__module
__ = func
.__module
__
153 def argsGE(self
, num
):
155 def checkargs(bot
, user
, chan
, realtarget
, *args
):
157 return func(bot
, user
, chan
, realtarget
, *args
)
159 bot
.msg(user
, self
.WRONGARGS
)
160 checkargs
.__name
__ = func
.__name
__
161 checkargs
.__module
__ = func
.__module
__
165 def help(self
, *args
, **kwargs
):
166 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
168 help("<user> <pass>", "login")
169 ^ Help will only be one line. Command name determined based on function name.
170 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
171 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
172 help(None, "do stuff", "This command is really complicated.")
173 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
174 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
177 if self
.parent
is not None:
179 self
.mod('help').reghelp(func
, *args
, **kwargs
)
182 self
.helps
.append((func
,args
,kwargs
))