<!DOCTYPE html>
<html><head>
    <meta charset="UTF-8">
</head><body><p><strong>I have created a custom module in ejabberd that should print hello buddy in logs only when there is a exchange of message stanzas between the client and server: Here is the code-></strong></p><p>-module(newCust).<br>-behaviour(gen_mod).</p><p>-export([]).</p><p>-export([start/2,stop/1,depends/2,mod_opt_type/1,myMessage/4]).<br>-include("logger.hrl").<br>-include("xmpp.hrl").<br>-include("ejabberd.hrl").</p><p>start(_Host, _Opts) -></p><p>?INFO_MSG("Inside customMod start function ",[]),<br> ejabberd_hooks:add(user_send_packet, _Host, ?MODULE,<br> myMessage, 105),<br> ok.<br>stop(_Host) -> <br> ejabberd_hooks:delete(user_send_packet, _Host, ?MODULE, myMessage,105).</p><p>depends(_Host, _Opts)->[{?MODULE,soft}].</p><p>mod_opt_type(_Option)-><br> ok.</p><p>myMessage(#xmlel{name = <<"message">>} = Pkt, _C2SState, _From, _To)-></p><p>?INFO_MSG("hello buddy ",[]),<br>Pkt.<br></p><p><br></p><p><strong>The log values are :</strong></p><p>2017-04-05 17:43:02.498 [info] <0.7.0> Application lager started on node ejabberd@localhost<br>2017-04-05 17:43:02.502 [info] <0.7.0> Application crypto started on node ejabberd@localhost<br>2017-04-05 17:43:02.511 [info] <0.7.0> Application sasl started on node ejabberd@localhost<br>2017-04-05 17:43:02.521 [info] <0.7.0> Application asn1 started on node ejabberd@localhost<br>2017-04-05 17:43:02.521 [info] <0.7.0> Application public_key started on node ejabberd@localhost<br>2017-04-05 17:43:02.529 [info] <0.7.0> Application ssl started on node ejabberd@localhost<br>2017-04-05 17:43:02.543 [info] <0.7.0> Application fast_yaml started on node ejabberd@localhost<br>2017-04-05 17:43:02.559 [info] <0.7.0> Application fast_tls started on node ejabberd@localhost<br>2017-04-05 17:43:02.575 [info] <0.7.0> Application fast_xml started on node ejabberd@localhost<br>2017-04-05 17:43:02.579 [info] <0.7.0> Application p1_utils started on node ejabberd@localhost<br>2017-04-05 17:43:02.581 [info] <0.7.0> Application stringprep started on node ejabberd@localhost<br>2017-04-05 17:43:02.582 [info] <0.7.0> Application xmpp started on node ejabberd@localhost<br>2017-04-05 17:43:02.595 [info] <0.7.0> Application cache_tab started on node ejabberd@localhost<br>2017-04-05 17:43:02.618 [info] <0.7.0> Application elixir started on node ejabberd@localhost<br>2017-04-05 17:43:02.786 [info] <0.7.0> Application mnesia started on node ejabberd@localhost<br>2017-04-05 17:43:02.900 [warning] <0.37.0>@ejabberd_config:emit_deprecation_warning:1427 Module mod_last_odbc is deprecated, use mod_last with 'db_type: sql' instead<br>2017-04-05 17:43:02.911 [warning] <0.37.0>@ejabberd_config:emit_deprecation_warning:1427 Module mod_offline_odbc is deprecated, use mod_offline with 'db_type: sql' instead<br>2017-04-05 17:43:02.918 [warning] <0.37.0>@ejabberd_config:emit_deprecation_warning:1427 Module mod_privacy_odbc is deprecated, use mod_privacy with 'db_type: sql' instead<br>2017-04-05 17:43:02.922 [warning] <0.37.0>@ejabberd_config:emit_deprecation_warning:1427 Module mod_private_odbc is deprecated, use mod_private with 'db_type: sql' instead<br>2017-04-05 17:43:02.926 [warning] <0.37.0>@ejabberd_config:emit_deprecation_warning:1427 Module mod_pubsub_odbc is deprecated, use mod_pubsub with 'db_type: sql' instead<br>2017-04-05 17:43:02.930 [warning] <0.37.0>@ejabberd_config:emit_deprecation_warning:1427 Module mod_roster_odbc is deprecated, use mod_roster with 'db_type: sql' instead<br>2017-04-05 17:43:02.944 [warning] <0.37.0>@ejabberd_config:emit_deprecation_warning:1427 Module mod_vcard_odbc is deprecated, use mod_vcard with 'db_type: sql' instead<br>2017-04-05 17:43:03.643 [info] <0.37.0>@ejabberd_app:add_windows_nameservers:132 Adding machine's DNS IPs to Erlang system:<br>[]<br>2017-04-05 17:43:03.654 [info] <0.279.0>@cyrsasl_digest:start:62 FQDN used to check DIGEST-MD5 SASL authentication: DESKTOP-RI8MDR6<br>2017-04-05 17:43:04.176 [info] <0.7.0> Application p1_mysql started on node ejabberd@localhost<br>2017-04-05 17:43:04.308 [warning] <0.277.0>@gen_mod:sort_modules:149 module 'mod_mam' is recommended for module 'mod_muc' but is not found in the config<br>2017-04-05 17:43:04.309 [warning] <0.277.0>@gen_mod:sort_modules:157 cyclic dependency detected between modules: [newCust,newCust]<br>2017-04-05 17:43:04.314 [info] <0.277.0>@newCust:start:17 Inside customMod start function <br>2017-04-05 17:43:04.443 [info] <0.7.0> Application inets started on node ejabberd@localhost<br>2017-04-05 17:43:04.458 [info] <0.325.0>@ejabberd_listener:init_tcp:149 Start accepting TCP connections at 0.0.0.0:5222 for ejabberd_c2s<br>2017-04-05 17:43:04.458 [info] <0.326.0>@ejabberd_listener:init_tcp:149 Start accepting TCP connections at 0.0.0.0:5269 for ejabberd_s2s_in<br>2017-04-05 17:43:04.458 [info] <0.37.0>@ejabberd_app:start:59 ejabberd 17.03 is started in the node ejabberd@localhost in 2.22s<br>2017-04-05 17:43:04.458 [info] <0.327.0>@ejabberd_listener:init_tcp:149 Start accepting TCP connections at 0.0.0.0:5280 for ejabberd_http<br>2017-04-05 17:43:04.458 [info] <0.7.0> Application ejabberd started on node ejabberd@localhost<br>2017-04-05 17:43:09.676 [info] <0.325.0>@ejabberd_listener:accept:311 (<0.491.0>) Accepted connection 127.0.0.1:63321 -> 127.0.0.1:5222<br>2017-04-05 17:43:09.807 [info] <0.492.0>@ejabberd_c2s:handle_auth_success:439 (tls|<0.491.0>) Accepted c2s PLAIN authentication for admin2@casino.com by sql backend from 127.0.0.1<br>2017-04-05 17:43:09.981 [info] <0.492.0>@ejabberd_c2s:bind:410 (tls|<0.491.0>) <strong>Opened c2s session for admin2@casino.com/-</strong><br>2017-04-05 17:43:09.991 [error] <0.492.0>@<strong>ejabberd_hooks:safe_apply:383 Hook user_send_packet crashed when running newCust:myMessage/1:</strong><br>** <strong>Reason</strong> = {error,undef,[{newCust,myMessage,[],[{{iq,<<"purple701889db">>,set,<<"en">>,{jid,<<"admin2">>,<<"casino.com">>,<<"-">>,<<"admin2">>,<<"casino.com">>,<<"-">>},{jid,<<"admin2">>,<<"casino.com">>,<<>>,<<"admin2">>,<<"casino.com">>,<<>>},[{xmlel,<<"session">>,[{<<"xmlns">>,<<"urn:ietf:params:xml:ns:xmpp-session">>}],[]}],#{ip => {127,0,0,1}}},#{mgmt_stanzas_in => 0,xmlns => <<"jabber:client">>,stream_encrypted => true,tls_enabled => true,socket => {socket_state,fast_tls,{tlssock,#Port<0.23143>,#Port<0.23190>},<0.491.0>},mod => ejabberd_c2s,server => <<"casino.com">>,mgmt_state => inactive,mgmt_max_timeout => 300,stream_restarted => true,auth_module => ejabberd_auth_sql,stream_id => <<"15132958958157338735">>,mgmt_ack_timeout => 60000,privacy_list => {userlist,none,[],false},stream_timeout => infinity,sid => {{1491,394389,1},<0.492.0>},pres_f => {1,{{<<"admin2">>,<<"casino.com">>,<<>>},nil,nil}},tls_options => [compression_none,{protocol_options,<<"no_sslv3">>},{certfile,<<"C:\\ProgramData\\ejabberd\\conf\\server.pem">>}],tls_required => false,mgmt_timeout => 300,socket_monitor => #Ref<0.0.2.141>,stream_compressed => false,mgmt_resend => false,mgmt_stanzas_out => 0,csi_state => active,lserver => <<"casino.com">>,zlib => false,lang => <<"en">>,ip => {{127,0,0,1},63321},mgmt_max_queue => 1000,stream_version => {1,0},resource => <<"-">>,jid => {jid,<<"admin2">>,<<"casino.com">>,<<"-">>,<<"admin2">>,<<"casino.com">>,<<"-">>},csi_queue => {0,0,#{}},shaper => c2s_shaper,user => <<"admin2">>,access => c2s,stream_state => established,tls_verify => false,sockmod => ejabberd_socket,stream_direction => in,mgmt_stanzas_req => 0,pres_t => {1,{{<<"admin2">>,<<"casino.com">>,<<>>},nil,nil}},owner => <0.492.0>,stream_header_sent => true,pres_a => {0,nil},stream_authenticated => true,conn => c2s_tls}}]},{ejabberd_hooks,safe_apply,[{file,"src/ejabberd_hooks.erl"},{line,380}],4},{ejabberd_hooks,run_fold1,[{file,"src/ejabberd_hooks.erl"},{line,364}],4},{ejabberd_c2s,...},...]}</p><p><br></p><p><br>** <strong>Arguments</strong> = [{{iq,<<"purple701889db">>,set,<<"en">>,{jid,<<"admin2">>,<<"casino.com">>,<<"-">>,<<"admin2">>,<<"casino.com">>,<<"-">>},{jid,<<"admin2">>,<<"casino.com">>,<<>>,<<"admin2">>,<<"casino.com">>,<<>>},[{xmlel,<<"session">>,[{<<"xmlns">>,<<"urn:ietf:params:xml:ns:xmpp-session">>}],[]}],#{ip => {127,0,0,1}}},#{mgmt_stanzas_in => 0,xmlns => <<"jabber:client">>,stream_encrypted => true,tls_enabled => true,socket => {socket_state,fast_tls,{tlssock,#Port<0.23143>,#Port<0.23190>},<0.491.0>},mod => ejabberd_c2s,server => <<"casino.com">>,mgmt_state => inactive,mgmt_max_timeout => 300,stream_restarted => true,auth_module => ejabberd_auth_sql,stream_id => <<"15132958958157338735">>,mgmt_ack_timeout => 60000,privacy_list => {userlist,none,[],false},stream_timeout => infinity,sid => {{1491,394389,1},<0.492.0>},pres_f => {1,{{<<"admin2">>,<<"casino.com">>,<<>>},nil,nil}},tls_options => [compression_none,{protocol_options,<<"no_sslv3">>},{certfile,<<"C:\\ProgramData\\ejabberd\\conf\\server.pem">>}],tls_required => false,mgmt_timeout => 300,socket_monitor => #Ref<0.0.2.141>,stream_compressed => false,mgmt_resend => false,mgmt_stanzas_out => 0,csi_state => active,lserver => <<"casino.com">>,zlib => false,lang => <<"en">>,ip => {{127,0,0,1},63321},mgmt_max_queue => 1000,stream_version => {1,0},resource => <<"-">>,jid => {jid,<<"admin2">>,<<"casino.com">>,<<"-">>,<<"admin2">>,<<"casino.com">>,<<"-">>},csi_queue => {0,0,#{}},shaper => c2s_shaper,user => <<"admin2">>,access => c2s,stream_state => established,tls_verify => false,sockmod => ejabberd_socket,stream_direction => in,mgmt_stanzas_req => 0,pres_t => {1,{{<<"admin2">>,<<"casino.com">>,<<>>},nil,nil}},owner => <0.492.0>,stream_header_sent => true,pres_a => {0,nil},stream_authenticated => true,conn => c2s_tls}}]</p><p><br></p><p><strong>And the same error and reason is getting printed again and again after some time as the client tries to connect with the server.</strong></p><p>Can anyone help please?</p><p>Also i am new to erlang and ejabberd so can anyone tell me how to create custom hooks in ejabberd?</p><p>Thank you in advance</p></body></html>