25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +01:00
xmpp.chapril.org-ejabberd/src/ejabberd_router_sql.erl

134 lines
4.1 KiB
Erlang
Raw Normal View History

2017-03-28 15:31:37 +02:00
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created : 28 Mar 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
2020-01-28 13:34:02 +01:00
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
2017-03-28 15:31:37 +02: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.
%%%
%%%-------------------------------------------------------------------
-module(ejabberd_router_sql).
-behaviour(ejabberd_router).
%% API
-export([init/0, register_route/5, unregister_route/3, find_routes/1,
get_all_routes/0]).
2017-03-28 15:31:37 +02:00
-include("logger.hrl").
-include("ejabberd_sql_pt.hrl").
-include("ejabberd_router.hrl").
-include("ejabberd_stacktrace.hrl").
2017-03-28 15:31:37 +02:00
%%%===================================================================
%%% API
%%%===================================================================
init() ->
Node = erlang:atom_to_binary(node(), latin1),
2017-04-15 14:47:00 +02:00
?DEBUG("Cleaning SQL 'route' table...", []),
2017-03-28 15:31:37 +02:00
case ejabberd_sql:sql_query(
ejabberd_config:get_myname(), ?SQL("delete from route where node=%(Node)s")) of
2017-03-28 15:31:37 +02:00
{updated, _} ->
ok;
Err ->
2019-06-24 19:32:34 +02:00
?ERROR_MSG("Failed to clean 'route' table: ~p", [Err]),
2017-03-28 15:31:37 +02:00
Err
end.
register_route(Domain, ServerHost, LocalHint, _, Pid) ->
PidS = misc:encode_pid(Pid),
2017-03-28 15:31:37 +02:00
LocalHintS = enc_local_hint(LocalHint),
Node = erlang:atom_to_binary(node(Pid), latin1),
case ?SQL_UPSERT(ejabberd_config:get_myname(), "route",
2017-03-28 15:31:37 +02:00
["!domain=%(Domain)s",
"!server_host=%(ServerHost)s",
"!node=%(Node)s",
"!pid=%(PidS)s",
"local_hint=%(LocalHintS)s"]) of
ok ->
ok;
_ ->
{error, db_failure}
2017-03-28 15:31:37 +02:00
end.
unregister_route(Domain, _, Pid) ->
PidS = misc:encode_pid(Pid),
2017-03-28 15:31:37 +02:00
Node = erlang:atom_to_binary(node(Pid), latin1),
case ejabberd_sql:sql_query(
ejabberd_config:get_myname(),
?SQL("delete from route where domain=%(Domain)s "
"and pid=%(PidS)s and node=%(Node)s")) of
{updated, _} ->
ok;
_ ->
{error, db_failure}
2017-03-28 15:31:37 +02:00
end.
find_routes(Domain) ->
2017-03-28 15:31:37 +02:00
case ejabberd_sql:sql_query(
ejabberd_config:get_myname(),
?SQL("select @(server_host)s, @(node)s, @(pid)s, @(local_hint)s "
"from route where domain=%(Domain)s")) of
{selected, Rows} ->
{ok, lists:flatmap(
fun(Row) ->
row_to_route(Domain, Row)
end, Rows)};
_ ->
{error, db_failure}
2017-03-28 15:31:37 +02:00
end.
get_all_routes() ->
case ejabberd_sql:sql_query(
ejabberd_config:get_myname(),
2017-03-28 15:31:37 +02:00
?SQL("select @(domain)s from route where domain <> server_host")) of
{selected, Domains} ->
{ok, [Domain || {Domain} <- Domains]};
_ ->
{error, db_failure}
2017-03-28 18:34:04 +02:00
end.
2017-03-28 15:31:37 +02:00
%%%===================================================================
%%% Internal functions
%%%===================================================================
enc_local_hint(undefined) ->
<<"">>;
enc_local_hint(LocalHint) ->
misc:term_to_expr(LocalHint).
2017-03-28 15:31:37 +02:00
dec_local_hint(<<"">>) ->
undefined;
dec_local_hint(S) ->
ejabberd_sql:decode_term(S).
row_to_route(Domain, {ServerHost, NodeS, PidS, LocalHintS} = Row) ->
try [#route{domain = Domain,
server_host = ServerHost,
pid = misc:decode_pid(PidS, NodeS),
2017-03-28 15:31:37 +02:00
local_hint = dec_local_hint(LocalHintS)}]
catch _:{bad_node, _} ->
2017-03-28 15:31:37 +02:00
[];
2019-07-07 21:12:14 +02:00
?EX_RULE(Class, Reason, St) ->
StackTrace = ?EX_STACK(St),
2019-06-24 19:32:34 +02:00
?ERROR_MSG("Failed to decode row from 'route' table:~n"
2019-07-07 21:12:14 +02:00
"** Row = ~p~n"
"** Domain = ~ts~n"
"** ~ts",
2019-07-07 21:12:14 +02:00
[Row, Domain,
misc:format_exception(2, Class, Reason, StackTrace)]),
2017-03-28 15:31:37 +02:00
[]
end.