Module to handle custom iq stanzas does not work with ejabberd-18.09

倾然丶 夕夏残阳落幕 提交于 2019-12-11 19:06:35

问题


I am trying to move to ejabberd-18.09 from ejabberd-17.03. I have this module so that I can send custom iq stanzas to ejabberd.

-module(mod_test_custom).

%% ====================================================================
%% API functions
%% ====================================================================
-export([start/2, stop/1, process_local_iq/3,depends/2,mod_opt_type/1]).
-include("ejabberd.hrl").
-include("logger.hrl").
-include("xmpp.hrl").



%% ====================================================================
%% Internal functions
%% ====================================================================


-define(NS_IQ_CUSTOM, <<"ns:custom">>).
start(Host, Opts) ->
    IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
                             one_queue),
    gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_IQ_CUSTOM, ?MODULE, process_local_iq, IQDisc),
    xmpp:register_codec(mod_custom),


    ?INFO_MSG("Inside mod_test_custom”,[]),

ok.


stop(Host) ->
    gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_IQ_CUSTOM),
    xmpp:unregister_codec(mod_custom),

ok.




depends(_Host, _Opts)->[{?MODULE,soft}].

mod_opt_type(_Option)->
    [].






process_local_iq(From,To,{iq, ID, get, NS, Language, SubElement} = IQ)->

    ?INFO_MSG("Inside mod_test_custom, IQ is ~p~n ",[IQ]),
    ?INFO_MSG("Inside mod_test_custom, IQ is ~p~n ",[From]),
    ?INFO_MSG("Inside mod_test_custom, IQ is ~p~n ",[To]),
    { _,ID ,  Type, Feature, Lang, {_,_,_,Sub_els}  } = IQ,
    Type,Lang,ID,Feature,
    ?INFO_MSG("Inside mod_test_custom, IQ is ~p~n ",[Sub_els]),
    Sum = 2+2,
    FoundSubEls = list_to_binary(integer_to_list(Sum)),
    SubElement,

{iq, ID, result, NS, Language,[{xmlel,<<"query">>,[{<<"xmlns">>,<<"ns:custom">>}],[{xmlel,<<"response">>,[{<<"op">>,<<"sum">>},{<<"val">>,FoundSubEls}],[]}]}]}.

This module works fine with ejabberd-17.03 . But when I use it with ejabberd-18.09 I get the following error:

[info] (tcp|<0.523.0>) Received XML on stream = <<"<iq to='example.com' id='tZU4h-20' type='get'><query xmlns='ns:custom'><abc op='sum'></abc></query></iq><r xmlns='urn:xmpp:sm:3'/>">>
 [debug] route:
#iq{id = <<"tZU4h-20">>,type = get,lang = <<"en">>,
    from = 
        #jid{
            user = <<"caLmPSeeWQo">>,server = <<"example.com">>,
            resource = <<"Smack">>,luser = <<"caLmPSeeWQo">>,
            lserver = <<"example.com">>,lresource = <<"Smack">>},
    to = 
        #jid{
            user = <<>>,server = <<"example.com">>,resource = <<>>,luser = <<>>,
            lserver = <<"example.com">>,lresource = <<>>},
    sub_els = 
        [#xmlel{
             name = <<"query">>,
             attrs = [{<<"xmlns">>,<<"ns:custom">>}],
             children = 
                 [#xmlel{
                      name = <<"abc">>,
                      attrs = [{<<"op">>,<<"sum">>}],
                      children = []}]}],
    meta = #{ip => {}}}
 [debug] local route:
#iq{id = <<"tZU4h-20">>,type = get,lang = <<"en">>,
    from = 
        #jid{
            user = <<"caLmPSeeWQo">>,server = <<"example.com">>,
            resource = <<"Smack">>,luser = <<"caLmPSeeWQo">>,
            lserver = <<"example.com">>,lresource = <<"Smack">>},
    to = 
        #jid{
            user = <<>>,server = <<"example.com">>,resource = <<>>,luser = <<>>,
            lserver = <<"example.com">>,lresource = <<>>},
    sub_els = 
        [#xmlel{
             name = <<"query">>,
             attrs = [{<<"xmlns">>,<<"ns:custom">>}],
             children = 
                 [#xmlel{
                      name = <<"abc">>,
                      attrs = [{<<"op">>,<<"sum">>}],
                      children = []}]}],
    meta = #{ip => {}}}
 [error] failed to process iq:
