From 9550971042b9d9d16ec5da778e8d85b2c360a380 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Wed, 27 Aug 2003 07:45:48 +0000 Subject: [PATCH] Added ability for commands to raise callbacks.CannotNest if they are not nestable. --- plugins/FunDB.py | 32 +++++++++++++++++--------------- src/callbacks.py | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/plugins/FunDB.py b/plugins/FunDB.py index e7f8834a2..aff9cc48f 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -159,22 +159,22 @@ class FunDB(callbacks.Privmsg): if cursor.rowcount == 0: irc.error(msg, 'There are currently no available insults.') return + (id, insult) = cursor.fetchone() + sql = """UPDATE insults SET use_count=use_count+1, requested_by=%s + WHERE id=%s""" + cursor.execute(sql, msg.prefix, id) + nick = nick.strip() + if nick in (irc.nick, 'yourself', 'me'): + insultee = msg.nick else: - (id, insult) = cursor.fetchone() - sql = """UPDATE insults SET use_count=use_count+1, requested_by=%s - WHERE id=%s""" - cursor.execute(sql, msg.prefix, id) - if nick.strip() in (irc.nick, 'himself', 'me'): - insultee = msg.nick - else: - insultee = nick - if ircutils.isChannel(msg.args[0]): - means = msg.args[0] - s = '%s: %s (#%s)' % (insultee, insult, id) - else: - means = insultee - s = insult - irc.queueMsg(ircmsgs.privmsg(means, s)) + insultee = nick + if ircutils.isChannel(msg.args[0]): + means = msg.args[0] + s = '%s: %s (#%s)' % (insultee, insult, id) + else: + means = insultee + s = insult + irc.queueMsg(ircmsgs.privmsg(means, s)) def crossword(self, irc, msg, args): """ @@ -380,6 +380,7 @@ class FunDB(callbacks.Privmsg): lartee = nick lart = lart.replace("$who", lartee) irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (lart, id))) + raise callbacks.CannotNest def praise(self, irc, msg, args): """[] @@ -408,6 +409,7 @@ class FunDB(callbacks.Privmsg): praisee = nick praise = praise.replace("$who", praisee) irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (praise, id))) + raise callbacks.CannotNest def addword(self, irc, msg, args): """ diff --git a/src/callbacks.py b/src/callbacks.py index 28f6a87ec..e99492d8d 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -179,6 +179,10 @@ class ArgumentError(Error): """The bot replies with a help message when this is raised.""" pass +class CannotNest(Error): + """Exception to be raised by commands that cannot be nested.""" + pass + class Tokenizer: # This will be used as a global environment to evaluate strings in. # Evaluation is, of course, necessary in order to allowed escaped @@ -310,6 +314,9 @@ class IrcObjectProxy: else: s = 'Invalid arguments for %s.' % name 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: self.reply(self.msg, debug.exnToString(e)) except Exception, e: @@ -330,7 +337,10 @@ class IrcObjectProxy: self.evalArgs() 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): if not isinstance(self.irc, irclib.Irc): @@ -385,6 +395,10 @@ class CommandThread(threading.Thread): else: s = 'Invalid arguments for %s.' % self.commandName 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: self.irc.reply(self.msg, debug.exnToString(e)) except Exception, e: @@ -453,7 +467,7 @@ class Privmsg(irclib.IrcCallback): args = tokenize(s) self.Proxy(irc, msg, args) except SyntaxError, e: - irc.queueMsg(reply(msg, debug.exnToString(e))) + irc.queueMsg(reply(msg, str(e))) def isCommand(self, methodName): # This function is ugly, but I don't want users to call methods like