2015-03-09 14:41:13 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : ejabberd_sm_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2015-03-09 14:41:13 +01:00
|
|
|
%%% Created : 9 Mar 2015 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-20 08:10:34 +02:00
|
|
|
-module(ejabberd_sm_sql).
|
2015-03-09 14:41:13 +01:00
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
|
2015-03-09 14:41:13 +01:00
|
|
|
-behaviour(ejabberd_sm).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/0,
|
|
|
|
set_session/1,
|
2017-04-14 12:57:52 +02:00
|
|
|
delete_session/1,
|
2015-03-09 14:41:13 +01:00
|
|
|
get_sessions/0,
|
|
|
|
get_sessions/1,
|
2017-04-14 12:57:52 +02:00
|
|
|
get_sessions/2]).
|
2015-03-09 14:41:13 +01:00
|
|
|
|
|
|
|
-include("ejabberd_sm.hrl").
|
|
|
|
-include("logger.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2015-03-09 14:41:13 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
-spec init() -> ok | {error, any()}.
|
|
|
|
init() ->
|
2017-03-24 09:03:23 +01:00
|
|
|
Node = erlang:atom_to_binary(node(), latin1),
|
2017-04-15 14:47:00 +02:00
|
|
|
?DEBUG("Cleaning SQL SM table...", []),
|
2015-03-09 14:41:13 +01:00
|
|
|
lists:foldl(
|
|
|
|
fun(Host, ok) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
Host, ?SQL("delete from sm where node=%(Node)s")) of
|
2015-03-09 14:41:13 +01:00
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to clean 'sm' table: ~p", [Err]),
|
2018-07-15 08:52:03 +02:00
|
|
|
{error, db_failure}
|
2015-03-09 14:41:13 +01:00
|
|
|
end;
|
|
|
|
(_, Err) ->
|
|
|
|
Err
|
2016-02-19 14:15:11 +01:00
|
|
|
end, ok, ejabberd_sm:get_vh_by_backend(?MODULE)).
|
2015-03-09 14:41:13 +01:00
|
|
|
|
|
|
|
set_session(#session{sid = {Now, Pid}, usr = {U, LServer, R},
|
|
|
|
priority = Priority, info = Info}) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
InfoS = misc:term_to_expr(Info),
|
2015-03-09 14:41:13 +01:00
|
|
|
PrioS = enc_priority(Priority),
|
|
|
|
TS = now_to_timestamp(Now),
|
2017-04-11 12:13:58 +02:00
|
|
|
PidS = misc:encode_pid(Pid),
|
2017-03-24 09:03:23 +01:00
|
|
|
Node = erlang:atom_to_binary(node(Pid), latin1),
|
2016-05-04 20:01:05 +02:00
|
|
|
case ?SQL_UPSERT(LServer, "sm",
|
|
|
|
["!usec=%(TS)d",
|
|
|
|
"!pid=%(PidS)s",
|
|
|
|
"node=%(Node)s",
|
|
|
|
"username=%(U)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(LServer)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"resource=%(R)s",
|
|
|
|
"priority=%(PrioS)s",
|
|
|
|
"info=%(InfoS)s"]) of
|
2015-03-09 14:41:13 +01:00
|
|
|
ok ->
|
|
|
|
ok;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, db_failure}
|
2015-03-09 14:41:13 +01:00
|
|
|
end.
|
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
delete_session(#session{usr = {_, LServer, _}, sid = {Now, Pid}}) ->
|
2015-03-09 14:41:13 +01:00
|
|
|
TS = now_to_timestamp(Now),
|
|
|
|
PidS = list_to_binary(erlang:pid_to_list(Pid)),
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2015-03-11 12:46:57 +01:00
|
|
|
LServer,
|
2017-04-14 12:57:52 +02:00
|
|
|
?SQL("delete from sm where usec=%(TS)d and pid=%(PidS)s")) of
|
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, db_failure}
|
2015-03-09 14:41:13 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_sessions() ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(LServer) ->
|
|
|
|
get_sessions(LServer)
|
2016-02-19 14:15:11 +01:00
|
|
|
end, ejabberd_sm:get_vh_by_backend(?MODULE)).
|
2015-03-09 14:41:13 +01:00
|
|
|
|
|
|
|
get_sessions(LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
LServer,
|
2017-03-24 09:03:23 +01:00
|
|
|
?SQL("select @(usec)d, @(pid)s, @(node)s, @(username)s,"
|
2017-11-02 15:03:30 +01:00
|
|
|
" @(resource)s, @(priority)s, @(info)s from sm"
|
|
|
|
" where %(LServer)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rows} ->
|
2017-03-24 09:03:23 +01:00
|
|
|
lists:flatmap(
|
|
|
|
fun(Row) ->
|
|
|
|
try [row_to_session(LServer, Row)]
|
2017-03-30 16:51:37 +02:00
|
|
|
catch _:{bad_node, _} -> []
|
2017-03-24 09:03:23 +01:00
|
|
|
end
|
|
|
|
end, Rows);
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2015-03-09 14:41:13 +01:00
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_sessions(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
LServer,
|
2017-03-24 09:03:23 +01:00
|
|
|
?SQL("select @(usec)d, @(pid)s, @(node)s, @(username)s,"
|
2016-05-04 20:01:05 +02:00
|
|
|
" @(resource)s, @(priority)s, @(info)s from sm"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rows} ->
|
2017-04-14 12:57:52 +02:00
|
|
|
{ok, lists:flatmap(
|
|
|
|
fun(Row) ->
|
|
|
|
try [row_to_session(LServer, Row)]
|
|
|
|
catch _:{bad_node, _} -> []
|
|
|
|
end
|
|
|
|
end, Rows)};
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, db_failure}
|
2015-03-09 14:41:13 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
now_to_timestamp({MSec, Sec, USec}) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
(MSec * 1000000 + Sec) * 1000000 + USec.
|
2015-03-09 14:41:13 +01:00
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
timestamp_to_now(I) ->
|
2015-03-09 14:41:13 +01:00
|
|
|
Head = I div 1000000,
|
|
|
|
USec = I rem 1000000,
|
|
|
|
MSec = Head div 1000000,
|
2017-04-14 19:40:39 +02:00
|
|
|
Sec = Head rem 1000000,
|
2015-03-09 14:41:13 +01:00
|
|
|
{MSec, Sec, USec}.
|
|
|
|
|
|
|
|
dec_priority(Prio) ->
|
2016-09-24 22:34:28 +02:00
|
|
|
case catch binary_to_integer(Prio) of
|
2015-03-09 14:41:13 +01:00
|
|
|
{'EXIT', _} ->
|
|
|
|
undefined;
|
|
|
|
Int ->
|
|
|
|
Int
|
|
|
|
end.
|
|
|
|
|
|
|
|
enc_priority(undefined) ->
|
|
|
|
<<"">>;
|
|
|
|
enc_priority(Int) when is_integer(Int) ->
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(Int).
|
2015-03-09 14:41:13 +01:00
|
|
|
|
2017-03-24 09:03:23 +01:00
|
|
|
row_to_session(LServer, {USec, PidS, NodeS, User, Resource, PrioS, InfoS}) ->
|
2015-03-09 14:41:13 +01:00
|
|
|
Now = timestamp_to_now(USec),
|
2017-04-11 12:13:58 +02:00
|
|
|
Pid = misc:decode_pid(PidS, NodeS),
|
2015-03-09 14:41:13 +01:00
|
|
|
Priority = dec_priority(PrioS),
|
2016-04-20 11:27:32 +02:00
|
|
|
Info = ejabberd_sql:decode_term(InfoS),
|
2015-03-09 14:41:13 +01:00
|
|
|
#session{sid = {Now, Pid}, us = {User, LServer},
|
|
|
|
usr = {User, LServer, Resource},
|
|
|
|
priority = Priority,
|
|
|
|
info = Info}.
|