def __req_accept_chat_handler(self, i_req, client, *args):
        """ Mathod the handler accept chat request """

        msg = Msg.from_dict(i_req.body)
        if msg.to not in self.users:
            self.logger.warning(f'{msg.to} not found')
            return
        self.__send_to_client(self.users[msg.to],
                              Response(ACCEPT_CHAT, str(msg)))
    def __req_message_handler(self, i_req, client, *args):
        """ Mathod the handler message request """

        other_clients = args[0]
        msg = Msg.from_dict(i_req.body)
        self.storage.user_stat_update(msg.sender, ch_sent=1)
        if msg.to.upper() != 'ALL' and msg.to in self.users:
            self.storage.user_stat_update(msg.to, ch_recv=1)
            self.storage.add_message(msg.sender, msg.to, msg.text)
            self.__send_to_client(self.users[msg.to],
                                  Response(LETTER, str(msg)))
        else:
            self.__send_to_all(other_clients, Response(LETTER, str(msg)))
            for u in self.storage.get_users_online():
                if str(u) == msg.sender:
                    continue
                self.storage.user_stat_update(str(u), ch_recv=1)
                self.storage.add_message(msg.sender, str(u), msg.text)
    def __req_message_handler(self, i_req, client, *args):
        """ Mathod the handler message request """

        other_clients = args[0]
        msg = Msg.from_dict(i_req.body)
        self.storage.user_stat_update(msg.sender, ch_sent=1)
        if msg.to.upper() != '@ALL' and msg.to in self.users:
            self.storage.user_stat_update(msg.to, ch_recv=1)
            self.storage.add_message(msg.sender, msg.to, msg.text)
            self.__send_to_client(self.users[msg.to],
                                  Response(LETTER, str(msg)))
        else:
            text = b64decode(msg.text).decode().lower()
            spam_filter = [s for s in self.SPAM if s in text]
            if len(spam_filter) > 0:
                self.logger.warning('SPAM DETECT')
                return
            self.storage.add_message_to_room(msg.to.upper()[1:], msg.text,
                                             msg.sender)

            self.__send_to_all(other_clients, Response(LETTER, str(msg)))
Пример #4
0
    def __send_responses(self, requests, o_clients):

        for client, i_req in requests.items():
            other_clients = [c for c in o_clients if c != client]
            self.logger.info(client)
            self.logger.info(i_req)

            if i_req.action == RequestAction.PRESENCE:
                self.__send_to_client(client, Response(OK))
                self.__send_to_all(other_clients,
                                   Response(CONNECTED, i_req.body))
            elif i_req.action == RequestAction.QUIT:
                self.__client_disconnect(client)
            elif i_req.action == RequestAction.MESSAGE:
                msg = Msg.from_dict(i_req.body)
                self.storage.user_stat_update(msg.sender, ch_sent=1)
                if msg.to.upper() != 'ALL' and msg.to in self.users:
                    self.storage.user_stat_update(msg.to, ch_recv=1)
                    self.storage.add_message(msg.sender, msg.to, msg.text)
                    self.__send_to_client(self.users[msg.to],
                                          Response(LETTER, str(msg)))
                else:
                    self.__send_to_all(other_clients,
                                       Response(LETTER, str(msg)))
                    for u in self.storage.get_users_online():
                        if str(u) == msg.sender:
                            continue
                        self.storage.user_stat_update(str(u), ch_recv=1)
                        self.storage.add_message(msg.sender, str(u), msg.text)

            elif i_req.action == RequestAction.COMMAND:
                command, *args = i_req.body.split()
                user = [u for u, c in self.users.items() if c == client].pop()
                if len(args) < 1 or args[0] != user:
                    args.insert(0, user)
                o_resp = self.__execute_command(command, *args)
                self.__send_to_client(client, o_resp)
            else:
                self.__send_to_client(client, Response(INCORRECT_REQUEST))
                self.logger.error(f'Incorrect request:\n {i_req}')
    def __send_responses(self, requests, o_clients):

        for client, i_req in requests.items():
            other_clients = [c for c in o_clients if c != client]
            self.logger.info(client)
            self.logger.info(i_req)

            if i_req.action == RequestAction.PRESENCE:
                prv, pub = gen_keys()
                self.client_keys[i_req.body] = (client, prv)
                self.__send_to_client(client, Response(AUTH, pub.export_key().decode()))
            elif i_req.action == RequestAction.AUTH:
                user = [u for u, c in self.users.items() if c == client]
                if len(user) == 0:
                    self.logger.warning(f'AUTH: user not found')
                    continue
                user = user[0]
                cl, key = self.client_keys[user]
                if cl != client:
                    self.logger.warning('AUTH: connection sockets not equals')
                    continue
                try:
                    password = decrypt_password(key, i_req.body)
                except Exception as e:
                    self.logger.error(e)
                    password = None
                if password is None:
                    self.logger.warning('AUTH: decrypt error')
                    continue
                if not self.storage.authorization_user(user, get_hash_password(password, user.encode())):
                    self.__send_to_client(client, Response(UNAUTHORIZED))
                    self.clients.remove(client)
                    self.users.pop(user)
                    continue
                self.storage.login_user(user, client.getpeername()[0])
                self.__send_to_client(client, Response(OK))
                self.__send_to_all(other_clients, Response(CONNECTED, user))
                pass
            elif i_req.action == RequestAction.QUIT:
                self.__client_disconnect(client)
            elif i_req.action == RequestAction.START_CHAT:
                msg = Msg.from_dict(i_req.body)
                if msg.to not in self.users:
                    self.logger.warning(f'{msg.to} not found')
                    continue
                self.__send_to_client(self.users[msg.to], Response(START_CHAT, str(msg)))
            elif i_req.action == RequestAction.ACCEPT_CHAT:
                msg = Msg.from_dict(i_req.body)
                if msg.to not in self.users:
                    self.logger.warning(f'{msg.to} not found')
                    continue
                self.__send_to_client(self.users[msg.to], Response(ACCEPT_CHAT, str(msg)))
            elif i_req.action == RequestAction.MESSAGE:
                msg = Msg.from_dict(i_req.body)
                self.storage.user_stat_update(msg.sender, ch_sent=1)
                if msg.to.upper() != 'ALL' and msg.to in self.users:
                    self.storage.user_stat_update(msg.to, ch_recv=1)
                    self.storage.add_message(msg.sender, msg.to, msg.text)
                    self.__send_to_client(self.users[msg.to], Response(LETTER, str(msg)))
                else:
                    self.__send_to_all(other_clients, Response(LETTER, str(msg)))
                    for u in self.storage.get_users_online():
                        if str(u) == msg.sender:
                            continue
                        self.storage.user_stat_update(str(u), ch_recv=1)
                        self.storage.add_message(msg.sender, str(u), msg.text)
            elif i_req.action == RequestAction.COMMAND:
                command, *args = i_req.body.split()
                user = [u for u, c in self.users.items() if c == client].pop()
                if len(args) < 1 or args[0] != user:
                    args.insert(0, user)
                o_resp = self.__execute_command(command, *args)
                self.__send_to_client(client, o_resp)
            else:
                self.__send_to_client(client, Response(INCORRECT_REQUEST))
                self.logger.error(f'Incorrect request:\n {i_req}')