2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_last_odbc.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-12-14 00:00:12 +01: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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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
|
|
|
|
%%%
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_last_odbc).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-14 00:00:12 +01:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2004-12-14 00:00:12 +01:00
|
|
|
process_local_iq/3,
|
|
|
|
process_sm_iq/3,
|
2005-05-23 21:47:57 +02:00
|
|
|
on_presence_update/4,
|
|
|
|
store_last_info/4,
|
2007-11-27 19:54:06 +01:00
|
|
|
get_last_info/2,
|
2005-07-13 05:24:13 +02:00
|
|
|
remove_user/2]).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
-include("ejabberd.hrl").
|
2007-07-26 11:37:16 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2004-12-14 00:00:12 +01: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),
|
2004-12-14 00:00:12 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_LAST_ACTIVITY,
|
2004-12-14 00:00:12 +01: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-14 00:00:12 +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-14 00:00:12 +01:00
|
|
|
?MODULE, on_presence_update, 50).
|
|
|
|
|
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).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%%%
|
|
|
|
%%% Uptime of ejabberd node
|
|
|
|
%%%
|
|
|
|
|
2008-10-02 16:59:48 +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-10-02 16:59:48 +02:00
|
|
|
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('seconds', Sec)]},
|
2008-10-02 16:59:48 +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').
|
2004-12-14 00:00:12 +01:00
|
|
|
|
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-10-02 16:59:48 +02:00
|
|
|
process_sm_iq(From, To, #iq{type = get} = IQ_Rec) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
User = exmpp_jid:lnode_as_list(To),
|
|
|
|
Server = exmpp_jid:ldomain_as_list(To),
|
2008-10-02 16:59:48 +02:00
|
|
|
{Subscription, _Groups} =
|
|
|
|
ejabberd_hooks:run_fold(
|
2009-01-03 16:15:38 +01:00
|
|
|
roster_get_jid_info, exmpp_jid:ldomain(To),
|
|
|
|
{none, []}, [exmpp_jid:lnode(To), exmpp_jid:ldomain(To), From]),
|
2008-10-02 16:59:48 +02:00
|
|
|
if
|
|
|
|
(Subscription == both) or (Subscription == from) ->
|
|
|
|
UserListRecord = ejabberd_hooks:run_fold(
|
2009-01-03 16:15:38 +01:00
|
|
|
privacy_get_user_list, exmpp_jid:ldomain(To),
|
2008-10-02 16:59:48 +02:00
|
|
|
#userlist{},
|
2009-01-03 16:15:38 +01:00
|
|
|
[exmpp_jid:lnode(To), exmpp_jid:ldomain(To)]),
|
2008-10-02 16:59:48 +02:00
|
|
|
case ejabberd_hooks:run_fold(
|
2009-01-03 16:15:38 +01:00
|
|
|
privacy_check_packet, exmpp_jid:ldomain(To),
|
2008-10-02 16:59:48 +02:00
|
|
|
allow,
|
2009-01-03 16:15:38 +01:00
|
|
|
[exmpp_jid:lnode(To), exmpp_jid:ldomain(To), UserListRecord,
|
2008-10-02 16:59:48 +02:00
|
|
|
{From, To,
|
|
|
|
exmpp_presence:available()},
|
|
|
|
out]) of
|
|
|
|
allow ->
|
|
|
|
get_last(IQ_Rec, User, Server);
|
|
|
|
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').
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2007-11-27 19:54:06 +01:00
|
|
|
%% TODO: This function could use get_last_info/2
|
2008-10-02 16:59:48 +02:00
|
|
|
get_last(IQ_Rec, LUser, LServer) ->
|
2004-12-14 00:00:12 +01:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2006-09-03 17:15:46 +02:00
|
|
|
case catch odbc_queries:get_last(LServer, Username) of
|
2004-12-14 00:00:12 +01:00
|
|
|
{selected, ["seconds","state"], []} ->
|
2008-10-02 16:59:48 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'service-unavailable');
|
2004-12-14 00:00:12 +01:00
|
|
|
{selected, ["seconds","state"], [{STimeStamp, Status}]} ->
|
|
|
|
case catch list_to_integer(STimeStamp) of
|
|
|
|
TimeStamp when is_integer(TimeStamp) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
TimeStamp2 = now_to_seconds(now()),
|
2004-12-14 00:00:12 +01: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-08-06 15:44:58 +02:00
|
|
|
children = [#xmlcdata{cdata = list_to_binary(Status)}]},
|
2008-10-02 16:59:48 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Response);
|
2004-12-14 00:00:12 +01:00
|
|
|
_ ->
|
2008-10-02 16:59:48 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error')
|
2008-09-16 16:39:57 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2008-10-02 16:59:48 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error')
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2005-05-23 21:47:57 +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-10-02 16:59:48 +02:00
|
|
|
try
|
2009-01-03 16:15:38 +01:00
|
|
|
%LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
%LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
LUser = binary_to_list(User),
|
|
|
|
LServer = binary_to_list(Server),
|
|
|
|
|
2008-10-02 16:59:48 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Seconds = ejabberd_odbc:escape(integer_to_list(TimeStamp)),
|
2009-01-03 16:15:38 +01:00
|
|
|
State = ejabberd_odbc:escape(Status),
|
2008-10-02 16:59:48 +02:00
|
|
|
odbc_queries:set_last_t(LServer, Username, Seconds, State)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2006-09-03 17:15:46 +02: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
|
2007-11-27 19:54:06 +01:00
|
|
|
get_last_info(LUser, LServer) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
case catch odbc_queries:get_last(LServer, Username) of
|
|
|
|
{selected, ["seconds","state"], []} ->
|
|
|
|
not_found;
|
|
|
|
{selected, ["seconds","state"], [{STimeStamp, Status}]} ->
|
|
|
|
case catch list_to_integer(STimeStamp) of
|
|
|
|
TimeStamp when is_integer(TimeStamp) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
{ok, TimeStamp, Status};
|
2007-11-27 19:54:06 +01:00
|
|
|
_ ->
|
|
|
|
not_found
|
2008-09-16 16:39:57 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
not_found
|
2007-11-27 19:54:06 +01:00
|
|
|
end.
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
remove_user(User, Server) ->
|
2008-10-02 16:59:48 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
odbc_queries:del_last(LServer, Username)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|