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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 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.
|
|
|
|
%%%
|
|
|
|
%%% 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) ->
|
2004-12-14 00:00:12 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2008-08-06 15:44:58 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_LAST_ACTIVITY_s,
|
2004-12-14 00:00:12 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc),
|
2008-08-06 15:44:58 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_LAST_ACTIVITY_s,
|
2004-12-14 00:00:12 +01:00
|
|
|
?MODULE, process_sm_iq, IQDisc),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(remove_user, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(unset_presence_hook, Host,
|
2004-12-14 00:00:12 +01:00
|
|
|
?MODULE, on_presence_update, 50).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
|
|
|
ejabberd_hooks:delete(remove_user, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?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),
|
2008-08-06 15:44:58 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_LAST_ACTIVITY_s),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_LAST_ACTIVITY_s).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
process_local_iq(_From, _To, IQ) ->
|
|
|
|
case exmpp_iq:get_type(IQ) of
|
2004-12-14 00:00:12 +01:00
|
|
|
set ->
|
2008-08-06 15:44:58 +02:00
|
|
|
exmpp_iq:error(IQ, 'not-allowed');
|
2004-12-14 00:00:12 +01:00
|
|
|
get ->
|
|
|
|
Sec = trunc(element(1, erlang:statistics(wall_clock))/1000),
|
2008-08-06 15:44:58 +02:00
|
|
|
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query', attrs =
|
|
|
|
[#xmlattr{name = 'seconds', value = integer_to_list(Sec)}]},
|
|
|
|
exmpp_iq:result(IQ, Response)
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
process_sm_iq(From, To, IQ) ->
|
|
|
|
case exmpp_iq:get_type(IQ) of
|
2004-12-14 00:00:12 +01:00
|
|
|
set ->
|
2008-08-06 15:44:58 +02:00
|
|
|
exmpp_iq:error(IQ, 'not-allowed');
|
2004-12-14 00:00:12 +01:00
|
|
|
get ->
|
2008-08-06 15:44:58 +02:00
|
|
|
User = To#jid.lnode,
|
|
|
|
Server = To#jid.ldomain,
|
2004-12-14 00:00:12 +01:00
|
|
|
{Subscription, _Groups} =
|
2004-12-19 21:47:35 +01:00
|
|
|
ejabberd_hooks:run_fold(
|
2005-06-20 05:18:13 +02:00
|
|
|
roster_get_jid_info, Server,
|
2005-07-26 05:04:26 +02:00
|
|
|
{none, []}, [User, Server, From]),
|
2004-12-14 00:00:12 +01:00
|
|
|
if
|
|
|
|
(Subscription == both) or (Subscription == from) ->
|
2007-07-26 11:37:16 +02:00
|
|
|
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,
|
|
|
|
{From, To,
|
2008-08-06 15:44:58 +02:00
|
|
|
exmpp_presence:available()},
|
2007-07-26 11:37:16 +02:00
|
|
|
out]) of
|
|
|
|
allow ->
|
2008-08-06 15:44:58 +02:00
|
|
|
get_last(IQ, User, Server);
|
2007-07-26 11:37:16 +02:00
|
|
|
deny ->
|
2008-08-06 15:44:58 +02:00
|
|
|
exmpp_iq:error(IQ, 'not-allowed')
|
2004-12-14 00:00:12 +01:00
|
|
|
end;
|
|
|
|
true ->
|
2008-08-06 15:44:58 +02:00
|
|
|
exmpp_iq:error(IQ, 'not-allowed')
|
2004-12-14 00:00:12 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2007-11-27 19:54:06 +01:00
|
|
|
%% TODO: This function could use get_last_info/2
|
2008-08-06 15:44:58 +02:00
|
|
|
get_last(IQ, 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-08-06 15:44:58 +02:00
|
|
|
exmpp_iq:error(IQ, '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) ->
|
|
|
|
{MegaSecs, Secs, _MicroSecs} = now(),
|
|
|
|
TimeStamp2 = MegaSecs * 1000000 + Secs,
|
|
|
|
Sec = TimeStamp2 - TimeStamp,
|
2008-08-06 15:44:58 +02:00
|
|
|
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query',
|
|
|
|
attrs = [#xmlattr{name = 'seconds', value = integer_to_list(Sec)}],
|
|
|
|
children = [#xmlcdata{cdata = list_to_binary(Status)}]},
|
|
|
|
exmpp_iq:result(IQ, Response);
|
2004-12-14 00:00:12 +01:00
|
|
|
_ ->
|
2008-08-06 15:44:58 +02:00
|
|
|
exmpp_iq:error(IQ, 'internal-server-error')
|
2008-09-16 16:39:57 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
exmpp_iq:error(IQ, '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) ->
|
2004-12-14 00:00:12 +01:00
|
|
|
{MegaSecs, Secs, _MicroSecs} = now(),
|
|
|
|
TimeStamp = MegaSecs * 1000000 + Secs,
|
2005-05-23 21:47:57 +02:00
|
|
|
store_last_info(User, Server, TimeStamp, Status).
|
|
|
|
|
|
|
|
store_last_info(User, Server, TimeStamp, Status) ->
|
2008-08-06 15:44:58 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2004-12-14 00:00:12 +01:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
Seconds = ejabberd_odbc:escape(integer_to_list(TimeStamp)),
|
|
|
|
State = ejabberd_odbc:escape(Status),
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:set_last_t(LServer, Username, Seconds, State).
|
|
|
|
|
2007-11-27 19:54:06 +01:00
|
|
|
%% Returns: {ok, Timestamp, Status} | not_found
|
|
|
|
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) ->
|
|
|
|
{ok, TimeStamp, Status};
|
|
|
|
_ ->
|
|
|
|
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-08-06 15:44:58 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2004-12-14 00:00:12 +01:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:del_last(LServer, Username).
|