0

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

-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}]}]}

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

4

1 回答 1

1

你正在传递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),因为如果您真的想在这个特定的处理程序之前插入一些处理程序到处理程序链中,这给您带来的灵活性很小。

于 2015-03-05T09:27:47.200 回答