Пример #1
0
def xmpp_heartbeat(mqttc, userdata, topic, payload):
    """
    xmpp 心跳请求
    :param mqttc: 发送的mqtt客户端
    :param userdata:
    :param topic: 主题
    :param payload: 内容
    :return: None
    """

    mxid = topic.split("/")[-1]
    jid, access_token = MxidMgr().valid_mxid(mxid)
    log_params = "jid:%s, mxid:%s, access_token:%s" % (jid, mxid, access_token)
    if not jid\
            or not JidMgr().is_jid(jid):
        logger.info("xmpp_message_send ERROR_MXID, %s" % log_params)
        return

    if not access_token:
        s2c_message.xmpp_heartbeat_res(jid, error_code.ERROR_MXID)
        logger.info("xmpp_message_send ERROR_MXID, %s" % log_params)
        return

    xmpp_client = XmppAdapterMgr().get_xmpp_adapter(jid)
    if not xmpp_client:
        s2c_message.xmpp_heartbeat_res(jid, error_code.ERROR_JID_HAS_NOT_LOGIN)
        logger.info("xmpp_roster_del_req ERROR_JID_HAS_NOT_LOGIN, %s" % log_params)
        return

    xmpp_client.heartbeat_time = time.time()
    s2c_message.xmpp_heartbeat_res(jid, error_code.ERROR_SUCCESS)
Пример #2
0
def xmpp_roster_add_req(mqttc, userdata, topic, payload):
    """
    xmpp roster 添加请求
    :param mqttc:
    :param userdata:
    :param topic: 主题
    :param payload: 内容
    :return:None
    """
    mxid = topic.split("/")[-1]
    jid, access_token = MxidMgr().valid_mxid(mxid)
    log_params = "jid:%s, mxid:%s, access_token:%s" % (jid, mxid, access_token)
    if not jid\
            or not JidMgr().is_jid(jid):
        logger.info("xmpp_roster_add_req ERROR_MXID, %s" % log_params)
        return

    if not access_token:
        s2c_message.xmpp_roster_add_res(jid, error_code.ERROR_MXID)
        logger.info("xmpp_roster_add_req ERROR_MXID, %s" % log_params)
        return

    add_jid = payload.get('jid', None)
    name = payload.get('name', None)
    groups = payload.get('groups', None)
    if not add_jid\
            or not JidMgr().is_jid(add_jid):
        s2c_message.xmpp_roster_add_res(jid, error_code.ERROR_PARAMS_ERROR)
        logger.info("xmpp_roster_add_req ERROR_PARAMS_ERROR, %s" % log_params)
        return

    xmpp_client = XmppAdapterMgr().get_xmpp_adapter(jid)
    if not xmpp_client:
        s2c_message.xmpp_roster_add_res(jid, error_code.ERROR_JID_HAS_NOT_LOGIN)
        logger.info("xmpp_roster_add_req ERROR_JID_HAS_NOT_LOGIN, %s" % log_params)
        return

    logger.info("jid:%s roster:%s" % (jid, xmpp_client.get_roster_items()))
    if xmpp_client.is_jid_in_roster(add_jid):
        s2c_message.xmpp_roster_add_res(jid, error_code.ERROR_JID_ALREADY_IN_ROSTER)
        logger.info("xmpp_roster_add_req ERROR_JID_ALREADY_IN_ROSTER, %s" % log_params)
        return

    xmpp_client.add_roster(JID(add_jid), name, groups)
    s2c_message.xmpp_roster_add_res(jid, error_code.ERROR_SUCCESS)
Пример #3
0
def xmpp_message_bcast(mqttc, userdata, topic, payload):
    """
    xmpp 消息广播请求
    :param mqttc: 发送的mqtt客户端
    :param userdata:
    :param topic: 主题
    :param payload: 内容
    :return: None
    """

    mxid = topic.split("/")[-1]
    jid, access_token = MxidMgr().valid_mxid(mxid)
    log_params = "jid:%s, mxid:%s, access_token:%s" % (jid, mxid, access_token)
    if not jid\
            or not JidMgr().is_jid(jid):
        logger.info("xmpp_message_send ERROR_MXID, %s" % log_params)
        return

    if not access_token:
        s2c_message.xmpp_message_bcast_res(jid, error_code.ERROR_MXID)
        logger.info("xmpp_message_send ERROR_MXID, %s" % log_params)
        return
    subject = payload.get('subject', None)
    body = payload.get('body', None)
    if not subject\
            or not body:
        s2c_message.xmpp_message_bcast_res(jid, error_code.ERROR_PARAMS_ERROR)
        logger.info("xmpp_message_send ERROR_PARAMS_ERROR, %s" % log_params)
        return

    xmpp_client = XmppAdapterMgr().get_xmpp_adapter(jid)
    if not xmpp_client:
        s2c_message.xmpp_message_bcast_res(jid, error_code.ERROR_JID_HAS_NOT_LOGIN)
        logger.info("xmpp_roster_del_req ERROR_JID_HAS_NOT_LOGIN, %s" % log_params)
        return

    # 循环roster进行广播
    for roster_dic in xmpp_client.get_roster_items():
        jid = JidMgr().get_jid_bare_str(roster_dic['jid'])
        xmpp_client.send_async(jid, subject, body)

    s2c_message.xmpp_message_bcast_res(jid, error_code.ERROR_SUCCESS)
Пример #4
0
def xmpp_login_req(mqttc, userdata, topic, payload):
    """
    xmpp 登陆请求
    :param mqttc:请求客户端
    :param userdata:
    :param topic:主题
    :param payload:内容
    :return:
    """
    jid = topic.split("/")[-1]
    access_token = payload.get('access_token', None)
    jid_pass = payload.get('jid_pass', None)
    log_params = "jid:%s, jid_pass:%s, access_token:%s" % (jid, jid_pass, access_token)

    # check params
    if not access_token\
            or not jid\
            or not JidMgr().is_jid(jid)\
            or not jid_pass:
        s2c_message.xmpp_login_res(jid, error_code.ERROR_PARAMS_ERROR, "")
        logger.info("xmpp_login_req ERROR_WRONG_PARAMS, %s" % log_params)
        return

    if not verify_access_token(ServiceMgrCacher().get_connection(US_REGISTER), access_token):
        s2c_message.xmpp_login_res(jid, error_code.ERROR_ACCESS_TOKEN_ERROR, "")
        logger.info("xmpp_login_req ERROR_ACCESS_TOKEN_ERROR, %s" % log_params)
        return

    # 不管重登与否,access_token都有可能发生变化
    XmppAdapterMgr().add_access_token(jid, access_token)

    xmpp_adapter = XmppAdapterMgr().get_xmpp_adapter(jid)
    if xmpp_adapter:
        xmpp_adapter.on_login(relogin=True)
        return

    XmppAdapterMgr().login(jid, jid_pass)