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,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):
"""<word>
@ -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):
"""[<channel>] <nick>
@ -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):
"""<word>

View File

@ -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