#iq{id = <<"tZU4h-20">>,type = get,lang = <<"en">>,
    from = 
        #jid{
            user = <<"caLmPSeeWQo">>,server = <<"example.com">>,
            resource = <<"Smack">>,luser = <<"caLmPSeeWQo">>,
            lserver = <<"example.com">>,lresource = <<"Smack">>},
    to = 
        #jid{
            user = <<>>,server = <<"example.com">>,resource = <<>>,luser = <<>>,
            lserver = <<"example.com">>,lresource = <<>>},
    sub_els = 
        [#xmlel{
             name = <<"query">>,
             attrs = [{<<"xmlns">>,<<"ns:custom">>}],
             children = 
                 [#xmlel{
                      name = <<"abc">>,
                      attrs = [{<<"op">>,<<"sum">>}],
                      children = []}]}],
    meta = #{ip => {}}}
Reason = {error,{undef,[{mod_test_custom,process_local_iq,[{iq,<<"tZU4h-20">>,get,<<"en">>,{jid,<<"caLmPSeeWQo">>,<<"example.com">>,<<"Smack">>,<<"caLmPSeeWQo">>,<<"example.com">>,<<"Smack">>},{jid,<<>>,<<"example.com">>,<<>>,<<>>,<<"example.com">>,<<>>},[{response,<<>>,<<>>}],#{ip => {}}}],[]},{gen_iq_handler,process_iq,3,[{file,"src/gen_iq_handler.erl"},{line,132}]},{gen_iq_handler,process_iq,4,[{file,"src/gen_iq_handler.erl"},{line,111}]},{ejabberd_local,route,1,[{file,"src/ejabberd_local.erl"},{line,72}]},{ejabberd_router,do_route,1,[{file,"src/ejabberd_router.erl"},{line,368}]},{ejabberd_router,route,1,[{file,"src/ejabberd_router.erl"},{line,92}]},{ejabberd_c2s,check_privacy_then_route,2,[{file,"src/ejabberd_c2s.erl"},{line,826}]},{xmpp_stream_in,process_authenticated_packet,2,[{file,"src/xmpp_stream_in.erl"},{line,637}]}]}}
 [debug] route:
#iq{id = <<"tZU4h-20">>,type = error,lang = <<"en">>,
    from = 
        #jid{
            user = <<>>,server = <<"example.com">>,resource = <<>>,luser = <<>>,
            lserver = <<"example.com">>,lresource = <<>>},
    to = 
        #jid{
            user = <<"caLmPSeeWQo">>,server = <<"example.com">>,
            resource = <<"Smack">>,luser = <<"caLmPSeeWQo">>,
            lserver = <<"example.com">>,lresource = <<"Smack">>},
    sub_els = 
        [#xmlel{
             name = <<"query">>,
             attrs = [{<<"xmlns">>,<<"ns:custom">>}],
             children = 
                 [#xmlel{
                      name = <<"abc">>,
                      attrs = [{<<"op">>,<<"sum">>}],
                      children = []}]},
         #stanza_error{
             type = wait,code = 500,by = <<>>,
             reason = 'internal-server-error',
             text = 
                 [#text{
                      lang = <<"en">>,
                      data = <<"Module failed to handle the query">>}],
             sub_els = []}],
    meta = #{ip => {}}}
 [debug] local route:
#iq{id = <<"tZU4h-20">>,type = error,lang = <<"en">>,
    from = 
        #jid{
            user = <<>>,server = <<"example.com">>,resource = <<>>,luser = <<>>,
            lserver = <<"example.com">>,lresource = <<>>},
    to = 
        #jid{
            user = <<"caLmPSeeWQo">>,server = <<"example.com">>,
            resource = <<"Smack">>,luser = <<"caLmPSeeWQo">>,
            lserver = <<"example.com">>,lresource = <<"Smack">>},
    sub_els = 
        [#xmlel{
             name = <<"query">>,
             attrs = [{<<"xmlns">>,<<"ns:custom">>}],
             children = 
                 [#xmlel{
                      name = <<"abc">>,
                      attrs = [{<<"op">>,<<"sum">>}],
                      children = []}]},
         #stanza_error{
             type = wait,code = 500,by = <<>>,
             reason = 'internal-server-error',
             text = 
                 [#text{
                      lang = <<"en">>,
                      data = <<"Module failed to handle the query">>}],
             sub_els = []}],
    meta = #{ip => {}}}
 [debug] processing packet to full JID:
