<!DOCTYPE html>
<html><head>
    <meta charset="UTF-8">
</head><body><p>I have written down the following hook to run a simple tic-tac-toe game using ejabberd<br></p><p><code><br></code></p><p><code>-module(tic_hook).</code><br><code>-behaviour(gen_mod).</code></p><p><br></p><p><code>-export([start/2,stop/1,depends/2,mod_opt_type/1,myMessage/1]).</code><br><code>-include("logger.hrl").</code><br><code>-include("xmpp.hrl").</code><br><code>-include("ejabberd.hrl").</code></p><p><code>-record(store_player_id1, {pid,name}).</code><br><code>-record(store_player_id2, {pid,name}).</code><br><code>-record(store_from,{from,to}).</code></p><p><code>start(_Host, _Opts) -> </code><br> <br><code> ?INFO_MSG("Inside tic_hook start function ",[]),</code><br><code> gameserver:start(),</code><br><code> mnesia:create_table(store_player_id1, [{type, bag},{attributes, record_info(fields,store_player_id1)}]),</code><br><code> mnesia:create_table(store_player_id2, [{type, bag},{attributes, record_info(fields,store_player_id2)}]),</code><br><code> mnesia:create_table(store_from, [{type, bag},{attributes, record_info(fields,store_from)}]),</code><br><code> ejabberd_hooks:add(user_send_packet, _Host, ?MODULE,</code><br><code> myMessage, 95),</code><br><code> ok.</code></p><p><br><code>stop(_Host) -> </code><br><code> mnesia:clear_table(store_player_id1),</code><br><code> mnesia:clear_table(store_player_id2),</code><br><code> mnesia:clear_table(store_from),</code><br><code> ejabberd_hooks:delete(user_send_packet, _Host, ?MODULE, myMessage,95).</code></p><p><code>depends(_Host, _Opts)->[{?MODULE,soft}].</code></p><p><code>mod_opt_type(_Option)-></code><br><code> [].</code></p><p><br><code>myMessage({Packet, C2SState})-></code><br> <br><code> PacketType=xmpp:get_name(Packet),</code><br><code> case PacketType of</code><br> <br><code> <<"iq">>-></code><br><code> ok;</code><br><code> <<"presence">>-></code><br><code> ok;</code><br><code> <<"message">>-></code></p><p><br><code> CatchAll=[{'_',[],['$_']}],</code><br><code> PidTabLIst1=mnesia:dirty_select(store_player_id1, CatchAll),</code><br><code> PidTabLIst2=mnesia:dirty_select(store_player_id2, CatchAll),</code><br><code> MessageSendingUserList=mnesia:dirty_select(store_from, CatchAll),</code><br><code>Bin=xmpp:get_text(Packet#message.body),</code><br><code>PL1Tuple=Packet#message.from,</code><br><code>PL2Tuple=Packet#message.to,</code><br><code>PL1Binary=element(2,PL1Tuple),</code><br><code>PL2Binary=element(2,PL2Tuple),</code><br><code>PL1=binary_to_list(PL1Binary),</code><br><code> PL2=binary_to_list(PL2Binary),</code><br> <br><br> <br><code> if</code><br><code> (length(MessageSendingUserList)==0)-></code><br><code> T3 = fun() -></code><br><code> X3 = #store_from{from=PL1,</code><br><code> to=PL2</code><br><code> },</code><br><code> mnesia:write(X3)</code><br><code> end,</code><br><code> mnesia:transaction(T3);</code><br><code> (length(MessageSendingUserList)/=0)-></code><br><code> ok</code><br><code> end,</code><br></p><p><code>StrMessage=binary_to_list(Bin),</code><br><code> Message=list_to_atom(StrMessage),</code><br> <br><br> <br><code> if</code><br><code> (Message==login)-></code><br><code> Player1 = gameclient:login(PL1),</code></p><p><code>Player2 = gameclient:login(PL2),</code><br><code> T1 = fun() -></code><br><code> X1 = #store_player_id1{pid=Player1,</code><br><code> name=PL1</code><br><code> },</code><br><code> mnesia:write(X1)</code><br><code> end,</code><br><code> mnesia:transaction(T1),</code><br><code> T2 = fun() -></code><br><code> X2 = #store_player_id2{pid=Player2,</code><br><code> name=PL2</code><br><code> },</code><br><code> mnesia:write(X2)</code><br><code> end,</code><br><code> mnesia:transaction(T2),</code><br><code> Player2,</code><br><code>gameclient:new_game(Player1, PL2);</code></p><p><code>(Message/=login)-></code><br><code> Player1=element(2,hd(PidTabLIst1)),</code><br><code>Player2=element(2,hd(PidTabLIst2)),</code><br><code>ChallengingPlayer=element(2,hd(MessageSendingUserList)),</code><br><code> ChallengedPlayer=element(3,hd(MessageSendingUserList)),</code><br> <br><br> <br><code> if</code><br><code> (PL1==ChallengingPlayer)-></code></p><p><br><code>ChallengedPlayer=element(3,hd(MessageSendingUserList)),</code><br><code> gameclient:make_move(Player1, ChallengedPlayer, Message),</code><br><code> timer:sleep(5);</code><br> <br><code> (PL1==ChallengedPlayer)-></code><br> <br><code> ChallengingPlayer=element(2,hd(MessageSendingUserList)), </code><br><code> gameclient:make_move(Player2, ChallengingPlayer, Message),</code><br><code> timer:sleep(5)</code></p><p><code>end</code><br><code>end</code><br><code>end,</code><br><code>{Packet, C2SState}.</code></p><p><code><br></code></p><p> .But this code only works in case of one to one chats. </p><p><br></p><p>But when I tried running it on MUC room named 'room' I got the following output :</p><p><code>17:34:13.121 [error] Hook user_send_packet crashed when running test_hook:myMessage/1:<br>** Reason = {error,badarg,[{erlang,hd,[],[[]]},{test_hook,myMessage,[{file,"test_hook.erl"},{line,137}],1},{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,handle_authenticated_packet,[{file,"ejabberd_c2s.erl"},{line,472}],2},{xmpp_stream_in,process_authenticated_packet,[{file,"xmpp_stream_in.erl"},{line,594}],2},{xmpp_stream_in,handle_info,[{file,"xmpp_stream_in.erl"},{line,339}],2},{p1_server,handle_msg,[{file,"src/p1_server.erl"},{line,696}],8}]}</code></p><p><code><br>** Arguments = [{{message,<<"purple45192e38">>,groupchat,<<"en">>,{jid,<<"praveen">>,<<"casino.com">>,<<"-">>,<<"praveen">>,<<"casino.com">>,<<"-">>},{jid,<<"room">>,<<"conference.casino.com">>,<<>>,<<"room">>,<<"conference.casino.com">>,<<>>},[],[{text,<<>>,<<"a2">>}],undefined,[],#{ip => {127,0,0,1}}},#{csi_queue => {0,0,#{}},resource => <<"-">>,stream_compressed => false,mgmt_max_timeout => 300,lserver => <<"casino.com">>,stream_encrypted => true,stream_state => established,mgmt_resend => false,tls_verify => false,mgmt_ack_timeout => 60000,stream_authenticated => true,xmlns => <<"jabber:client">>,shaper => c2s_shaper,mgmt_stanzas_out => 0,auth_module => ejabberd_auth_sql,privacy_list => {userlist,none,[],false},mgmt_stanzas_req => 0,stream_direction => in,socket_monitor => #Ref<0.0.1.11300>,stream_header_sent => true,stream_timeout => infinity,pres_timestamp => {1499,342589,182000},zlib => false,server => <<"casino.com">>,tls_enabled => true,jid => {jid,<<"praveen">>,<<"casino.com">>,<<"-">>,<<"praveen">>,<<"casino.com">>,<<"-">>},ip => {{127,0,0,1},55611},access => c2s,caps_resources => {1,{{<<"praveen">>,<<"casino.com">>,<<"-">>},{caps,<<"http://pidgin.im/">>,<<"I22W7CegORwdbnu0ZiQwGpxr0Go=">>,<<"sha-1">>,[]},nil,nil}},tls_options => [compression_none,{protocol_options,<<"no_sslv3">>},{certfile,<<"C:\\ProgramData\\ejabberd\\conf\\server.pem">>}],pres_t => {1,{{<<"praveen">>,<<"casino.com">>,<<>>},nil,nil}},conn => c2s_tls,sockmod => ejabberd_socket,stream_id => <<"11446533041510383550">>,pres_f => {1,{{<<"praveen">>,<<"casino.com">>,<<>>},nil,nil}},user => <<"praveen">>,stream_restarted => true,mgmt_max_queue => 1000,mod => ejabberd_c2s,pres_a => {2,{{<<"praveen">>,<<"casino.com">>,<<>>},nil,{{<<"room">>,<<"conference.casino.com">>,<<"praveen">>},nil,nil}}},csi_state => active,owner => <0.506.0>,socket => {socket_state,fast_tls,{tlssock,#Port<0.23159>,#Port<0.23222>},<0.505.0>},mgmt_timeout => 300,sid => {{1499,342588,1},<0.506.0>},stream_version => {1,0},mgmt_state => inactive,tls_required => false,lang => <<"en">>,pres_last => {presence,<<>>,available,<<"en">>,{jid,<<"praveen">>,<<"casino.com">>,<<"-">>,<<"praveen">>,<<"casino.com">>,<<"-">>},{jid,<<"praveen">>,<<"casino.com">>,<<>>,<<"praveen">>,<<"casino.com">>,<<>>},undefined,[{text,<<>>,<<"I like cricket">>}],1,[{xmlel,<<"c">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/caps">>},{<<"node">>,<<"http://pidgin.im/">>},{<<"hash">>,<<"sha-1">>},{<<"ver">>,<<"I22W7CegORwdbnu0ZiQwGpxr0Go=">>}],[]},{xmlel,<<"x">>,[{<<"xmlns">>,<<"vcard-temp:x:update">>}],[{xmlel,<<"photo">>,[],[]}]}],#{ip => {127,0,0,1}}},mgmt_stanzas_in => 0}}]<br></code><br></p><p>Can anybody guide me how to run the game on the MUC room also?</p></body></html>