Fixed the wacky addressed problems.

This commit is contained in:
Jeremy Fincher 2004-05-06 15:48:09 +00:00
parent a764359d3d
commit b4f977e2dd
3 changed files with 37 additions and 34 deletions

View File

@ -65,21 +65,29 @@ import plugins
import ircmsgs
import ircutils
def addressed(nick, msg):
def addressed(nick, msg, prefixChars=None, whenAddressedByNick=None):
"""If msg is addressed to 'name', returns the portion after the address.
Otherwise returns the empty string.
"""
assert msg.command == 'PRIVMSG'
if prefixChars is None:
prefixChars = conf.supybot.prefixChars()
if whenAddressedByNick is None:
whenAddressedByNick = conf.supybot.reply.whenAddressedByNick()
nick = ircutils.toLower(nick)
if ircutils.nickEqual(msg.args[0], nick):
if msg.args[1][0] in conf.supybot.prefixChars():
return msg.args[1][1:].strip()
(target, payload) = msg.args
# Ok, let's see if it's a private message.
if ircutils.nickEqual(target, nick):
if payload[0] in prefixChars:
return payload[1:].strip()
else:
return msg.args[1].strip()
elif conf.supybot.reply.whenAddressedByNick() and \
ircutils.toLower(msg.args[1]).startswith(nick):
return payload.strip()
# Ok, not private. Does it start with our nick?
elif whenAddressedByNick and \
ircutils.toLower(payload).startswith(nick):
try:
(maybeNick, rest) = msg.args[1].split(None, 1)
while not ircutils.isNick(maybeNick):
(maybeNick, rest) = payload.split(None, 1)
while not ircutils.isNick(maybeNick, strictRfc=True):
if maybeNick[-1].isalnum():
return ''
maybeNick = maybeNick[:-1]
@ -89,10 +97,10 @@ def addressed(nick, msg):
return ''
except ValueError: # split didn't work.
return ''
elif msg.args[1] and msg.args[1][0] in conf.supybot.prefixChars():
return msg.args[1][1:].strip()
elif payload and payload[0] in prefixChars:
return payload[1:].strip()
elif conf.supybot.reply.whenNotAddressed():
return msg.args[1]
return payload
else:
return ''
@ -824,7 +832,7 @@ class Privmsg(irclib.IrcCallback):
return group.get(id)()
def setUserValue(self, prefixOrName, name, value,
ignoreNoUser=False, setValue=False):
ignoreNoUser=True, setValue=False):
try:
id = str(ircdb.users.getUserId(prefixOrName))
except KeyError:

View File

@ -494,28 +494,20 @@ registerGlobalValue(supybot.databases.channels, 'filename',
###
# Protocol information.
###
class StrictRfc(registry.Boolean):
def __init__(self, *args, **kwargs):
self.originalIsNick = ircutils.isNick
registry.Boolean.__init__(self, *args, **kwargs)
def setValue(self, v):
registry.Boolean.setValue(self, v)
# Now let's replace ircutils.isNick.
if self.value:
ircutils.isNick = self.originalIsNick
else:
def unstrictIsNick(s):
return not ircutils.isChannel(s)
ircutils.isNick = unstrictIsNick
originalIsNick = ircutils.isNick
def isNick(s, strictRfc=None):
if strictRfc is None:
strictRfc = supybot.protocols.irc.strictRfc()
return originalIsNick(s, strictRfc=strictRfc)
ircutils.isNick = isNick
registerGroup(supybot, 'protocols')
registerGroup(supybot.protocols, 'irc')
registerGlobalValue(supybot.protocols.irc, 'strictRfc',
StrictRfc(False, """Determines whether the bot will strictly follow the
RFC; currently this only affects what strings are considered to be nicks.
If you're using a server or a network that requires you to message a nick
such as services@this.network.server then you you should set this to
registry.Boolean(False, """Determines whether the bot will strictly follow
the RFC; currently this only affects what strings are considered to be
nicks. If you're using a server or a network that requires you to message
a nick such as services@this.network.server then you you should set this to
False."""))
registerGlobalValue(supybot.protocols.irc, 'maxHistoryLength',

View File

@ -110,9 +110,12 @@ _nickchars = r'_[]\`^{}|-'
nickRe = re.compile(r'^[A-Za-z%s][0-9A-Za-z%s]*$'
% (re.escape(_nickchars), re.escape(_nickchars)))
def isNick(s):
def isNick(s, strictRfc=True):
"""Returns True if s is a valid IRC nick."""
return bool(nickRe.match(s))
if strictRfc:
return bool(nickRe.match(s))
else:
return not isChannel(s)
def isChannel(s):
"""Returns True if s is a valid IRC channel name."""