0

I need client side verification that message reached Ejabberd server. Easiest way to me seems to have a hook return the message back to the user and then code so my client verifies.

I found mod_stanza_ack with I've verified is triggered by all incoming messages.

So the question is:

How do I send a message back to the user from within the hook?

I need a quick solution.

-module(mod_stanza_ack).
-behaviour(gen_mod).

-include("ejabberd.hrl").

-export([start/2,
         stop/1]).

-export([on_user_send_packet/3]).


start(Host, _Opts) ->
    ?INFO_MSG("mod_stanza_ack starting", []),
    ejabberd_hooks:add(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
    ok.

stop(Host) ->
    ?INFO_MSG("mod_stanza_ack stopping", []),
    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
    ok.

on_user_send_packet(From, To, Packet) ->
    ?INFO_MSG("mod_stanza_ack a package has been sent coming from: ~p", [From]),
    ?INFO_MSG("mod_stanza_ack a package has been sent to: ~p", [To]),
    ?INFO_MSG("mod_stanza_ack a package has been sent with the following packet: ~p",     [Packet]),
    Packet.
4

1 回答 1

4

好的,所以我想通了。这是一个非常简单的 return all 钩子:

%% name of module must match file name
-module(mod_echo_msg).

%% Every ejabberd module implements the gen_mod behavior
%% The gen_mod behavior requires two functions: start/2 and stop/1
-behaviour(gen_mod).

%% public methods for this module
-export([start/2, stop/1]).
-export([on_user_send_packet/3]).

%% included for writing to ejabberd log file
-include("ejabberd.hrl").

%% ejabberd functions for JID manipulation called jlib.
-include("jlib.hrl").

%%add and remove hook module on startup and close

start(Host, _Opts) ->
    ?INFO_MSG("mod_echo_msg starting", []),
    ejabberd_hooks:add(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
    ok.

stop(Host) ->
    ?INFO_MSG("mod_echo_msg stopping", []),
    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
    ok.

on_user_send_packet(From, To, Packet) ->
        return_message_reciept_to_sender(From, To, Packet),
        Packet.


return_message_reciept_to_sender(From, _To, Packet) ->
    IDS = xml:get_tag_attr_s("id", Packet),
    FeedIDS = xml:get_tag_attr_s("feedid", Packet),
    ReturnRecieptType = "serverreceipt",
%%    ?INFO_MSG("mod_echo_msg - FeedID: ~p MsgID: ~p To: ~p From: ~p", [FeedIDS, IDS, _To, From]),
    send_message(From, From, ReturnRecieptType, FeedIDS, IDS, "").



send_message(From, To, TypeStr, FeedIDS, IDS, BodyStr) ->
    XmlBody = {xmlelement, "message",
           [{"type", TypeStr},
        {"from", jlib:jid_to_string(From)},
        {"to", jlib:jid_to_string(To)},
        {"id", IDS},
        {"feedid", FeedIDS}],
           [{xmlelement, "body", [],
         [{xmlcdata, BodyStr}]}]},
    ejabberd_router:route(From, To, XmlBody).

如果您想限制返回的消息类型,请将此函数更改为以下内容:

on_user_send_packet(From, To, Packet) ->
    TypeS = xml:get_tag_attr_s("type", Packet),
    if TypeS == "sometype0"; TypeS == "sometype1"; TypeS == "sometype2" ->
        return_message_reciept_to_sender(From, To, Packet);
        true-> none
    end,
    Packet.
于 2013-11-12T20:07:05.220 回答