Beispiel #1
0
    def get_chat_message(*args):
        _message, _address, _connection = args
        _sender = _message.get(FIELD_SENDER)
        _receiver = _message.get(FIELD_RECEIVER)
        if not is_chat(_receiver):
            _msg_receiver = Server.connected_clients.get(_receiver)
            if _msg_receiver:
                print('-->', _message)
                _msg_receiver.sendall(json.dumps(_message).encode())
                return [RESPONSE_OK]
        else:
            with ServerStore() as store:
                _contacts = store.get_contacts(
                    Client(_message.get(FIELD_RECEIVER)))

            if _contacts:
                _receivers = {
                    k: v
                    for k, v in Server.connected_clients.items()
                    if k in _contacts and k != _sender
                }
                for r in _receivers.values():
                    print('-->', _message)
                    r.sendall(json.dumps(_message).encode())

                return [RESPONSE_OK]
Beispiel #2
0
 def leave_chat(*args):
     _message, _address, _connection = args
     with ServerStore() as store:
         _client = store.get_client(Client(_message.get(FIELD_USERID)))
         _chat = store.get_or_create(Client(_message.get(FIELD_ROOM)))
         store.del_contact(_chat, _client)
         return [RESPONSE_OK]
Beispiel #3
0
 def get_presence(*args):
     _message, _address, _connection = args
     _client = Client(_message.get(FIELD_USER))
     Server.connected_clients.update({_client.clientName: _connection})
     with ServerStore() as store:
         if store.get_or_create(_client) and store.upd_history(
                 _client, _address):
             return [RESPONSE_OK]
Beispiel #4
0
 def del_contact(*args):
     _message, _address, _connection = args
     with ServerStore() as store:
         _client = store.get_client(Client(_message.get(FIELD_USERID)))
         _contact = store.get_client(
             Client(_message.get(FIELD_CONTACT_NAME)))
         if _client and _contact:
             store.del_contact(_client, _contact)
             return [RESPONSE_OK]
Beispiel #5
0
 def add_contact(*args):
     _message, _client, _connection = args
     with ServerStore() as store:
         _client = store.get_client(Client(_message.get(FIELD_USERID)))
         _contact = store.get_client(
             Client(_message.get(FIELD_CONTACT_NAME)))
         if _client and _contact:
             store.add_contact(_client, _contact)
             return [RESPONSE_OK]
         else:
             return [
                 JIMResponseMessage({
                     'response': 400,
                     'error': 'User not found.'
                 })
             ]
Beispiel #6
0
    def get_contacts(*args):
        _message, _address, _connection = args
        _response = []
        with ServerStore() as store:
            _contacts = store.get_contacts(Client(_message.get(FIELD_USERID)))

        if _contacts:
            _response = [
                JIMResponseMessage({
                    FIELD_RESPONSE: CODE_ACCEPTED,
                    FIELD_QUANTITY: len(_contacts)
                })
            ]
            _response.extend(
                JIMActionMessage.contact_list(c) for c in _contacts)

        return _response