Ejabberd只打印“消息”包

时间:2015-10-11 09:30:53

标签: erlang ejabberd

以下是该功能中记录的数据包之一,由 filter_packet 挂钩调用。

I(<0.10945.0>:my_module:46) : in_filter_packet: {xmlelement,"message",
                                                 [{"type","headline"}],
                                                 [{xmlelement,"event",
                                                   [{"xmlns",
                                                     "http://jabber.org/protocol/pubsub#event"}],
                                                   [{xmlelement,"items",
                                                     [{"node",
                                                       "http://jabber.org/protocol/tune"}],
                                                     [{xmlelement,"item",
                                                       [{"id",
                                                         "5A487A38503FE"}],
                                                       [{xmlelement,"tune",
                                                         [{"xmlns",
                                                           "http://jabber.org/protocol/tune"}],
                                                         []}]}]}]},
                                                  {xmlelement,"addresses",
                                                   [{"xmlns",
                                                     "http://jabber.org/protocol/address"}],
                                                   [{xmlelement,"address",
                                                     [{"type","replyto"},
                                                      {"jid",
                                                       "test1@ubuntu/10042049171444555575238273"}],
                                                     []}]}]} 

如何仅滤除“消息”,“输入”数据包?当前钩子函数看起来像

on_filter_packet({From, To, Packet} = Input) ->
        ?INFO_MSG("in_filter_packet: ~p ", [Packet]), %[gen_mod:get_module_opt(global, ?MODULE, debug, false)]),
        Input.

如何编写代码** if(packet.type == message)只打印**?

1 个答案:

答案 0 :(得分:1)

type是XML属性,因此您需要使用函数xml:get_tag_attr_s来获取值。然后,根据值使用case进行切换:

on_filter_packet({From, To, Packet} = Input) ->
    ?INFO_MSG("in_filter_packet: ~p ", [Packet]), %[gen_mod:get_module_opt(global, ?MODULE, debug, false)]),
    case xml:get_tag_attr_s("type", Packet) of
        "message" ->
            %% Do something with "message" packets,
            %% and finally return a value.
            Input;
        _ ->
            %% Something other than "message".
            %% Ignore it and return the original packet.
            Input
    end.