From 3af8f1c790e38372d64dbdea7e516bd313939f7d Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 18 Sep 2003 07:26:21 +0000 Subject: [PATCH] Added private and action keywords to IrcObjectProxy.reply and converted FunDB to use them. --- plugins/FunDB.py | 36 +++++++++++++++--------------------- src/callbacks.py | 23 +++++++++++++++++++---- test/test_FunDB.py | 12 ++++++------ test/test_callbacks.py | 13 +++++++++++-- 4 files changed, 51 insertions(+), 33 deletions(-) diff --git a/plugins/FunDB.py b/plugins/FunDB.py index 0c2254407..4fe3a51d8 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -268,8 +268,8 @@ class FunDB(callbacks.Privmsg): sql = """SELECT id FROM %ss WHERE %s=%%s""" % (table, table) cursor.execute(sql, s) id = cursor.fetchone()[0] - response = [conf.replySuccess,'(%s #%s)' % (table,id)] - irc.reply(msg, ' '.join(response)) + response = '%s (%s #%s)' % (conf.replySuccess, table, id) + irc.reply(msg, response) def dbremove(self, irc, msg, args): """ @@ -417,19 +417,17 @@ class FunDB(callbacks.Privmsg): irc.reply(msg, reply) def lart(self, irc, msg, args): - """[] + """ [for ] - The argument is only necessary if the message isn't being - sent in the channel itself. Uses a lart on . + Uses a lart on (giving the reason, if offered). """ - channel = privmsgs.getChannel(msg, args) nick = privmsgs.getArgs(args) try: (nick, reason) = map(' '.join, utils.itersplit('for'.__eq__, nick.split(), 1)) except ValueError: nick = ' '.join(args) - reason = "" + reason = '' cursor = self.db.cursor() cursor.execute("""SELECT id, lart FROM larts WHERE lart NOTNULL @@ -445,26 +443,23 @@ class FunDB(callbacks.Privmsg): lartee = nick lart = lart.replace("$who", lartee) if len(reason) > 0: - irc.queueMsg(ircmsgs.action(channel, '%s for %s (#%s)' %\ - (lart, reason, id))) + s = '%s for %s (#%s)' % (lart, reason, id) else: - irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (lart, id))) - raise callbacks.CannotNest + s = '%s (#%s)' % (lart, id) + irc.reply(msg, s, action=True) def praise(self, irc, msg, args): - """[] + """ [for ] - The argument is only necessary if the message isn't being - sent in the channel itself. Uses a praise on . + Uses a praise on (giving the reason, if offered). """ - channel = privmsgs.getChannel(msg, args) nick = privmsgs.getArgs(args) try: (nick, reason) = map(' '.join, utils.itersplit('for'.__eq__, nick.split(), 1)) except ValueError: nick = ' '.join(args) - reason = "" + reason = '' cursor = self.db.cursor() cursor.execute("""SELECT id, praise FROM praises WHERE praise NOTNULL @@ -474,17 +469,16 @@ class FunDB(callbacks.Privmsg): irc.error(msg, 'There are currently no available praises.') else: (id, praise) = cursor.fetchone() - if nick == irc.nick or nick == 'me': + if nick == msg.nick or nick == 'me': praisee = msg.nick else: praisee = nick praise = praise.replace("$who", praisee) if len(reason) > 0: - irc.queueMsg(ircmsgs.action(channel, '%s for %s (#%s)' %\ - (praise, reason, id))) + s = '%s for %s (#%s)' % (praise, reason, id) else: - irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' %(praise, id))) - raise callbacks.CannotNest + s = '%s (#%s)' % (praise, id) + irc.reply(msg, s, action=True) def addword(self, irc, msg, args): """ diff --git a/src/callbacks.py b/src/callbacks.py index 05eedfd82..3d744f451 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -297,6 +297,8 @@ class IrcObjectProxy: self.args = args self.counter = 0 self.finalEvaled = False + self.action = False + self.private = False self.prefixName = True self.noLengthCheck = False world.commandsProcessed += 1 @@ -369,14 +371,22 @@ class IrcObjectProxy: debug.recoverableException() self.error(self.msg, debug.exnToString(e)) - def reply(self, msg, s, noLengthCheck=False, prefixName=True): - self.noLengthCheck |= noLengthCheck + def reply(self, msg, s, noLengthCheck=False, prefixName=True, + action=False, private=False): + # These use |= or &= based on whether or not they default to True or + # False. Those that default to True use &=; those that default to + # False use |=. + self.action |= action + self.private |= private self.prefixName &= prefixName + self.noLengthCheck |= noLengthCheck if self.finalEvaled: if isinstance(self.irc, self.__class__): self.irc.reply(msg, s, self.noLengthCheck, self.prefixName) elif self.noLengthCheck: self.irc.queueMsg(reply(msg, s, self.prefixName)) + elif self.action: + self.irc.queueMsg(ircmsgs.action(msg.args[0], s)) else: # The size of a PRIVMSG is: # 1 for the colon @@ -404,8 +414,13 @@ class IrcObjectProxy: utils.nItems(len(msgs), 'message', 'more') mask = msg.prefix.split('!', 1)[1] Privmsg._mores[mask] = msgs - Privmsg._mores[msg.nick]=(ircutils.isChannel(msg.args[0]),msgs) - self.irc.queueMsg(reply(msg, response, self.prefixName)) + private = self.private or not ircutils.isChannel(msg.args[0]) + Privmsg._mores[msg.nick] = (private, msgs) + if self.private: + debug.printf('got here!') + self.irc.queueMsg(ircmsgs.privmsg(msg.nick, response)) + else: + self.irc.queueMsg(reply(msg, response, self.prefixName)) else: self.args[self.counter] = s self.evalArgs() diff --git a/test/test_FunDB.py b/test/test_FunDB.py index ba68e0e85..ad0ea6003 100644 --- a/test/test_FunDB.py +++ b/test/test_FunDB.py @@ -44,9 +44,9 @@ class TestFunDB(PluginTestCase, PluginDocumentation): def testLart(self): self.assertNotError('dbadd lart jabs $who') - self.assertResponse('lart #foo jemfinch for being dumb', '\x01ACTION'\ + self.assertResponse('lart jemfinch for being dumb', '\x01ACTION'\ ' jabs jemfinch for being dumb (#1)\x01') - self.assertResponse('lart #foo jemfinch', '\x01ACTION jabs jemfinch'\ + self.assertResponse('lart jemfinch', '\x01ACTION jabs jemfinch'\ ' (#1)\x01') self.assertNotError('dbnum lart') self.assertNotError('dbremove lart 1') @@ -72,10 +72,10 @@ class TestFunDB(PluginTestCase, PluginDocumentation): def testPraise(self): self.assertNotError('dbadd praise pets $who') - self.assertNotError('praise #foo jemfinch') - self.assertResponse('praise #foo jemfinch for being him', '\x01ACTION'\ + self.assertNotError('praise jemfinch') + self.assertResponse('praise jemfinch for being him', '\x01ACTION'\ ' pets jemfinch for being him (#1)\x01') - self.assertResponse('praise #foo jemfinch', '\x01ACTION pets jemfinch'\ + self.assertResponse('praise jemfinch', '\x01ACTION pets jemfinch'\ ' (#1)\x01') self.assertNotError('dbnum praise') self.assertNotError('dbremove praise 1') @@ -107,7 +107,7 @@ class TestFunDB(PluginTestCase, PluginDocumentation): def testDbChange(self): self.assertNotError('dbadd praise teaches $who perl') self.assertNotError('dbchange praise 1 s/perl/python/') - self.assertResponse('praise #foo jemfinch', '\x01ACTION teaches'\ + self.assertResponse('praise jemfinch', '\x01ACTION teaches'\ ' jemfinch python (#1)\x01') self.assertNotError('dbremove praise 1') diff --git a/test/test_callbacks.py b/test/test_callbacks.py index ab09ed5e9..40380ca83 100644 --- a/test/test_callbacks.py +++ b/test/test_callbacks.py @@ -147,9 +147,18 @@ class FunctionsTestCase(unittest.TestCase): ['foo', 'bar', 'baz']) -class PrivmsgTestCase(PluginTestCase): - plugins = ('Utilities',) +class PrivmsgTestCase(ChannelPluginTestCase): + plugins = ('Utilities', 'OwnerCommands') + conf.allowEval = True def testEmptySquareBrackets(self): self.assertResponse('echo []', '[]') + def testSimpleReply(self): + self.assertResponse("eval irc.reply(msg, 'foo')", 'foo') + + def testSimpleReplyAction(self): + self.assertResponse("eval irc.reply(msg, 'foo', action=True)", + '\x01ACTION foo\x01') + + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: