Added supybot.reply.whenAddressedByNick.atEnd.

This commit is contained in:
Jeremy Fincher 2004-10-09 02:48:08 +00:00
parent 28185185dd
commit 3dceea823d
3 changed files with 28 additions and 6 deletions

View File

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

View File

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

View File

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