Beispiel #1
0
def i_delete_buddy(user, uri, delete_both, debug=True):
    call_id = user.get_global_call_id()

    from_sid = user.sid
    sip_type = SIP(SIP.SERVICE)
    sip_event = SIPEvent(SIPEvent.DELETE_BUDDY_V4)

    target_contact = user.group_agent.get_contact_by_uri(uri)
    user_id = target_contact.user_id

    headers = {
        "F": from_sid,
        "I": str(call_id),
        "Q": "1 %s" % (str(sip_type)),
        "N": str(sip_event)
    }

    if delete_both:
        delete_me_from_his_contact_list = YES
    else:
        delete_me_from_his_contact_list = NO

    body = _DELETE_BUDDY_TPL % (user_id, delete_me_from_his_contact_list)
    sip_conn = SIPConnection(user.get_sock(),
                             sip_type=sip_type,
                             headers=headers,
                             body=body)
    sip_conn.send(recv=False, debug=debug)
    """ TODO:
        add this request message into un-ack list, delete buddy after get OK response.
    """

    user.group_agent.delete_contact_by_uri(uri)
Beispiel #2
0
def i_add_buddy(user, account, myname, debug=False):
    call_id = str(user.get_global_call_id())

    sip_type = SIP(SIP.SERVICE)
    from_sid = user.sid
    sip_event = str(SIPEvent(SIPEvent.ADD_BUDDY_V4))

    headers = {
        "F": from_sid,
        "I": call_id,
        "Q": "1 %s" % sip_type,
        "N": sip_event,
    }

    buddy_list_name = ""
    desc = myname
    expose_mobile_no = YES
    expose_name = YES
    add_buddy_phrase_id = 0

    body = _ADD_BUDDY_TPL % \
           (account, buddy_list_name, desc, expose_mobile_no, expose_name, add_buddy_phrase_id)
    sip_conn = SIPConnection(user.get_sock(),
                             sip_type=sip_type,
                             headers=headers,
                             body=body)
    sip_conn.send(recv=False, debug=debug)
    """ TODO:
Beispiel #3
0
def i_reply_add_buddy(user,
                      res_obj,
                      result,
                      refuse_reason,
                      decline_add_req_forever,
                      debug=False):

    body_dom = minidom.parseString(res_obj.body)
    app_data = i_parse_add_buddy_application(body_dom)

    #attr = res_obj.headers.get_field_value

    call_id = user.get_global_call_id()

    from_sid = user.sid
    sip_type = SIP(SIP.SERVICE)
    sip_event = SIPEvent(SIPEvent.HANDLE_CONTACT_REQUEST_V4)

    headers = {
        "F": from_sid,
        "I": str(call_id),
        "Q": "1 %s" % (str(sip_type)),
        "N": str(sip_event)
    }

    if result == ReplyAddBuddyApplication.ACCEPT:
        buddy_list_id = 0
        local_name = ""
        expose_mobile_no = YES
        expose_name = YES
        body = _REPLY_ACCEPT_FOR_ADD_BUDDY_TPL % (
            app_data["user_id"], result, buddy_list_id, local_name,
            expose_mobile_no, expose_name)

    elif result == ReplyAddBuddyApplication.REFUSE:
        if decline_add_req_forever:
            accept_instant_message = NO
        else:
            accept_instant_message = YES
        expose_basic_presence = UserPresence.OFFLINE_OR_INVISIBLE
        body = _REPLY_REFUSE_FOR_ADD_BUDDY_TPL % (
            app_data["user_id"], result, accept_instant_message,
            expose_basic_presence, refuse_reason)

        if decline_add_req_forever:
            i_add_to_black_list(user,
                                app_data["uri"],
                                app_data["who"],
                                debug=True)

    elif result == ReplyAddBuddyApplication.IGNORE:
        body = _REPLY_IGNORE_FOR_ADD_BUDDY_TPL % (app_data["user_id"], result)

    sip_conn = SIPConnection(user.get_sock(),
                             sip_type=sip_type,
                             headers=headers,
                             body=body)
    sip_conn.send(recv=False, debug=debug)
Beispiel #4
0
def i_send_msg(user, to_uri, msg, debug = True):
    conversations = user.get_conversations()
    if to_uri in conversations:
        conv = conversations[to_uri]

        if conv.listen_thread and not conv.listen_thread.isFinished():
            sock = conv.sock
        else:
            start_chat(user, to_uri, debug = debug)
            res_obj = get_sock_for_send_msg(user, to_uri, debug = debug)
            if res_obj:
                sock = conv.sock
            else:
                conv = conversations[to_uri]
                conv.sock = user.get_sock()
                sock = conv.sock
    else:
        conv = conversations[to_uri]
        conv.sock = user.get_sock()
        sock = conv.sock

    call_id = str(user.get_global_call_id())

    from_sid = user.sid
    sip_type = SIP(SIP.MESSAGE)
    sip_event = SIPEvent(SIPEvent.CAT_MESSAGE)

    headers = {
        "F" : from_sid,
        "I" : call_id,
        "Q" : "%d %s" % (DEFAULT_SIP_SEQUENCE, str(sip_type)),

        "N" : str(sip_event),

        "T" : to_uri,
        "C" : "text/plain",
        "K" : "SaveHistory",
    }

    body = to_byte_str(msg)
    sip_conn = SIPConnection(sock, sip_type = sip_type, headers = headers, body = body)

    """TODO: we should add this msg into unack_msg_list,
        traversal un-ack message list in 20s interval,
        pop up a send message failed warning for no un-ack message. """

    unack_msg = {
        "send_ts" : int(time.time()),
        "call_id" : call_id,
        "to_uri" : to_uri,
        "content" : msg,
        }
    user.append_to_unack_msg_list(unack_msg)

    sip_conn.send(recv = False, debug = debug)
Beispiel #5
0
def i_keep_connection_busy(user, sock, debug=False):
    from_sid = user.sid
    sip_type = SIP(SIP.OPTION)
    sip_event = SIPEvent(SIPEvent.KEEP_CONNECTION_BUSY)

    call_id = str(user.get_global_call_id())

    headers = {
        "F": from_sid,
        "I": call_id,
        "Q": "1 %s" % str(sip_type),
        "N": str(sip_event),
    }

    sip_conn = SIPConnection(sock, sip_type=sip_type, headers=headers)
    sip_conn.send(recv=False, debug=debug)
Beispiel #6
0
def start_chat(user, to_uri, debug = False):
    """ Get (ip, port) to create a socket which used to start a online conversation """
    from_sid = user.sid
    sip_type = SIP(SIP.SERVICE)
    sip_event = SIPEvent(SIPEvent.START_CHAT)

    call_id = str(user.get_global_call_id())

    headers = {
        "F" : from_sid,
        "I" : call_id,
        "Q" : "%d %s" % (DEFAULT_SIP_SEQUENCE, str(sip_type)),
        "N" : str(sip_event),
    }

    conversations = user.get_conversations()
    conversations[to_uri].call_id = call_id

    sip_conn = SIPConnection(user.get_sock(), sip_type = sip_type, headers = headers)
    sip_conn.send(recv = False, debug = debug)
Beispiel #7
0
def i_subscribe_contact_list(user, debug=False):
    from_sid = user.sid
    call_id = user.get_global_call_id()

    sip_type = SIP(SIP.SUBSCRIPTION)
    sip_event = SIPEvent(SIPEvent.PRESENCE_V4)

    headers = dict({
        "F": from_sid,
        "I": str(call_id),
        "Q": "%d %s" % (DEFAULT_SIP_SEQUENCE, str(sip_type)),
        "N": str(sip_event),
    })

    body = '''<args><subscription self="v4default;mail-count" buddy="v4default" version="0"/></args>'''
    sip_conn = SIPConnection(user.get_sock(),
                             sip_type=sip_type,
                             headers=headers,
                             body=body)
    sip_conn.send(recv=False, debug=debug)
Beispiel #8
0
def i_add_to_black_list(user, uri, local_name, debug=False):
    call_id = str(user.get_global_call_id())

    sip_type = SIP(SIP.SERVICE)
    sip_event = SIPEvent(SIPEvent.ADD_TO_BLACK_LIST)
    from_sid = user.sid

    headers = {
        "F": from_sid,
        "I": call_id,
        "Q": "1 %s" % str(sip_type),
        "N": str(sip_event),
    }

    body = _ADD_TO_BLACK_LIST_TPL % (uri, local_name)

    sip_conn = SIPConnection(user.get_sock(),
                             sip_type=sip_type,
                             headers=headers,
                             body=body)
    sip_conn.send(recv=False, debug=debug)
Beispiel #9
0
def get_sock_for_send_msg(user, to_uri, debug = False):
    """ Parse (ip, port) get by start_chat request,
        create a new socket object to register and invite buddy to start a online conversation. """

    conversations = user.get_conversations()
    conv = conversations[to_uri]
    while not conv.start_chat_response:
        time.sleep(0.1)
    res_obj = conv.start_chat_response

    auth_val = res_obj.headers.get_field_value("A")
    ip, port, credential = _get_ip_port_credential_from_auth_field(auth_val)

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    conv.sock = sock
    sock.connect((ip, port))

    call_id = conv.call_id
    sip_type = SIP(SIP.REGISTER)
    sid = user.sid

    headers = {
        "F" : sid,
        "I" : call_id,
        "Q" : "%d %s" % (DEFAULT_SIP_SEQUENCE, str(sip_type)),

        "K" : ["text/html-fragment", "multiparty", "nudge"],
        "A" : 'TICKS auth="%s"' % credential,
    }

    conn = SIPConnection(sock, sip_type = sip_type, headers = headers)
    conn.send(debug = debug)

    # invite_buddy_to_start_chat(user, call_id, to_uri, debug)
    sip_type = SIP(SIP.SERVICE)
    sip_event = SIPEvent(SIPEvent.INVITE_BUDDY)

    headers = {
        "F" : sid,
        "I" : call_id,
        "Q" : "%d %s" % (DEFAULT_SIP_SEQUENCE, str(sip_type)),
        "N" : str(sip_event),
    }

    body = '<args><contacts><contact uri="%s" /></contacts></args>' % to_uri

    conn = SIPConnection(sock, sip_type = sip_type, headers = headers, body = body)
    res_obj = conn.send(debug = debug)

    if res_obj.code != SIPResponse.OK:
        sock.close()
        logger.error("get sock for cat failed")
        return None

    # recevie option response, we can do detect user if is offline or hide from response
    buf = SIPConnection.recv(sock, debug = debug)
    res_obj = SIPResponse(buf)
    
    logger.info("user is offline: %s" % str(detect_user_if_is_offline_from_invite_response(res_obj)))
    if detect_user_if_is_offline_from_invite_response(res_obj):
        sock.close()
        return None

    assert res_obj != None

    # recevie conversation response
    buf = SIPConnection.recv(sock, debug = debug)
    res_obj = SIPResponse(buf)
    assert res_obj != None

    return res_obj
Beispiel #10
0
    def _consume_notification(self, res_obj, sock):
        assert (sock is not None)
        sip_event_str = res_obj.headers.get_field_value("N")
        sip_event = SIPEvent.get_const_by_str(SIPEvent, sip_event_str)
        events = get_conversation_events(res_obj)

        body_dom = minidom.parseString(res_obj.body)
        CONTACT_PRESENCE_CHANGED = sip_event == SIPEvent.PRESENCE_V4 and \
                                   NotificationEvent.PRESENCE_CHANGED in events

        CONTACT_LEFT = sip_event == SIPEvent.CONVERSATION and \
                       NotificationEvent.USER_LEFT in events

        SYNC_USER_INFO_V4 = sip_event == SIPEvent.SYNC_USER_INFO_V4 and \
                        NotificationEvent.SYNC_USER_INFO in events

        BEEN_DISCONNECTED = sip_event == SIPEvent.REGISTRATION and \
                                NotificationEvent.DEREGISTRATION in events

        ADD_BUDDY_REFUSED = sip_event == SIPEvent.SYSTEM_NOTIFY_V4 and \
                            NotificationEvent.ADD_BUDDY_REFUSED in events

        USER_DYNAMICS_CHANGED = sip_event == SIPEvent.SYSTEM_NOTIFY_V4 and \
                                NotificationEvent.USER_DYNAMICS_CHANGED

        ADD_BUDDY_APPLICATION = sip_event == SIPEvent.CONTACT and \
                                NotificationEvent.ADD_BUDDY_APPLICATION in events

        convs = self.user.get_conversations()
        if CONTACT_PRESENCE_CHANGED:
            offline_uri_list = update_contacts_presence_from_response(
                self.user, body_dom)
            for uri in offline_uri_list:
                if uri in convs:
                    conv = convs[uri]
                    assert conv.sock != self.user.get_sock()
                    conv.over()
                    #convs.pop(uri)
            self.update_portraits_t_run()

            self.emit(
                QtCore.SIGNAL(
                    'dataChanged ( const QModelIndex & , const QModelIndex &  )'
                ))

        elif CONTACT_LEFT:
            member_nodes = body_dom.getElementsByTagName("member")
            for member_node in member_nodes:
                uri = member_node.getAttribute("uri")
                conv = convs[uri]
                assert conv.sock != self.user.get_sock()
                conv.over()
                #convs.pop(uri)

        elif SYNC_USER_INFO_V4:
            self._consume_noti_sync_user_info_v4(res_obj)

        elif BEEN_DISCONNECTED:
            OFFLINE_ALERTS = "You have been disconnected" \
                "as someone has signed in with your ID on another computer." \
                "<br /><br />" \
                "Please note that if this was not intentional, some may have stolen your passwrod. "\
                "Please change your password."
            popup_error(self, OFFLINE_ALERTS)
            self.switch_presence_to_offline()
            #"Sign in again", "OK"

        elif ADD_BUDDY_REFUSED:
            uri, reason = i_parse_add_buddy_refused(body_dom)
            assert uri != reason
            logger.error(
                "User (URI: %s) refused your add buddy application, reason: %s"
                % (uri, reason))

        elif ADD_BUDDY_APPLICATION:
            self._consume_add_buddy_application(res_obj)

        elif USER_DYNAMICS_CHANGED:
            pass
Beispiel #11
0
    def _consume_notification(self, res_obj, sock):
        assert (sock is not None)
        sip_event_str = res_obj.headers.get_field_value("N")
        sip_event = SIPEvent.get_const_by_str(SIPEvent, sip_event_str)
        events = get_conversation_events(res_obj)

        body_dom = minidom.parseString(res_obj.body)
        CONTACT_PRESENCE_CHANGED = sip_event == SIPEvent.PRESENCE_V4 and \
                                   NotificationEvent.PRESENCE_CHANGED in events

        CONTACT_LEFT = sip_event == SIPEvent.CONVERSATION and \
                       NotificationEvent.USER_LEFT in events

        SYNC_USER_INFO_V4 = sip_event == SIPEvent.SYNC_USER_INFO_V4 and \
                        NotificationEvent.SYNC_USER_INFO in events

        BEEN_DISCONNECTED = sip_event == SIPEvent.REGISTRATION and \
                                NotificationEvent.DEREGISTRATION in events

        ADD_BUDDY_REFUSED = sip_event == SIPEvent.SYSTEM_NOTIFY_V4 and \
                            NotificationEvent.ADD_BUDDY_REFUSED in events

        USER_DYNAMICS_CHANGED = sip_event == SIPEvent.SYSTEM_NOTIFY_V4 and \
                                NotificationEvent.USER_DYNAMICS_CHANGED

        ADD_BUDDY_APPLICATION = sip_event == SIPEvent.CONTACT and \
                                NotificationEvent.ADD_BUDDY_APPLICATION in events

        convs = self.user.get_conversations()
        if CONTACT_PRESENCE_CHANGED:
            offline_uri_list = update_contacts_presence_from_response(self.user, body_dom)
            for uri in offline_uri_list:
                if uri in convs:
                    conv = convs[uri]
                    assert conv.sock != self.user.get_sock()
                    conv.over()
                    #convs.pop(uri)
            self.update_portraits_t_run()

            self.emit(QtCore.SIGNAL('dataChanged ( const QModelIndex & , const QModelIndex &  )'))

        elif CONTACT_LEFT:
            member_nodes = body_dom.getElementsByTagName("member")
            for member_node in member_nodes:
                uri = member_node.getAttribute("uri")
                conv = convs[uri]
                assert conv.sock != self.user.get_sock()
                conv.over()
                #convs.pop(uri)

        elif SYNC_USER_INFO_V4:
            self._consume_noti_sync_user_info_v4(res_obj)

        elif BEEN_DISCONNECTED:
            OFFLINE_ALERTS = "You have been disconnected" \
                "as someone has signed in with your ID on another computer." \
                "<br /><br />" \
                "Please note that if this was not intentional, some may have stolen your passwrod. "\
                "Please change your password."
            popup_error(self, OFFLINE_ALERTS)
            self.switch_presence_to_offline()
            #"Sign in again", "OK"

        elif ADD_BUDDY_REFUSED:
            uri, reason = i_parse_add_buddy_refused(body_dom)
            assert uri != reason
            logger.error("User (URI: %s) refused your add buddy application, reason: %s" % (uri, reason))

        elif ADD_BUDDY_APPLICATION:
            self._consume_add_buddy_application(res_obj)

        elif USER_DYNAMICS_CHANGED:
            pass