Error ejabberd_hooks:run_fold1:368 {function_clause,[{mod_privacy,check_packet

I'm trying an Ejabberd module (mod_confirm_delivery) that will wait for a responce from the client. If no responce is received in 10 seconds, the module will resend the message (this might be overkill) and save the message offline, but seems that something block the delivery receipt message sent by the recipient user (user 2) to the sender (user 1), in fact in the android studio log i can see the correct delivery of the delivery receipt by the recipient, but nothing is received by the message listener of the sender. So i've checked the ejabberd log and i've found two ejabberd_hooks errors, the first concerns mod_privacy,check_packet, the second ejabberd_sm,do_route, but i'm not sure how to solve them..

NOTE: The mod_confirm_delivery in its original version had an old formatting for my Ejabberd version (16.9), so i've upgraded the formatting but i'm not sure if everything is ok because i'm not practice of erlang!

Ejabberd.log (it start from the message sent by the sender user)

2017-07-21 15:34:12.444 [debug] <0.802.0>@ejabberd_receiver:process_data:284 Received XML on stream = <<"<presence to='2@mydomain.com' id='6MIuo-99' type='subscribe'></presence>">>
2017-07-21 15:34:12.444 [debug] <0.802.0>@shaper:update:120 State: {maxrate,1000,428.6951692574539,1500643989925388}, Size=78
M=49.640272513602476, I=62519.219
2017-07-21 15:34:12.444 [debug] <0.803.0>@mod_confirm_delivery:send_packet:134 send_packet FromJID {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>} ToJID {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>} Packet {xmlel,<<"presence">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<"2@mydomain.com">>},{<<"id">>,<<"6MIuo-99">>},{<<"type">>,<<"subscribe">>}],[]}
2017-07-21 15:34:12.444 [debug] <0.803.0>@mod_confirm_delivery:send_packet:137 Message Type <<"subscribe">>
2017-07-21 15:34:12.445 [debug] <0.803.0>@mod_confirm_delivery:send_packet:140 Message Body <<>>
2017-07-21 15:34:12.445 [debug] <0.803.0>@mod_confirm_delivery:send_packet:143 send_packet LUser <<"2">>
2017-07-21 15:34:12.445 [debug] <0.803.0>@mod_confirm_delivery:send_packet:146 send_packet LServer <<"mydomain.com">>
2017-07-21 15:34:12.445 [debug] <0.803.0>@mod_confirm_delivery:send_packet:149 Session: [{session,{{1500,643988,315342},<0.821.0>},{<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{<<"2">>,<<"mydomain.com">>},0,[{ip,{{5,90,33,112},18236}},{conn,c2s_tls},{auth_module,undefined}]}]
2017-07-21 15:34:12.445 [debug] <0.803.0>@mod_confirm_delivery:send_packet:155 receive_confirmation_packet Received Tag false
2017-07-21 15:34:12.445 [debug] <0.803.0>@ejabberd_router:do_route:351 route
from {jid,<<"1">>,<<"mydomain.com">>,<<>>,<<"1">>,<<"mydomain.com">>,<<>>}
to {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"presence">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<"2@mydomain.com">>},{<<"id">>,<<"6MIuo-99">>},{<<"type">>,<<"subscribe">>}],[]}
2017-07-21 15:34:12.445 [debug] <0.803.0>@ejabberd_local:do_route:261 local route
from {jid,<<"1">>,<<"mydomain.com">>,<<>>,<<"1">>,<<"mydomain.com">>,<<>>}
to {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"presence">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<...>>},{<<...>>,...},{...}],[]}
2017-07-21 15:34:12.445 [debug] <0.803.0>@ejabberd_sm:do_route:420 session manager
from {jid,<<"1">>,<<"mydomain.com">>,<<>>,<<"1">>,<<"mydomain.com">>,<<>>}
to {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"presence">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<...>>},{<<...>>,...},{...}],[]}
2017-07-21 15:34:12.446 [debug] <0.803.0>@ejabberd_router:do_route:351 route
from {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
to {jid,<<"1">>,<<"mydomain.com">>,<<>>,<<"1">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"presence">>,[{<<"type">>,<<"subscribed">>}],[]}
2017-07-21 15:34:12.446 [debug] <0.803.0>@ejabberd_local:do_route:261 local route
from {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
to {jid,<<"1">>,<<"mydomain.com">>,<<>>,<<"1">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"presence">>,[{<<"type">>,<<"subs"...>>}],[]}
2017-07-21 15:34:12.446 [debug] <0.803.0>@ejabberd_sm:do_route:420 session manager
from {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
to {jid,<<"1">>,<<"mydomain.com">>,<<>>,<<"1">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"presence">>,[{<<"type">>,<<"subs"...>>}],[]}
2017-07-21 15:34:12.519 [debug] <0.802.0>@ejabberd_receiver:process_data:284 Received XML on stream = <<"<iq to='mydomain.com' id='6MIuo-100' type='set'><register xmlns='https://android.googleapis.com/gcm'><key>cgdzyufJOsE:APA91bGKe1u-Rh2vCszMBJ6h-oYlnl14eTlSTL6MdCI4Xyt-TA9ee-HPhmLeeOy36l2J5NOmpWfuIdT21Ij5z5mtlM8D9Wu4RN9wuytyTHvkh5eHkgcvnSwgjZHWtJaLltw4qHO7tbG_</key></register></iq><r xmlns='urn:xmpp:sm:3'/><message to='2@mydomain.com' id='5614958331449_326532683' type='chat'><body>{&quot;Date&quot;:&quot;21 lug 2017&quot;,&quot;Status&quot;:&quot;not_sent&quot;,&quot;Time&quot;:&quot;1500644052&quot;,&quot;body&quot;:&quot;test&quot;,&quot;isMine&quot;:true,&quot;msgid&quot;:&quot;5614958331449_326532683&quot;,&quot;random_id&quot;:&quot;326532683&quot;,&quot;reason&quot;:&quot;chat&quot;,&quot;receiver&quot;:&quot;2&quot;,&quot;sender&quot;:&quot;1&quot;,&quot;senderName&quot;:&quot;1&quot;}</body><thread>90b21cb3-f3df-4447-892e-3ed45d58f845</thread><request xmlns='urn:xmpp:receipts'/></message><r xmlns='urn:xmpp:sm:3'/>">>
2017-07-21 15:34:12.519 [debug] <0.802.0>@shaper:update:120 State: {maxrate,1000,214.97139196707812,1500644052444691}, Size=945
M=529.4032800075835, I=74.78
2017-07-21 15:34:12.520 [debug] <0.803.0>@mod_confirm_delivery:send_packet:134 send_packet FromJID {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>} ToJID {jid,<<>>,<<"mydomain.com">>,<<>>,<<>>,<<"mydomain.com">>,<<>>} Packet {xmlel,<<"iq">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<"mydomain.com">>},{<<"id">>,<<"6MIuo-100">>},{<<"type">>,<<"set">>}],[{xmlel,<<"register">>,[{<<"xmlns">>,<<"https://android.googleapis.com/gcm">>}],[{xmlel,<<"key">>,[],[{xmlcdata,<<"cgdzyufJOsE:APA91bGKe1u-Rh2vCszMBJ6h-oYlnl14eTlSTL6MdCI4Xyt-TA9ee-HPhmLeeOy36l2J5NOmpWfuIdT21Ij5z5mtlM8D9Wu4RN9wuytyTHvkh5eHkgcvnSwgjZHWtJaLltw4qHO7tbG_">>}]}]}]}
2017-07-21 15:34:12.520 [debug] <0.803.0>@mod_confirm_delivery:send_packet:137 Message Type <<"set">>
2017-07-21 15:34:12.520 [debug] <0.803.0>@mod_confirm_delivery:send_packet:140 Message Body <<>>
2017-07-21 15:34:12.520 [debug] <0.803.0>@mod_confirm_delivery:send_packet:143 send_packet LUser <<>>
2017-07-21 15:34:12.520 [debug] <0.803.0>@mod_confirm_delivery:send_packet:146 send_packet LServer <<"mydomain.com">>
2017-07-21 15:34:12.520 [debug] <0.803.0>@mod_confirm_delivery:send_packet:149 Session: []
2017-07-21 15:34:12.520 [debug] <0.803.0>@mod_confirm_delivery:send_packet:155 receive_confirmation_packet Received Tag false
2017-07-21 15:34:12.520 [debug] <0.803.0>@ejabberd_router:do_route:351 route
from {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
to {jid,<<>>,<<"mydomain.com">>,<<>>,<<>>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"iq">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<"mydomain.com">>},{<<"id">>,<<"6MIuo-100">>},{<<"type">>,<<"set">>}],[{xmlel,<<"register">>,[{<<"xmlns">>,<<"https://android.googleapis.com/gcm">>}],[{xmlel,<<"key">>,[],[{xmlcdata,<<"cgdzyufJOsE:APA91bGKe1u-Rh2vCszMBJ6h-oYlnl14eTlSTL6MdCI4Xyt-TA9ee-HPhmLeeOy36l2J5NOmpWfuIdT21Ij5z5mtlM8D9Wu4RN9wuytyTHvkh5eHkgcvnSwgjZHWtJaLltw4qHO7tbG_">>}]}]}]}
2017-07-21 15:34:12.521 [debug] <0.803.0>@ejabberd_local:do_route:261 local route
from {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
to {jid,<<>>,<<"mydomain.com">>,<<>>,<<>>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"iq">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<...>>},{<<...>>,...},{...}],[{xmlel,<<...>>,...}]}
2017-07-21 15:34:12.521 [debug] <0.803.0>@mod_gcm:iq:156 mod_gcm: updating last_seen for user 1@mydomain.com
2017-07-21 15:34:12.521 [debug] <0.803.0>@ejabberd_router:do_route:351 route
from {jid,<<>>,<<"mydomain.com">>,<<>>,<<>>,<<"mydomain.com">>,<<>>}
to {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
packet {xmlel,<<"iq">>,[{<<"id">>,<<"6MIuo-100">>},{<<"type">>,<<"result">>}],[]}
2017-07-21 15:34:12.521 [debug] <0.803.0>@ejabberd_local:do_route:261 local route
from {jid,<<>>,<<"mydomain.com">>,<<>>,<<>>,<<"mydomain.com">>,<<>>}
to {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
packet {xmlel,<<"iq">>,[{<<"id">>,<<"6MIu"...>>},{<<"type">>,<<...>>}],[]}
2017-07-21 15:34:12.522 [debug] <0.803.0>@ejabberd_sm:do_route:420 session manager
from {jid,<<>>,<<"mydomain.com">>,<<>>,<<>>,<<"mydomain.com">>,<<>>}
to {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
packet {xmlel,<<"iq">>,[{<<"id">>,<<"6MIu"...>>},{<<"type">>,<<...>>}],[]}
2017-07-21 15:34:12.522 [debug] <0.803.0>@ejabberd_sm:do_route:538 sending to process <0.803.0>
2017-07-21 15:34:12.522 [debug] <0.803.0>@ejabberd_c2s:send_text:1823 Send XML on stream = <<"<a xmlns='urn:xmpp:sm:3' h='8'/>">>
2017-07-21 15:34:12.522 [debug] <0.803.0>@mod_confirm_delivery:send_packet:134 send_packet FromJID {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>} ToJID {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>} Packet {xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<"2@mydomain.com">>},{<<"id">>,<<"5614958331449_326532683">>},{<<"type">>,<<"chat">>}],[{xmlel,<<"body">>,[],[{xmlcdata,<<"{\"Date\":\"21 lug 2017\",\"Status\":\"not_sent\",\"Time\":\"1500644052\",\"body\":\"test\",\"isMine\":true,\"msgid\":\"5614958331449_326532683\",\"random_id\":\"326532683\",\"reason\":\"chat\",\"receiver\":\"2\",\"sender\":\"1\",\"senderName\":\"1\"}">>}]},{xmlel,<<"thread">>,[],[{xmlcdata,<<"90b21cb3-f3df-4447-892e-3ed45d58f845">>}]},{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}]}
2017-07-21 15:34:12.522 [debug] <0.803.0>@mod_confirm_delivery:send_packet:137 Message Type <<"chat">>
2017-07-21 15:34:12.523 [debug] <0.803.0>@mod_confirm_delivery:send_packet:140 Message Body <<"{\"Date\":\"21 lug 2017\",\"Status\":\"not_sent\",\"Time\":\"1500644052\",\"body\":\"test\",\"isMine\":true,\"msgid\":\"5614958331449_326532683\",\"random_id\":\"326532683\",\"reason\":\"chat\",\"receiver\":\"2\",\"sender\":\"1\",\"senderName\":\"1\"}">>
2017-07-21 15:34:12.523 [debug] <0.803.0>@mod_confirm_delivery:send_packet:143 send_packet LUser <<"2">>
2017-07-21 15:34:12.523 [debug] <0.803.0>@mod_confirm_delivery:send_packet:146 send_packet LServer <<"mydomain.com">>
2017-07-21 15:34:12.523 [debug] <0.803.0>@mod_confirm_delivery:send_packet:149 Session: [{session,{{1500,643988,315342},<0.821.0>},{<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{<<"2">>,<<"mydomain.com">>},0,[{ip,{{5,90,33,112},18236}},{conn,c2s_tls},{auth_module,undefined}]}]
2017-07-21 15:34:12.523 [debug] <0.803.0>@mod_confirm_delivery:send_packet:155 receive_confirmation_packet Received Tag false
2017-07-21 15:34:12.524 [debug] <0.803.0>@ejabberd_router:do_route:351 route
from {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
to {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<"2@mydomain.com">>},{<<"id">>,<<"5614958331449_326532683">>},{<<"type">>,<<"chat">>}],[{xmlel,<<"body">>,[],[{xmlcdata,<<"{\"Date\":\"21 lug 2017\",\"Status\":\"not_sent\",\"Time\":\"1500644052\",\"body\":\"test\",\"isMine\":true,\"msgid\":\"5614958331449_326532683\",\"random_id\":\"326532683\",\"reason\":\"chat\",\"receiver\":\"2\",\"sender\":\"1\",\"senderName\":\"1\"}">>}]},{xmlel,<<"thread">>,[],[{xmlcdata,<<"90b21cb3-f3df-4447-892e-3ed45d58f845">>}]},{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}]}
2017-07-21 15:34:12.524 [debug] <0.803.0>@ejabberd_local:do_route:261 local route
from {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
to {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<...>>},{<<...>>,...},{...}],[{xmlel,<<...>>,...},{xmlel,...},{...}]}
2017-07-21 15:34:12.524 [debug] <0.803.0>@ejabberd_sm:do_route:420 session manager
from {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
to {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>}
packet {xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<...>>},{<<...>>,...},{...}],[{xmlel,<<...>>,...},{xmlel,...},{...}]}
2017-07-21 15:34:12.524 [debug] <0.803.0>@ejabberd_sm:route_message:584 sending to process <0.821.0>
2017-07-21 15:34:12.525 [debug] <0.803.0>@ejabberd_c2s:send_text:1823 Send XML on stream = <<"<a xmlns='urn:xmpp:sm:3' h='9'/>">>
2017-07-21 15:34:12.527 [debug] <0.821.0>@mod_confirm_delivery:receive_packet:221 receive_packet JID: {jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>} From: {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>} To: {jid,<<"2">>,<<"mydomain.com">>,<<>>,<<"2">>,<<"mydomain.com">>,<<>>} C2SState: {state,{socket_state,fast_tls,{tlssock,#Port<0.16674>,#Port<0.16675>},<0.820.0>},ejabberd_socket,#Ref<0.0.1.17221>,false,<<"6211059257183592171">>,undefined,c2s,c2s_shaper,false,true,true,true,[verify_none,compression_none,{certfile,<<"/etc/ejabberd/ejabberd.pem">>}],true,{jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,{{1500,643988,315342},<0.821.0>},{3,{{<<"2">>,<<"mydomain.com">>,<<>>},{{<<"1">>,<<"mydomain.com">>,<<>>},nil,nil},{{<<"3">>,<<"mydomain.com">>,<<>>},nil,nil}}},{3,{{<<"2">>,<<"mydomain.com">>,<<>>},{{<<"1">>,<<"mydomain.com">>,<<>>},nil,nil},{{<<"3">>,<<"mydomain.com">>,<<>>},nil,nil}}},{3,{{<<"1">>,<<"mydomain.com">>,<<>>},nil,{{<<"2">>,<<"mydomain.com">>,<<>>},nil,{{<<"3">>,<<"mydomain.com">>,<<>>},nil,nil}}}},{xmlel,<<"presence">>,[{<<"xml:lang">>,<<"en">>},{<<"id">>,<<"qWi7I-6">>}],[{xmlel,<<"c">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/caps">>},{<<"hash">>,<<"sha-1">>},{<<"node">>,<<"http://www.igniterealtime.org/projects/smack">>},{<<"ver">>,<<"NfJ3flI83zSdUDzCEICtbypursw=">>}],[]}]},{1500,643989,326377},{userlist,none,[],false},c2s_tls,undefined,{{5,90,33,112},18236},[{caps_resources,{2,{{<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},{caps,<<"http://www.igniterealtime.org/projects/smack">>,<<"NfJ3flI83zSdUDzCEICtbypursw=">>,<<"sha-1">>,[]},nil,{{<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{caps,<<"http://www.igniterealtime.org/projects/smack">>,<<"NfJ3flI83zSdUDzCEICtbypursw=">>,<<"sha-1">>,[]},nil,nil}}}}],active,[],active,<<"urn:xmpp:sm:3">>,{[],[]},1000,undefined,300000,300,false,3,4,true,<<"en">>} Packet: {xmlel,<<"message">>,[{<<"from">>,<<"1@mydomain.com/cgdzyufJOsE">>},{<<"to">>,<<"2@mydomain.com">>},{<<"xml:lang">>,<<"en">>},{<<"id">>,<<"5614958331449_326532683">>},{<<"type">>,<<"chat">>}],[{xmlel,<<"body">>,[],[{xmlcdata,<<"{\"Date\":\"21 lug 2017\",\"Status\":\"not_sent\",\"Time\":\"1500644052\",\"body\":\"test\",\"isMine\":true,\"msgid\":\"5614958331449_326532683\",\"random_id\":\"326532683\",\"reason\":\"chat\",\"receiver\":\"2\",\"sender\":\"1\",\"senderName\":\"1\"}">>}]},{xmlel,<<"thread">>,[],[{xmlcdata,<<"90b21cb3-f3df-4447-892e-3ed45d58f845">>}]},{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}]}
2017-07-21 15:34:12.527 [debug] <0.821.0>@mod_confirm_delivery:receive_packet:223 Message Type <<"chat">>
2017-07-21 15:34:12.527 [debug] <0.803.0>@mod_confirm_delivery:receive_packet:221 receive_packet JID: {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>} From: {jid,<<>>,<<"mydomain.com">>,<<>>,<<>>,<<"mydomain.com">>,<<>>} To: {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>} C2SState: {state,{socket_state,fast_tls,{tlssock,#Port<0.16666>,#Port<0.16667>},<0.802.0>},ejabberd_socket,#Ref<0.0.1.16990>,false,<<"12998887551003528396">>,undefined,c2s,c2s_shaper,false,true,true,true,[verify_none,compression_none,{certfile,<<"/etc/ejabberd/ejabberd.pem">>}],true,{jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,{{1500,639490,256339},<0.803.0>},{4,{{<<"3">>,<<"mydomain.com">>,<<>>},{{<<"2">>,<<"mydomain.com">>,<<>>},{{<<"1">>,<<"mydomain.com">>,<<>>},nil,nil},nil},{{<<"6">>,<<"mydomain.com">>,<<>>},nil,nil}}},{4,{{<<"3">>,<<"mydomain.com">>,<<>>},{{<<"2">>,<<"mydomain.com">>,<<>>},{{<<"1">>,<<"mydomain.com">>,<<>>},nil,nil},nil},{{<<"6">>,<<"mydomain.com">>,<<>>},nil,nil}}},{4,{{<<"1">>,<<"mydomain.com">>,<<>>},nil,{{<<"2">>,<<"mydomain.com">>,<<>>},nil,{{<<"3">>,<<"mydomain.com">>,<<>>},nil,{{<<"6">>,<<"mydomain.com">>,<<>>},nil,nil}}}}},{xmlel,<<"presence">>,[{<<"xml:lang">>,<<"en">>},{<<"id">>,<<"6MIuo-83">>}],[{xmlel,<<"c">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/caps">>},{<<"hash">>,<<"sha-1">>},{<<"node">>,<<"http://www.igniterealtime.org/projects/smack">>},{<<"ver">>,<<"NfJ3flI83zSdUDzCEICtbypursw=">>}],[]}]},{1500,639491,664688},{userlist,none,[],false},c2s_tls,undefined,{{99,99,99,99},43891},[{caps_resources,{2,{{<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},{caps,<<"http://www.igniterealtime.org/projects/smack">>,<<"NfJ3flI83zSdUDzCEICtbypursw=">>,<<"sha-1">>,[]},nil,{{<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{caps,<<"http://www.igniterealtime.org/projects/smack">>,<<"NfJ3flI83zSdUDzCEICtbypursw=">>,<<"sha-1">>,[]},nil,nil}}}}],active,[],active,<<"urn:xmpp:sm:3">>,{[],[]},1000,undefined,300000,300,...} Packet: {xmlel,<<"iq">>,[{<<"from">>,<<"mydomain.com">>},{<<"to">>,<<"1@mydomain.com/cgdzyufJOsE">>},{<<"id">>,<<"6MIuo-100">>},{<<"type">>,<<"result">>}],[]}
2017-07-21 15:34:12.527 [debug] <0.803.0>@mod_confirm_delivery:receive_packet:223 Message Type <<"result">>
2017-07-21 15:34:12.527 [debug] <0.803.0>@mod_confirm_delivery:receive_packet:226 Message Body <<>>
2017-07-21 15:34:12.527 [debug] <0.803.0>@mod_confirm_delivery:receive_packet:229 send_packet LUser <<"1">>
2017-07-21 15:34:12.527 [debug] <0.803.0>@mod_confirm_delivery:receive_packet:232 send_packet LServer <<"mydomain.com">>
2017-07-21 15:34:12.527 [debug] <0.821.0>@mod_confirm_delivery:receive_packet:226 Message Body <<"{\"Date\":\"21 lug 2017\",\"Status\":\"not_sent\",\"Time\":\"1500644052\",\"body\":\"test\",\"isMine\":true,\"msgid\":\"5614958331449_326532683\",\"random_id\":\"326532683\",\"reason\":\"chat\",\"receiver\":\"2\",\"sender\":\"1\",\"senderName\":\"1\"}">>
2017-07-21 15:34:12.528 [debug] <0.821.0>@mod_confirm_delivery:receive_packet:229 send_packet LUser <<"2">>
2017-07-21 15:34:12.528 [debug] <0.821.0>@mod_confirm_delivery:receive_packet:232 send_packet LServer <<"mydomain.com">>
2017-07-21 15:34:12.528 [debug] <0.803.0>@ejabberd_c2s:send_text:1823 Send XML on stream = <<"<iq from='mydomain.com' to='1@mydomain.com/cgdzyufJOsE' id='6MIuo-100' type='result'/>">>
2017-07-21 15:34:12.528 [debug] <0.821.0>@ejabberd_c2s:send_text:1823 Send XML on stream = <<"<message from='1@mydomain.com/cgdzyufJOsE' to='2@mydomain.com' xml:lang='en' id='5614958331449_326532683' type='chat'><body>{&quot;Date&quot;:&quot;21 lug 2017&quot;,&quot;Status&quot;:&quot;not_sent&quot;,&quot;Time&quot;:&quot;1500644052&quot;,&quot;body&quot;:&quot;test&quot;,&quot;isMine&quot;:true,&quot;msgid&quot;:&quot;5614958331449_326532683&quot;,&quot;random_id&quot;:&quot;326532683&quot;,&quot;reason&quot;:&quot;chat&quot;,&quot;receiver&quot;:&quot;2&quot;,&quot;sender&quot;:&quot;1&quot;,&quot;senderName&quot;:&quot;1&quot;}</body><thread>90b21cb3-f3df-4447-892e-3ed45d58f845</thread><request xmlns='urn:xmpp:receipts'/></message>">>
2017-07-21 15:34:12.530 [debug] <0.803.0>@ejabberd_c2s:send_text:1823 Send XML on stream = <<"<r xmlns='urn:xmpp:sm:3'/>">>
2017-07-21 15:34:12.531 [debug] <0.821.0>@ejabberd_c2s:send_text:1823 Send XML on stream = <<"<r xmlns='urn:xmpp:sm:3'/>">>
2017-07-21 15:34:12.975 [debug] <0.802.0>@ejabberd_receiver:process_data:284 Received XML on stream = <<"<a xmlns='urn:xmpp:sm:3' h='8'/>">>
2017-07-21 15:34:12.976 [debug] <0.802.0>@shaper:update:120 State: {maxrate,1000,998.6403495082678,1500644052974902}, Size=32
M=31.956550260723944, I=1.18
2017-07-21 15:34:12.976 [debug] <0.803.0>@ejabberd_c2s:check_h_attribute:2782 1@mydomain.com/cgdzyufJOsE acknowledged 8 of 8 stanzas
2017-07-21 15:34:13.352 [debug] <0.820.0>@ejabberd_receiver:process_data:284 Received XML on stream = <<"<a xmlns='urn:xmpp:sm:3' h='5'/>">>
2017-07-21 15:34:13.352 [debug] <0.820.0>@shaper:update:120 State: {maxrate,1000,998.5248260397263,1500643989888145}, Size=32
M=31.95286396711954, I=63464.074
2017-07-21 15:34:13.352 [debug] <0.821.0>@ejabberd_c2s:check_h_attribute:2782 2@mydomain.com/cEORXKujaTg acknowledged 5 of 5 stanzas
2017-07-21 15:34:13.373 [debug] <0.820.0>@ejabberd_receiver:process_data:284 Received XML on stream = <<"<message to='1@mydomain.com/cgdzyufJOsE' id='qWi7I-13' type='chat'><received xmlns='urn:xmpp:receipts' id='5614958331449_326532683'/></message><r xmlns='urn:xmpp:sm:3'/>">>
2017-07-21 15:34:13.374 [debug] <0.820.0>@shaper:update:120 State: {maxrate,1000,499.5145237976541,1500644053352310}, Size=175
M=116.62891962334503, I=21.724
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:134 send_packet FromJID {jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>} ToJID {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>} Packet {xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"to">>,<<"1@mydomain.com/cgdzyufJOsE">>},{<<"id">>,<<"qWi7I-13">>},{<<"type">>,<<"chat">>}],[{xmlel,<<"received">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>},{<<"id">>,<<"5614958331449_326532683">>}],[]}]}
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:137 Message Type <<"chat">>
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:140 Message Body <<>>
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:143 send_packet LUser <<"1">>
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:146 send_packet LServer <<"mydomain.com">>
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:149 Session: [{session,{{1500,639490,256339},<0.803.0>},{<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},{<<"1">>,<<"mydomain.com">>},0,[{ip,{{99,99,99,99},43891}},{conn,c2s_tls},{auth_module,undefined}]}]
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:155 receive_confirmation_packet Received Tag false
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:181 Message_trace Received MessageId <<"5614958331449_326532683">> Send_packet
2017-07-21 15:34:13.374 [debug] <0.821.0>@mod_confirm_delivery:send_packet:183 receive_packet Record: []
2017-07-21 15:34:13.375 [error] <0.821.0>@ejabberd_hooks:run_fold1:368 {function_clause,[{mod_privacy,check_packet,[allow,<<"2">>,<<"mydomain.com">>,{userlist,none,[],false},{{jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},ok},out],[{file,"src/mod_privacy.erl"},{line,836}]},{ejabberd_hooks,safe_apply,3,[{file,"src/ejabberd_hooks.erl"},{line,382}]},{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,365}]},{ejabberd_c2s,check_privacy_route,5,[{file,"src/ejabberd_c2s.erl"},{line,2098}]},{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1271}]},{p1_fsm,handle_msg,10,[{file,"src/p1_fsm.erl"},{line,582}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,240}]}]}
running hook: {privacy_check_packet,[<<"2">>,<<"mydomain.com">>,{userlist,none,[],false},{{jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},ok},out]}
2017-07-21 15:34:13.375 [debug] <0.821.0>@ejabberd_router:do_route:351 route
from {jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>}
to {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
packet ok
2017-07-21 15:34:13.375 [debug] <0.821.0>@ejabberd_local:do_route:261 local route
from {jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>}
to {jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>}
packet ok
2017-07-21 15:34:13.375 [error] <0.821.0>@ejabberd_sm:route:116 {function_clause,[{ejabberd_sm,do_route,[{jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},ok],[{file,"src/ejabberd_sm.erl"},{line,397}]},{ejabberd_sm,route,3,[{file,"src/ejabberd_sm.erl"},{line,114}]},{ejabberd_local,route,3,[{file,"src/ejabberd_local.erl"},{line,112}]},{ejabberd_router,route,3,[{file,"src/ejabberd_router.erl"},{line,77}]},{ejabberd_c2s,check_privacy_route,5,[{file,"src/ejabberd_c2s.erl"},{line,2110}]},{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1271}]},{p1_fsm,handle_msg,10,[{file,"src/p1_fsm.erl"},{line,582}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,240}]}]}
when processing: {{jid,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>,<<"2">>,<<"mydomain.com">>,<<"cEORXKujaTg">>},{jid,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>,<<"1">>,<<"mydomain.com">>,<<"cgdzyufJOsE">>},ok}
2017-07-21 15:34:13.375 [debug] <0.821.0>@ejabberd_c2s:send_text:1823 Send XML on stream = <<"<a xmlns='urn:xmpp:sm:3' h='4'/>">>

