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>
|
2010-04-14 01:00:14 +02:00
|
|
|
%%% Purpose : jabber:iq:last support (XEP-0012)
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 24 Oct 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2003-10-24 21:21:07 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_last).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 12, '2.0'}).
|
|
|
|
|
2003-10-24 21:21:07 +02:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, stop/1, process_local_iq/3, export/1,
|
2015-06-01 14:38:27 +02:00
|
|
|
process_sm_iq/3, on_presence_update/4, import/1,
|
|
|
|
import/3, store_last_info/4, get_last_info/2,
|
|
|
|
remove_user/2, transform_options/1, mod_opt_type/1,
|
|
|
|
opt_type/1]).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-10-24 21:21:07 +02:00
|
|
|
-include("jlib.hrl").
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-include("mod_privacy.hrl").
|
2016-04-13 08:59:39 +02:00
|
|
|
-include("mod_last.hrl").
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2016-04-13 08:59:39 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback import(binary(), #last_activity{}) -> ok | pass.
|
|
|
|
-callback get_last(binary(), binary()) ->
|
|
|
|
{ok, non_neg_integer(), binary()} | not_found | {error, any()}.
|
|
|
|
-callback store_last_info(binary(), binary(), non_neg_integer(), binary()) ->
|
|
|
|
{atomic, any()}.
|
|
|
|
-callback remove_user(binary(), binary()) -> {atomic, any()}.
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
2016-04-13 08:59:39 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(Host, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_LAST, ?MODULE, process_local_iq, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_LAST, ?MODULE, process_sm_iq, IQDisc),
|
|
|
|
ejabberd_hooks:add(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
|
|
|
ejabberd_hooks:add(unset_presence_hook, Host, ?MODULE,
|
|
|
|
on_presence_update, 50).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(unset_presence_hook, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, on_presence_update, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_LAST),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_LAST).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2008-12-23 02:34:52 +01:00
|
|
|
%%%
|
|
|
|
%%% Uptime of ejabberd node
|
|
|
|
%%%
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_local_iq(_From, _To,
|
2016-03-31 10:00:29 +02:00
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2003-10-24 21:21:07 +02:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get ->
|
|
|
|
Sec = get_node_uptime(),
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_LAST},
|
|
|
|
{<<"seconds">>,
|
|
|
|
iolist_to_binary(integer_to_list(Sec))}],
|
|
|
|
children = []}]}
|
2003-10-24 21:21:07 +02:00
|
|
|
end.
|
|
|
|
|
2008-12-23 02:34:52 +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() ->
|
2013-08-12 14:25:05 +02:00
|
|
|
case ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
node_start,
|
2013-08-12 14:25:05 +02:00
|
|
|
fun(S) when is_integer(S), S >= 0 -> S end) of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
|
|
|
trunc(element(1, erlang:statistics(wall_clock)) / 1000);
|
2013-08-12 14:25:05 +02:00
|
|
|
Now ->
|
2015-12-07 16:08:57 +01:00
|
|
|
p1_time_compat:system_time(seconds) - Now
|
2008-12-23 02:34:52 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
now_to_seconds({MegaSecs, Secs, _MicroSecs}) ->
|
|
|
|
MegaSecs * 1000000 + Secs.
|
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Serve queries about user last online
|
|
|
|
%%%
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_sm_iq(From, To,
|
2016-03-31 10:00:29 +02:00
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2003-10-24 21:21:07 +02:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get ->
|
|
|
|
User = To#jid.luser,
|
|
|
|
Server = To#jid.lserver,
|
|
|
|
{Subscription, _Groups} =
|
|
|
|
ejabberd_hooks:run_fold(roster_get_jid_info, Server,
|
|
|
|
{none, []}, [User, Server, From]),
|
|
|
|
if (Subscription == both) or (Subscription == from) or
|
|
|
|
(From#jid.luser == To#jid.luser) and
|
|
|
|
(From#jid.lserver == To#jid.lserver) ->
|
|
|
|
UserListRecord =
|
|
|
|
ejabberd_hooks:run_fold(privacy_get_user_list, Server,
|
|
|
|
#userlist{}, [User, Server]),
|
|
|
|
case ejabberd_hooks:run_fold(privacy_check_packet,
|
|
|
|
Server, allow,
|
|
|
|
[User, Server, UserListRecord,
|
|
|
|
{To, From,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs = [],
|
|
|
|
children = []}},
|
|
|
|
out])
|
|
|
|
of
|
|
|
|
allow -> get_last_iq(IQ, SubEl, User, Server);
|
|
|
|
deny ->
|
2016-04-05 12:09:44 +02:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
true ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Not subscribed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_FORBIDDEN(Lang, Txt)]}
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2003-10-24 21:21:07 +02:00
|
|
|
end.
|
|
|
|
|
2010-11-05 18:32:25 +01:00
|
|
|
%% @spec (LUser::string(), LServer::string()) ->
|
|
|
|
%% {ok, TimeStamp::integer(), Status::string()} | not_found | {error, Reason}
|
|
|
|
get_last(LUser, LServer) ->
|
2016-04-13 08:59:39 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_last(LUser, LServer).
|
2010-11-05 18:32:25 +01:00
|
|
|
|
2016-03-31 10:00:29 +02:00
|
|
|
get_last_iq(#iq{lang = Lang} = IQ, SubEl, LUser, LServer) ->
|
2010-11-05 18:38:42 +01:00
|
|
|
case ejabberd_sm:get_user_resources(LUser, LServer) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
case get_last(LUser, LServer) of
|
|
|
|
{error, _Reason} ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Database failure">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
IQ#iq{type = error,
|
2016-03-31 10:00:29 +02:00
|
|
|
sub_el = [SubEl, ?ERRT_INTERNAL_SERVER_ERROR(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
not_found ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"No info about last activity found">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
IQ#iq{type = error,
|
2016-03-31 10:00:29 +02:00
|
|
|
sub_el = [SubEl, ?ERRT_SERVICE_UNAVAILABLE(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, TimeStamp, Status} ->
|
2015-12-07 16:08:57 +01:00
|
|
|
TimeStamp2 = p1_time_compat:system_time(seconds),
|
2013-03-14 10:33:02 +01:00
|
|
|
Sec = TimeStamp2 - TimeStamp,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_LAST},
|
|
|
|
{<<"seconds">>,
|
|
|
|
iolist_to_binary(integer_to_list(Sec))}],
|
|
|
|
children = [{xmlcdata, Status}]}]}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_LAST},
|
|
|
|
{<<"seconds">>, <<"0">>}],
|
|
|
|
children = []}]}
|
2003-10-24 21:21:07 +02:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
on_presence_update(User, Server, _Resource, Status) ->
|
2015-12-07 16:08:57 +01:00
|
|
|
TimeStamp = p1_time_compat:system_time(seconds),
|
2005-05-23 21:47:57 +02:00
|
|
|
store_last_info(User, Server, TimeStamp, Status).
|
|
|
|
|
|
|
|
store_last_info(User, Server, TimeStamp, Status) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-13 08:59:39 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:store_last_info(LUser, LServer, TimeStamp, Status).
|
2008-12-23 02:34:52 +01:00
|
|
|
|
2009-01-03 22:29:54 +01:00
|
|
|
%% @spec (LUser::string(), LServer::string()) ->
|
2009-12-09 18:46:51 +01:00
|
|
|
%% {ok, TimeStamp::integer(), Status::string()} | not_found
|
2007-11-27 19:54:06 +01:00
|
|
|
get_last_info(LUser, LServer) ->
|
2010-11-05 18:32:25 +01:00
|
|
|
case get_last(LUser, LServer) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, _Reason} -> not_found;
|
|
|
|
Res -> Res
|
2007-11-27 19:54:06 +01:00
|
|
|
end.
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-13 08:59:39 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:remove_user(LUser, LServer).
|
2014-07-14 05:49:02 +02:00
|
|
|
|
2016-04-13 08:59:39 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
|
|
|
import(LServer) ->
|
2016-04-13 08:59:39 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:import(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-04-13 08:59:39 +02:00
|
|
|
import(LServer, DBType, LA) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, LA).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
|
|
|
transform_options(Opts) ->
|
|
|
|
lists:foldl(fun transform_options/2, [], Opts).
|
|
|
|
|
|
|
|
transform_options({node_start, {_, _, _} = Now}, Opts) ->
|
|
|
|
?WARNING_MSG("Old 'node_start' format detected. This is still supported "
|
|
|
|
"but it is better to fix your config.", []),
|
|
|
|
[{node_start, now_to_seconds(Now)}|Opts];
|
|
|
|
transform_options(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(_) -> [db_type, iqdisc].
|
|
|
|
|
|
|
|
opt_type(node_start) ->
|
|
|
|
fun (S) when is_integer(S), S >= 0 -> S end;
|
|
|
|
opt_type(_) -> [node_start].
|