Esempio n. 1
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)
Esempio n. 2
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)