xml、append_subtags 时出现 Ejabberd 错误

Ejabberd error while xml,append_subtags

我是 erlang 和 ejabbered/mongooseIM 的新手。我正在尝试编写一个非常简单的 mongoose 模块,它将在发送之前向数据包添加一个额外的子元素。下面是我的代码:

-module(mod_test).

-behavior(gen_mod).


-export([start/2, stop/1]).
-export([add_child/1]).

-include("ejabberd.hrl").

start(Host, Opts) ->
    ejabberd_hooks:add(filter_packet, Host, ?MODULE, add_child, 0),
    ?DEBUG(" MOD_TEST Started",[]),
    ok.

stop(Host) ->
    ejabberd_hooks:delete(filter_packet, Host, ?MODULE, add_child, 0),
    ok.

add_child({From, To, XML} = Packet) ->
    Tag = {"a","b"},
    NewPacket = xml:append_subtags(Packet, [Tag]),
    ?INFO_MSG("  To party: ~p~n",[To]),
    NewPacket.

我可以成功编译和启动模块。但是,我在 运行:

时收到以下错误
2015-03-04 12:16:30.506 [error] <0.760.0>@ejabberd_hooks:run_fold1:259 
{function_clause,
[{xml,append_subtags,[{{jid,<<"user2">>,<<"localhost">>,<<"Damien">>,<<"user2">>,<<"localhost">>,<<"Damien">>},{jid,<<"user1">>,<<"localhost">>,<<"Gajim">>,<<"user1">>,<<"localhost">>,<<"Gajim">>},{xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"type">>,<<"chat">>},{<<"to">>,<<"user1@localhost/Gajim">>},{<<"id">>,<<"ab68a">>}],[{xmlcdata,<<"\n">>},{xmlel,<<"body">>,[],[{xmlcdata,<<"mmmmm">>}]},{xmlcdata,<<"\n">>},{xmlel,<<"active">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/chatstates">>}],[]},{xmlcdata,<<"\n">>}]}},
[{"a","b"}]],[{file,"src/xml.erl"},{line,276}]},
{safely,apply,3,[{file,"src/safely.erl"},{line,19}]},
{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,255}]},
{ejabberd_router,do_route,3,[{file,"src/ejabberd_router.erl"},{line,246}]},
{ejabberd_router,route,3,[{file,"src/ejabberd_router.erl"},{line,78}]},
{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1098}]},
{p1_fsm_old,handle_msg,10,[{file,"src/p1_fsm_old.erl"},{line,542}]},
{proc_lib,wake_up,3,[{file,"proc_lib.erl"},{line,247}]}]}

问题是什么以及如何解决?

您正在将 Packet(一个元组:{From, To, XML})传递给 xml:append_subtags/2,而您应该只传递 XML。您的 add_child/1 应该更像:

add_child({From, To, XML} = Packet) ->
    Tag = {<<"a">>, <<"b">>},
    NewPacket = {From, To, xml:append_subtags(XML, [Tag])},
    ?INFO_MSG("  To party: ~p~n",[To]),
    NewPacket.

我还将 {"a", "b"} 更改为 {<<"a">>, <<"b">>},因为二进制文件在 MongooseIM 代码库中随处用于字符串处理。

此外,不建议使用优先级 0(ejabberd_hooks:add/5 的最后一个参数),因为如果您实际上想在处理程序链中的这个特定处理程序之前插入一些处理程序,这会给您带来很小的灵活性。