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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2012-02-23 16:52:34 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2012 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
|
|
|
%%%
|
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).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-10-24 21:21:07 +02:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-export([start/2, stop/1, process_local_iq/3, export/1,
|
|
|
|
process_sm_iq/3, on_presence_update/4,
|
|
|
|
store_last_info/4, get_last_info/2, remove_user/2]).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2003-10-24 21:21:07 +02:00
|
|
|
-include("jlib.hrl").
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-record(last_activity, {us = {<<"">>, <<"">>} :: {binary(), binary()},
|
|
|
|
timestamp = 0 :: non_neg_integer(),
|
|
|
|
status = <<"">> :: binary()}).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
2012-04-27 11:52:05 +02:00
|
|
|
case gen_mod:db_type(Opts) of
|
2012-09-11 15:45:59 +02:00
|
|
|
mnesia ->
|
|
|
|
mnesia:create_table(last_activity,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes,
|
|
|
|
record_info(fields, last_activity)}]),
|
|
|
|
update_table();
|
|
|
|
_ -> ok
|
2012-04-27 11:52:05 +02:00
|
|
|
end,
|
2012-09-11 15:45:59 +02: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) ->
|
2012-09-11 15:45:59 +02: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),
|
2012-09-11 15:45:59 +02: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
|
|
|
|
%%%
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
process_local_iq(_From, _To,
|
|
|
|
#iq{type = Type, sub_el = SubEl} = IQ) ->
|
2003-10-24 21:21:07 +02:00
|
|
|
case Type of
|
2012-09-11 15:45:59 +02:00
|
|
|
set ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
|
|
|
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
|
|
|
get_node_uptime() ->
|
2012-09-11 15:45:59 +02:00
|
|
|
case ejabberd_config:get_local_option(
|
|
|
|
node_start,
|
|
|
|
fun({MegaSecs, Secs, MicroSecs} = Now)
|
|
|
|
when is_integer(MegaSecs), MegaSecs >= 0,
|
|
|
|
is_integer(Secs), Secs >= 0,
|
|
|
|
is_integer(MicroSecs), MicroSecs >= 0 ->
|
|
|
|
Now
|
|
|
|
end) of
|
|
|
|
undefined ->
|
|
|
|
trunc(element(1, erlang:statistics(wall_clock)) / 1000);
|
|
|
|
StartNow ->
|
|
|
|
now_to_seconds(now()) - now_to_seconds(StartNow)
|
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
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
process_sm_iq(From, To,
|
|
|
|
#iq{type = Type, sub_el = SubEl} = IQ) ->
|
2003-10-24 21:21:07 +02:00
|
|
|
case Type of
|
2012-09-11 15:45:59 +02:00
|
|
|
set ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
|
|
|
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 ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
|
|
|
|
end
|
2003-10-24 21:21:07 +02:00
|
|
|
end.
|
|
|
|
|
2010-11-05 18:32:25 +01:00
|
|
|
get_last(LUser, LServer) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
get_last(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
get_last(LUser, LServer, mnesia) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
case catch mnesia:dirty_read(last_activity,
|
|
|
|
{LUser, LServer})
|
|
|
|
of
|
|
|
|
{'EXIT', Reason} -> {error, Reason};
|
|
|
|
[] -> not_found;
|
|
|
|
[#last_activity{timestamp = TimeStamp,
|
|
|
|
status = Status}] ->
|
|
|
|
{ok, TimeStamp, Status}
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
|
|
|
get_last(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
case catch odbc_queries:get_last(LServer, Username) of
|
2012-09-11 15:45:59 +02:00
|
|
|
{selected, [<<"seconds">>, <<"state">>], []} ->
|
|
|
|
not_found;
|
|
|
|
{selected, [<<"seconds">>, <<"state">>],
|
|
|
|
[[STimeStamp, Status]]} ->
|
|
|
|
case catch jlib:binary_to_integer(STimeStamp) of
|
|
|
|
TimeStamp when is_integer(TimeStamp) ->
|
|
|
|
{ok, TimeStamp, Status};
|
|
|
|
Reason -> {error, {invalid_timestamp, Reason}}
|
|
|
|
end;
|
|
|
|
Reason -> {error, {invalid_result, Reason}}
|
2010-11-05 18:32:25 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_last_iq(IQ, SubEl, LUser, LServer) ->
|
2010-11-05 18:38:42 +01:00
|
|
|
case ejabberd_sm:get_user_resources(LUser, LServer) of
|
2012-09-11 15:45:59 +02:00
|
|
|
[] ->
|
|
|
|
case get_last(LUser, LServer) of
|
|
|
|
{error, _Reason} ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]};
|
|
|
|
not_found ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_SERVICE_UNAVAILABLE]};
|
|
|
|
{ok, TimeStamp, Status} ->
|
|
|
|
TimeStamp2 = now_to_seconds(now()),
|
|
|
|
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) ->
|
2008-12-23 02:34:52 +01:00
|
|
|
TimeStamp = now_to_seconds(now()),
|
2005-05-23 21:47:57 +02:00
|
|
|
store_last_info(User, Server, TimeStamp, Status).
|
|
|
|
|
|
|
|
store_last_info(User, Server, TimeStamp, Status) ->
|
2004-12-12 22:00:34 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2012-04-27 11:52:05 +02:00
|
|
|
DBType = gen_mod:db_type(LServer, ?MODULE),
|
2012-09-11 15:45:59 +02:00
|
|
|
store_last_info(LUser, LServer, TimeStamp, Status,
|
|
|
|
DBType).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
store_last_info(LUser, LServer, TimeStamp, Status,
|
|
|
|
mnesia) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2012-09-11 15:45:59 +02:00
|
|
|
F = fun () ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:write(#last_activity{us = US,
|
2004-07-11 22:51:54 +02:00
|
|
|
timestamp = TimeStamp,
|
|
|
|
status = Status})
|
2003-10-24 21:21:07 +02:00
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
2012-09-11 15:45:59 +02:00
|
|
|
store_last_info(LUser, LServer, TimeStamp, Status,
|
|
|
|
odbc) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2012-09-11 15:45:59 +02:00
|
|
|
Seconds =
|
|
|
|
ejabberd_odbc:escape(iolist_to_binary(integer_to_list(TimeStamp))),
|
2012-04-27 11:52:05 +02:00
|
|
|
State = ejabberd_odbc:escape(Status),
|
2012-09-11 15:45:59 +02:00
|
|
|
odbc_queries:set_last_t(LServer, Username, Seconds,
|
|
|
|
State).
|
2008-12-23 02:34:52 +01:00
|
|
|
|
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
|
2012-09-11 15:45:59 +02: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) ->
|
2003-11-01 22:06:50 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2012-04-27 11:52:05 +02:00
|
|
|
DBType = gen_mod:db_type(LServer, ?MODULE),
|
|
|
|
remove_user(LUser, LServer, DBType).
|
|
|
|
|
|
|
|
remove_user(LUser, LServer, mnesia) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2012-09-11 15:45:59 +02:00
|
|
|
F = fun () -> mnesia:delete({last_activity, US}) end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
remove_user(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
odbc_queries:del_last(LServer, Username).
|
2004-07-11 22:51:54 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, last_activity),
|
|
|
|
case mnesia:table_info(last_activity, attributes) of
|
2012-09-11 15:45:59 +02:00
|
|
|
Fields ->
|
|
|
|
ejabberd_config:convert_table_to_binary(
|
|
|
|
last_activity, Fields, set,
|
|
|
|
fun(#last_activity{us = {U, _}}) -> U end,
|
|
|
|
fun(#last_activity{us = {U, S}, status = Status} = R) ->
|
|
|
|
R#last_activity{us = {iolist_to_binary(U),
|
|
|
|
iolist_to_binary(S)},
|
|
|
|
status = iolist_to_binary(Status)}
|
|
|
|
end);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating last_activity table", []),
|
|
|
|
mnesia:transform_table(last_activity, ignore, Fields)
|
2004-07-11 22:51:54 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
export(_Server) ->
|
|
|
|
[{last_activity,
|
|
|
|
fun(Host, #last_activity{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp, status = Status})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Seconds =
|
|
|
|
ejabberd_odbc:escape(jlib:integer_to_binary(TimeStamp)),
|
|
|
|
State = ejabberd_odbc:escape(Status),
|
|
|
|
[[<<"delete from last where username='">>, Username, <<"';">>],
|
|
|
|
[<<"insert into last(username, seconds, "
|
|
|
|
"state) values ('">>,
|
|
|
|
Username, <<"', '">>, Seconds, <<"', '">>, State,
|
|
|
|
<<"');">>]];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|