ejabberd offline_message_hook未调用

时间:2013-05-05 00:53:12

标签: iphone erlang xmpp ejabberd

我正在尝试让我的ejabberd服务器使用自定义的offline_message_hook模块发送离线推送通知。问题是钩子似乎永远不会被调用。我已经尝试将钩子的优先级设置为0,49和50但仍然不起作用。

这是模块的代码:

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

%% 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, create_message/3]).

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

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

start(_Host, _Opt) ->
    ?INFO_MSG("mod_offline_push loading", []),
    inets:start(),
    ?INFO_MSG("HTTP client started", []),
    ejabberd_hooks:add(offline_message_hook, _Host, ?MODULE, create_message, 0).

stop (_Host) ->
    ?INFO_MSG("stopping mod_offline_push", []),
    ejabberd_hooks:delete(offline_message_hook, _Host, ?MODULE, create_message, 0).

create_message(_From, _To, Packet) ->
    ?INFO_MSG("creting offline message", []),
    Type = xml:get_tag_attr_s("type", Packet),
    FromS = xml:get_tag_attr_s("from", Packet),
    ToS = xml:get_tag_attr_s("to", Packet),
    if (Type == "chat") ->
        post_offline_message(FromS, ToS)
    end.

post_offline_message(From, To) ->
    ?INFO_MSG("Posting From ~p To ~p~n",[From, To]),
     httpc:request(post, {"http://host.com/push.php",[],
     "application/x-www-form-urlencoded",
     lists:concat(["From=", From,"&To=", To])}, [], []),
    ?INFO_MSG("post request sent", []).

2 个答案:

答案 0 :(得分:1)

将主机或全局传递给挂钩,而不是_Host。钩子需要注册到特定主机或全局钩子。

答案 1 :(得分:0)

很可能你启用了mod_offline_odbc,它在调用相同的钩子后停止发送。

为什么不尝试将优先级提高到> 50? 您还可以更改mod_offline_ *的实现,但它与ejabberd_sm中的其他挂钩同步。

小语法建议 - 请从_Host中删除下划线。它保留给不受限制的变量供以后使用。