mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-24 19:52:54 +01:00
Added an exception for malformed IRC messages and a little more error checking to the string parsing for IrcMsgs.
This commit is contained in:
parent
5ace516fb1
commit
41339e80e7
@ -47,6 +47,9 @@ import ircutils
|
||||
# IrcMsg class -- used for representing IRC messages acquired from a network.
|
||||
###
|
||||
|
||||
class MalformedIrcMsg(ValueError):
|
||||
pass
|
||||
|
||||
class IrcMsg(object):
|
||||
"""Class to represent an IRC message.
|
||||
|
||||
@ -79,24 +82,28 @@ class IrcMsg(object):
|
||||
def __init__(self, s='', command='', args=None, prefix='', msg=None):
|
||||
assert not (msg and s), 'Ircmsg.__init__ cannot accept both s and 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._repr = None
|
||||
self._hash = None
|
||||
self._len = None
|
||||
if s:
|
||||
self._str = s
|
||||
if s[0] == ':':
|
||||
self.prefix, s = s[1:].split(None, 1)
|
||||
else:
|
||||
self.prefix = ''
|
||||
if ' :' in s:
|
||||
s, last = s.split(' :', 1)
|
||||
self.args = s.split()
|
||||
self.args.append(last.rstrip('\r\n'))
|
||||
else:
|
||||
self.args = s.split()
|
||||
self.command = self.args.pop(0)
|
||||
originalString = s
|
||||
try:
|
||||
self._str = s
|
||||
if s[0] == ':':
|
||||
self.prefix, s = s[1:].split(None, 1)
|
||||
else:
|
||||
self.prefix = ''
|
||||
if ' :' in s:
|
||||
s, last = s.split(' :', 1)
|
||||
self.args = s.split()
|
||||
self.args.append(last.rstrip('\r\n'))
|
||||
else:
|
||||
self.args = s.split()
|
||||
self.command = self.args.pop(0)
|
||||
except (IndexError, ValueError):
|
||||
raise MalformedIrcMsg, repr(originalString)
|
||||
else:
|
||||
if msg is not None:
|
||||
if prefix:
|
||||
|
@ -110,6 +110,10 @@ class IrcMsgTestCase(unittest.TestCase):
|
||||
self.assertEqual(msg2.command, 'PRIVMSG')
|
||||
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):
|
||||
def testIsAction(self):
|
||||
|
Loading…
Reference in New Issue
Block a user