[erlang-questions] running custom module in ejabberd by using hooks

Abhishek Ranjan abhishek@REDACTED
Tue Apr 4 15:54:23 CEST 2017


I have developed an application in erlang and have place it inside the ebin folder of ejabberd. I had to decide a server to be able to host the game hence I chose ejabberd for it.I am using Pidgin as a client side messaging server.My strategy was to detect the message stanza sent by the client and extract the actual message from its body tag and use it as a function call to run my application.

I have created a custom module as


-module(customMod).
-behaviour(gen_mod).


-export([start/2,stop/1,depends/2,mod_opt_type/1,extractMessage/1]).
-include("logger.hrl").
-include("xmpp.hrl").
-include("ejabberd.hrl").

start(_Host, _Opts) ->
ejabberd_hooks:add(user_send_packet, _Host, ?MODULE,
extractMessage, 95).


stop(_Host) ->
ejabberd_hooks:delete(user_send_packet, _Host, ?MODULE, extractMessage,95).

depends(_Host, _Opts)->[{?MODULE,soft}].

mod_opt_type(_Option)->
ok.


extractMessage(Args)->
A=10,
?INFO_MSG("hello buddy ~p",[A]),
Args.


Now to call this module I have written the following code snippet in ejabberd_router inside do_route:

do_route(OrigPacket) ->
?DEBUG("route:~n~s", [xmpp:pp(OrigPacket)]),
 

A=1,
To1 = xmpp:get_to(OrigPacket),
LDstDomain = To1#jid.lserver,
case xmpp:get_type(OrigPacket) of
get->
get;
result->
result;
error->
error;
probe->
probe;
available->
available;
set->
set;
chat->
?INFO_MSG("Inside ejabberd router value of PacketType is ~p",[A]),
ejabberd_hooks:run_fold(user_receive_packet, LDstDomain, OrigPacket, ["dharun"])
end,

case ejabberd_hooks:run_fold(filter_packet, OrigPacket, []) of
drop ->
ok;
Packet ->
To = xmpp:get_to(Packet),
LDstDomain = To#jid.lserver,
Mod = get_backend(),
case Mod:find_routes(LDstDomain) of
[] ->
ejabberd_s2s:route(Packet);
[Route] ->
do_route(Packet, Route);
Routes ->
>From = xmpp:get_from(Packet),
balancing_route(From, To, Packet, Routes)
end,
ok
end.


So according to me my custom module should be called fro ejabberd_router but it is being called as I start my ejabberd and that to it is being called multiple times

ejabberd_log is:

