]>
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/
6 def __init__(self
, desc
):
9 return False #object will test to False
11 return '<modlib.error %r>' % self
.errormsg
13 return str(self
.errormsg
)
16 # default (global) access levels
24 # (channel) access levels
30 PUBLIC
= 0 #anyone (use glevel to control auth-needed)
33 WRONGARGS
= "Wrong number of arguments."
35 def __init__(self
, name
):
43 def modstart(self
, parent
):
45 for cmd
, func
in self
.hooks
.iteritems():
46 self
.parent
.hook(cmd
, func
)
47 for num
, func
in self
.numhooks
.iteritems():
48 self
.parent
.hooknum(num
, func
)
49 for chan
, func
in self
.chanhooks
.iteritems():
50 self
.parent
.hookchan(chan
, func
)
52 def modstop(self
, parent
):
53 for cmd
, func
in self
.hooks
.iteritems():
54 self
.parent
.unhook(cmd
, func
)
55 for num
, func
in self
.numhooks
.iteritems():
56 self
.parent
.unhooknum(num
, func
)
57 for chan
, func
in self
.chanhooks
.iteritems():
58 self
.parent
.unhookchan(chan
, func
)
61 def hooknum(self
, num
):
63 self
.numhooks
[str(num
)] = func
64 if self
.parent
is not None:
65 self
.parent
.hooknum(str(num
), func
)
69 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
71 self
.chanhooks
[chan
] = func
72 if self
.parent
is not None:
73 self
.parent
.hookchan(chan
, func
)
77 def hook(self
, cmd
=None, needchan
=True, glevel
=ANYONE
, clevel
=PUBLIC
):
78 _cmd
= cmd
#save this since it gets wiped out...
80 cmd
= _cmd
#...and restore it
82 cmd
= func
.__name
__ # default to function name
84 func
.needchan
= needchan
85 func
.reqglevel
= glevel
86 func
.reqclevel
= clevel
88 self
.hooks
[cmd
] = func
89 if self
.parent
is not None:
90 self
.parent
.hook(cmd
, func
)
94 def mod(self
, modname
):
95 if self
.parent
is not None:
96 return self
.parent
.module(modname
)
98 return error('unknown parent')
100 def argsEQ(self
, num
):
102 def checkargs(bot
, user
, chan
, realtarget
, *args
):
104 return func(bot
, user
, chan
, realtarget
, *args
)
106 bot
.msg(user
, self
.WRONGARGS
)
107 checkargs
.__name
__ = func
.__name
__
111 def argsGE(self
, num
):
113 def checkargs(bot
, user
, chan
, realtarget
, *args
):
115 return func(bot
, user
, chan
, realtarget
, *args
)
117 bot
.msg(user
, self
.WRONGARGS
)
118 checkargs
.__name
__ = func
.__name
__
122 def help(self
, *args
, **kwargs
):
123 """help(syntax, shorthelp, longhelp, more lines longhelp, cmd=...?)
125 help("<user> <pass>", "login")
126 ^ Help will only be one line. Command name determined based on function name.
127 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
128 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
129 help(None, "do stuff", "This command is really complicated.")
130 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
131 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
134 self
.mod('help').reghelp(*args
, **kwargs
)