ejabberd - 无法将自定义 IQ 发送给其他用户

ejabberd - not able to send a custom IQ to another user

我正在编写自定义 ejabberd 模块。当我使用 strophe js 发送自定义 IQ 节时,ejabberd 处理请求并将结果 IQ returns 返回给发件人。

下面是我用strophe js发送的IQ请求,

connection.sendIQ($iq({
    to: 'john@localhost',
    type: 'set',
    id: 'abc1234567890'
})
.c('query', {
    xmlns: 'jabber:iq:custom_module',
    msg_id: 'xyz9876543210'
})
.tree());

这是 ejabberd 模块代码,

-module(mod_custom_module).
-behaviour(gen_mod).
-define(NS_CUSTOM_MODULE, <<"jabber:iq:custom_module">>).

-export([start/2, stop/1, depends/2, mod_options/1, process_sm_iq/1, decode_iq_subel/1]).

-include("xmpp.hrl").
-include("logger.hrl").
-include("ejabberd_sql_pt.hrl").

start(_Host, _Opts) ->
    gen_iq_handler:add_iq_handler(ejabberd_sm, _Host, ?NS_CUSTOM_MODULE, ?MODULE, process_sm_iq, one_queue).
stop(_Host) ->
    gen_iq_handler:remove_iq_handler(ejabberd_sm, _Host, ?NS_CUSTOM_MODULE).

depends(_Host, _Opts) ->
    [].

mod_options(_Host) ->
    [].

-spec decode_iq_subel(xmpp_element()) -> xmpp_element();
            (xmlel()) -> xmlel().
decode_iq_subel(El) ->
    El.

-spec process_sm_iq(iq()) -> iq().
process_sm_iq(#iq{from = _From, to = _To, sub_els = _sub_els} = IQ) ->
    % My module actions here...
    [First | Rest] = _sub_els,
    xmpp:make_iq_result(IQ, First).

处理完IQ后,我还想通知其他用户'john@localhost'自定义事件。我尝试使用 ejabberd_router:route/3 执行此操作,但没有成功。

我不知道我做错了什么。

更新

当我使用以下代码时,其他用户没有收到节。

NewIQ = #iq{id = _Id, type = result, to = _To, from = _From, sub_els = _sub_els},

ejabberd_router:route(xmpp:set_from_to(NewIQ, _From, _To)),
% or ejabberd_router:route(NewIQ),
% or ejabberd_sm:route(NewIQ),

当我检查调试控制台时,它显示了以下消息。不确定这是否相关,因为它只是一条 debug 类型的消息,没有其他失败错误消息。

17:07:47.173 [debug] Dropping packet to unavailable resource:
#iq{id = <<"abc1234567890">>,type = result,lang = <<>>,
from = #jid{user = <<"nikhil">>,server = <<"localhost">>,
            resource = <<"49230572059507447681762">>,luser = <<"nikhil">>,
            lserver = <<"localhost">>,
            lresource = <<"49230572059507447681762">>},
to = #jid{user = <<"john">>,server = <<"localhost">>,
          resource = <<>>,luser = <<"john">>,
          lserver = <<"localhost">>,lresource = <<>>},
sub_els = [#xmlel{name = <<"query">>,
            attrs = [{<<"xmlns">>,<<"jabber:iq:custom_module">>},
            {<<"msg_id">>,<<"xyz9876543210">>}],
            children = []}],
meta = #{}}

试试这个功能。它向目标帐户发送标题消息,其中包含有关原始 IQ 的一些详细信息。

process_sm_iq(#iq{from = From, to = To, sub_els = SubEls} = IQ) ->
    [First | Rest] = SubEls,
    MsgId = fxml:get_tag_attr_s(<<"msg_id">>, First),
    Subject = "Event alert",
    Body = "An IQ was received by custom_module with msg_id: "++MsgId,
    Packet = #message{from = From,
                      to = To,
                      type = headline,
                      body = xmpp:mk_text(Body),
                      subject = xmpp:mk_text(Subject)},
    ejabberd_router:route(Packet),
    xmpp:make_iq_result(IQ, First).