2015-10-11 74 views
2

以下是被掛鉤的函數中記錄的數據包之一filter_packet hook。Ejabberd只打印「消息」數據包

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", 
                 "[email protected]/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)only then print **?

回答

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.