2017-03-30 10:45:09 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Created : 30 Mar 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
|
|
|
%%%
|
|
|
|
%%% 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(mod_carboncopy_redis).
|
|
|
|
-behaviour(mod_carboncopy).
|
2017-04-14 12:57:52 +02:00
|
|
|
-behaviour(gen_server).
|
2017-03-30 10:45:09 +02:00
|
|
|
|
|
|
|
%% API
|
2017-04-14 12:57:52 +02:00
|
|
|
-export([init/2, enable/4, disable/3, list/2, cache_nodes/1]).
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
|
|
|
|
terminate/2, code_change/3, start_link/0]).
|
2017-03-30 10:45:09 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("logger.hrl").
|
2017-04-14 12:57:52 +02:00
|
|
|
-include("mod_carboncopy.hrl").
|
|
|
|
|
|
|
|
-define(CARBONCOPY_KEY, <<"ejabberd:carboncopy">>).
|
|
|
|
|
|
|
|
-record(state, {}).
|
2017-03-30 10:45:09 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
2017-04-14 12:57:52 +02:00
|
|
|
Spec = {?MODULE, {?MODULE, start_link, []},
|
|
|
|
transient, 5000, worker, [?MODULE]},
|
|
|
|
case supervisor:start_child(ejabberd_backend_sup, Spec) of
|
|
|
|
{ok, _Pid} -> ok;
|
|
|
|
Err -> Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec start_link() -> {ok, pid()} | {error, any()}.
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
cache_nodes(_LServer) ->
|
|
|
|
[node()].
|
2017-03-30 10:45:09 +02:00
|
|
|
|
|
|
|
enable(LUser, LServer, LResource, NS) ->
|
|
|
|
USKey = us_key(LUser, LServer),
|
|
|
|
NodeKey = node_key(),
|
|
|
|
JID = jid:encode({LUser, LServer, LResource}),
|
2017-04-14 12:57:52 +02:00
|
|
|
Data = term_to_binary({NS, node()}),
|
2017-04-02 10:56:09 +02:00
|
|
|
case ejabberd_redis:multi(
|
|
|
|
fun() ->
|
2017-04-14 12:57:52 +02:00
|
|
|
ejabberd_redis:hset(USKey, LResource, Data),
|
|
|
|
ejabberd_redis:sadd(NodeKey, [JID]),
|
|
|
|
ejabberd_redis:publish(
|
|
|
|
?CARBONCOPY_KEY,
|
|
|
|
term_to_binary({delete, {LUser, LServer}}))
|
2017-04-02 10:56:09 +02:00
|
|
|
end) of
|
|
|
|
{ok, _} ->
|
2017-03-30 10:45:09 +02:00
|
|
|
ok;
|
2017-04-07 08:10:33 +02:00
|
|
|
{error, _} ->
|
|
|
|
{error, db_failure}
|
2017-03-30 10:45:09 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
disable(LUser, LServer, LResource) ->
|
|
|
|
USKey = us_key(LUser, LServer),
|
|
|
|
NodeKey = node_key(),
|
|
|
|
JID = jid:encode({LUser, LServer, LResource}),
|
2017-04-02 10:56:09 +02:00
|
|
|
case ejabberd_redis:multi(
|
|
|
|
fun() ->
|
|
|
|
ejabberd_redis:hdel(USKey, [LResource]),
|
2017-04-14 12:57:52 +02:00
|
|
|
ejabberd_redis:srem(NodeKey, [JID]),
|
|
|
|
ejabberd_redis:publish(
|
|
|
|
?CARBONCOPY_KEY,
|
|
|
|
term_to_binary({delete, {LUser, LServer}}))
|
2017-04-02 10:56:09 +02:00
|
|
|
end) of
|
|
|
|
{ok, _} ->
|
2017-03-30 10:45:09 +02:00
|
|
|
ok;
|
2017-04-07 08:10:33 +02:00
|
|
|
{error, _} ->
|
|
|
|
{error, db_failure}
|
2017-03-30 10:45:09 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
list(LUser, LServer) ->
|
|
|
|
USKey = us_key(LUser, LServer),
|
2017-04-02 10:56:09 +02:00
|
|
|
case ejabberd_redis:hgetall(USKey) of
|
2017-04-14 12:57:52 +02:00
|
|
|
{ok, Pairs} ->
|
|
|
|
{ok, lists:flatmap(
|
|
|
|
fun({Resource, Data}) ->
|
|
|
|
try
|
|
|
|
{NS, Node} = binary_to_term(Data),
|
|
|
|
[{Resource, NS, Node}]
|
|
|
|
catch _:_ ->
|
|
|
|
?ERROR_MSG("invalid term stored in Redis "
|
|
|
|
"(key = ~s): ~p",
|
|
|
|
[USKey, Data]),
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Pairs)};
|
2017-04-07 08:10:33 +02:00
|
|
|
{error, _} ->
|
2017-04-14 12:57:52 +02:00
|
|
|
{error, db_failure}
|
2017-03-30 10:45:09 +02:00
|
|
|
end.
|
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
|
|
|
init([]) ->
|
|
|
|
ejabberd_redis:subscribe([?CARBONCOPY_KEY]),
|
|
|
|
clean_table(),
|
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
handle_info({redis_message, ?CARBONCOPY_KEY, Data}, State) ->
|
|
|
|
case binary_to_term(Data) of
|
|
|
|
{delete, Key} ->
|
|
|
|
ets_cache:delete(?CARBONCOPY_CACHE, Key);
|
|
|
|
Msg ->
|
|
|
|
?WARNING_MSG("unexpected redis message: ~p", [Msg])
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(Info, State) ->
|
|
|
|
?ERROR_MSG("unexpected info: ~p", [Info]),
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
2017-03-30 10:45:09 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
clean_table() ->
|
2017-04-15 14:47:00 +02:00
|
|
|
?DEBUG("Cleaning Redis 'carboncopy' table...", []),
|
2017-03-30 10:45:09 +02:00
|
|
|
NodeKey = node_key(),
|
2017-04-02 10:56:09 +02:00
|
|
|
case ejabberd_redis:smembers(NodeKey) of
|
2017-03-30 10:45:09 +02:00
|
|
|
{ok, JIDs} ->
|
2017-04-07 08:10:33 +02:00
|
|
|
ejabberd_redis:multi(
|
|
|
|
fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(JID) ->
|
|
|
|
{U, S, R} = jid:split(jid:decode(JID)),
|
|
|
|
USKey = us_key(U, S),
|
|
|
|
ejabberd_redis:hdel(USKey, [R])
|
|
|
|
end, JIDs)
|
|
|
|
end);
|
|
|
|
{error, _} ->
|
|
|
|
ok
|
2017-03-30 10:45:09 +02:00
|
|
|
end,
|
2017-04-07 08:10:33 +02:00
|
|
|
ejabberd_redis:del([NodeKey]),
|
|
|
|
ok.
|
2017-03-30 10:45:09 +02:00
|
|
|
|
|
|
|
us_key(LUser, LServer) ->
|
|
|
|
<<"ejabberd:carboncopy:users:", LUser/binary, $@, LServer/binary>>.
|
|
|
|
|
|
|
|
node_key() ->
|
|
|
|
Node = erlang:atom_to_binary(node(), latin1),
|
|
|
|
<<"ejabberd:carboncopy:nodes:", Node/binary>>.
|