def __init__(self, port, ip_address, db, username, passwd, keys): """Initializes client-server network interface, connects and authenticates with the server. :type db: :class:`messenger.client.client_database.ClientBase` """ threading.Thread.__init__(self) QObject.__init__(self) self._db = db self.username = username self.password = passwd self._socket = None self.keys = keys self.pubkey = None self.connection_init(port, ip_address) try: self.user_list_update() self.contact_list_update() except OSError as err: if err.errno: CLIENT_LOG.critical('Connection lost.') raise ServerError('Server connection lost!') CLIENT_LOG.error('Timed out while updating user lists!') except json.JSONDecodeError: CLIENT_LOG.critical('Connection lost.') raise ServerError('Server connection lost!') self.running = True
def user_list_update(self): """Method for performing a user list update.""" CLIENT_LOG.debug( f'Requesting a list of known users for {self.username}') req = self.form_users_request() with socket_lock: send_message(req, self._socket, CLIENT_LOG) ans = receive_message(self._socket, CLIENT_LOG) if JIM.RESPONSE in ans\ and ans[JIM.RESPONSE] == ResCodes.ACCEPTED: self._db.add_known_users(ans[JIM.DATA_LIST]) else: CLIENT_LOG.error('Failed to update a list of known users!')
def contact_list_update(self): """Method for performing a contact list update.""" CLIENT_LOG.debug( f'Requesting contact list for the user {self.username}') req = self.form_contacts_request() with socket_lock: send_message(req, self._socket, CLIENT_LOG) ans = receive_message(self._socket, CLIENT_LOG) if JIM.RESPONSE in ans\ and ans[JIM.RESPONSE] == ResCodes.ACCEPTED: for contact in ans[JIM.DATA_LIST]: self._db.add_contact(contact) else: CLIENT_LOG.error('Failed to update a contact list!')
def make_key_request(self, user): """Method for performing a public key request""" CLIENT_LOG.debug(f'Requesting a public key for {user}') req = { JIM.ACTION: JIM.Actions.PUBLIC_KEY_REQUEST, JIM.TIME: int(datetime.now().timestamp()), JIM.USER: { JIM.UserData.ACCOUNT_NAME: user, }, } with socket_lock: send_message(req, self._socket, CLIENT_LOG) ans = receive_message(self._socket, CLIENT_LOG) if JIM.RESPONSE in ans and ans[JIM.RESPONSE] == ResCodes.AUTH_PROCESS: return ans[JIM.DATA] else: CLIENT_LOG.error(f"Couldn't retrieve a public key for '{user}'")