【问题标题】:Ejabberd print only "message" packetsEjabberd 仅打印“消息”数据包
【发布时间】:2016-01-08 21:22:31
【问题描述】:

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

如何只过滤掉 "message" , "type" 数据包?即当前钩子函数看起来像

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 ** ?

【问题讨论】:

    标签: erlang ejabberd


    【解决方案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.
    

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 1970-01-01
      • 2012-05-25
      • 2016-07-03
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2018-01-13
      相关资源
      最近更新 更多