def _io_read(self): # TODO: The cost from using a string for our buffer is probably # pretty high. Evaluate alternatives like bytearray. message_buffer = '' while True: gevent.socket.wait_read(self.socket.fileno()) message_chunk = self.socket.recv(self._chunk_size) if not message_chunk: # If recv() returned but the result is empty, then # the remote end disconnected. break message_buffer += message_chunk while '\r\n' in message_buffer: line, message_buffer = message_buffer.split('\r\n', 1) message = unpack_message(line) gevent.spawn( signals.on_raw_message.send, self, prefix=message[0], command=message[1], args=message[2] ) gevent.spawn( getattr(signals.m, 'on_' + message[1]).send, self, prefix=message[0], args=message[2] )
def test_parse_full_prefix(): """ Ensure we can parse a message with full prefix (nick, user, host). """ prefix, command, args = unpack_message( ':[email protected] JOIN :#test\r\n' ) assert(prefix == ('TestNick', 'TestUsername', 'test.host')) assert(command == 'JOIN') assert(len(args) == 1) assert(args[0] == '#test')
def test_parse_no_prefix(): """ Ensure we can parse a message that has no prefix. """ prefix, command, args = unpack_message( 'ERROR :Closing Link: failed to pass the test.\r\n' ) assert(prefix is None) assert(command == 'ERROR') assert(len(args) == 1) assert(args[0] == 'Closing Link: failed to pass the test.')
def test_parse_server_prefix(): """ Ensure we can parse a message with a server-origin prefix. """ prefix, command, args = unpack_message( ':irc.test.host 001 TestNick :Welcome to the test server!\r\n' ) assert(prefix == ('irc.test.host', None, None)) assert(command == '001') assert(len(args) == 2) assert(args[0] == 'TestNick') assert(args[1] == 'Welcome to the test server!')
def test_parse_005(): """ Ensure we can parse a 005 message and unpack it correctly. """ prefix, command, args = unpack_message( ':rajaniemi.freenode.net 005 utopiatestbot123 CHANTYPES=# EXCEPTS ' 'INVEX CHANMODES=eIbq,k,flj,CFLMPQScgimnprstz CHANLIMIT=#:120 ' 'PREFIX=(ov)@+ MAXLIST=bqeI:100 MODES=4 NETWORK=freenode KNOCK ' 'STATUSMSG=@+ CALLERID=g :are supported by this server' ) assert(prefix == ('rajaniemi.freenode.net', None, None)) assert(prefix.nick == 'rajaniemi.freenode.net') assert(prefix.user is None) assert(prefix.host is None) assert(command == '005') assert(args == [ 'utopiatestbot123', 'CHANTYPES=#', 'EXCEPTS', 'INVEX', 'CHANMODES=eIbq,k,flj,CFLMPQScgimnprstz', 'CHANLIMIT=#:120', 'PREFIX=(ov)@+', 'MAXLIST=bqeI:100', 'MODES=4', 'NETWORK=freenode', 'KNOCK', 'STATUSMSG=@+', 'CALLERID=g', 'are supported by this server' ]) r, p = unpack_005(args) assert(r == ['EXCEPTS', 'INVEX', 'KNOCK']) assert(p == { 'CALLERID': 'g', 'CHANLIMIT': {'#': 120}, 'CHANMODES': ('eIbq', 'k', 'flj', 'CFLMPQScgimnprstz'), 'CHANTYPES': ('#',), 'MAXLIST': {'bqeI': 100}, 'MODES': 4, 'NETWORK': 'freenode', 'PREFIX': {'o': '@', 'v': '+'}, 'STATUSMSG': ('@', '+') })
def _timed_parse(): prefix, command, args = unpack_message( ':[email protected] JOIN :#test\r\n' )