#iq{id = <<"tZU4h-20">>,type = error,lang = <<"en">>,
    from = 
        #jid{
            user = <<>>,server = <<"example.com">>,resource = <<>>,luser = <<>>,
            lserver = <<"example.com">>,lresource = <<>>},
    to = 
        #jid{
            user = <<"caLmPSeeWQo">>,server = <<"example.com">>,
            resource = <<"Smack">>,luser = <<"caLmPSeeWQo">>,
            lserver = <<"example.com">>,lresource = <<"Smack">>},
    sub_els = 
        [#xmlel{
             name = <<"query">>,
             attrs = [{<<"xmlns">>,<<"ns:custom">>}],
             children = 
                 [#xmlel{
                      name = <<"abc">>,
                      attrs = [{<<"op">>,<<"sum">>}],
                      children = []}]},
         #stanza_error{
             type = wait,code = 500,by = <<>>,
             reason = 'internal-server-error',
             text = 
                 [#text{
                      lang = <<"en">>,
                      data = <<"Module failed to handle the query">>}],
             sub_els = []}],
    meta = #{ip => {}}}
 [debug] sending to process <0.523.0>:
#iq{id = <<"tZU4h-20">>,type = error,lang = <<"en">>,
    from = 
        #jid{
            user = <<>>,server = <<"example.com">>,resource = <<>>,luser = <<>>,
            lserver = <<"example.com">>,lresource = <<>>},
    to = 
        #jid{
            user = <<"caLmPSeeWQo">>,server = <<"example.com">>,
            resource = <<"Smack">>,luser = <<"caLmPSeeWQo">>,
            lserver = <<"example.com">>,lresource = <<"Smack">>},
    sub_els = 
        [#xmlel{
             name = <<"query">>,
             attrs = [{<<"xmlns">>,<<"ns:custom">>}],
             children = 
                 [#xmlel{
                      name = <<"abc">>,
                      attrs = [{<<"op">>,<<"sum">>}],
                      children = []}]},
         #stanza_error{
             type = wait,code = 500,by = <<>>,
             reason = 'internal-server-error',
             text = 
                 [#text{
                      lang = <<"en">>,
                      data = <<"Module failed to handle the query">>}],
             sub_els = []}],
    meta = #{ip => {}}}
 [debug] Won't add stanza for caLmPSeeWQo@example.com/Smack to CSI queue
 [info] (tcp|<0.523.0>) Send XML on stream = <<"<a h='5' xmlns='urn:xmpp:sm:3'/>">>
 [debug] Flushing packets of @example.com from CSI queue of caLmPSeeWQo@example.com/Smack
 [info] (tcp|<0.523.0>) Send XML on stream = <<"<iq xml:lang='en' to='caLmPSeeWQo@example.com/Smack' from='example.com' type='error' id='tZU4h-20'><query xmlns='ns:custom'><abc op='sum'/></query><error code='500' type='wait'><internal-server-error xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>Module failed to handle the query</text></error></iq>">>
 [info] (tcp|<0.523.0>) Send XML on stream = <<"<r xmlns='urn:xmpp:sm:3'/>">>
 [debug] caLmPSeeWQo@example.com/Smack acknowledged 7 of 7 stanzas
 [info] (tcp|<0.523.0>) Received XML on stream = <<"<a xmlns='urn:xmpp:sm:3' h='7'/>">>

I have followed these steps to register a custom iq https://github.com/processone/xmpp/issues/9 but still no luck.This is the element which I added in my xmpp/specs/xmpp_codec.spec file

-xml(response,
     #elem{name = <<"query">>,
           xmlns = <<"ns:custom">>,
           module = mod_custom,
           result = {response, '$op','$val'},
           attrs = [#attr{name = <<"op">>},
                    #attr{name = <<"val">>}]}).

Am I missing something or is there any change in the way it should be done in ejabberd-18.09?

来源:https://stackoverflow.com/questions/53424985/module-to-handle-custom-iq-stanzas-does-not-work-with-ejabberd-18-09

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!