<!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><em>-module(tic_hook).</em><br><em>-behaviour(gen_mod).</em></p><p><em>-export([start/2,stop/1,depends/2,mod_opt_type/1,myMessage/1]).</em><br><em>-include("logger.hrl").</em><br><em>-include("xmpp.hrl").</em><br><em>-include("ejabberd.hrl").</em></p><p><em>-record(store_player_id1, {pid,name}).</em><br><em>-record(store_player_id2, {pid,name}).</em><br><em>-record(store_from,{from,to}).</em></p><p><em>start(_Host, _Opts) -> </em><br><br><em>?INFO_MSG("Inside tic_hook start function ",[]),</em><br><em>gameserver:start(),</em><br><em>mnesia:create_table(store_player_id1, [{type, bag},{attributes, record_info(fields,store_player_id1)}]),</em><br><em>mnesia:create_table(store_player_id2, [{type, bag},{attributes, record_info(fields,store_player_id2)}]),</em><br><em>mnesia:create_table(store_from, [{type, bag},{attributes, record_info(fields,store_from)}]),</em><br><em>ejabberd_hooks:add(user_send_packet, _Host, ?MODULE,</em><br><em>myMessage, 95),</em><br><em>ok.</em></p><p><br><em>stop(_Host) -> </em><br><em>mnesia:clear_table(store_player_id1),</em><br><em>mnesia:clear_table(store_player_id2),</em><br><em>mnesia:clear_table(store_from),</em><br><em>ejabberd_hooks:delete(user_send_packet, _Host, ?MODULE, myMessage,95).</em></p><p><em>depends(_Host, _Opts)->[{?MODULE,soft}].</em></p><p><em>mod_opt_type(_Option)-></em><br><em>[].</em></p><p><br><em>myMessage({Packet, C2SState})-></em><br><br><em>PacketType=xmpp:get_name(Packet),</em><br><em>case PacketType of</em><br><br><em><<"iq">>-></em><br><em>ok;</em><br><em><<"presence">>-></em><br><em>ok;</em><br><em><<"message">>-></em></p><p><br><em>CatchAll=[{'_',[],['$_']}],</em><br><em>PidTabLIst1=mnesia:dirty_select(store_player_id1, CatchAll),</em><br><em>PidTabLIst2=mnesia:dirty_select(store_player_id2, CatchAll),</em><br><em>MessageSendingUserList=mnesia:dirty_select(store_from, CatchAll),</em><br><em>Bin=xmpp:get_text(Packet#message.body),</em><br><em>PL1Tuple=Packet#message.from,</em><br><em>PL2Tuple=Packet#message.to,</em><br><em>PL1Binary=element(2,PL1Tuple),</em><br><em>PL2Binary=element(2,PL2Tuple),</em><br><em>PL1=binary_to_list(PL1Binary),</em><br><em>PL2=binary_to_list(PL2Binary),</em><br><br><br><br><em>if</em><br><em>(length(MessageSendingUserList)==0)-></em><br><em>T3 = fun() -></em><br><em>X3 = #store_from{from=PL1,</em><br><em>to=PL2</em><br><em>},</em><br><em>mnesia:write(X3)</em><br><em>end,</em><br><em>mnesia:transaction(T3);</em><br><em>(length(MessageSendingUserList)/=0)-></em><br><em>ok</em><br><em>end,</em><br></p><p><em>StrMessage=binary_to_list(Bin),</em><br><em>Message=list_to_atom(StrMessage),</em><br><br><br><br><em>if</em><br><em>(Message==login)-></em><br><em>Player1 = gameclient:login(PL1),</em></p><p><em>Player2 = gameclient:login(PL2),</em><br><em>T1 = fun() -></em><br><em>X1 = #store_player_id1{pid=Player1,</em><br><em>name=PL1</em><br><em>},</em><br><em>mnesia:write(X1)</em><br><em>end,</em><br><em>mnesia:transaction(T1),</em><br><em>T2 = fun() -></em><br><em>X2 = #store_player_id2{pid=Player2,</em><br><em>name=PL2</em><br><em>},</em><br><em>mnesia:write(X2)</em><br><em>end,</em><br><em>mnesia:transaction(T2),</em><br><em>Player2,</em><br><em>gameclient:new_game(Player1, PL2);</em></p><p><em>(Message/=login)-></em><br><em>Player1=element(2,hd(PidTabLIst1)),</em><br><em>Player2=element(2,hd(PidTabLIst2)),</em><br><em>ChallengingPlayer=element(2,hd(MessageSendingUserList)),</em><br><em>ChallengedPlayer=element(3,hd(MessageSendingUserList)),</em><br><br><br><br><em>if</em><br><em>(PL1==ChallengingPlayer)-></em></p><p><br><em>ChallengedPlayer=element(3,hd(MessageSendingUserList)),</em><br><em>gameclient:make_move(Player1, ChallengedPlayer, Message),</em><br><em>timer:sleep(5);</em><br><br><em>(PL1==ChallengedPlayer)-></em><br><br><em>ChallengingPlayer=element(2,hd(MessageSendingUserList)), </em><br><em>gameclient:make_move(Player2, ChallengingPlayer, Message),</em><br><em>timer:sleep(5)</em></p><p><em>end</em><br><em>end</em><br><em>end,</em><br><em>{Packet, C2SState}.</em></p><p>.But this code only works in case of one to one chats.</p><p>But when I tried running it on MUC room named 'room' I got the following output :</p><p><em>17:34:13.121 [error] Hook user_send_packet crashed when running test_hook:myMessage/1:** 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}]}</em></p><p><em>** 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}}]</em><br></p><p>Can anybody guide me how to run the game on the MUC room also?</p></body></html>