Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dukzcry committed Sep 1, 2015
1 parent 0d2804a commit 4f80162
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
15 changes: 6 additions & 9 deletions eggbnc/eggbnc.erl
Expand Up @@ -101,7 +101,7 @@ send_messages(T,S) ->
mnesia:clear_table(T),
mnesia:dump_tables([T])
end.
add_delayed(TS,#xmlel{ns = NS} = Message) ->
add_delayed(TS,#xmlel{ns=NS}=Message) ->
case exmpp_xml:get_element(Message,NS,'delay') of
undefined ->
{{Year,Month,Day},{Hour,Minute,Second}} = calendar:now_to_universal_time(TS),
Expand Down Expand Up @@ -168,13 +168,16 @@ client_handler(So,Se,P) ->
inet:setopts(So,[{active,once}]),
receive
{tcp,So,Data} ->
case exmpp_xml:parse(P,Data) of
try exmpp_xml:parse(P,Data) of
continue ->
true;
E ->
Elements = exmpp_xml:remove_whitespaces_from_list(E),
%io:format("from client ~p~n", [Data]),
lists:foreach(fun(X) -> handle_packet(X,Se) end,Elements)
catch
_ ->
true
end,
client_handler(So,Se,P);
{tcp_closed,So} ->
Expand Down Expand Up @@ -263,13 +266,7 @@ get_all(T) ->
{atomic,MR} = mnesia:transaction(F),
MR.
dirty_get_all(T) ->
try mnesia:dirty_select(T,[{'_',[],['$_']}]) of
All ->
All
catch
_ ->
[]
end.
mnesia:dirty_select(T,[{'_',[],['$_']}]).
find_session(J) ->
F = fun() -> mnesia:match_object(#sessions{jid=J,_='_'}) end,
{atomic,MR} = mnesia:transaction(F),
Expand Down
33 changes: 10 additions & 23 deletions eggbnc/v2/eggbnc.erl
Expand Up @@ -99,31 +99,21 @@ send_messages(T,S) ->
[] -> true;
M ->
%io:format("off msgs ~p~n", M),
lists:foreach(fun(X) -> send(S,add_delayed(X#messages.stamp,X#messages.msg)), mnesia:dirty_delete_object(T,X) end,lists:sort(M)),
lists:foreach(fun(X) -> mnesia:dirty_delete_object(T,X), send(S,add_delayed(X#messages.stamp,X#messages.msg)) end,lists:sort(M)),
mnesia:dump_tables([T])
end.
send_room_messages(T,S,To) ->
case dirty_get_all(T,groupchat) of
[] -> true;
M ->
FM = lists:filter(fun(X) -> case exmpp_xml:get_attribute(X#messages.msg,<<"from">>,[]) of
[] ->
false;
From ->
try exmpp_jid:parse(From) of
JID ->
exmpp_jid:bare_compare(JID,To)
catch
_ ->
false
end
end
FM = lists:filter(fun(X) -> mnesia:dirty_delete_object(T,X),
exmpp_jid:bare_compare(exmpp_jid:parse(exmpp_xml:get_attribute(X#messages.msg,<<"from">>,[])),To)
end,M),
%io:format("room msgs ~p~n", FM),
lists:foreach(fun(X) -> send(S,add_delayed(X#messages.stamp,X#messages.msg)), mnesia:dirty_delete_object(T,X) end,lists:sort(FM)),
lists:foreach(fun(X) -> send(S,add_delayed(X#messages.stamp,X#messages.msg)) end,lists:sort(FM)),
mnesia:dump_tables([T])
end.
add_delayed(TS,#xmlel{ns = NS} = Message) ->
add_delayed(TS,#xmlel{ns=NS}=Message) ->
case exmpp_xml:get_element(Message,NS,'delay') of
undefined ->
{{Year,Month,Day},{Hour,Minute,Second}} = calendar:now_to_universal_time(TS),
Expand Down Expand Up @@ -233,13 +223,16 @@ client_handler(So,Se,Pid,P,Pr,BJ,T) ->
inet:setopts(So,[{active,once}]),
receive
{tcp,So,Data} ->
case exmpp_xml:parse(P,Data) of
try exmpp_xml:parse(P,Data) of
continue ->
true;
E ->
Elements = exmpp_xml:remove_whitespaces_from_list(E),
%io:format("from client ~p~n", [Data]),
lists:foreach(fun(X) -> handle_packet(X,Pr,Se,BJ,T,So) end,Elements)
catch
_ ->
true
end,
client_handler(So,Se,Pid,P,Pr,BJ,T);
{tcp_closed,So} ->
Expand Down Expand Up @@ -358,13 +351,7 @@ get_all(T) ->
{atomic,MR} = mnesia:transaction(F),
MR.
dirty_get_all(T,TY) ->
try mnesia:dirty_match_object(T,#messages{type=TY,_='_'}) of
All ->
All
catch
_ ->
[]
end.
mnesia:dirty_match_object(T,#messages{type=TY,_='_'}).
find_session(J) ->
F = fun() -> mnesia:match_object(#sessions{jid=J,_='_'}) end,
{atomic,MR} = mnesia:transaction(F),
Expand Down

0 comments on commit 4f80162

Please sign in to comment.