我需要客户端验证消息到达Ejabberd服务器。对我来说最简单的方法似乎有一个钩子将消息返回给用户然后编码,以便我的客户端验证。
我发现我验证过的mod_stanza_ack是由所有传入消息触发的。
所以问题是:
如何从钩子中向用户发送消息?
我需要快速解决方案。
-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.
答案 0 :(得分:3)
%% 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.