def test_serialize_msg(): msg_bytes = bytes.fromhex('f9beb4d976657261636b000000000000000000005df6e0e2') msg = network.parse_msg(BytesIO(msg_bytes)) assert network.serialize_msg(msg) == msg_bytes msg_bytes = bytes.fromhex( 'f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001') msg = network.parse_msg(BytesIO(msg_bytes)) assert network.serialize_msg(msg) == msg_bytes
def test_parse_msg(): msg_bytes = bytes.fromhex('f9beb4d976657261636b000000000000000000005df6e0e2') msg = network.parse_msg(BytesIO(msg_bytes)) assert msg['command'] == b'verack' assert msg['payload_bytes'] == b'' msg_bytes = bytes.fromhex( 'f9beb4d976657273696f6e0000000000650000005f1a69d2721101000100000000000000bc8f5e5400000000010000000000000000000000000000000000ffffc61b6409208d010000000000000000000000000000000000ffffcb0071c0208d128035cbc97953f80f2f5361746f7368693a302e392e332fcf05050001') msg = network.parse_msg(BytesIO(msg_bytes)) assert msg['command'] == b'version' assert msg['payload_bytes'] == msg_bytes[24:]
def muc_message(self, room, sender, text): """Recieved a muc message""" self.recorder.record(sender, text) if text is not None and sender != self.get_my_jid(): matched = False cmdlist = network.parse_msg(text) if cmdlist is not None: self._incoming_commands(sender, cmdlist) else: self._incoming_chat(sender, text)
def replay_to(self, time, players, create_player): """Replay all commands up to a certain point in time""" if self.get_current_time() > time: while self.get_current_time() > time: sender, inv_cmds = self._reverse_log[self._current_pos] self.replay_cmds(self, sender, inv_cmds) self._current_pos -= 1 else: self._current_pos += 1 while self.get_current_time() < time: sender, msg = self._log[self._current_pos][1:3] cmds = network.parse_msg(msg) # Check if the message need to be inverted if len(self._reverse_log) < self._current_pos + 1: if cmds is None: self._reverse_log.append((sender, [])) else: pl = [pl for pl in players if pl.jid == sender] # Dirty hack: create player if it hasn't been yet if len(pl) == 0: player = create_player(self, sender, "") else: player = pl[0] inv_cmds = [] for (cmd, args) in cmds[::-1]: inv_cmds.extend( self._invert_cmd(player, cmd, *args)) self._reverse_log.append((sender, inv_cmds)) if cmds is not None: self.replay_cmds(self, sender, cmds) self._current_pos += 1 continue if len(msg) == 0: self._current_pos += 1 continue # Ignore message if msg[0] == '\\': msg = msg[1:] self.replay_chat(self, sender, msg) self._current_pos += 1 self._current_pos -= 1
def wait_for(sock, stream, commands, testnet=False): """Wait for one of the messages in the list""" # initialize the command we have, which should be None command = None # loop until the command is in the commands we want while command not in commands: # get the next network message msg = network.parse_msg(stream, testnet) # set the command to be evaluated command = msg['command'] # we know how to respond to version and ping, handle that here if command == b'version': # send verack msg_bytes = network.serialize_verack(network.gen_verack()) sock.sendall(msg_bytes) elif command == b'ping': # send pong msg_bytes = network.serialize_verack( network.gen_pong(msg['payload_bytes'])) sock.sendall(msg_bytes) return msg