示例#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)
示例#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:
示例#3
0
def i_sipc_auth(user, verification = None, debug = False):
    # register to SIPC server
    config = user.get_config()
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    user.set_sock(sock)
    splits = config.sipc_proxy.split(":")
    ip, port = splits[0], splits[1]
    sock.connect((ip, int(port)))
#    SIPC_PROXY_IP = '58.68.229.64'
#    SIPC_PROXY_PORT = 8080
#    sock.connect((SIPC_PROXY_IP, SIPC_PROXY_PORT))

    from_sid = user.sid
    call_id = user.get_global_call_id()
    sip_type = SIP(SIP.REGISTER)

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

        "CN" : _generate_cnouce(),
        "CL" : 'type="PC" ,version="%s"' % PROTOCOL_VERSION
        })

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


    call_id = user.get_global_call_id()
    sip_type = SIP(SIP.REGISTER)

    response = _generate_sipc_auth_response(user, res_obj)

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

        "AK" : "ak-value",
        "A" : 'Digest response="%s",algorithm="SHA1-sess-v4"' % response,
        })

    # append verification created in SSI auth
    if verification:
        auth = 'Verify response="%s",algorithm="%s",type="%s",chid="%s"' % \
            (verification.chars, verification.algorithm, verification._type, verification.pid)
        old_val = headers["A"]
        headers["A"] = [old_val, auth]

    body = _generate_sipc_auth_body(user)

    sip_conn = SIPConnection(user.get_sock(), sip_type = sip_type, headers = headers, body = body)
    res_obj = sip_conn.send(debug = debug)

    return res_obj
示例#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)
示例#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)
示例#6
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)
示例#7
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)
示例#8
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)
示例#9
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)
示例#10
0
def get_sock_for_recv_msg(user, res_obj, debug = False):
    attr = res_obj.headers.get_field_value

    auth = attr("A")
    ip, port, credential = _get_ip_port_credential_from_auth_field(auth)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))

    from_sid = user.sid
    call_id = attr("I")

    sip_type = SIP(SIP.REGISTER)

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

        # "K" : kinds = attr("K")
        # `K` means `media kind` ?
        "K" : ["text/html-fragment", "multiparty", "nudge"],
        "A" : 'TICKS auth="%s"' % credential,
    }

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


    if res_obj.code != SIPResponse.OK:
        logger.error("!!! get_sock_for_recv_msg failed")
    else:
        from_uri = attr("F")
        conversations = user.get_conversations()
        conv = conversations[from_uri]
        conv.call_id = call_id
        conv.sock = sock

    return res_obj
示例#11
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