1
votes

I have been developing a custom module in ejabberd. I need to extract out message's body content and also check whether stanza has a message text or not . Here is my code snippet

-module(mod_push_generate).

-behaviour(gen_mod).

-include("logger.hrl").
-include("ejabberd_sql_pt.hrl").
-include("ejabberd_http.hrl").
-include("xmpp.hrl").
%-include("jlib.hrl").

%% gen_mod API callbacks
-export([start/2, stop/1, depends/2, mod_options/1, fetch_token/2, on_user_send_packet/1]).

start(Host,_Opts) ->
  ?INFO_MSG("Mod PUSH GENARATOR has been started",[]),
  inets:start(),
  ejabberd_hooks:add(user_send_packet, Host, ?MODULE, on_user_send_packet, 50),
  ok.

stop(Host) ->
  ?INFO_MSG("Module has been stopped",[]),
  ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, on_user_send_packet, 50),
  ok.

depends(_Host, _Opts) ->
  [].


mod_options(_Host) ->
  [].

%-spec on_user_send_packet({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.

%on_user_send_packet({From, To, XML} = Pkt) ->
on_user_send_packet({#message{to = Peer} = Pkt, #{jid := JID} = C2SState}) ->
  ?INFO_MSG("on user send packet called from  ",[Pkt]),
  Els = xmpp:get_els(Pkt),
  ?INFO_MSG("Body: ",[Els]),
  {Pkt, C2SState};

on_user_send_packet(Acc) ->
    ?INFO_MSG("Inside nothing box.",[]),
    Acc.

Logging Pkt is giving

12:41:17.956 [info] FORMAT ERROR: "on user send packet called from  " [{message,<<"2fe6c6a3-e89e-4f05-ac4d-b096b75f0f32">>,chat,<<"en">>,{jid,<<"sagar">>,<<"192.168.4.69">>,<<"sagar.webApp">>,<<"sagar">>,<<"192.168.4.69">>,<<"sagar.webApp">>},{jid,<<"tony">>,<<"192.168.4.69">>,<<>>,<<"tony">>,<<"192.168.4.69">>,<<>>},[],[{text,<<>>,<<"xasXASXAS">>}],undefined,[{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}],#{ip => {0,0,0,0,0,65535,49320,1093}}}]

and logging Els is giving

14:04:25.906 [info] FORMAT ERROR: "Body: " [[{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}]]

second line shows that of i take out component of message stanza the only xml element detecting is "request" element . Why there is no "body element " in message stanza .

Please also tell me how can i take out(or check the existence of) the message text in the stanza .

Thanks

1

1 Answers

0
votes

Why there is no "body element " in message stanza .

The #message structure keeps the body content in #message.body, not in #message.sub_els. See the examples in https://docs.ejabberd.im/developer/guide/#xmpp-codec

I need to extract out message's body content

Try this line:

  ?INFO_MSG("Body: ~p", [ xmpp:get_text(Pkt#message.body) ]),

It will log things like these:

17:08:12.897 [info] Body: <<"hey, are you there?">>
17:08:12.926 [info] Body: <<>>

and also check whether stanza has a message text or not .

When the Body is <<>>, it means it has no body.