[erlang-questions] Problem running custom code in Multi-User-Chatroom
Abhishek Ranjan
abhishek@REDACTED
Thu Jul 6 14:26:09 CEST 2017
I have written down the following hook to run a simple tic-tac-toe game using ejabberd
-module(tic_hook).
-behaviour(gen_mod).
-export([start/2,stop/1,depends/2,mod_opt_type/1,myMessage/1]).
-include("logger.hrl").
-include("xmpp.hrl").
-include("ejabberd.hrl").
-record(store_player_id1, {pid,name}).
-record(store_player_id2, {pid,name}).
-record(store_from,{from,to}).
start(_Host, _Opts) ->
?INFO_MSG("Inside tic_hook start function ",[]),
gameserver:start(),
mnesia:create_table(store_player_id1, [{type, bag},{attributes, record_info(fields,store_player_id1)}]),
mnesia:create_table(store_player_id2, [{type, bag},{attributes, record_info(fields,store_player_id2)}]),
mnesia:create_table(store_from, [{type, bag},{attributes, record_info(fields,store_from)}]),
ejabberd_hooks:add(user_send_packet, _Host, ?MODULE,
myMessage, 95),
ok.
stop(_Host) ->
mnesia:clear_table(store_player_id1),
mnesia:clear_table(store_player_id2),
mnesia:clear_table(store_from),
ejabberd_hooks:delete(user_send_packet, _Host, ?MODULE, myMessage,95).
depends(_Host, _Opts)->[{?MODULE,soft}].
mod_opt_type(_Option)->
[].
myMessage({Packet, C2SState})->
PacketType=xmpp:get_name(Packet),
case PacketType of
<<"iq">>->
ok;
<<"presence">>->
ok;
<<"message">>->
CatchAll=[{'_',[],['$_']}],
PidTabLIst1=mnesia:dirty_select(store_player_id1, CatchAll),
PidTabLIst2=mnesia:dirty_select(store_player_id2, CatchAll),
MessageSendingUserList=mnesia:dirty_select(store_from, CatchAll),
Bin=xmpp:get_text(Packet#message.body),
PL1Tuple=Packet#message.from,
PL2Tuple=Packet#message.to,
PL1Binary=element(2,PL1Tuple),
PL2Binary=element(2,PL2Tuple),
PL1=binary_to_list(PL1Binary),
PL2=binary_to_list(PL2Binary),
if
(length(MessageSendingUserList)==0)->
T3 = fun() ->
X3 = #store_from{from=PL1,
to=PL2
},
mnesia:write(X3)
end,
mnesia:transaction(T3);
(length(MessageSendingUserList)/=0)->
ok
end,
StrMessage=binary_to_list(Bin),
Message=list_to_atom(StrMessage),
if
(Message==login)->
Player1 = gameclient:login(PL1),
Player2 = gameclient:login(PL2),
T1 = fun() ->
X1 = #store_player_id1{pid=Player1,
name=PL1
},
mnesia:write(X1)
end,
mnesia:transaction(T1),
T2 = fun() ->
X2 = #store_player_id2{pid=Player2,
name=PL2
},
mnesia:write(X2)
end,
mnesia:transaction(T2),
Player2,
gameclient:new_game(Player1, PL2);
(Message/=login)->
Player1=element(2,hd(PidTabLIst1)),
Player2=element(2,hd(PidTabLIst2)),
ChallengingPlayer=element(2,hd(MessageSendingUserList)),
ChallengedPlayer=element(3,hd(MessageSendingUserList)),
if
(PL1==ChallengingPlayer)->
ChallengedPlayer=element(3,hd(MessageSendingUserList)),
gameclient:make_move(Player1, ChallengedPlayer, Message),
timer:sleep(5);
(PL1==ChallengedPlayer)->
ChallengingPlayer=element(2,hd(MessageSendingUserList)),
gameclient:make_move(Player2, ChallengingPlayer, Message),
timer:sleep(5)
end
end
end,
{Packet, C2SState}.
.But this code only works in case of one to one chats.
But when I tried running it on MUC room named 'room' I got the following output :
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}]}
** 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">>,<<"ca
sino.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_v
ersion => {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}}]
Can anybody guide me how to run the game on the MUC room also?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170706/a9c64dfe/attachment.htm>
More information about the erlang-questions
mailing list