def _process_send(self, conn, data): from_name = data[0] to_name = data[1] msg = data[2] from_user = self.conn2user(conn) error = '' if from_user is None: error = 'attempting to send a message before logging in, for message: from: %s | to: %s | body: %s' % (from_name, to_name, msg) elif from_name != from_user.username: error = 'specified from name is not the sender. Got username %s, current username is %s, for message: from: %s | to: %s | body: %s' % (from_name, from_user.username, from_name, to_name, msg) elif to_name not in [u.username for u in self.users]: error = 'specific recipient %s does not exist, for message: from: %s | to: %s | body: %s' % (to_name, from_name, to_name, msg) if error: conn.send_buffer += WireProtocol.data_to_bytes(CMD.RESPONSE, error) return to_user = [u for u in self.users if u.username == to_name][0] # Send to recipient immediately if they are online if to_user.connection is not None: to_user.connection.send_buffer += WireProtocol.data_to_bytes(CMD.SEND, *data) else: # If they're not online, add to undelivered message list to_user.undelivered_messages.append(data) # In any event, send back to the sender for printing from_user.connection.send_buffer += WireProtocol.data_to_bytes(CMD.SEND, *data)
def test_data_to_bytes_multiarg(): command = CMD.SEND to_str = 'toname' # 6 bytes from_str = 'fromname' # 8 bytes msg = 'hello world!' # 12 bytes res = WireProtocol.data_to_bytes(command, to_str, from_str, msg) # total len is 26 + 3*2 = 32 assert res == b'\x00\x02' + b'\x00' * 39 + b'\x20' + b'toname|||fromname|||hello world!'
def _process_deliver(self, conn, data): print('conn uuid: %s' % str(conn.uuid)) for u in self.users: print(u.username) print(u.connection) if u.connection: print(u.connection.uuid) from_user = self.conn2user(conn) error = '' if from_user is None: error = 'attempting to deliver undelivered messages before logging in' conn.send_buffer += WireProtocol.data_to_bytes(CMD.RESPONSE, error) return for msg in from_user.undelivered_messages: conn.send_buffer += WireProtocol.data_to_bytes(CMD.SEND, *msg) from_user.undelivered_messages = []
def _process_create(self, conn, data): username = data error = '' if username in [u.username for u in self.users]: error = 'username already exists' elif CMD.DELIM.decode('ascii') in username: error = 'illegal character sequence %s in username' % CMD.DELIM.decode('ascii') else: user = User(username) user.login(conn) self.users.append(user) # send response with potential error conn.send_buffer += WireProtocol.data_to_bytes(CMD.RESPONSE, error)
def _process_login(self, conn, data): username = data error = '' if username not in [u.username for u in self.users]: error = 'username does not exist' else: user = [u for u in self.users if u.username == username][0] # check if user is already logged in if user.connection is not None: error = 'user is already logged in' else: user.login(conn) print('user logged in, username: %s, conn %s' % (user.username, user.connection)) # send response with potential error conn.send_buffer += WireProtocol.data_to_bytes(CMD.RESPONSE, error)
def test_data_to_bytes_singlearg(): command = CMD.CREATE username = '******' res = WireProtocol.data_to_bytes(command, username) assert res == b'\x00\x00' + b'\x00' * 39 + b'\x08' + b'testname'
def _process_list(self, conn, data): # check if we need to filter by anything names = [u.username for u in self.users] if data is not None: names = [n for n in names if fnmatch.fnmatch(n, data)] conn.send_buffer += WireProtocol.data_to_bytes(CMD.LISTRESPONSE, *names)
def send_to_server(self, command, *args): msg = WireProtocol.data_to_bytes(command, *args) i = 0 while i < len(msg): sent = self.socket.send(msg[i:]) i += sent