0
votes

I have simple ejabebrd plugin and i need to send message from it.Here i posted my code sample and error which i got. Kindly give me your feedback on this.

My code:

-module(mod_final).
-on_load(send_message/0).
-behaviour(gen_mod).
-export([start/2, stop/1]).

-include("ejabberd.hrl").
-include("logger.hrl").
-include("jlib.hrl").

start(_Host, _Opt) ->
        ?INFO_MSG("mod_final starting", []),
        ok.

stop(_Host) ->
    ?INFO_MSG("mod_final stoping", []),
        ok.

send_message() ->
    LUser = "test1",
    LServer = "localhost",
    From = jlib:make_jid(LUser, LServer, []),

    TUser = "test2",
    TServer = "localhost",
    To = jlib:make_jid(TUser, TServer, []),

    FromAddress = jlib:jid_to_string(From),
    ToAddress = jlib:jid_to_string(To),

    ?INFO_MSG("send message starting", []),

    XmlBody = {xmlelement, "message", [{"id", []},{"type", "chat"}, {"from", FromAddress}, {"to", ToAddress}], [{xmlelement, "body", [], [{xmlcdata, <<"Test Message">>}]}]},

   ejabberd_router:route(From, To, XmlBody).

But i got this error:

   D(<0.252.0>:ejabberd_router:313) : route
    from {jid,"test1","localhost",[],"test1",
                  "localhost",[]}
    to {jid,"test2","localhost",[],"test2",
                "localhost",[]}
    packet {xmlelement,"message",
                   [{"type","error"},
                    {"to","test2@localhost"},
                    {"from","test1@localhost"},
                    {"id",[]}],
                   [{xmlelement,"body",[],[{xmlcdata,<<"Test Message">>}]},
                    {xmlelement,"error",
                        [{"code","503"},{"type","cancel"}],
                        [{xmlelement,"service-unavailable",
                             [{"xmlns","urn:ietf:params:xml:ns:xmpp-stanzas"}],
                             []}]}]}

Appreciate your help.

1

1 Answers

0
votes

My suggestion is to not create own XML. You can use this function to catch the packet and then write to error log

on_filter_packet({From, To, XML} = Packet) ->
    %% does something with a packet
    %% should return modified Packet or atom `drop` to drop the packet

    error_logger:info_msg(XML, [a_module]),

    %% or use error_logger:info_msg(xml:get_subtag(XML, <<"body">>), [a_module]),
    %% to take a body
    Packet.

Then you can just use the same xml, but with changed xmlcdata