This is the mod_confirm_delivery module

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

-author("Rinku Pandit- rinkuinagra@gmail.com").

%% 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, send_packet/4, receive_packet/5, get_session/7, set_offline_message/7, offlinemessageidstore_hook/3]).

%% included for writing to ejabberd log file
-include("ejabberd.hrl").
-include("logger.hrl").
-ifdef(LAGER).
-compile([{parse_transform, lager_transform}]).
-endif.
%%-define(INFO_MSG(Format, Args),
%% lager:info(Format, Args)).
-define (Rtype, "received").
-define (Ctype, "chat").
-record(session, {sid, usr, us, priority, info}).
-record(offline_msg, {us, timestamp, expire, from, to, packet}).
-record(confirm_delivery, {messageid, timerref}).
-record(offline_message_id, {messageid, mestype}).
start(_Host, _Opt) ->

        ?DEBUG("mod_confirm_delivery loading", []),
        mnesia:create_table(confirm_delivery,
            [{attributes, record_info(fields, confirm_delivery)}]),
        mnesia:clear_table(confirm_delivery),
        ?DEBUG("created timer ref table", []),
mnesia:create_table(offline_message_id,
            [{attributes, record_info(fields, offline_message_id)}]),
        mnesia:clear_table(offline_message_id),
ejabberd_hooks:add(offline_message_hook, _Host, ?MODULE, offlinemessageidstore_hook, 1),
        ?DEBUG("start user_send_packet hook", []),
        ejabberd_hooks:add(user_send_packet, _Host, ?MODULE, send_packet, 50),  
        ?DEBUG("start user_receive_packet hook", []),
        ejabberd_hooks:add(user_receive_packet, _Host, ?MODULE, receive_packet, 50), 
        ?DEBUG("after start user_receive_packet hook", []).

