Added an exception for malformed IRC messages and a little more error checking to the string parsing for IrcMsgs.

This commit is contained in:
Jeremy Fincher 2003-09-17 18:21:35 +00:00
parent 5ace516fb1
commit 41339e80e7
2 changed files with 24 additions and 13 deletions

View File

@ -47,6 +47,9 @@ import ircutils
# IrcMsg class -- used for representing IRC messages acquired from a network. # IrcMsg class -- used for representing IRC messages acquired from a network.
### ###
class MalformedIrcMsg(ValueError):
pass
class IrcMsg(object): class IrcMsg(object):
"""Class to represent an IRC message. """Class to represent an IRC message.
@ -79,12 +82,14 @@ class IrcMsg(object):
def __init__(self, s='', command='', args=None, prefix='', msg=None): def __init__(self, s='', command='', args=None, prefix='', msg=None):
assert not (msg and s), 'Ircmsg.__init__ cannot accept both s and msg' assert not (msg and s), 'Ircmsg.__init__ cannot accept both s and msg'
if not s and not command and not msg: if not s and not command and not msg:
raise ValueError, 'IRC messages require a command.' raise MalformedIrcMsg, 'IRC messages require a command.'
self._str = None self._str = None
self._repr = None self._repr = None
self._hash = None self._hash = None
self._len = None self._len = None
if s: if s:
originalString = s
try:
self._str = s self._str = s
if s[0] == ':': if s[0] == ':':
self.prefix, s = s[1:].split(None, 1) self.prefix, s = s[1:].split(None, 1)
@ -97,6 +102,8 @@ class IrcMsg(object):
else: else:
self.args = s.split() self.args = s.split()
self.command = self.args.pop(0) self.command = self.args.pop(0)
except (IndexError, ValueError):
raise MalformedIrcMsg, repr(originalString)
else: else:
if msg is not None: if msg is not None:
if prefix: if prefix:

View File

@ -110,6 +110,10 @@ class IrcMsgTestCase(unittest.TestCase):
self.assertEqual(msg2.command, 'PRIVMSG') self.assertEqual(msg2.command, 'PRIVMSG')
self.assertEqual(msg2.args, msg.args) self.assertEqual(msg2.args, msg.args)
def testMalformedIrcMsgRaised(self):
self.assertRaises(ircmsgs.MalformedIrcMsg, ircmsgs.IrcMsg, ':foo')
self.assertRaises(ircmsgs.MalformedIrcMsg, ircmsgs.IrcMsg,
args=('biff',), prefix='foo!bar@baz')
class FunctionsTestCase(unittest.TestCase): class FunctionsTestCase(unittest.TestCase):
def testIsAction(self): def testIsAction(self):