Guarded asserts with strictRfc.

This commit is contained in:
Jeremy Fincher 2004-02-24 11:21:12 +00:00
parent 8b23e0ff78
commit de512b24de
4 changed files with 131 additions and 101 deletions

View File

@ -322,8 +322,7 @@ if __name__ == '__main__':
import callbacks import callbacks
import Owner import Owner
import ircutils conf.strictRfc = options.strictRfc
ircutils.strictRfc = options.strictRfc
irc = irclib.Irc(nick, user, ident, password) irc = irclib.Irc(nick, user, ident, password)
callback = Owner.Class() callback = Owner.Class()

View File

@ -53,6 +53,12 @@ _pluginsDir = os.path.join(installDir, 'plugins')
### ###
allowEval = False allowEval = False
###
# strictRfc: Determines whether the bot will very strictly follow the RCE
# or whether it will allow things like @ and . in nicks.
###
strictRfc = False
supybot = registry.Group() supybot = registry.Group()
supybot.setName('supybot') supybot.setName('supybot')

View File

@ -43,6 +43,7 @@ import fix
import re import re
import string import string
import conf
import ircutils import ircutils
### ###
@ -282,22 +283,26 @@ isUserHostmask = ircutils.isUserHostmask
def pong(payload, prefix=''): def pong(payload, prefix=''):
"""Takes a payload and returns the proper PONG IrcMsg.""" """Takes a payload and returns the proper PONG IrcMsg."""
if conf.strictRfc:
assert payload, 'PONG requires a payload' assert payload, 'PONG requires a payload'
return IrcMsg(prefix=prefix, command='PONG', args=(payload,)) return IrcMsg(prefix=prefix, command='PONG', args=(payload,))
def ping(payload, prefix=''): def ping(payload, prefix=''):
"""Takes a payload and returns the proper PING IrcMsg.""" """Takes a payload and returns the proper PING IrcMsg."""
if conf.strictRfc:
assert payload, 'PING requires a payload' assert payload, 'PING requires a payload'
return IrcMsg(prefix=prefix, command='PING', args=(payload,)) return IrcMsg(prefix=prefix, command='PING', args=(payload,))
def op(channel, nick, prefix=''): def op(channel, nick, prefix=''):
"""Returns a MODE to op nick on channel.""" """Returns a MODE to op nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+o', nick)) return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+o', nick))
def ops(channel, nicks, prefix=''): def ops(channel, nicks, prefix=''):
"""Returns a MODE to op each of nicks on channel.""" """Returns a MODE to op each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert nicks, 'Nicks must not be empty.' assert nicks, 'Nicks must not be empty.'
assert all(isNick, nicks), nicks assert all(isNick, nicks), nicks
@ -306,12 +311,14 @@ def ops(channel, nicks, prefix=''):
def deop(channel, nick, prefix=''): def deop(channel, nick, prefix=''):
"""Returns a MODE to deop nick on channel.""" """Returns a MODE to deop nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-o', nick)) return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-o', nick))
def deops(channel, nicks, prefix=''): def deops(channel, nicks, prefix=''):
"""Returns a MODE to deop each of nicks on channel.""" """Returns a MODE to deop each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert nicks, 'Nicks must not be empty.' assert nicks, 'Nicks must not be empty.'
assert all(isNick, nicks), nicks assert all(isNick, nicks), nicks
@ -320,12 +327,14 @@ def deops(channel, nicks, prefix=''):
def halfop(channel, nick, prefix=''): def halfop(channel, nick, prefix=''):
"""Returns a MODE to halfop nick on channel.""" """Returns a MODE to halfop nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+h', nick)) return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+h', nick))
def halfops(channel, nicks, prefix=''): def halfops(channel, nicks, prefix=''):
"""Returns a MODE to halfop each of nicks on channel.""" """Returns a MODE to halfop each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert nicks, 'Nicks must not be empty.' assert nicks, 'Nicks must not be empty.'
assert all(isNick, nicks), nicks assert all(isNick, nicks), nicks
@ -335,12 +344,14 @@ def halfops(channel, nicks, prefix=''):
def dehalfop(channel, nick, prefix=''): def dehalfop(channel, nick, prefix=''):
"""Returns a MODE to dehalfop nick on channel.""" """Returns a MODE to dehalfop nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-h', nick)) return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-h', nick))
def dehalfops(channel, nicks, prefix=''): def dehalfops(channel, nicks, prefix=''):
"""Returns a MODE to dehalfop each of nicks on channel.""" """Returns a MODE to dehalfop each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert nicks, 'Nicks must not be empty.' assert nicks, 'Nicks must not be empty.'
assert all(isNick, nicks), nicks assert all(isNick, nicks), nicks
@ -349,12 +360,14 @@ def dehalfops(channel, nicks, prefix=''):
def voice(channel, nick, prefix=''): def voice(channel, nick, prefix=''):
"""Returns a MODE to voice nick on channel.""" """Returns a MODE to voice nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+v', nick)) return IrcMsg(prefix=prefix, command=MODE, args=(channel, '+v', nick))
def voices(channel, nicks, prefix=''): def voices(channel, nicks, prefix=''):
"""Returns a MODE to voice each of nicks on channel.""" """Returns a MODE to voice each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert nicks, 'Nicks must not be empty.' assert nicks, 'Nicks must not be empty.'
assert all(isNick, nicks) assert all(isNick, nicks)
@ -363,12 +376,14 @@ def voices(channel, nicks, prefix=''):
def devoice(channel, nick, prefix=''): def devoice(channel, nick, prefix=''):
"""Returns a MODE to devoice nick on channel.""" """Returns a MODE to devoice nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-v', nick)) return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-v', nick))
def devoices(channel, nicks, prefix=''): def devoices(channel, nicks, prefix=''):
"""Returns a MODE to devoice each of nicks on channel.""" """Returns a MODE to devoice each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert nicks, 'Nicks must not be empty.' assert nicks, 'Nicks must not be empty.'
assert all(isNick, nicks), nicks assert all(isNick, nicks), nicks
@ -377,6 +392,7 @@ def devoices(channel, nicks, prefix=''):
def ban(channel, hostmask, exception='', prefix=''): def ban(channel, hostmask, exception='', prefix=''):
"""Returns a MODE to ban nick on channel.""" """Returns a MODE to ban nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isUserHostmask(hostmask), repr(hostmask) assert isUserHostmask(hostmask), repr(hostmask)
modes = [('+b', hostmask)] modes = [('+b', hostmask)]
@ -387,6 +403,7 @@ def ban(channel, hostmask, exception='', prefix=''):
def bans(channel, hostmasks, exceptions=(), prefix=''): def bans(channel, hostmasks, exceptions=(), prefix=''):
"""Returns a MODE to ban each of nicks on channel.""" """Returns a MODE to ban each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert all(isUserHostmask, hostmasks), hostmasks assert all(isUserHostmask, hostmasks), hostmasks
modes = [('+b', s) for s in hostmasks] + [('+e', s) for s in exceptions] modes = [('+b', s) for s in hostmasks] + [('+e', s) for s in exceptions]
@ -395,12 +412,14 @@ def bans(channel, hostmasks, exceptions=(), prefix=''):
def unban(channel, hostmask, prefix=''): def unban(channel, hostmask, prefix=''):
"""Returns a MODE to unban nick on channel.""" """Returns a MODE to unban nick on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isUserHostmask(hostmask), repr(hostmask) assert isUserHostmask(hostmask), repr(hostmask)
return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-b', hostmask)) return IrcMsg(prefix=prefix, command=MODE, args=(channel, '-b', hostmask))
def unbans(channel, hostmasks, prefix=''): def unbans(channel, hostmasks, prefix=''):
"""Returns a MODE to unban each of nicks on channel.""" """Returns a MODE to unban each of nicks on channel."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert all(isUserHostmask, hostmasks), hostmasks assert all(isUserHostmask, hostmasks), hostmasks
return IrcMsg(prefix=prefix, command=MODE, return IrcMsg(prefix=prefix, command=MODE,
@ -408,6 +427,7 @@ def unbans(channel, hostmasks, prefix=''):
def kick(channel, nick, msg='', prefix=''): def kick(channel, nick, msg='', prefix=''):
"""Returns a KICK to kick nick from channel with the message msg.""" """Returns a KICK to kick nick from channel with the message msg."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
if msg: if msg:
@ -418,6 +438,7 @@ def kick(channel, nick, msg='', prefix=''):
def kicks(channel, nicks, msg='', prefix=''): def kicks(channel, nicks, msg='', prefix=''):
"""Returns a KICK to kick each of nicks from channel with the message msg. """Returns a KICK to kick each of nicks from channel with the message msg.
""" """
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
assert all(isNick, nicks), nicks assert all(isNick, nicks), nicks
if msg: if msg:
@ -429,28 +450,33 @@ def kicks(channel, nicks, msg='', prefix=''):
def privmsg(recipient, msg, prefix=''): def privmsg(recipient, msg, prefix=''):
"""Returns a PRIVMSG to recipient with the message msg.""" """Returns a PRIVMSG to recipient with the message msg."""
if conf.strictRfc:
assert (isChannel(recipient) or isNick(recipient)), repr(recipient) assert (isChannel(recipient) or isNick(recipient)), repr(recipient)
assert msg, 'msg must not be empty.' assert msg, 'msg must not be empty.'
return IrcMsg(prefix=prefix, command='PRIVMSG', args=(recipient, msg)) return IrcMsg(prefix=prefix, command='PRIVMSG', args=(recipient, msg))
def action(recipient, msg, prefix=''): def action(recipient, msg, prefix=''):
"""Returns a PRIVMSG ACTION to recipient with the message msg.""" """Returns a PRIVMSG ACTION to recipient with the message msg."""
if conf.strictRfc:
assert (isChannel(recipient) or isNick(recipient)), repr(recipient) assert (isChannel(recipient) or isNick(recipient)), repr(recipient)
return IrcMsg(prefix=prefix, command='PRIVMSG', return IrcMsg(prefix=prefix, command='PRIVMSG',
args=(recipient,'\x01ACTION %s\x01'% msg)) args=(recipient,'\x01ACTION %s\x01'% msg))
def notice(recipient, msg, prefix=''): def notice(recipient, msg, prefix=''):
"""Returns a NOTICE to recipient with the message msg.""" """Returns a NOTICE to recipient with the message msg."""
if conf.strictRfc:
assert (isChannel(recipient) or isNick(recipient)), repr(recipient) assert (isChannel(recipient) or isNick(recipient)), repr(recipient)
assert msg, 'msg must not be empty.' assert msg, 'msg must not be empty.'
return IrcMsg(prefix=prefix, command='NOTICE', args=(recipient, msg)) return IrcMsg(prefix=prefix, command='NOTICE', args=(recipient, msg))
def join(channel, key=None, prefix=''): def join(channel, key=None, prefix=''):
"""Returns a JOIN to a channel""" """Returns a JOIN to a channel"""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
if key is None: if key is None:
return IrcMsg(prefix=prefix, command='JOIN', args=(channel,)) return IrcMsg(prefix=prefix, command='JOIN', args=(channel,))
else: else:
if conf.strictRfc:
assert key.translate(string.ascii, string.ascii[128:]) == key and \ assert key.translate(string.ascii, string.ascii[128:]) == key and \
'\x00' not in key and \ '\x00' not in key and \
'\r' not in key and \ '\r' not in key and \
@ -463,16 +489,18 @@ def join(channel, key=None, prefix=''):
def joins(channels, keys=None, prefix=''): def joins(channels, keys=None, prefix=''):
"""Returns a JOIN to each of channels.""" """Returns a JOIN to each of channels."""
if conf.strictRfc:
assert all(isChannel, channels), channels assert all(isChannel, channels), channels
if keys is None: if keys is None:
keys = [] keys = []
assert len(keys) <= len(channels) assert len(keys) <= len(channels), 'Got more keys than channels.'
if not keys: if not keys:
return IrcMsg(prefix=prefix, return IrcMsg(prefix=prefix,
command='JOIN', command='JOIN',
args=(','.join(channels),)) args=(','.join(channels),))
else: else:
for key in keys: for key in keys:
if conf.strictRfc:
assert key.translate(string.ascii,string.ascii[128:])==key and\ assert key.translate(string.ascii,string.ascii[128:])==key and\
'\x00' not in key and \ '\x00' not in key and \
'\r' not in key and \ '\r' not in key and \
@ -487,6 +515,7 @@ def joins(channels, keys=None, prefix=''):
def part(channel, msg='', prefix=''): def part(channel, msg='', prefix=''):
"""Returns a PART from channel with the message msg.""" """Returns a PART from channel with the message msg."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
if msg: if msg:
return IrcMsg(prefix=prefix, command='PART', args=(channel, msg)) return IrcMsg(prefix=prefix, command='PART', args=(channel, msg))
@ -495,6 +524,7 @@ def part(channel, msg='', prefix=''):
def parts(channels, msg='', prefix=''): def parts(channels, msg='', prefix=''):
"""Returns a PART from each of channels with the message msg.""" """Returns a PART from each of channels with the message msg."""
if conf.strictRfc:
assert all(isChannel, channels), channels assert all(isChannel, channels), channels
if msg: if msg:
return IrcMsg(prefix=prefix, command='PART', return IrcMsg(prefix=prefix, command='PART',
@ -512,6 +542,7 @@ def quit(msg='', prefix=''):
def topic(channel, topic=None, prefix=''): def topic(channel, topic=None, prefix=''):
"""Returns a TOPIC for channel with the topic topic.""" """Returns a TOPIC for channel with the topic topic."""
if conf.strictRfc:
assert isChannel(channel), repr(channel) assert isChannel(channel), repr(channel)
if topic is None: if topic is None:
return IrcMsg(prefix=prefix, command='TOPIC', args=(channel,)) return IrcMsg(prefix=prefix, command='TOPIC', args=(channel,))
@ -520,11 +551,13 @@ def topic(channel, topic=None, prefix=''):
def nick(nick, prefix=''): def nick(nick, prefix=''):
"""Returns a NICK with nick nick.""" """Returns a NICK with nick nick."""
if conf.strictRfc:
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command='NICK', args=(nick,)) return IrcMsg(prefix=prefix, command='NICK', args=(nick,))
def user(ident, user, prefix=''): def user(ident, user, prefix=''):
"""Returns a USER with ident ident and user user.""" """Returns a USER with ident ident and user user."""
if conf.strictRfc:
assert '\x00' not in ident and \ assert '\x00' not in ident and \
'\r' not in ident and \ '\r' not in ident and \
'\n' not in ident and \ '\n' not in ident and \
@ -534,12 +567,14 @@ def user(ident, user, prefix=''):
def who(hostmaskOrChannel, prefix=''): def who(hostmaskOrChannel, prefix=''):
"""Returns a WHO for the hostmask or channel hostmaskOrChannel.""" """Returns a WHO for the hostmask or channel hostmaskOrChannel."""
assert isChannel(hostmaskOrChannel) or isUserHostmask(hostmaskOrChannel), \ if conf.strictRfc:
repr(hostmaskOrChannel) assert isChannel(hostmaskOrChannel) or \
isUserHostmask(hostmaskOrChannel), repr(hostmaskOrChannel)
return IrcMsg(prefix=prefix, command='WHO', args=(hostmaskOrChannel,)) return IrcMsg(prefix=prefix, command='WHO', args=(hostmaskOrChannel,))
def whois(nick, mask='', prefix=''): def whois(nick, mask='', prefix=''):
"""Returns a WHOIS for nick.""" """Returns a WHOIS for nick."""
if conf.strictRfc:
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command='WHOIS', args=(nick, mask)) return IrcMsg(prefix=prefix, command='WHOIS', args=(nick, mask))
@ -552,11 +587,13 @@ def mode(channel, args=None, prefix=''):
def invite(nick, channel, prefix=''): def invite(nick, channel, prefix=''):
"""Returns an INVITE for nick.""" """Returns an INVITE for nick."""
if conf.strictRfc:
assert isNick(nick), repr(nick) assert isNick(nick), repr(nick)
return IrcMsg(prefix=prefix, command='INVITE', args=(nick, channel)) return IrcMsg(prefix=prefix, command='INVITE', args=(nick, channel))
def password(password, prefix=''): def password(password, prefix=''):
"""Returns a PASS command for accessing a server.""" """Returns a PASS command for accessing a server."""
if conf.strictRfc:
assert password, 'password must not be empty.' assert password, 'password must not be empty.'
return IrcMsg(prefix=prefix, command='PASS', args=(password,)) return IrcMsg(prefix=prefix, command='PASS', args=(password,))

View File

@ -54,12 +54,6 @@ from cStringIO import StringIO as sio
import utils import utils
###
# strictRfc: Determines whether the bot will very strictly follow the RCE
# or whether it will allow things like @ and . in nicks.
###
strictRfc = False
def isUserHostmask(s): def isUserHostmask(s):
"""Returns whether or not the string s is a valid User hostmask.""" """Returns whether or not the string s is a valid User hostmask."""
p1 = s.find('!') p1 = s.find('!')
@ -112,17 +106,11 @@ def nickEqual(nick1, nick2):
_nickchars = r'_[]\`^{}|-' _nickchars = r'_[]\`^{}|-'
strictNickRe = re.compile(r'^[A-Za-z%s][0-9A-Za-z%s]*$'
% (re.escape(_nickchars), re.escape(_nickchars)))
_nickchars += '@.'
nickRe = re.compile(r'^[A-Za-z%s][0-9A-Za-z%s]*$' nickRe = re.compile(r'^[A-Za-z%s][0-9A-Za-z%s]*$'
% (re.escape(_nickchars), re.escape(_nickchars))) % (re.escape(_nickchars), re.escape(_nickchars)))
def isNick(s): def isNick(s):
"""Returns True if s is a valid IRC nick.""" """Returns True if s is a valid IRC nick."""
if strictRfc:
return bool(strictNickRe.match(s))
else:
return bool(nickRe.match(s)) return bool(nickRe.match(s))
def isChannel(s): def isChannel(s):