Talespin_Kit
Talespin_Kit

Reputation: 21877

Ejabberd print only "message" packets

The following is one of the packet logged in the function which is called by filter_packet hook.

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"}],
                                                     []}]}]} 

How to filter out only the "message" , "type" packets ? i.e currently the hooked function looks like

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

How to write the code ** if (packet.type == message) only then print ** ?

Upvotes: 2

Views: 244

Answers (1)

legoscia
legoscia

Reputation: 41527

type is an XML attribute, so you need to use the function xml:get_tag_attr_s to get the value. Then, use case to switch depending on the value:

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.

Upvotes: 1

Related Questions