25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Clean mod_carboncopy.erl from DB specific code

This commit is contained in:
Evgeniy Khramtsov 2016-04-15 15:48:56 +03:00
parent 64fdbe7866
commit 222572bd56
3 changed files with 89 additions and 28 deletions

View File

@ -0,0 +1,4 @@
-type matchspec_atom() :: '_' | '$1' | '$2' | '$3'.
-record(carboncopy, {us :: {binary(), binary()} | matchspec_atom(),
resource :: binary() | matchspec_atom(),
version :: binary() | matchspec_atom()}).

View File

@ -43,12 +43,11 @@
-include("logger.hrl"). -include("logger.hrl").
-include("jlib.hrl"). -include("jlib.hrl").
-define(PROCNAME, ?MODULE). -define(PROCNAME, ?MODULE).
-define(TABLE, carboncopy).
-type matchspec_atom() :: '_' | '$1' | '$2' | '$3'. -callback init(binary(), gen_mod:opts()) -> any().
-record(carboncopy,{us :: {binary(), binary()} | matchspec_atom(), -callback enable(binary(), binary(), binary(), binary()) -> ok | {error, any()}.
resource :: binary() | matchspec_atom(), -callback disable(binary(), binary(), binary()) -> ok | {error, any()}.
version :: binary() | matchspec_atom()}). -callback list(binary(), binary()) -> [{binary(), binary()}].
is_carbon_copy(Packet) -> is_carbon_copy(Packet) ->
is_carbon_copy(Packet, <<"sent">>) orelse is_carbon_copy(Packet, <<"sent">>) orelse
@ -69,17 +68,8 @@ start(Host, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts,fun gen_iq_handler:check_type/1, one_queue), IQDisc = gen_mod:get_opt(iqdisc, Opts,fun gen_iq_handler:check_type/1, one_queue),
mod_disco:register_feature(Host, ?NS_CARBONS_1), mod_disco:register_feature(Host, ?NS_CARBONS_1),
mod_disco:register_feature(Host, ?NS_CARBONS_2), mod_disco:register_feature(Host, ?NS_CARBONS_2),
Fields = record_info(fields, ?TABLE), Mod = gen_mod:db_mod(Host, ?MODULE),
try mnesia:table_info(?TABLE, attributes) of Mod:init(Host, Opts),
Fields -> ok;
_ -> mnesia:delete_table(?TABLE) %% recreate..
catch _:_Error -> ok %%probably table don't exist
end,
mnesia:create_table(?TABLE,
[{ram_copies, [node()]},
{attributes, record_info(fields, ?TABLE)},
{type, bag}]),
mnesia:add_table_copy(?TABLE, node(), ram_copies),
ejabberd_hooks:add(unset_presence_hook,Host, ?MODULE, remove_connection, 10), ejabberd_hooks:add(unset_presence_hook,Host, ?MODULE, remove_connection, 10),
%% why priority 89: to define clearly that we must run BEFORE mod_logdb hook (90) %% why priority 89: to define clearly that we must run BEFORE mod_logdb hook (90)
ejabberd_hooks:add(user_send_packet,Host, ?MODULE, user_send_packet, 89), ejabberd_hooks:add(user_send_packet,Host, ?MODULE, user_send_packet, 89),
@ -244,18 +234,13 @@ build_forward_packet(JID, Packet, Sender, Dest, Direction, ?NS_CARBONS_1) ->
enable(Host, U, R, CC)-> enable(Host, U, R, CC)->
?DEBUG("enabling for ~p", [U]), ?DEBUG("enabling for ~p", [U]),
try mnesia:dirty_write(#carboncopy{us = {U, Host}, resource=R, version = CC}) of Mod = gen_mod:db_mod(Host, ?MODULE),
ok -> ok Mod:enable(U, Host, R, CC).
catch _:Error -> {error, Error}
end.
disable(Host, U, R)-> disable(Host, U, R)->
?DEBUG("disabling for ~p", [U]), ?DEBUG("disabling for ~p", [U]),
ToDelete = mnesia:dirty_match_object(?TABLE, #carboncopy{us = {U, Host}, resource = R, version = '_'}), Mod = gen_mod:db_mod(Host, ?MODULE),
try lists:foreach(fun mnesia:dirty_delete_object/1, ToDelete) of Mod:disable(U, Host, R).
ok -> ok
catch _:Error -> {error, Error}
end.
complete_packet(From, #xmlel{name = <<"message">>, attrs = OrigAttrs} = Packet, sent) -> complete_packet(From, #xmlel{name = <<"message">>, attrs = OrigAttrs} = Packet, sent) ->
%% if this is a packet sent by user on this host, then Packet doesn't %% if this is a packet sent by user on this host, then Packet doesn't
@ -290,8 +275,12 @@ has_non_empty_body(Packet) ->
%% list {resource, cc_version} with carbons enabled for given user and host %% list {resource, cc_version} with carbons enabled for given user and host
list(User, Server) -> list(User, Server) ->
mnesia:dirty_select(?TABLE, [{#carboncopy{us = {User, Server}, resource = '$2', version = '$3'}, [], [{{'$2','$3'}}]}]). Mod = gen_mod:db_mod(Server, ?MODULE),
Mod:list(User, Server).
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1; mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
mod_opt_type(_) -> [iqdisc]. mod_opt_type(db_type) ->
fun(internal) -> mnesia;
(mnesia) -> mnesia
end;
mod_opt_type(_) -> [db_type, iqdisc].

View File

@ -0,0 +1,68 @@
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2016, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 15 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_carboncopy_mnesia).
-behaviour(mod_carboncopy).
%% API
-export([init/2, enable/4, disable/3, list/2]).
-include("mod_carboncopy.hrl").
%%%===================================================================
%%% API
%%%===================================================================
init(_Host, _Opts) ->
Fields = record_info(fields, carboncopy),
try mnesia:table_info(carboncopy, attributes) of
Fields ->
ok;
_ ->
%% recreate..
mnesia:delete_table(carboncopy)
catch _:_Error ->
%% probably table don't exist
ok
end,
mnesia:create_table(carboncopy,
[{ram_copies, [node()]},
{attributes, record_info(fields, carboncopy)},
{type, bag}]),
mnesia:add_table_copy(carboncopy, node(), ram_copies).
enable(LUser, LServer, LResource, NS) ->
try mnesia:dirty_write(
#carboncopy{us = {LUser, LServer},
resource = LResource,
version = NS}) of
ok -> ok
catch _:Error ->
{error, Error}
end.
disable(LUser, LServer, LResource) ->
ToDelete = mnesia:dirty_match_object(
#carboncopy{us = {LUser, LServer},
resource = LResource,
version = '_'}),
try lists:foreach(fun mnesia:dirty_delete_object/1, ToDelete) of
ok -> ok
catch _:Error ->
{error, Error}
end.
list(LUser, LServer) ->
mnesia:dirty_select(
carboncopy,
[{#carboncopy{us = {LUser, LServer}, resource = '$2', version = '$3'},
[], [{{'$2','$3'}}]}]).
%%%===================================================================
%%% Internal functions
%%%===================================================================