Aaron
Aaron

Reputation: 382

ejabberd offline_message_hook not called

I'm trying to get my ejabberd server to send offline push notifications by using a custom offline_message_hook module. The problem is the hook never seems to get called. I've tried setting the priority of the hook to 0, 49, and 50 but still doesn't work.

This is the code for the module:

% 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", []).

Upvotes: 0

Views: 1007

Answers (2)

Ashwin Giridharan
Ashwin Giridharan

Reputation: 11

Pass Host or global to the hook, rather than _Host. The hook needs to be registered to a specific Host or as a global hook.

Upvotes: 1

user425720
user425720

Reputation: 3598

Most likely you enabled mod_offline_odbc which issues stop after calling the same hook.

Why don't you try to increase priority to >50 ? You can also change implementation of mod_offline_*, but it is synchronized with other hooks in ejabberd_sm.

Small syntax advice -- please get rid of that underscore from _Host. It is reserved for variables which are not bounded for later use.

Upvotes: 0

Related Questions