stop(_Host) ->
        ?DEBUG("stopping mod_confirm_delivery", []),
ejabberd_hooks:delete(offline_message_hook, _Host, ?MODULE, offlinemessageidstore_hook, 1),
        ejabberd_hooks:delete(user_send_packet, _Host, ?MODULE, send_packet, 50),
        ejabberd_hooks:delete(user_receive_packet, _Host, ?MODULE, receive_packet, 50).

offlinemessageidstore_hook(From, To, Packet) ->
%%offlinemessageidstore_hook(Packet, C2SState, From, To) ->
case offlinemessageidstore(From, To, Packet) of
%%case offlinemessageidstore(Packet, C2SState, From, To) of
true ->
stop;
_ ->
ok
end.

offlinemessageidstore(From, To, Packet) ->
%%offlinemessageidstore(Packet, C2SState, From, To) ->
Type = fxml:get_tag_attr_s(<<"type">>, Packet),
?DEBUG("offlinemessageidstore TYPE ~p", [Type]),
Received = fxml:get_subtag(Packet, <<"received">>),
if Received =/= false andalso Received =/= [] ->
MessageId = fxml:get_tag_attr_s(<<"id">>, Received),
delete_confirm_delivery(MessageId, ?Rtype);

Type == "chat" orelse Type == "groupchat" ->
MessageId = fxml:get_tag_attr_s(<<"id">>, Packet),
delete_confirm_delivery(MessageId, ?Ctype);
true ->
MessageId = []
end,
?DEBUG("offlinemessageidstore before Case MesID ~p Received ~p ", [MessageId, Received]),
case mod_offline:check_event_chatstates(From, To, Packet) of
true ->
?DEBUG("offlinemessageidstore in Case ", []),

