2003-10-24 21:21:07 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_last.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-10-24 21:21:07 +02:00
|
|
|
%%% Purpose : jabber:iq:last support (JEP-0012)
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 24 Oct 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-10-24 21:21:07 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_last).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-10-24 21:21:07 +02:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2003-10-24 21:21:07 +02:00
|
|
|
process_local_iq/3,
|
|
|
|
process_sm_iq/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
on_presence_update/4,
|
2005-05-23 21:47:57 +02:00
|
|
|
store_last_info/4,
|
2007-11-27 19:54:06 +01:00
|
|
|
get_last_info/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/2]).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-10-24 21:21:07 +02:00
|
|
|
-include("ejabberd.hrl").
|
2007-07-26 11:29:52 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(last_activity, {us, timestamp, status}).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2003-10-24 21:21:07 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
|
|
|
mnesia:create_table(last_activity,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, last_activity)}]),
|
2004-07-11 22:51:54 +02:00
|
|
|
update_table(),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_LAST_ACTIVITY,
|
2003-10-24 21:21:07 +02:00
|
|
|
?MODULE, process_local_iq, IQDisc),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_LAST_ACTIVITY,
|
2004-12-12 22:00:34 +01:00
|
|
|
?MODULE, process_sm_iq, IQDisc),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(unset_presence_hook, HostB,
|
2004-12-12 22:00:34 +01:00
|
|
|
?MODULE, on_presence_update, 50).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(unset_presence_hook, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, on_presence_update, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, HostB, ?NS_LAST_ACTIVITY),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_LAST_ACTIVITY).
|
2008-08-14 15:36:11 +02:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%%%
|
|
|
|
%%% Uptime of ejabberd node
|
|
|
|
%%%
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_local_iq(_From, _To, #iq{type = get} = IQ_Rec) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
Sec = get_node_uptime(),
|
2008-08-14 15:36:11 +02:00
|
|
|
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('seconds', Sec)]},
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Response);
|
|
|
|
process_local_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec () -> integer()
|
|
|
|
%% @doc Get the uptime of the ejabberd node, expressed in seconds.
|
|
|
|
%% When ejabberd is starting, ejabberd_config:start/0 stores the datetime.
|
|
|
|
get_node_uptime() ->
|
|
|
|
case ejabberd_config:get_local_option(node_start) of
|
|
|
|
{_, _, _} = StartNow ->
|
|
|
|
now_to_seconds(now()) - now_to_seconds(StartNow);
|
|
|
|
_undefined ->
|
|
|
|
trunc(element(1, erlang:statistics(wall_clock))/1000)
|
|
|
|
end.
|
|
|
|
|
|
|
|
now_to_seconds({MegaSecs, Secs, _MicroSecs}) ->
|
|
|
|
MegaSecs * 1000000 + Secs.
|
|
|
|
|
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Serve queries about user last online
|
|
|
|
%%%
|
2008-08-14 15:36:11 +02:00
|
|
|
|
|
|
|
process_sm_iq(From, To, #iq{type = get} = IQ_Rec) ->
|
|
|
|
{Subscription, _Groups} =
|
|
|
|
ejabberd_hooks:run_fold(
|
2009-06-01 18:37:15 +02:00
|
|
|
roster_get_jid_info, exmpp_jid:prep_domain(To),
|
2009-06-01 18:39:36 +02:00
|
|
|
{none, []}, [exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To), From]),
|
2008-08-14 15:36:11 +02:00
|
|
|
if
|
|
|
|
(Subscription == both) or (Subscription == from) ->
|
|
|
|
UserListRecord = ejabberd_hooks:run_fold(
|
2009-06-01 18:37:15 +02:00
|
|
|
privacy_get_user_list, exmpp_jid:prep_domain(To),
|
2008-08-14 15:36:11 +02:00
|
|
|
#userlist{},
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To)]),
|
2008-08-14 15:36:11 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
2009-06-01 18:37:15 +02:00
|
|
|
privacy_check_packet, exmpp_jid:prep_domain(To),
|
2008-08-14 15:36:11 +02:00
|
|
|
allow,
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To), UserListRecord,
|
2008-08-14 15:36:11 +02:00
|
|
|
{From, To,
|
|
|
|
exmpp_presence:available()},
|
|
|
|
out]) of
|
|
|
|
allow ->
|
2009-06-01 18:39:36 +02:00
|
|
|
get_last(IQ_Rec, exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To));
|
2008-08-14 15:36:11 +02:00
|
|
|
deny ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed')
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed')
|
|
|
|
end;
|
|
|
|
process_sm_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2007-11-27 19:54:06 +01:00
|
|
|
%% TODO: This function could use get_last_info/2
|
2008-08-14 15:36:11 +02:00
|
|
|
get_last(IQ_Rec, LUser, LServer) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch mnesia:dirty_read(last_activity, {LUser, LServer}) of
|
2003-10-24 21:21:07 +02:00
|
|
|
{'EXIT', _Reason} ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error');
|
2003-10-24 21:21:07 +02:00
|
|
|
[] ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'service-unavailable');
|
2004-07-11 22:51:54 +02:00
|
|
|
[#last_activity{timestamp = TimeStamp, status = Status}] ->
|
2009-01-19 11:14:04 +01:00
|
|
|
TimeStamp2 = now_to_seconds(now()),
|
2003-10-24 21:21:07 +02:00
|
|
|
Sec = TimeStamp2 - TimeStamp,
|
2008-08-06 15:44:58 +02:00
|
|
|
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query',
|
2009-01-21 14:34:26 +01:00
|
|
|
attrs = [?XMLATTR('seconds', Sec)],
|
2008-09-29 12:34:06 +02:00
|
|
|
children = [#xmlcdata{cdata = Status}]},
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Response)
|
2003-10-24 21:21:07 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
on_presence_update(User, Server, _Resource, Status) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
TimeStamp = now_to_seconds(now()),
|
2005-05-23 21:47:57 +02:00
|
|
|
store_last_info(User, Server, TimeStamp, Status).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
store_last_info(User, Server, TimeStamp, Status)
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-09-29 12:34:06 +02:00
|
|
|
try
|
2009-01-03 16:15:38 +01:00
|
|
|
US = {User, Server},
|
2008-09-29 12:34:06 +02:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#last_activity{us = US,
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
status = Status})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2009-01-19 11:14:04 +01:00
|
|
|
|
2009-01-19 12:59:40 +01:00
|
|
|
%% @spec (LUser::string(), LServer::string()) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
%% {ok, Timestamp::integer(), Status::string()} | not_found
|
2009-12-09 18:47:50 +01:00
|
|
|
get_last_info(LUser, LServer) when is_list(LUser), is_list(LServer) ->
|
|
|
|
get_last_info(list_to_binary(LUser), list_to_binary(LServer));
|
2009-01-03 16:15:38 +01:00
|
|
|
get_last_info(LUser, LServer) when is_binary(LUser), is_binary(LServer) ->
|
2007-11-27 19:54:06 +01:00
|
|
|
case catch mnesia:dirty_read(last_activity, {LUser, LServer}) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
not_found;
|
|
|
|
[] ->
|
|
|
|
not_found;
|
|
|
|
[#last_activity{timestamp = TimeStamp, status = Status}] ->
|
|
|
|
{ok, TimeStamp, Status}
|
|
|
|
end.
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
remove_user(User, Server) when is_binary(User), is_binary(Server) ->
|
2008-09-29 12:34:06 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({last_activity, US})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2004-07-11 22:51:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, last_activity),
|
|
|
|
case mnesia:table_info(last_activity, attributes) of
|
|
|
|
Fields ->
|
2008-09-29 12:34:06 +02:00
|
|
|
convert_to_exmpp();
|
2005-04-17 20:08:34 +02:00
|
|
|
[user, timestamp, status] ->
|
|
|
|
?INFO_MSG("Converting last_activity table from {user, timestamp, status} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
mnesia:transform_table(last_activity, ignore, Fields),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write_lock_table(last_activity),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun({_, U, T, S} = R, _) ->
|
2008-09-29 12:34:06 +02:00
|
|
|
U1 = convert_jid_to_exmpp(U),
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete_object(R),
|
|
|
|
mnesia:write(
|
2008-09-29 12:34:06 +02:00
|
|
|
#last_activity{us = {U1, Host},
|
2005-04-17 20:08:34 +02:00
|
|
|
timestamp = T,
|
2008-09-29 12:34:06 +02:00
|
|
|
status = list_to_binary(S)})
|
2005-04-17 20:08:34 +02:00
|
|
|
end, ok, last_activity)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F);
|
2004-07-11 22:51:54 +02:00
|
|
|
[user, timestamp] ->
|
|
|
|
?INFO_MSG("Converting last_activity table from {user, timestamp} format", []),
|
2005-04-17 20:08:34 +02:00
|
|
|
Host = ?MYNAME,
|
2004-07-11 22:51:54 +02:00
|
|
|
mnesia:transform_table(
|
|
|
|
last_activity,
|
|
|
|
fun({_, U, T}) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
#last_activity{us = U,
|
|
|
|
timestamp = T,
|
2008-09-29 12:34:06 +02:00
|
|
|
status = <<>>}
|
2005-04-17 20:08:34 +02:00
|
|
|
end, Fields),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write_lock_table(last_activity),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun({_, U, T, S} = R, _) ->
|
|
|
|
mnesia:delete_object(R),
|
|
|
|
mnesia:write(
|
|
|
|
#last_activity{us = {U, Host},
|
|
|
|
timestamp = T,
|
|
|
|
status = S})
|
|
|
|
end, ok, last_activity)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F);
|
2004-07-11 22:51:54 +02:00
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating last_activity table", []),
|
|
|
|
mnesia:transform_table(last_activity, ignore, Fields)
|
|
|
|
end.
|
|
|
|
|
2008-09-29 12:34:06 +02:00
|
|
|
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.
|
2009-01-03 16:15:38 +01:00
|
|
|
New_LA = LA#last_activity{us = {list_to_binary(U1), list_to_binary(S)},
|
|
|
|
status = Status1},
|
2008-09-29 12:34:06 +02:00
|
|
|
% Write the new record.
|
|
|
|
mnesia:write(New_LA),
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
convert_jid_to_exmpp("") -> undefined;
|
|
|
|
convert_jid_to_exmpp(V) -> V.
|