xmlxmppchatejabberdejabberd-hooks

How do I append custom data in the body of a xmpp message in ejabberd


I have set up a chat settong using Pidgin and Ejabberd.I have written down a custom module in ejabberd using user_send_packet:

ejabberd_hooks:add(user_send_packet, _Host, ?MODULE,
           myMessage, 95),

The function myMessage is as follows:

myMessage({Packet, C2SState})->


PacketType=xmpp:get_name(Packet),
case PacketType of
<<"iq">>->
ok;
<<"presence">>->
ok;
<<"message">>->

Sum=2+2,
?INFO_MSG("Sum is ~p~n",[Sum])

end,

{Packet,C2SState}.

Basically what this function does is that whenever someone sends a chat message say "hello there", the value of Sum gets calculated and printed on the server and its logs and the message ""hello there" is sent to the second user.

But Now I want send the value of Sum along with the message "hello there" to the second user for example:

"hello there Sum is 4" 

Can anyone help me out with this?

Thanks in advance.


Solution

  • Here it is:

    process_message({#message{body = Body} = Msg, C2SState})->
        Sum = calc_sum_and_return_as_binary(),
        NewBody = lists:map(
            fun(#text{data = Data} = Txt) ->
                Txt#text{data = <<Data/binary, Sum/binary>>}
            end, Body),
        {Msg#message{body = NewBody}, C2SState};
    process_message(Acc) ->
        Acc.
    

    Note, that #text{} record contains lang field which can be used if you want to support internationalization of the text being appended.