2015-03-09 14:41:13 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% @copyright (C) 2015-2016, Evgeny Khramtsov
|
2015-03-09 14:41:13 +01:00
|
|
|
%%% @doc
|
|
|
|
%%%
|
|
|
|
%%% @end
|
|
|
|
%%% Created : 9 Mar 2015 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%-------------------------------------------------------------------
|
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
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2015-03-09 14:41:13 +01:00
|
|
|
-behaviour(ejabberd_sm).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/0,
|
|
|
|
set_session/1,
|
2015-03-11 12:46:57 +01:00
|
|
|
delete_session/4,
|
2015-03-09 14:41:13 +01:00
|
|
|
get_sessions/0,
|
|
|
|
get_sessions/1,
|
|
|
|
get_sessions/2,
|
|
|
|
get_sessions/3]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-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() ->
|
2016-05-04 20:01:05 +02:00
|
|
|
Node = jlib:atom_to_binary(node()),
|
2015-03-11 12:46:57 +01:00
|
|
|
?INFO_MSG("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 ->
|
|
|
|
?ERROR_MSG("failed to clean 'sm' table: ~p", [Err]),
|
|
|
|
Err
|
|
|
|
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}) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
InfoS = jlib:term_to_expr(Info),
|
2015-03-09 14:41:13 +01:00
|
|
|
PrioS = enc_priority(Priority),
|
|
|
|
TS = now_to_timestamp(Now),
|
|
|
|
PidS = list_to_binary(erlang:pid_to_list(Pid)),
|
2016-05-04 20:01:05 +02:00
|
|
|
Node = jlib:atom_to_binary(node(Pid)),
|
|
|
|
case ?SQL_UPSERT(LServer, "sm",
|
|
|
|
["!usec=%(TS)d",
|
|
|
|
"!pid=%(PidS)s",
|
|
|
|
"node=%(Node)s",
|
|
|
|
"username=%(U)s",
|
|
|
|
"resource=%(R)s",
|
|
|
|
"priority=%(PrioS)s",
|
|
|
|
"info=%(InfoS)s"]) of
|
2015-03-09 14:41:13 +01:00
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to update 'sm' table: ~p", [Err])
|
|
|
|
end.
|
|
|
|
|
2015-03-11 12:46:57 +01:00
|
|
|
delete_session(_LUser, LServer, _LResource, {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,
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
|
|
|
" @(resource)s, @(priority)s, @(info)s "
|
|
|
|
"from sm where usec=%(TS)d and pid=%(PidS)s")) of
|
|
|
|
{selected, [Row]} ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("delete from sm"
|
|
|
|
" where usec=%(TS)d and pid=%(PidS)s")),
|
2015-03-11 12:46:57 +01:00
|
|
|
{ok, row_to_session(LServer, Row)};
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, []} ->
|
2015-03-11 12:46:57 +01:00
|
|
|
{error, notfound};
|
2015-03-09 14:41:13 +01:00
|
|
|
Err ->
|
2015-03-11 12:46:57 +01:00
|
|
|
?ERROR_MSG("failed to delete from 'sm' table: ~p", [Err]),
|
|
|
|
{error, notfound}
|
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,
|
|
|
|
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
|
|
|
" @(resource)s, @(priority)s, @(info)s from sm")) of
|
|
|
|
{selected, Rows} ->
|
2015-03-09 14:41:13 +01:00
|
|
|
[row_to_session(LServer, Row) || Row <- Rows];
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
|
|
|
[]
|
|
|
|
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,
|
|
|
|
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
|
|
|
" @(resource)s, @(priority)s, @(info)s from sm"
|
|
|
|
" where username=%(LUser)s")) of
|
|
|
|
{selected, Rows} ->
|
2015-03-09 14:41:13 +01:00
|
|
|
[row_to_session(LServer, Row) || Row <- Rows];
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_sessions(LUser, LServer, LResource) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
LServer,
|
|
|
|
?SQL("select @(usec)d, @(pid)s, @(username)s,"
|
|
|
|
" @(resource)s, @(priority)s, @(info)s from sm"
|
|
|
|
" where username=%(LUser)s and resource=%(LResource)s")) of
|
|
|
|
{selected, Rows} ->
|
2015-03-09 14:41:13 +01:00
|
|
|
[row_to_session(LServer, Row) || Row <- Rows];
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to select from 'sm' table: ~p", [Err]),
|
|
|
|
[]
|
|
|
|
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,
|
|
|
|
Sec = Head div 1000000,
|
|
|
|
{MSec, Sec, USec}.
|
|
|
|
|
|
|
|
dec_priority(Prio) ->
|
2015-03-09 15:50:14 +01:00
|
|
|
case catch jlib: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) ->
|
2015-03-09 15:50:14 +01:00
|
|
|
jlib:integer_to_binary(Int).
|
2015-03-09 14:41:13 +01:00
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
row_to_session(LServer, {USec, PidS, User, Resource, PrioS, InfoS}) ->
|
2015-03-09 14:41:13 +01:00
|
|
|
Now = timestamp_to_now(USec),
|
|
|
|
Pid = erlang:list_to_pid(binary_to_list(PidS)),
|
|
|
|
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}.
|