Added ability for commands to raise callbacks.CannotNest if they are not nestable.

This commit is contained in:
Jeremy Fincher 2003-08-27 07:45:48 +00:00
parent fff995221e
commit 9550971042
2 changed files with 33 additions and 17 deletions

View File

@ -159,12 +159,12 @@ class FunDB(callbacks.Privmsg):
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'There are currently no available insults.') irc.error(msg, 'There are currently no available insults.')
return return
else:
(id, insult) = cursor.fetchone() (id, insult) = cursor.fetchone()
sql = """UPDATE insults SET use_count=use_count+1, requested_by=%s sql = """UPDATE insults SET use_count=use_count+1, requested_by=%s
WHERE id=%s""" WHERE id=%s"""
cursor.execute(sql, msg.prefix, id) cursor.execute(sql, msg.prefix, id)
if nick.strip() in (irc.nick, 'himself', 'me'): nick = nick.strip()
if nick in (irc.nick, 'yourself', 'me'):
insultee = msg.nick insultee = msg.nick
else: else:
insultee = nick insultee = nick
@ -380,6 +380,7 @@ class FunDB(callbacks.Privmsg):
lartee = nick lartee = nick
lart = lart.replace("$who", lartee) lart = lart.replace("$who", lartee)
irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (lart, id))) irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (lart, id)))
raise callbacks.CannotNest
def praise(self, irc, msg, args): def praise(self, irc, msg, args):
"""[<channel>] <nick> """[<channel>] <nick>
@ -408,6 +409,7 @@ class FunDB(callbacks.Privmsg):
praisee = nick praisee = nick
praise = praise.replace("$who", praisee) praise = praise.replace("$who", praisee)
irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (praise, id))) irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (praise, id)))
raise callbacks.CannotNest
def addword(self, irc, msg, args): def addword(self, irc, msg, args):
"""<word> """<word>

View File

@ -179,6 +179,10 @@ class ArgumentError(Error):
"""The bot replies with a help message when this is raised.""" """The bot replies with a help message when this is raised."""
pass pass
class CannotNest(Error):
"""Exception to be raised by commands that cannot be nested."""
pass
class Tokenizer: class Tokenizer:
# This will be used as a global environment to evaluate strings in. # This will be used as a global environment to evaluate strings in.
# Evaluation is, of course, necessary in order to allowed escaped # Evaluation is, of course, necessary in order to allowed escaped
@ -310,6 +314,9 @@ class IrcObjectProxy:
else: else:
s = 'Invalid arguments for %s.' % name s = 'Invalid arguments for %s.' % name
self.reply(self.msg, s) self.reply(self.msg, s)
except CannotNest, e:
if not isinstance(self.irc, irclib.Irc):
self.error(self.msg, 'Command %r cannot be nested.' % name)
except (SyntaxError, Error), e: except (SyntaxError, Error), e:
self.reply(self.msg, debug.exnToString(e)) self.reply(self.msg, debug.exnToString(e))
except Exception, e: except Exception, e:
@ -330,7 +337,10 @@ class IrcObjectProxy:
self.evalArgs() self.evalArgs()
def error(self, msg, s): def error(self, msg, s):
self.reply(msg, 'Error: ' + s) if isinstance(self.irc, self.__class__):
self.irc.error(msg, s)
else:
self.irc.queueMsg(reply(msg, 'Error: ' + s))
def killProxy(self): def killProxy(self):
if not isinstance(self.irc, irclib.Irc): if not isinstance(self.irc, irclib.Irc):
@ -385,6 +395,10 @@ class CommandThread(threading.Thread):
else: else:
s = 'Invalid arguments for %s.' % self.commandName s = 'Invalid arguments for %s.' % self.commandName
self.irc.reply(self.msg, s) self.irc.reply(self.msg, s)
except CannotNest:
if not isinstance(self.irc.irc, irclib.Irc):
s = 'Command %r cannot be nested.' % self.commandName
self.irc.error(self.msg, s)
except (SyntaxError, Error), e: except (SyntaxError, Error), e:
self.irc.reply(self.msg, debug.exnToString(e)) self.irc.reply(self.msg, debug.exnToString(e))
except Exception, e: except Exception, e:
@ -453,7 +467,7 @@ class Privmsg(irclib.IrcCallback):
args = tokenize(s) args = tokenize(s)
self.Proxy(irc, msg, args) self.Proxy(irc, msg, args)
except SyntaxError, e: except SyntaxError, e:
irc.queueMsg(reply(msg, debug.exnToString(e))) irc.queueMsg(reply(msg, str(e)))
def isCommand(self, methodName): def isCommand(self, methodName):
# This function is ugly, but I don't want users to call methods like # This function is ugly, but I don't want users to call methods like