25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-30 16:36:29 +01:00

o Add try/catch block around exmpp_stringprep:*prep/1 uses.

o  Add table conversion.
o  In get_last/3, do not convert status to binary because it's already
one.

SVN Revision: 1580
This commit is contained in:
Jean-Sébastien Pédron 2008-09-29 10:34:06 +00:00
parent 9a4f5bb2d9
commit d8153b702a
2 changed files with 69 additions and 21 deletions

View File

@ -11,6 +11,10 @@
stanza, default to an empty binary, not an empty string; this is what stanza, default to an empty binary, not an empty string; this is what
mod_last expects. mod_last expects.
* src/mod_last.erl: Add try/catch block around
exmpp_stringprep:*prep/1 uses. Add table conversion.
(get_last): Do not convert status to binary because it's already one.
2008-09-25 Jean-Sébastien Pédron <js.pedron@meetic-corp.com> 2008-09-25 Jean-Sébastien Pédron <js.pedron@meetic-corp.com>
* src/jlib.erl (timestamp_to_xml): Create an #xmlel element, not an * src/jlib.erl (timestamp_to_xml): Create an #xmlel element, not an

View File

@ -122,7 +122,7 @@ get_last(IQ_Rec, LUser, LServer) ->
Sec = TimeStamp2 - TimeStamp, Sec = TimeStamp2 - TimeStamp,
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query', Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query',
attrs = [#xmlattr{name = 'seconds', value = integer_to_list(Sec)}], attrs = [#xmlattr{name = 'seconds', value = integer_to_list(Sec)}],
children = [#xmlcdata{cdata = list_to_binary(Status)}]}, children = [#xmlcdata{cdata = Status}]},
exmpp_iq:result(IQ_Rec, Response) exmpp_iq:result(IQ_Rec, Response)
end. end.
@ -134,15 +134,20 @@ on_presence_update(User, Server, _Resource, Status) ->
store_last_info(User, Server, TimeStamp, Status). store_last_info(User, Server, TimeStamp, Status).
store_last_info(User, Server, TimeStamp, Status) -> store_last_info(User, Server, TimeStamp, Status) ->
LUser = exmpp_stringprep:nodeprep(User), try
LServer = exmpp_stringprep:nameprep(Server), LUser = exmpp_stringprep:nodeprep(User),
US = {LUser, LServer}, LServer = exmpp_stringprep:nameprep(Server),
F = fun() -> US = {LUser, LServer},
mnesia:write(#last_activity{us = US, F = fun() ->
timestamp = TimeStamp, mnesia:write(#last_activity{us = US,
status = Status}) timestamp = TimeStamp,
end, status = Status})
mnesia:transaction(F). end,
mnesia:transaction(F)
catch
_ ->
ok
end.
%% Returns: {ok, Timestamp, Status} | not_found %% Returns: {ok, Timestamp, Status} | not_found
get_last_info(LUser, LServer) -> get_last_info(LUser, LServer) ->
@ -156,20 +161,25 @@ get_last_info(LUser, LServer) ->
end. end.
remove_user(User, Server) -> remove_user(User, Server) ->
LUser = exmpp_stringprep:nodeprep(User), try
LServer = exmpp_stringprep:nameprep(Server), LUser = exmpp_stringprep:nodeprep(User),
US = {LUser, LServer}, LServer = exmpp_stringprep:nameprep(Server),
F = fun() -> US = {LUser, LServer},
mnesia:delete({last_activity, US}) F = fun() ->
end, mnesia:delete({last_activity, US})
mnesia:transaction(F). end,
mnesia:transaction(F)
catch
_ ->
ok
end.
update_table() -> update_table() ->
Fields = record_info(fields, last_activity), Fields = record_info(fields, last_activity),
case mnesia:table_info(last_activity, attributes) of case mnesia:table_info(last_activity, attributes) of
Fields -> Fields ->
ok; convert_to_exmpp();
[user, timestamp, status] -> [user, timestamp, status] ->
?INFO_MSG("Converting last_activity table from {user, timestamp, status} format", []), ?INFO_MSG("Converting last_activity table from {user, timestamp, status} format", []),
Host = ?MYNAME, Host = ?MYNAME,
@ -178,11 +188,12 @@ update_table() ->
mnesia:write_lock_table(last_activity), mnesia:write_lock_table(last_activity),
mnesia:foldl( mnesia:foldl(
fun({_, U, T, S} = R, _) -> fun({_, U, T, S} = R, _) ->
U1 = convert_jid_to_exmpp(U),
mnesia:delete_object(R), mnesia:delete_object(R),
mnesia:write( mnesia:write(
#last_activity{us = {U, Host}, #last_activity{us = {U1, Host},
timestamp = T, timestamp = T,
status = S}) status = list_to_binary(S)})
end, ok, last_activity) end, ok, last_activity)
end, end,
mnesia:transaction(F); mnesia:transaction(F);
@ -194,7 +205,7 @@ update_table() ->
fun({_, U, T}) -> fun({_, U, T}) ->
#last_activity{us = U, #last_activity{us = U,
timestamp = T, timestamp = T,
status = ""} status = <<>>}
end, Fields), end, Fields),
F = fun() -> F = fun() ->
mnesia:write_lock_table(last_activity), mnesia:write_lock_table(last_activity),
@ -213,3 +224,36 @@ update_table() ->
mnesia:transform_table(last_activity, ignore, Fields) mnesia:transform_table(last_activity, ignore, Fields)
end. end.
convert_to_exmpp() ->
Fun = fun() ->
case mnesia:first(last_activity) of
'$end_of_table' ->
none;
Key ->
case mnesia:read({last_activity, Key}) of
[#last_activity{status = Status}] when is_binary(Status) ->
none;
[#last_activity{}] ->
mnesia:foldl(fun convert_to_exmpp2/2,
done, last_activity, write)
end
end
end,
mnesia:transaction(Fun).
convert_to_exmpp2(#last_activity{us = {U, S} = Key, status = Status} = LA,
Acc) ->
% Remove old entry.
mnesia:delete({last_activity, Key}),
% Convert "" to undefined in JIDs.
U1 = convert_jid_to_exmpp(U),
% Convert status.
Status1 = list_to_binary(Status),
% Prepare the new record.
New_LA = LA#last_activity{us = {U1, S}, status = Status1},
% Write the new record.
mnesia:write(New_LA),
Acc.
convert_jid_to_exmpp("") -> undefined;
convert_jid_to_exmpp(V) -> V.