(ejabberd@REDACTED)1> 18:44:06.134 [info] Application lager started on node ejabberd@REDACTED
18:44:06.137 [info] Application crypto started on node ejabberd@REDACTED
18:44:06.146 [info] Application sasl started on node ejabberd@REDACTED
18:44:06.156 [info] Application asn1 started on node ejabberd@REDACTED
18:44:06.156 [info] Application public_key started on node ejabberd@REDACTED
18:44:06.164 [info] Application ssl started on node ejabberd@REDACTED
18:44:06.176 [info] Application fast_yaml started on node ejabberd@REDACTED
18:44:06.191 [info] Application fast_tls started on node ejabberd@REDACTED
18:44:06.207 [info] Application fast_xml started on node ejabberd@REDACTED
18:44:06.212 [info] Application p1_utils started on node ejabberd@REDACTED
18:44:06.215 [info] Application stringprep started on node ejabberd@REDACTED
18:44:06.217 [info] Application xmpp started on node ejabberd@REDACTED
18:44:06.229 [info] Application cache_tab started on node ejabberd@REDACTED
18:44:06.252 [info] Application elixir started on node ejabberd@REDACTED
18:44:06.419 [info] Application mnesia started on node ejabberd@REDACTED
18:44:06.542 [warning] Module mod_last_odbc is deprecated, use mod_last with 'db_type: sql' instead
18:44:06.554 [warning] Module mod_offline_odbc is deprecated, use mod_offline with 'db_type: sql' instead
18:44:06.561 [warning] Module mod_privacy_odbc is deprecated, use mod_privacy with 'db_type: sql' instead
18:44:06.566 [warning] Module mod_private_odbc is deprecated, use mod_private with 'db_type: sql' instead
18:44:06.570 [warning] Module mod_pubsub_odbc is deprecated, use mod_pubsub with 'db_type: sql' instead
18:44:06.575 [warning] Module mod_roster_odbc is deprecated, use mod_roster with 'db_type: sql' instead
18:44:06.591 [warning] Module mod_vcard_odbc is deprecated, use mod_vcard with 'db_type: sql' instead
18:44:07.355 [info] Adding machine's DNS IPs to Erlang system:
[]
18:44:07.367 [info] FQDN used to check DIGEST-MD5 SASL authentication: DESKTOP-RI8MDR6
18:44:07.905 [info] Application p1_mysql started on node ejabberd@REDACTED
18:44:08.088 [warning] cyclic dependency detected between modules: [customMod,customMod]
18:44:08.088 [warning] module 'mod_mam' is recommended for module 'mod_muc' but is not found in the config
slot has started
18:44:08.100 [info] Inside customMod start function {ok,<0.444.0>}
18:44:08.187 [info] Application inets started on node ejabberd@REDACTED
18:44:08.202 [info] Start accepting TCP connections at 0.0.0.0:5222 for ejabberd_c2s
18:44:08.202 [info] ejabberd 17.03 is started in the node ejabberd@REDACTED in 2.38s
18:44:08.202 [info] Start accepting TCP connections at 0.0.0.0:5269 for ejabberd_s2s_in
18:44:08.202 [info] Application ejabberd started on node ejabberd@REDACTED
18:44:08.202 [info] Start accepting TCP connections at 0.0.0.0:5280 for ejabberd_http
18:44:15.864 [info] (<0.498.0>) Accepted connection 127.0.0.1:63043 -> 127.0.0.1:5222
18:44:16.046 [info] (tls|<0.498.0>) Accepted c2s PLAIN authentication for admin1@REDACTED by sql backend from 127.0.0.1
18:44:16.085 [info] (<0.500.0>) Accepted connection 127.0.0.1:63044 -> 127.0.0.1:5222
18:44:16.179 [info] (tls|<0.500.0>) Accepted c2s PLAIN authentication for admin2@REDACTED by sql backend from 127.0.0.1
18:44:16.204 [info] (tls|<0.498.0>) Opened c2s session for admin1@REDACTED/-
18:44:16.266 [info] hello buddy 10
18:44:16.266 [info] hello buddy 10
18:44:16.374 [info] (tls|<0.500.0>) Opened c2s session for admin2@REDACTED/-
18:44:16.436 [info] hello buddy 10
18:44:16.436 [info] hello buddy 10
18:44:16.468 [info] Outbound s2s connection started: casino.com -> proxy.eu.jabber.org
18:44:16.469 [info] hello buddy 10
18:44:16.469 [info] hello buddy 10
18:44:16.469 [info] hello buddy 10
18:44:16.469 [info] hello buddy 10
18:44:16.503 [info] hello buddy 10
18:44:16.503 [info] hello buddy 10
18:44:16.508 [info] Failed to establish outbound s2s connection casino.com -> proxy.eu.jabber.org: DNS lookup failed: non-existing domain; bouncing for 129 seconds
18:44:16.508 [info] hello buddy 10
18:44:16.639 [info] hello buddy 10
18:44:16.639 [info] hello buddy 10
18:44:16.640 [info] hello buddy 10
18:44:16.640 [info] hello buddy 10
18:44:16.641 [info] hello buddy 10
18:44:16.641 [info] hello buddy 10
18:44:16.642 [info] hello buddy 10
18:44:17.214 [info] hello buddy 10
18:44:17.309 [info] The value of mod inside sm is ejabberd_sm_mnesia
18:44:17.309 [info] hello buddy 10
18:44:17.379 [info] hello buddy 10
18:44:17.449 [info] The value of mod inside sm is ejabberd_sm_mnesia
18:44:17.449 [info] hello buddy 10
18:44:47.007 [info] hello buddy 10
18:44:47.007 [info] hello buddy 10
18:45:47.000 [info] hello buddy 10
18:45:47.000 [info] hello buddy 10
18:46:47.001 [info] hello buddy 10
18:46:47.002 [info] hello buddy 10
18:47:47.001 [info] hello buddy 10
18:47:47.001 [info] hello buddy 10
18:48:47.001 [info] hello buddy 10
18:48:47.001 [info] hello buddy 10
18:49:47.001 [info] hello buddy 10
18:49:47.002 [info] hello buddy 10
18:50:47.001 [info] hello buddy 10
18:50:47.002 [info] hello buddy 10
18:51:47.002 [info] hello buddy 10
18:51:47.004 [info] hello buddy 10
18:52:47.001 [info] hello buddy 10
18:52:47.002 [info] hello buddy 10
18:53:47.002 [info] hello buddy 10
18:53:47.003 [info] hello buddy 10

18:54:47.001 [info] hello buddy 10
18:54:47.002 [info] hello buddy 10
18:55:47.000 [info] hello buddy 10
18:55:47.001 [info] hello buddy 10
18:56:47.002 [info] hello buddy 10
18:56:47.002 [info] hello buddy 10
18:57:47.000 [info] hello buddy 10
18:57:47.001 [info] hello buddy 10
18:58:47.001 [info] hello buddy 10
18:58:47.001 [info] hello buddy 10
18:59:47.003 [info] hello buddy 10
18:59:47.004 [info] hello buddy 10
19:00:47.002 [info] hello buddy 10
19:00:47.004 [info] hello buddy 10
19:01:47.001 [info] hello buddy 10
19:01:47.001 [info] hello buddy 10
19:02:47.001 [info] hello buddy 10
19:02:47.001 [info] hello buddy 10
19:03:47.001 [info] hello buddy 10
19:03:47.002 [info] hello buddy 10
19:04:47.002 [info] hello buddy 10
19:04:47.003 [info] hello buddy 10
19:05:47.001 [info] hello buddy 10
19:05:47.002 [info] hello buddy 10
19:06:47.001 [info] hello buddy 10
19:06:47.002 [info] hello buddy 10
19:07:47.002 [info] hello buddy 10
19:07:47.003 [info] hello buddy 10
19:08:47.001 [info] hello buddy 10
19:08:47.002 [info] hello buddy 10
19:09:47.001 [info] hello buddy 10
19:09:47.001 [info] hello buddy 10
19:10:47.001 [info] hello buddy 10
19:10:47.001 [info] hello buddy 10
19:11:47.000 [info] hello buddy 10
19:11:47.001 [info] hello buddy 10
19:12:47.000 [info] hello buddy 10
19:12:47.000 [info] hello buddy 10
19:13:47.000 [info] hello buddy 10
19:13:47.000 [info] hello buddy 10
19:14:47.001 [info] hello buddy 10
19:14:47.001 [info] hello buddy 10
19:15:47.001 [info] hello buddy 10
19:15:47.001 [info] hello buddy 10


can anyone help?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170404/9e9392b6/attachment.htm>


More information about the erlang-questions mailing list