diff --git a/src/callbacks.py b/src/callbacks.py index 16477b4d6..2f5113ca9 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -66,7 +66,8 @@ import supybot.ircutils as ircutils import supybot.registry as registry def addressed(nick, msg, prefixChars=None, nicks=None, - prefixStrings=None, whenAddressedByNick=None): + prefixStrings=None, whenAddressedByNick=None, + whenAddressedByNickAtEnd=None): """If msg is addressed to 'name', returns the portion after the address. Otherwise returns the empty string. """ @@ -86,6 +87,9 @@ def addressed(nick, msg, prefixChars=None, nicks=None, prefixChars = get(conf.supybot.reply.whenAddressedBy.chars) if whenAddressedByNick is None: whenAddressedByNick = get(conf.supybot.reply.whenAddressedBy.nick) + if whenAddressedByNickAtEnd is None: + r = conf.supybot.reply.whenAddressedBy.nick.atEnd + whenAddressedByNickAtEnd = get(r) if prefixStrings is None: prefixStrings = get(conf.supybot.reply.whenAddressedBy.strings) if nicks is None: @@ -103,7 +107,8 @@ def addressed(nick, msg, prefixChars=None, nicks=None, # Ok, not private. Does it start with our nick? elif whenAddressedByNick: for nick in nicks: - if ircutils.toLower(payload).startswith(nick): + lowered = ircutils.toLower(payload) + if lowered.startswith(nick): try: (maybeNick, rest) = payload.split(None, 1) while not ircutils.isNick(maybeNick, strictRfc=True): @@ -116,6 +121,13 @@ def addressed(nick, msg, prefixChars=None, nicks=None, continue except ValueError: # split didn't work. continue + elif whenAddressedByNickAtEnd and lowered.endswith(nick): + rest = payload[:-len(nick)] + possiblePayload = rest.rstrip(' \t,;') + if possiblePayload != rest: + # There should be some separator between the nick and the + # previous alphanumeric character. + return possiblePayload if payload and any(payload.startswith, prefixStrings): return stripPrefixStrings(payload) elif payload and payload[0] in prefixChars: diff --git a/src/conf.py b/src/conf.py index 1bad0f677..9cc9d301d 100644 --- a/src/conf.py +++ b/src/conf.py @@ -410,11 +410,13 @@ registerChannelValue(supybot.reply.whenAddressedBy, 'strings', them), this variable is a space-separated list of strings, so you can set something like '@@ ??' and the bot will reply when a message is prefixed by either @@ or ??.""")) - registerChannelValue(supybot.reply.whenAddressedBy, 'nick', registry.Boolean(True, """Determines whether the bot will reply when people address it by its nick, rather than with a prefix character.""")) - +registerChannelValue(supybot.reply.whenAddressedBy.nick, 'atEnd', + registry.Boolean(False, """Determines whether the bot will reply when people + address it by its nick at the end of the message, rather than at the + beginning.""")) registerChannelValue(supybot.reply.whenAddressedBy, 'nicks', registry.SpaceSeparatedSetOfStrings([], """Determines what extra nicks the bot will always respond to when addressed by, even if its current nick is diff --git a/test/test_callbacks.py b/test/test_callbacks.py index 4fd5068c1..00ab3376e 100644 --- a/test/test_callbacks.py +++ b/test/test_callbacks.py @@ -203,8 +203,16 @@ class FunctionsTestCase(SupyTestCase): def testAddressedWithMultipleNicks(self): msg = ircmsgs.privmsg('#foo', 'bar: baz') - self.failUnless(callbacks.addressed('bar', msg)) - self.failUnless(callbacks.addressed('biff', msg, nicks=['bar'])) + self.assertEqual(callbacks.addressed('bar', msg), 'baz') + self.assertEqual(callbacks.addressed('biff', msg, nicks=['bar']), + 'baz') + + def testAddressedWithNickAtEnd(self): + msg = ircmsgs.privmsg('#foo', 'baz, bar') + self.assertEqual(callbacks.addressed('bar', msg, + whenAddressedByNickAtEnd=True), + 'baz') + def testReply(self): prefix = 'foo!bar@baz'