?DEBUG("offlinemessageidstore in Case after Received MesID ~p Type ~p ", [MessageId, Type]),
if MessageId =/= [] ->
if Type =/= [] ->
Record = mnesia:dirty_read(offline_message_id, {MessageId, ?Ctype});
true ->
Record = mnesia:dirty_read(offline_message_id, {MessageId, ?Rtype})
end;
true ->
Record = []
end,
?DEBUG("offlinemessageidstore Record ~p Append ~p", [Record, mesValue]),
if Record == [] ->
?DEBUG("offlinemessageidstore MessageId ~p ", [MessageId]),
F = fun() ->
if Type =/= [] ->
send_push(From, To, Packet),
mnesia:write(#offline_message_id{messageid={MessageId, ?Ctype}, mestype=now()});
true ->
mnesia:write(#offline_message_id{messageid={MessageId, ?Rtype}, mestype=now()}) end
end,

mnesia:transaction(F),  
?DEBUG("offlinemessageidstore After tranc Type ~p MessageId ~p ", [Type,MessageId]),
false;

true ->
true
end;
_ ->
false
end.

send_push(From, To, Packet) ->
?DEBUG("send_push STARTING.......",[]),
MesBody = fxml:get_path_s(Packet, [{elem, <<"body">>}, cdata]),
MesId = fxml:get_tag_attr_s(<<"id">>, Packet),
MesTo = element(2, To),
MesFrom = LUser = element(2, From),
Bin2 = base64:encode(MesBody),
Bin3 = re:replace(binary_to_list(Bin2), "\\+", "-", [global, {return,list}]),
re:replace(Bin3, "/", "_", [global, {return,list}]),
?DEBUG("send_push Notification From: ~p To: ~p MsgID: ~p Message: ~p~n",[MesFrom, MesTo, MesId, Bin3]),
Str = "php -f /var/www/html/freecell/erlang_push.php " ++ MesFrom ++ " " ++ MesTo ++ " " ++ MesId ++ " " ++ Bin3,
Flag = os:cmd(Str),
?DEBUG("send_push STOP With code => ~p.",[Flag]).

%%send_packet(From, To, Packet) ->
send_packet(Packet, C2SState, From, To) ->  
    ?DEBUG("send_packet FromJID ~p ToJID ~p Packet ~p~n",[From, To, Packet]),

    Type = fxml:get_tag_attr_s(<<"type">>, Packet),
    ?DEBUG("Message Type ~p~n",[Type]),

    Body = fxml:get_path_s(Packet, [{elem, <<"body">>}, cdata]),
    ?DEBUG("Message Body ~p~n",[Body]),

    LUser = element(2, To),
    ?DEBUG("send_packet LUser ~p~n",[LUser]),

    LServer = element(3, To),
    ?DEBUG("send_packet LServer ~p~n",[LServer]),

    Sessions = mnesia:dirty_index_read(session, {LUser, LServer}, #session.us),
    ?DEBUG("Session: ~p~n",[Sessions]),

    ReceivedConfirmation = fxml:get_subtag(Packet, <<"receivedconfirmation">>),

    Received = fxml:get_subtag(Packet, <<"received">>),

    ?DEBUG("receive_confirmation_packet Received Tag ~p~n",[ReceivedConfirmation]),
    if ReceivedConfirmation =/= false andalso ReceivedConfirmation =/= [] ->
        MessageId = fxml:get_tag_attr_s(<<"id">>, ReceivedConfirmation);
        Received =/= false andalso Received =/= [] ->
MessageId = fxml:get_tag_attr_s(<<"id">>, Received);
    true ->
        MessageId = fxml:get_tag_attr_s(<<"id">>, Packet)
    end,

if ReceivedConfirmation =/= false andalso ReceivedConfirmation =/= [] andalso MessageId =/= [] ->  
?DEBUG("Message_trace ReceivedConfirmation MessageId ~p Send_packet~n",[MessageId]),
         Record = mnesia:dirty_read(confirm_delivery, {MessageId, ?Rtype}),
?DEBUG("receive_packet Record: ~p~n",[Record]),
    if Record =/= [] ->
[R] = Record,
?DEBUG("receive_packet Record Elements ~p~n",[R]),
Ref = element(3, R),
?DEBUG("receive_packet Cancel Timer ~p~n",[Ref]),
timer:cancel(Ref),
mnesia:dirty_delete(confirm_delivery, {MessageId, ?Rtype}),
?DEBUG("confirm_delivery clean up",[]);    
        true ->
ok
    end;
  Received =/= false andalso Received =/= [] andalso MessageId =/= [] -> 
?DEBUG("Message_trace Received MessageId ~p Send_packet~n",[MessageId]),
Record = mnesia:dirty_read(confirm_delivery, {MessageId, ?Ctype}),
?DEBUG("receive_packet Record: ~p~n",[Record]),
    if Record =/= [] ->
[R] = Record,
?DEBUG("receive_packet Record Elements ~p~n",[R]),
Ref = element(3, R),
?DEBUG("receive_packet Cancel Timer ~p~n",[Ref]),
timer:cancel(Ref),
mnesia:dirty_delete(confirm_delivery, {MessageId, ?Ctype}),
?DEBUG("confirm_delivery clean up",[]);    
        true ->
ok
    end;
        
true ->            
        Packet
    end.  

delete_offline_msgid(MessageId, Type) ->
?DEBUG("delete_offline_msgid: MesID ~p Type ~p",[MessageId, Type]),
if MessageId =/= [] ->
mnesia:dirty_delete(offline_message_id, {MessageId, Type});
true ->
ok
end.

delete_confirm_delivery(MessageId, Type) ->
?DEBUG("delete_offline_msgid: MesID ~p Type ~p",[MessageId, Type]),
if MessageId =/= [] ->
mnesia:dirty_delete(confirm_delivery, {MessageId, Type});
true ->
ok
end.

%%receive_packet(_JID, From, To, Packet) ->
receive_packet(Packet, C2SState, _JID, From, To) ->
    ?DEBUG("receive_packet JID: ~p From: ~p To: ~p C2SState: ~p Packet: ~p~n",[_JID, From, To, C2SState, Packet]),
    Type = fxml:get_tag_attr_s(<<"type">>, Packet),
    ?DEBUG("Message Type ~p~n",[Type]),

    Body = fxml:get_path_s(Packet, [{elem, <<"body">>}, cdata]),
    ?DEBUG("Message Body ~p~n",[Body]),

    LUser = element(2, To),
    ?DEBUG("send_packet LUser ~p~n",[LUser]),

    LServer = element(3, To),
    ?DEBUG("send_packet LServer ~p~n",[LServer]),

    Received = fxml:get_subtag(Packet, <<"received">>),
   
    if Received =/= false andalso Received =/= [] ->
  MessageId = fxml:get_tag_attr_s(<<"id">>, Received),
   ?DEBUG("message_trace Received MessageId ~p receive_packet",[MessageId]),
  Record = mnesia:dirty_read(confirm_delivery, {MessageId, ?Rtype}),
  if Record == [] ->
   OfflineRecord = mnesia:dirty_read(offline_message_id, {MessageId, ?Rtype}),
   if OfflineRecord =/= [] ->
[R] = OfflineRecord,
?DEBUG("receive_packet Record Elements ~p~n",[R]),
Currenttime = element(3, R);
   true ->
Currenttime = now()
   end,

   delete_offline_msgid(MessageId , ?Rtype),
                %% set timer and enter in confirm_delivery
                {ok, Ref} = timer:apply_after(10000, mod_confirm_delivery, get_session, [LUser, LServer, From, To, Packet, ?Rtype, Currenttime]),

                ?DEBUG("Saving To p Ref p~n",[MessageId, Ref]),

          F = fun() ->
             mnesia:write(#confirm_delivery{messageid={MessageId, ?Rtype}, timerref=Ref})
          end,

           mnesia:transaction(F);

  true->
   delete_confirm_delivery(MessageId, ?Rtype)
  end;

    true ->
         MessageId = fxml:get_tag_attr_s(<<"id">>, Packet),
                if (Type=:= "chat" andalso Body =/= []) ->
   ?DEBUG("message_trace chat MessageId ~p receive_packet",[MessageId]),
   Record = mnesia:dirty_read(confirm_delivery, {MessageId, ?Ctype}),
   if Record == [] ->
OfflineRecord = mnesia:dirty_read(offline_message_id, {MessageId, ?Ctype}),
  if OfflineRecord =/= [] ->
[R] = OfflineRecord,
?DEBUG("receive_packet Record Elements ~p~n",[R]),
Currenttime = element(3, R);
  true ->
Currenttime = now()
end,
       delete_offline_msgid(MessageId , ?Ctype),
                    %% set timer and enter in confirm_delivery
                    {ok, Ref} = timer:apply_after(10000, mod_confirm_delivery, get_session, [LUser, LServer, From, To, Packet, ?Ctype, Currenttime]),

                 ?DEBUG("Saving To p Ref p~n",[MessageId, Ref]),

           F = fun() ->
              mnesia:write(#confirm_delivery{messageid={MessageId, ?Ctype}, timerref=Ref})
            end,

             mnesia:transaction(F);
       true->
       delete_confirm_delivery(MessageId, ?Ctype)
       end;
  true->
    Packet
  end
 
    end.

get_session(User, Server, From, To, Packet, Type, Currenttime) -> 

?DEBUG("get_session User: ~p Server: ~p From: ~p To ~p Packet ~p ~n",[User, Server, From, To, Packet]),  

    set_offline_message(User, Server, From, To, Packet, Type, Currenttime),
    ?DEBUG("Set offline message...",[]),

    ejabberd_router:route(From, To, Packet),
    ?DEBUG("Resend message Currenttime ~p...",[Currenttime]).

   

set_offline_message(User, Server, From, To, Packet, _Type, TimeStamp) ->
%%{MegaSecs, Secs, MicroSecs} = now(),
%%Secs1 = Secs-10,
%%TimeStamp = {MegaSecs, Secs1, MicroSecs},
       ?DEBUG("set_offline_message User: ~p Server: ~p From: ~p To ~p Packet ~p~n",[User, Server, From, To, Packet]),   

Type = fxml:get_tag_attr_s(<<"type">>, Packet),
?DEBUG("offlinesecondmessageidstore TYPE ~p argv ~p", [Type, argv]),
Received = fxml:get_subtag(Packet, <<"received">>),
if Received =/= false andalso Received =/= [] ->
MessageId = fxml:get_tag_attr_s(<<"id">>, Received);

Type == "chat" orelse Type == "groupchat" ->
MessageId = fxml:get_tag_attr_s(<<"id">>, Packet);
true ->
MessageId = []
end,
?DEBUG("offlinemessageidstore before Case MesID ~p Received ~p ", [MessageId, Received]),
case mod_offline:check_event_chatstates(From, To, Packet) of
true ->
?DEBUG("offlinemessageidstore in Case ", []),

?DEBUG("offlinemessageidstore in Case after Received MesID ~p Type ~p ", [MessageId, Type]),
if MessageId =/= [] ->
if Type =/= [] ->
Record = mnesia:dirty_read(offline_message_id, {MessageId, ?Ctype});
true ->
Record = mnesia:dirty_read(offline_message_id, {MessageId, ?Rtype})
end;
true ->
Record = []
end,
?DEBUG("offlinemessageidstore Record ~p Append ~p", [Record, mesValue]),
if Record == [] ->

?DEBUG("offlinemessageidstore MessageId ~p ", [MessageId]),
F = fun() ->
if Type =/= [] ->
send_push(From, To, Packet),
mnesia:write(#offline_message_id{messageid={MessageId, ?Ctype}, mestype=TimeStamp});
true ->
mnesia:write(#offline_message_id{messageid={MessageId, ?Rtype}, mestype=TimeStamp}) end
end,

mnesia:transaction(F),  
?DEBUG("offlinemessageidstore After tranc Type ~p MessageId ~p ", [Type,MessageId]),
J = fun() ->
        mnesia:write(#offline_msg{us = {User, Server}, timestamp = TimeStamp, expire = "never", from = From, to = To, packet = Packet})
    end,

    mnesia:transaction(J);

true ->
true
end;
_ ->
F = fun() ->
        mnesia:write(#offline_msg{us = {User, Server}, timestamp = TimeStamp, expire = "never", from = From, to = To, packet = Packet})
    end,

    mnesia:transaction(F)
end.

Syndicate content