2013-03-27 17:53:56 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_carboncopy.erl
|
|
|
|
%%% Author : Eric Cestari <ecestari@process-one.net>
|
|
|
|
%%% Purpose : Message Carbons XEP-0280 0.8
|
|
|
|
%%% Created : 5 May 2008 by Mickael Remond <mremond@process-one.net>
|
2013-08-12 14:25:05 +02:00
|
|
|
%%% Usage : Add the following line in modules section of ejabberd.yml:
|
2013-03-27 17:53:56 +01:00
|
|
|
%%% {mod_carboncopy, []}
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2013-03-27 17:53:56 +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.
|
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2013-03-27 17:53:56 +01:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
-module (mod_carboncopy).
|
2015-10-07 00:06:58 +02:00
|
|
|
|
2013-03-27 17:53:56 +01:00
|
|
|
-author ('ecestari@process-one.net').
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 280, '0.8'}).
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
-behavior(gen_mod).
|
|
|
|
|
|
|
|
%% API:
|
|
|
|
-export([start/2,
|
|
|
|
stop/1]).
|
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
-export([user_send_packet/4, user_receive_packet/5,
|
2015-06-01 14:38:27 +02:00
|
|
|
iq_handler2/3, iq_handler1/3, remove_connection/4,
|
|
|
|
is_carbon_copy/1, mod_opt_type/1]).
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-27 17:53:56 +01:00
|
|
|
-include("jlib.hrl").
|
|
|
|
-define(PROCNAME, ?MODULE).
|
|
|
|
-define(TABLE, carboncopy).
|
|
|
|
|
|
|
|
-type matchspec_atom() :: '_' | '$1' | '$2' | '$3'.
|
|
|
|
-record(carboncopy,{us :: {binary(), binary()} | matchspec_atom(),
|
|
|
|
resource :: binary() | matchspec_atom(),
|
2013-06-24 05:15:07 +02:00
|
|
|
version :: binary() | matchspec_atom()}).
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
is_carbon_copy(Packet) ->
|
2014-05-30 23:36:02 +02:00
|
|
|
is_carbon_copy(Packet, <<"sent">>) orelse
|
|
|
|
is_carbon_copy(Packet, <<"received">>).
|
|
|
|
|
|
|
|
is_carbon_copy(Packet, Direction) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag(Packet, Direction) of
|
2014-05-30 23:36:02 +02:00
|
|
|
#xmlel{name = Direction, attrs = Attrs} ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2014-07-20 20:20:44 +02:00
|
|
|
?NS_CARBONS_2 -> true;
|
|
|
|
?NS_CARBONS_1 -> true;
|
2013-03-27 17:53:56 +01:00
|
|
|
_ -> false
|
2014-05-30 23:36:02 +02:00
|
|
|
end;
|
|
|
|
_ -> false
|
|
|
|
end.
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
start(Host, Opts) ->
|
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts,fun gen_iq_handler:check_type/1, one_queue),
|
2014-07-20 20:20:44 +02:00
|
|
|
mod_disco:register_feature(Host, ?NS_CARBONS_1),
|
|
|
|
mod_disco:register_feature(Host, ?NS_CARBONS_2),
|
2013-03-27 17:53:56 +01:00
|
|
|
Fields = record_info(fields, ?TABLE),
|
|
|
|
try mnesia:table_info(?TABLE, attributes) of
|
|
|
|
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),
|
|
|
|
%% 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_receive_packet,Host, ?MODULE, user_receive_packet, 89),
|
2014-07-20 20:20:44 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_CARBONS_2, ?MODULE, iq_handler2, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_CARBONS_1, ?MODULE, iq_handler1, IQDisc).
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
2014-07-20 20:20:44 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_CARBONS_1),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_CARBONS_2),
|
|
|
|
mod_disco:unregister_feature(Host, ?NS_CARBONS_2),
|
|
|
|
mod_disco:unregister_feature(Host, ?NS_CARBONS_1),
|
2013-03-27 17:53:56 +01:00
|
|
|
%% why priority 89: to define clearly that we must run BEFORE mod_logdb hook (90)
|
|
|
|
ejabberd_hooks:delete(user_send_packet,Host, ?MODULE, user_send_packet, 89),
|
|
|
|
ejabberd_hooks:delete(user_receive_packet,Host, ?MODULE, user_receive_packet, 89),
|
|
|
|
ejabberd_hooks:delete(unset_presence_hook,Host, ?MODULE, remove_connection, 10).
|
|
|
|
|
|
|
|
iq_handler2(From, To, IQ) ->
|
2014-07-20 20:20:44 +02:00
|
|
|
iq_handler(From, To, IQ, ?NS_CARBONS_2).
|
2013-03-27 17:53:56 +01:00
|
|
|
iq_handler1(From, To, IQ) ->
|
2014-07-20 20:20:44 +02:00
|
|
|
iq_handler(From, To, IQ, ?NS_CARBONS_1).
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
iq_handler(From, _To, #iq{type=set, sub_el = #xmlel{name = Operation, children = []}} = IQ, CC)->
|
2014-05-29 15:21:11 +02:00
|
|
|
?DEBUG("carbons IQ received: ~p", [IQ]),
|
2015-11-24 16:44:13 +01:00
|
|
|
{U, S, R} = jid:tolower(From),
|
2013-03-27 17:53:56 +01:00
|
|
|
Result = case Operation of
|
|
|
|
<<"enable">>->
|
|
|
|
?INFO_MSG("carbons enabled for user ~s@~s/~s", [U,S,R]),
|
|
|
|
enable(S,U,R,CC);
|
|
|
|
<<"disable">>->
|
|
|
|
?INFO_MSG("carbons disabled for user ~s@~s/~s", [U,S,R]),
|
|
|
|
disable(S, U, R)
|
|
|
|
end,
|
2015-10-07 00:06:58 +02:00
|
|
|
case Result of
|
2013-03-27 17:53:56 +01:00
|
|
|
ok ->
|
2014-05-29 15:21:11 +02:00
|
|
|
?DEBUG("carbons IQ result: ok", []),
|
2013-03-27 17:53:56 +01:00
|
|
|
IQ#iq{type=result, sub_el=[]};
|
|
|
|
{error,_Error} ->
|
2014-05-29 15:21:11 +02:00
|
|
|
?WARNING_MSG("Error enabling / disabling carbons: ~p", [Result]),
|
2013-03-27 17:53:56 +01:00
|
|
|
IQ#iq{type=error,sub_el = [?ERR_BAD_REQUEST]}
|
|
|
|
end;
|
|
|
|
|
|
|
|
iq_handler(_From, _To, IQ, _CC)->
|
|
|
|
IQ#iq{type=error, sub_el = [?ERR_NOT_ALLOWED]}.
|
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
user_send_packet(Packet, _C2SState, From, To) ->
|
2014-04-07 22:10:08 +02:00
|
|
|
check_and_forward(From, To, Packet, sent).
|
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
user_receive_packet(Packet, _C2SState, JID, _From, To) ->
|
2014-04-07 22:10:08 +02:00
|
|
|
check_and_forward(JID, To, Packet, received).
|
2015-10-07 00:06:58 +02:00
|
|
|
|
|
|
|
% Modified from original version:
|
2013-03-27 17:53:56 +01:00
|
|
|
% - registered to the user_send_packet hook, to be called only once even for multicast
|
|
|
|
% - do not support "private" message mode, and do not modify the original packet in any way
|
|
|
|
% - we also replicate "read" notifications
|
2014-11-24 22:37:14 +01:00
|
|
|
check_and_forward(JID, To, Packet, Direction)->
|
2015-07-28 21:34:02 +02:00
|
|
|
case is_chat_message(Packet) andalso
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:get_subtag(Packet, <<"private">>) == false andalso
|
|
|
|
fxml:get_subtag(Packet, <<"no-copy">>) == false of
|
2014-05-30 23:44:19 +02:00
|
|
|
true ->
|
|
|
|
case is_carbon_copy(Packet) of
|
|
|
|
false ->
|
2015-06-22 15:56:08 +02:00
|
|
|
send_copies(JID, To, Packet, Direction),
|
|
|
|
Packet;
|
2014-05-30 23:44:19 +02:00
|
|
|
true ->
|
2015-10-07 00:06:58 +02:00
|
|
|
%% stop the hook chain, we don't want logging modules to duplicates
|
|
|
|
%% this message
|
2015-06-22 15:56:08 +02:00
|
|
|
{stop, Packet}
|
2014-05-30 23:44:19 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2015-06-22 15:56:08 +02:00
|
|
|
Packet
|
2015-02-11 00:52:47 +01:00
|
|
|
end.
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
remove_connection(User, Server, Resource, _Status)->
|
|
|
|
disable(Server, User, Resource),
|
|
|
|
ok.
|
2015-10-07 00:06:58 +02:00
|
|
|
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
%%% Internal
|
|
|
|
%% Direction = received | sent <received xmlns='urn:xmpp:carbons:1'/>
|
2014-04-07 22:10:08 +02:00
|
|
|
send_copies(JID, To, Packet, Direction)->
|
2015-11-24 16:44:13 +01:00
|
|
|
{U, S, R} = jid:tolower(JID),
|
2014-04-08 23:32:30 +02:00
|
|
|
PrioRes = ejabberd_sm:get_user_present_resources(U, S),
|
2015-02-11 16:12:08 +01:00
|
|
|
{_, AvailRs} = lists:unzip(PrioRes),
|
2014-11-05 19:04:02 +01:00
|
|
|
{MaxPrio, MaxRes} = case catch lists:max(PrioRes) of
|
|
|
|
{Prio, Res} -> {Prio, Res};
|
|
|
|
_ -> {0, undefined}
|
|
|
|
end,
|
2014-04-08 23:32:30 +02:00
|
|
|
|
2015-02-12 00:14:57 +01:00
|
|
|
%% unavailable resources are handled like bare JIDs
|
2014-04-08 23:32:30 +02:00
|
|
|
IsBareTo = case {Direction, To} of
|
|
|
|
{received, #jid{lresource = <<>>}} -> true;
|
2015-02-12 00:14:57 +01:00
|
|
|
{received, #jid{lresource = LRes}} -> not lists:member(LRes, AvailRs);
|
2014-04-08 23:32:30 +02:00
|
|
|
_ -> false
|
|
|
|
end,
|
2013-03-27 17:53:56 +01:00
|
|
|
%% list of JIDs that should receive a carbon copy of this message (excluding the
|
2014-04-07 22:10:08 +02:00
|
|
|
%% receiver(s) of the original message
|
2014-11-05 19:04:02 +01:00
|
|
|
TargetJIDs = case {IsBareTo, R} of
|
|
|
|
{true, MaxRes} ->
|
2014-04-07 22:10:08 +02:00
|
|
|
OrigTo = fun(Res) -> lists:member({MaxPrio, Res}, PrioRes) end,
|
2015-11-24 16:44:13 +01:00
|
|
|
[ {jid:make({U, S, CCRes}), CC_Version}
|
2015-02-11 16:12:08 +01:00
|
|
|
|| {CCRes, CC_Version} <- list(U, S),
|
|
|
|
lists:member(CCRes, AvailRs), not OrigTo(CCRes) ];
|
2014-11-05 19:04:02 +01:00
|
|
|
{true, _} ->
|
|
|
|
%% The message was sent to our bare JID, and we currently have
|
|
|
|
%% multiple resources with the same highest priority, so the session
|
|
|
|
%% manager routes the message to each of them. We create carbon
|
|
|
|
%% copies only from one of those resources (the one where R equals
|
|
|
|
%% MaxRes) in order to avoid duplicates.
|
|
|
|
[];
|
|
|
|
{false, _} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
[ {jid:make({U, S, CCRes}), CC_Version}
|
2015-02-11 16:12:08 +01:00
|
|
|
|| {CCRes, CC_Version} <- list(U, S),
|
|
|
|
lists:member(CCRes, AvailRs), CCRes /= R ]
|
2015-11-24 16:44:13 +01:00
|
|
|
%TargetJIDs = lists:delete(JID, [ jid:make({U, S, CCRes}) || CCRes <- list(U, S) ]),
|
2014-04-07 22:10:08 +02:00
|
|
|
end,
|
2013-03-27 17:53:56 +01:00
|
|
|
|
|
|
|
lists:map(fun({Dest,Version}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{_, _, Resource} = jid:tolower(Dest),
|
2013-03-27 17:53:56 +01:00
|
|
|
?DEBUG("Sending: ~p =/= ~p", [R, Resource]),
|
2015-11-24 16:44:13 +01:00
|
|
|
Sender = jid:make({U, S, <<>>}),
|
2013-03-27 17:53:56 +01:00
|
|
|
%{xmlelement, N, A, C} = Packet,
|
|
|
|
New = build_forward_packet(JID, Packet, Sender, Dest, Direction, Version),
|
|
|
|
ejabberd_router:route(Sender, Dest, New)
|
|
|
|
end, TargetJIDs),
|
|
|
|
ok.
|
|
|
|
|
2014-07-20 20:20:44 +02:00
|
|
|
build_forward_packet(JID, Packet, Sender, Dest, Direction, ?NS_CARBONS_2) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
#xmlel{name = <<"message">>,
|
2013-03-27 17:53:56 +01:00
|
|
|
attrs = [{<<"xmlns">>, <<"jabber:client">>},
|
2014-11-24 22:37:14 +01:00
|
|
|
{<<"type">>, message_type(Packet)},
|
2015-11-24 16:44:13 +01:00
|
|
|
{<<"from">>, jid:to_string(Sender)},
|
|
|
|
{<<"to">>, jid:to_string(Dest)}],
|
2015-10-07 00:06:58 +02:00
|
|
|
children = [
|
|
|
|
#xmlel{name = list_to_binary(atom_to_list(Direction)),
|
2014-07-20 20:20:44 +02:00
|
|
|
attrs = [{<<"xmlns">>, ?NS_CARBONS_2}],
|
2013-03-27 17:53:56 +01:00
|
|
|
children = [
|
2015-10-07 00:06:58 +02:00
|
|
|
#xmlel{name = <<"forwarded">>,
|
2013-03-27 17:53:56 +01:00
|
|
|
attrs = [{<<"xmlns">>, ?NS_FORWARD}],
|
|
|
|
children = [
|
|
|
|
complete_packet(JID, Packet, Direction)]}
|
|
|
|
]}
|
|
|
|
]};
|
2014-07-20 20:20:44 +02:00
|
|
|
build_forward_packet(JID, Packet, Sender, Dest, Direction, ?NS_CARBONS_1) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
#xmlel{name = <<"message">>,
|
2013-03-27 17:53:56 +01:00
|
|
|
attrs = [{<<"xmlns">>, <<"jabber:client">>},
|
2014-11-24 22:37:14 +01:00
|
|
|
{<<"type">>, message_type(Packet)},
|
2015-11-24 16:44:13 +01:00
|
|
|
{<<"from">>, jid:to_string(Sender)},
|
|
|
|
{<<"to">>, jid:to_string(Dest)}],
|
2015-10-07 00:06:58 +02:00
|
|
|
children = [
|
|
|
|
#xmlel{name = list_to_binary(atom_to_list(Direction)),
|
2014-07-20 20:20:44 +02:00
|
|
|
attrs = [{<<"xmlns">>, ?NS_CARBONS_1}]},
|
2015-10-07 00:06:58 +02:00
|
|
|
#xmlel{name = <<"forwarded">>,
|
2013-03-27 17:53:56 +01:00
|
|
|
attrs = [{<<"xmlns">>, ?NS_FORWARD}],
|
|
|
|
children = [complete_packet(JID, Packet, Direction)]}
|
|
|
|
]}.
|
|
|
|
|
|
|
|
|
|
|
|
enable(Host, U, R, CC)->
|
|
|
|
?DEBUG("enabling for ~p", [U]),
|
|
|
|
try mnesia:dirty_write(#carboncopy{us = {U, Host}, resource=R, version = CC}) of
|
|
|
|
ok -> ok
|
|
|
|
catch _:Error -> {error, Error}
|
|
|
|
end.
|
|
|
|
|
|
|
|
disable(Host, U, R)->
|
|
|
|
?DEBUG("disabling for ~p", [U]),
|
|
|
|
ToDelete = mnesia:dirty_match_object(?TABLE, #carboncopy{us = {U, Host}, resource = R, version = '_'}),
|
|
|
|
try lists:foreach(fun mnesia:dirty_delete_object/1, ToDelete) of
|
|
|
|
ok -> ok
|
|
|
|
catch _:Error -> {error, Error}
|
|
|
|
end.
|
|
|
|
|
|
|
|
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
|
|
|
|
%% include the 'from' attribute. We must add it.
|
|
|
|
Attrs = lists:keystore(<<"xmlns">>, 1, OrigAttrs, {<<"xmlns">>, <<"jabber:client">>}),
|
|
|
|
case proplists:get_value(<<"from">>, Attrs) of
|
|
|
|
undefined ->
|
2015-11-24 16:44:13 +01:00
|
|
|
Packet#xmlel{attrs = [{<<"from">>, jid:to_string(From)}|Attrs]};
|
2013-03-27 17:53:56 +01:00
|
|
|
_ ->
|
|
|
|
Packet#xmlel{attrs = Attrs}
|
|
|
|
end;
|
|
|
|
complete_packet(_From, #xmlel{name = <<"message">>, attrs=OrigAttrs} = Packet, received) ->
|
|
|
|
Attrs = lists:keystore(<<"xmlns">>, 1, OrigAttrs, {<<"xmlns">>, <<"jabber:client">>}),
|
|
|
|
Packet#xmlel{attrs = Attrs}.
|
|
|
|
|
2014-11-24 22:37:14 +01:00
|
|
|
message_type(#xmlel{attrs = Attrs}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr(<<"type">>, Attrs) of
|
2014-11-24 22:37:14 +01:00
|
|
|
{value, Type} -> Type;
|
|
|
|
false -> <<"normal">>
|
|
|
|
end.
|
|
|
|
|
2015-07-28 21:34:02 +02:00
|
|
|
is_chat_message(#xmlel{name = <<"message">>} = Packet) ->
|
2014-11-24 22:37:14 +01:00
|
|
|
case message_type(Packet) of
|
|
|
|
<<"chat">> -> true;
|
2015-07-28 21:34:02 +02:00
|
|
|
<<"normal">> -> has_non_empty_body(Packet);
|
2014-11-24 22:37:14 +01:00
|
|
|
_ -> false
|
|
|
|
end;
|
2015-07-28 21:34:02 +02:00
|
|
|
is_chat_message(_Packet) -> false.
|
|
|
|
|
|
|
|
has_non_empty_body(Packet) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:get_subtag_cdata(Packet, <<"body">>) =/= <<"">>.
|
2014-11-24 22:37:14 +01:00
|
|
|
|
2013-03-27 17:53:56 +01:00
|
|
|
%% list {resource, cc_version} with carbons enabled for given user and host
|
2015-10-07 00:06:58 +02:00
|
|
|
list(User, Server) ->
|
2013-03-27 17:53:56 +01:00
|
|
|
mnesia:dirty_select(?TABLE, [{#carboncopy{us = {User, Server}, resource = '$2', version = '$3'}, [], [{{'$2','$3'}}]}]).
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(_) -> [iqdisc].
|