2016-04-13 08:59:39 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_last_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-13 08:59:39 +02:00
|
|
|
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2016-12-27 10:44:07 +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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-13 08:59:39 +02:00
|
|
|
-module(mod_last_sql).
|
2016-12-27 10:44:07 +01:00
|
|
|
|
2016-04-13 08:59:39 +02:00
|
|
|
-behaviour(mod_last).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
|
2016-04-13 08:59:39 +02:00
|
|
|
%% API
|
|
|
|
-export([init/2, get_last/2, store_last_info/4, remove_user/2,
|
2016-11-22 14:48:01 +01:00
|
|
|
import/2, export/1]).
|
2016-04-13 08:59:39 +02:00
|
|
|
|
|
|
|
-include("mod_last.hrl").
|
|
|
|
-include("logger.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2016-04-13 08:59:39 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
get_last(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(seconds)d, @(state)s from last"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H")) of
|
2016-04-13 08:59:39 +02:00
|
|
|
{selected, []} ->
|
2017-05-18 12:21:17 +02:00
|
|
|
error;
|
2016-04-13 08:59:39 +02:00
|
|
|
{selected, [{TimeStamp, Status}]} ->
|
2017-05-18 12:21:17 +02:00
|
|
|
{ok, {TimeStamp, Status}};
|
2017-12-17 17:46:55 +01:00
|
|
|
_Reason ->
|
2017-05-18 12:21:17 +02:00
|
|
|
{error, db_failure}
|
2016-04-13 08:59:39 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
store_last_info(LUser, LServer, TimeStamp, Status) ->
|
2020-03-18 14:02:21 +01:00
|
|
|
TS = integer_to_binary(TimeStamp),
|
2017-05-23 11:25:13 +02:00
|
|
|
case ?SQL_UPSERT(LServer, "last",
|
|
|
|
["!username=%(LUser)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2020-03-18 14:02:21 +01:00
|
|
|
"seconds=%(TS)s",
|
2017-05-23 11:25:13 +02:00
|
|
|
"state=%(Status)s"]) of
|
2017-05-18 12:21:17 +02:00
|
|
|
ok ->
|
|
|
|
ok;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-05-18 12:21:17 +02:00
|
|
|
{error, db_failure}
|
|
|
|
end.
|
2016-04-13 08:59:39 +02:00
|
|
|
|
|
|
|
remove_user(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from last where username=%(LUser)s and %(LServer)H")).
|
2016-04-13 08:59:39 +02:00
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{last_activity,
|
|
|
|
fun(Host, #last_activity{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp, status = Status})
|
|
|
|
when LServer == Host ->
|
2020-03-18 14:02:21 +01:00
|
|
|
TS = integer_to_binary(TimeStamp),
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from last where username=%(LUser)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT("last",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
2020-03-18 14:02:21 +01:00
|
|
|
"seconds=%(TS)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"state=%(Status)s"])];
|
2016-04-13 08:59:39 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(_LServer, _LA) ->
|
|
|
|
pass.
|