2003-08-03 21:57:08 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_privacy.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-08-03 21:57:08 +02:00
|
|
|
%%% Purpose : jabber:iq:privacy support
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 21 Jul 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 ProcessOne
|
2007-12-24 13:58:05 +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.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-08-03 21:57:08 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%%% Database schema (version / storage / table)
|
|
|
|
%%%
|
|
|
|
%%% 2.1.x / mnesia / privacy
|
|
|
|
%%% us = {Username::string(), Host::string()}
|
|
|
|
%%% default = none | ListName::string()
|
|
|
|
%%% lists = [ {ListName::string(), [listitem()]} ]
|
|
|
|
%%%
|
|
|
|
%%% 2.1.x / odbc / privacy_default_list
|
|
|
|
%%% username = varchar250
|
|
|
|
%%% name = varchar250
|
|
|
|
%%% 2.1.x / odbc / privacy_list
|
|
|
|
%%% username = varchar250
|
|
|
|
%%% name = varchar250
|
|
|
|
%%% id = bigint-unsigned
|
|
|
|
%%% 2.1.x / odbc / privacy_list_data
|
|
|
|
%%% id = bigint
|
|
|
|
%%% t = character(1)
|
|
|
|
%%% value = text
|
|
|
|
%%% action = character(1)
|
|
|
|
%%% ord = NUMERIC
|
|
|
|
%%% match_all = boolean
|
|
|
|
%%% match_iq = boolean
|
|
|
|
%%% match_message = boolean
|
|
|
|
%%% match_presence_in = boolean
|
|
|
|
%%% match_presence_out = boolean
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-prealpha / mnesia / privacy
|
|
|
|
%%% us = {Username::string(), Host::string()}
|
|
|
|
%%% default = none | ListName::string()
|
|
|
|
%%% lists = [ {ListName::string(), [listitem()]} ]
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-prealpha / odbc / privacy
|
|
|
|
%%% Same as 2.1.x
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-alpha / mnesia / privacy_default_list
|
|
|
|
%%% user_host = {Username::string(), Server::string()}
|
|
|
|
%%% name = string()
|
|
|
|
%%% 3.0.0-alpha / mnesia / privacy_list
|
|
|
|
%%% user_host = {Username::string(), Server::string()}
|
|
|
|
%%% name = string()
|
|
|
|
%%% 3.0.0-alpha / mnesia / privacy_list_data
|
|
|
|
%%% user_host = {Username::string(), Server::string()}
|
|
|
|
%%% name = string()
|
|
|
|
%%% type = jid | group | subscription | none
|
|
|
|
%%% value = JID::binary() | Group::binary() | <<"none">> | <<"both">> | <<"from">> | <<"to">>
|
|
|
|
%%% action = allow | deny
|
|
|
|
%%% order = integer()
|
|
|
|
%%% match_all = boolean()
|
|
|
|
%%% match_iq = boolean()
|
|
|
|
%%% match_message = boolean()
|
|
|
|
%%% match_presence_in = boolean()
|
|
|
|
%%% match_presence_out = boolean()
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-alpha / odbc / privacy_default_list
|
|
|
|
%%% user = varchar150
|
|
|
|
%%% host = varchar150
|
|
|
|
%%% name = text
|
|
|
|
%%% 3.0.0-alpha / odbc / privacy_list
|
|
|
|
%%% user = varchar150
|
|
|
|
%%% host = varchar150
|
|
|
|
%%% name = varchar150
|
|
|
|
%%% 3.0.0-alpha / odbc / privacy_list_data
|
|
|
|
%%% user = varchar150
|
|
|
|
%%% host = varchar150
|
|
|
|
%%% name = varchar150
|
|
|
|
%%% type = varchar150
|
|
|
|
%%% value = varchar150
|
|
|
|
%%% action = varchar150
|
|
|
|
%%% order = int 11
|
|
|
|
%%% match_all = varchar150
|
|
|
|
%%% match_iq = varchar150
|
|
|
|
%%% match_message = varchar150
|
|
|
|
%%% match_presence_in = varchar150
|
|
|
|
%%% match_presence_out = varchar150
|
|
|
|
|
2003-08-03 21:57:08 +02:00
|
|
|
-module(mod_privacy).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2, stop/1,
|
2003-08-03 21:57:08 +02:00
|
|
|
process_iq/3,
|
2006-10-05 05:17:41 +02:00
|
|
|
process_iq_set/4,
|
|
|
|
process_iq_get/5,
|
|
|
|
get_user_list/3,
|
2009-01-19 11:14:04 +01:00
|
|
|
remove_user/2,
|
2010-07-22 18:44:33 +02:00
|
|
|
check_packet/6,
|
2006-10-05 05:17:41 +02:00
|
|
|
updated_list/3]).
|
2003-08-03 21:57:08 +02:00
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-08-03 21:57:08 +02:00
|
|
|
-include("ejabberd.hrl").
|
2007-07-26 11:13:31 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2003-08-03 21:57:08 +02:00
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
-record(privacy_list, {user_host, name}).
|
|
|
|
-record(privacy_default_list, {user_host, name}).
|
|
|
|
-record(privacy_list_data, {user_host, name,
|
2010-07-22 18:44:33 +02:00
|
|
|
type, value, action, order,
|
|
|
|
match_all, match_iq, match_message,
|
|
|
|
match_presence_in, match_presence_out}).
|
2003-08-03 21:57:08 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2003-08-03 21:57:08 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2010-07-22 18:44:33 +02:00
|
|
|
Backend = gen_mod:get_opt(backend, Opts, mnesia),
|
|
|
|
gen_storage:create_table(Backend, HostB, privacy_list,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{odbc_host, Host},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, privacy_list)},
|
2010-07-22 18:49:12 +02:00
|
|
|
{types, [{user_host, {text, text}}]}]),
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:create_table(Backend, HostB, privacy_default_list,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{odbc_host, Host},
|
|
|
|
{attributes, record_info(fields, privacy_default_list)},
|
2010-07-22 18:49:12 +02:00
|
|
|
{types, [{user_host, {text, text}}]}]),
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:create_table(Backend, HostB, privacy_list_data,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{odbc_host, Host},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, privacy_list_data)},
|
2010-07-22 18:49:12 +02:00
|
|
|
{types, [{user_host, {text, text}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{type, atom},
|
2010-07-22 18:49:12 +02:00
|
|
|
{value, binary},
|
|
|
|
{action, atom},
|
2010-07-22 18:44:33 +02:00
|
|
|
{order, int},
|
|
|
|
{match_all, atom},
|
|
|
|
{match_iq, atom},
|
|
|
|
{match_message, atom},
|
|
|
|
{match_presence_in, atom},
|
|
|
|
{match_presence_out, atom}
|
|
|
|
]}]),
|
2010-07-22 18:49:12 +02:00
|
|
|
update_tables(Host, Backend),
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:add_table_index(HostB, privacy_list, name),
|
|
|
|
gen_storage:add_table_index(HostB, privacy_list_data, name),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(privacy_iq_get, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, process_iq_get, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(privacy_iq_set, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, process_iq_set, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(privacy_get_user_list, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, get_user_list, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(privacy_check_packet, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, check_packet, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(privacy_updated_list, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, updated_list, 50),
|
2009-01-19 11:14:04 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_PRIVACY,
|
2003-08-03 21:57:08 +02:00
|
|
|
?MODULE, process_iq, IQDisc).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(privacy_iq_get, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, process_iq_get, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(privacy_iq_set, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, process_iq_set, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(privacy_get_user_list, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, get_user_list, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(privacy_check_packet, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, check_packet, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(privacy_updated_list, HostB,
|
2006-10-05 05:17:41 +02:00
|
|
|
?MODULE, updated_list, 50),
|
2009-01-19 11:14:04 +01:00
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_PRIVACY).
|
2003-12-17 21:13:21 +01:00
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_iq(_From, _To, IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_iq_get(_, From, _To, #iq{payload = SubEl},
|
2003-08-03 21:57:08 +02:00
|
|
|
#userlist{name = Active}) ->
|
2009-06-01 18:40:51 +02:00
|
|
|
LUser = exmpp_jid:prep_node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(From),
|
2008-10-06 17:01:36 +02:00
|
|
|
case exmpp_xml:get_child_elements(SubEl) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_lists_get(LUser, LServer, Active);
|
2008-10-06 17:01:36 +02:00
|
|
|
[#xmlel{name = Name} = Child] ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case Name of
|
2008-10-06 17:01:36 +02:00
|
|
|
list ->
|
2010-11-29 20:44:31 +01:00
|
|
|
ListName = exmpp_xml:get_attribute_as_list(Child, <<"name">>, false),
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_get(LUser, LServer, ListName);
|
2003-08-03 21:57:08 +02:00
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'bad-request'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'bad-request'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_lists_get(LUser, LServer, Active) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
LServerB = list_to_binary(LServer),
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
|
|
|
Default =
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:read(LServerB, {privacy_default_list, {LUser, LServer}}) of
|
2010-07-22 18:44:33 +02:00
|
|
|
[#privacy_default_list{name = Name}] ->
|
|
|
|
Name;
|
|
|
|
_ ->
|
|
|
|
none
|
|
|
|
end,
|
|
|
|
Lists = [List#privacy_list.name
|
2010-10-07 00:14:28 +02:00
|
|
|
|| List <- gen_storage:read(LServerB, {privacy_list, {LUser, LServer}})],
|
2010-07-22 18:44:33 +02:00
|
|
|
{Default, Lists}
|
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_list, F) of
|
|
|
|
{aborted, _Reason} ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'internal-server-error'};
|
2010-07-22 18:44:33 +02:00
|
|
|
{atomic, {Default, Lists}} ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case Lists of
|
|
|
|
[] ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{result, #xmlel{ns = ?NS_PRIVACY, name = 'query'}};
|
2003-08-03 21:57:08 +02:00
|
|
|
_ ->
|
2010-11-29 20:44:31 +01:00
|
|
|
LItems = [exmpp_xml:set_attribute(#xmlel{ns = ?NS_PRIVACY, name = list}, <<"name">>, N) || N <- Lists],
|
2003-08-03 21:57:08 +02:00
|
|
|
DItems =
|
|
|
|
case Default of
|
|
|
|
none ->
|
|
|
|
LItems;
|
|
|
|
_ ->
|
2010-11-29 20:44:31 +01:00
|
|
|
[exmpp_xml:set_attribute(#xmlel{ns = ?NS_PRIVACY, name = default}, <<"name">>, Default) | LItems]
|
2003-08-03 21:57:08 +02:00
|
|
|
end,
|
|
|
|
ADItems =
|
|
|
|
case Active of
|
|
|
|
none ->
|
|
|
|
DItems;
|
|
|
|
_ ->
|
2010-11-29 20:44:31 +01:00
|
|
|
[exmpp_xml:set_attribute(#xmlel{ns = ?NS_PRIVACY, name = active}, <<"name">>, Active) | DItems]
|
2003-08-03 21:57:08 +02:00
|
|
|
end,
|
2008-10-06 17:01:36 +02:00
|
|
|
{result, #xmlel{ns = ?NS_PRIVACY, name = 'query', children = ADItems}}
|
2003-08-03 21:57:08 +02:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_list_get(_LUser, _LServer, false) ->
|
|
|
|
{error, 'bad-request'};
|
|
|
|
|
|
|
|
process_list_get(LUser, LServer, Name) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
LServerB = list_to_binary(LServer),
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:select(LServerB, privacy_list,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}]) of
|
|
|
|
[] ->
|
|
|
|
none;
|
|
|
|
[#privacy_list{}] ->
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:select(LServerB, privacy_list_data,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}])
|
|
|
|
end
|
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_list, F) of
|
2010-07-22 18:44:33 +02:00
|
|
|
{aborted, _Reason} ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'internal-server-error'};
|
2010-07-22 18:44:33 +02:00
|
|
|
{atomic, none} ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'item-not-found'};
|
2010-07-22 18:44:33 +02:00
|
|
|
{atomic, List} ->
|
|
|
|
LItems = lists:map(fun item_to_xml/1, List),
|
2010-11-29 20:44:31 +01:00
|
|
|
ListEl = exmpp_xml:set_attribute(#xmlel{ns = ?NS_PRIVACY, name = list, children = LItems}, <<"name">>, Name),
|
2010-07-22 18:44:33 +02:00
|
|
|
{result,#xmlel{ns = ?NS_PRIVACY, name = 'query', children = [ListEl]}}
|
2008-10-06 17:01:36 +02:00
|
|
|
end.
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
item_to_xml(Item) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Attrs1 = [?XMLATTR(<<"action">>, action_to_binary(Item#privacy_list_data.action)),
|
|
|
|
?XMLATTR(<<"order">>, order_to_binary(Item#privacy_list_data.order))],
|
2010-07-22 18:44:33 +02:00
|
|
|
Attrs2 = case Item#privacy_list_data.type of
|
2003-08-03 21:57:08 +02:00
|
|
|
none ->
|
|
|
|
Attrs1;
|
|
|
|
Type ->
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XMLATTR(<<"type">>, type_to_binary(Type)),
|
|
|
|
?XMLATTR(<<"value">>, Item#privacy_list_data.value) |
|
2003-08-03 21:57:08 +02:00
|
|
|
Attrs1]
|
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
SubEls = case Item#privacy_list_data.match_all of
|
2003-08-03 21:57:08 +02:00
|
|
|
true ->
|
|
|
|
[];
|
|
|
|
false ->
|
2010-07-22 18:44:33 +02:00
|
|
|
SE1 = case Item#privacy_list_data.match_iq of
|
2003-08-03 21:57:08 +02:00
|
|
|
true ->
|
2008-10-06 17:01:36 +02:00
|
|
|
[#xmlel{ns = ?NS_PRIVACY, name = iq}];
|
2003-08-03 21:57:08 +02:00
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
SE2 = case Item#privacy_list_data.match_message of
|
2003-08-03 21:57:08 +02:00
|
|
|
true ->
|
2008-10-06 17:01:36 +02:00
|
|
|
[#xmlel{ns = ?NS_PRIVACY, name = message} | SE1];
|
2003-08-03 21:57:08 +02:00
|
|
|
false ->
|
|
|
|
SE1
|
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
SE3 = case Item#privacy_list_data.match_presence_in of
|
2003-08-03 21:57:08 +02:00
|
|
|
true ->
|
2008-10-06 17:01:36 +02:00
|
|
|
[#xmlel{ns = ?NS_PRIVACY, name = 'presence-in'} | SE2];
|
2003-08-03 21:57:08 +02:00
|
|
|
false ->
|
|
|
|
SE2
|
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
SE4 = case Item#privacy_list_data.match_presence_out of
|
2003-08-03 21:57:08 +02:00
|
|
|
true ->
|
2008-10-06 17:01:36 +02:00
|
|
|
[#xmlel{ns = ?NS_PRIVACY, name = 'presence-out'} | SE3];
|
2003-08-03 21:57:08 +02:00
|
|
|
false ->
|
|
|
|
SE3
|
|
|
|
end,
|
|
|
|
SE4
|
|
|
|
end,
|
2008-10-06 17:01:36 +02:00
|
|
|
exmpp_xml:set_attributes(#xmlel{ns = ?NS_PRIVACY, name = item, children = SubEls}, Attrs2).
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
action_to_binary(Action) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case Action of
|
2009-01-08 15:54:00 +01:00
|
|
|
allow -> <<"allow">>;
|
|
|
|
deny -> <<"deny">>
|
2003-08-03 21:57:08 +02:00
|
|
|
end.
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
order_to_binary(Order) ->
|
|
|
|
list_to_binary(integer_to_list(Order)).
|
2003-08-03 21:57:08 +02:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
type_to_binary(Type) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case Type of
|
2009-01-08 15:54:00 +01:00
|
|
|
jid -> <<"jid">>;
|
|
|
|
group -> <<"group">>;
|
|
|
|
subscription -> <<"subscription">>
|
2003-08-03 21:57:08 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
list_to_action(S) ->
|
|
|
|
case S of
|
|
|
|
"allow" -> allow;
|
|
|
|
"deny" -> deny
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_iq_set(_, From, _To, #iq{payload = SubEl}) ->
|
2009-06-01 18:40:51 +02:00
|
|
|
LUser = exmpp_jid:prep_node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(From),
|
2008-10-06 17:01:36 +02:00
|
|
|
case exmpp_xml:get_child_elements(SubEl) of
|
|
|
|
[#xmlel{name = Name} = Child] ->
|
2010-11-29 20:44:31 +01:00
|
|
|
ListName = exmpp_xml:get_attribute_as_list(Child, <<"name">>, false),
|
2003-08-03 21:57:08 +02:00
|
|
|
case Name of
|
2008-10-06 17:01:36 +02:00
|
|
|
list ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_set(LUser, LServer, ListName,
|
2008-10-06 17:01:36 +02:00
|
|
|
exmpp_xml:get_child_elements(Child));
|
|
|
|
active ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_active_set(LUser, LServer, ListName);
|
2008-10-06 17:01:36 +02:00
|
|
|
default ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_default_set(LUser, LServer, ListName);
|
2003-08-03 21:57:08 +02:00
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'bad-request'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'bad-request'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_default_set(LUser, LServer, false) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
LServerB = list_to_binary(LServer),
|
2003-08-03 21:57:08 +02:00
|
|
|
F = fun() ->
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:delete(LServerB, {privacy_default_list, {LUser, LServer}})
|
2003-08-03 21:57:08 +02:00
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_default_list, F) of
|
2010-07-22 18:44:33 +02:00
|
|
|
{atomic, _} ->
|
|
|
|
{result, []};
|
2003-08-03 21:57:08 +02:00
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'internal-server-error'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end;
|
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_default_set(LUser, LServer, Name) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
LServerB = list_to_binary(LServer),
|
2003-08-03 21:57:08 +02:00
|
|
|
F = fun() ->
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:select(LServerB, privacy_list,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}]) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'item-not-found'};
|
2010-07-22 18:44:33 +02:00
|
|
|
[#privacy_list{}] ->
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:write(LServerB,
|
2010-07-22 18:49:12 +02:00
|
|
|
#privacy_default_list{user_host = {LUser, LServer},
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name}),
|
|
|
|
{result, []}
|
2003-08-03 21:57:08 +02:00
|
|
|
end
|
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_list, F) of
|
2003-08-03 21:57:08 +02:00
|
|
|
{atomic, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, {result, _} = Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'internal-server-error'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_active_set(_LUser, _LServer, false) ->
|
|
|
|
{result, [], #userlist{}};
|
|
|
|
|
|
|
|
process_active_set(LUser, LServer, Name) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
LServerB = list_to_binary(LServer),
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:select(LServerB, privacy_list,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}]) of
|
|
|
|
[#privacy_list{}] ->
|
2010-10-07 00:14:28 +02:00
|
|
|
Data = gen_storage:select(LServerB, privacy_list_data,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}]),
|
2010-09-17 21:06:30 +02:00
|
|
|
List = list_data_to_items(Data),
|
|
|
|
NeedDb = is_list_needdb(List),
|
2010-07-22 18:44:33 +02:00
|
|
|
{result, [], #userlist{name = Name,
|
2010-09-17 21:06:30 +02:00
|
|
|
needdb = NeedDb,
|
|
|
|
list = List}};
|
2010-07-22 18:44:33 +02:00
|
|
|
[] ->
|
|
|
|
{error, 'item-not-found'}
|
|
|
|
end
|
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_list, F) of
|
2010-07-22 18:44:33 +02:00
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
{error, 'internal-server-error'}
|
2008-10-06 17:01:36 +02:00
|
|
|
end.
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-06 17:01:36 +02:00
|
|
|
process_list_set(_LUser, _LServer, false, _Els) ->
|
|
|
|
{error, 'bad-request'};
|
|
|
|
|
|
|
|
process_list_set(LUser, LServer, Name, Els) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
LServerB = list_to_binary(LServer),
|
2003-08-03 21:57:08 +02:00
|
|
|
case parse_items(Els) of
|
|
|
|
false ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'bad-request'};
|
2003-08-03 21:57:08 +02:00
|
|
|
remove ->
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:read(LServerB,
|
2010-07-22 18:44:33 +02:00
|
|
|
{privacy_default_list, {LUser, LServer}}) of
|
|
|
|
[#privacy_default_list{name = Default}] when Name == Default ->
|
|
|
|
{error, 'conflict'};
|
|
|
|
_ ->
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:delete_where(LServerB, privacy_list,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}]),
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:delete_where(LServerB, privacy_list_data,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}]),
|
|
|
|
{result, []}
|
2003-08-03 21:57:08 +02:00
|
|
|
end
|
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_list, F) of
|
2003-08-03 21:57:08 +02:00
|
|
|
{atomic, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, {result, _} = Res} ->
|
2010-06-18 13:20:10 +02:00
|
|
|
ListString = lists:flatten(io_lib:format("~p.", [#userlist{name = Name, list = []}])),
|
2003-08-03 21:57:08 +02:00
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(LUser, LServer),
|
|
|
|
exmpp_jid:make(LUser, LServer),
|
2010-06-18 13:20:10 +02:00
|
|
|
#xmlel{name = 'broadcast', ns = privacy_list,
|
2010-11-29 20:44:31 +01:00
|
|
|
attrs = [?XMLATTR(<<"list_name">>, Name)],
|
2010-06-18 13:20:10 +02:00
|
|
|
children = [exmpp_xml:cdata(ListString)]}),
|
2003-08-03 21:57:08 +02:00
|
|
|
Res;
|
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'internal-server-error'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end;
|
|
|
|
List ->
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
|
|
|
OldData =
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:select(LServerB, privacy_list_data,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Name}]),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Data1) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:delete_object(LServerB, Data1)
|
2010-07-22 18:44:33 +02:00
|
|
|
end, OldData),
|
|
|
|
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:write(LServerB, #privacy_list{user_host = {LUser, LServer},
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name}),
|
|
|
|
NewData = list_items_to_data(LUser, LServer, Name, List),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Data1) ->
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:write(LServerB, Data1)
|
2010-07-22 18:44:33 +02:00
|
|
|
end, NewData),
|
|
|
|
{result, []}
|
2003-08-03 21:57:08 +02:00
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_list, F) of
|
2003-08-03 21:57:08 +02:00
|
|
|
{atomic, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, {result, _} = Res} ->
|
2010-06-18 13:20:10 +02:00
|
|
|
ListString = lists:flatten(io_lib:format("~p.", [#userlist{name = Name, list = List}])),
|
2003-08-03 21:57:08 +02:00
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(LUser, LServer),
|
|
|
|
exmpp_jid:make(LUser, LServer),
|
2010-06-18 13:20:10 +02:00
|
|
|
#xmlel{name = 'broadcast', ns = privacy_list,
|
2010-11-29 20:44:31 +01:00
|
|
|
attrs = [?XMLATTR(<<"list_name">>, Name)],
|
2010-06-18 13:20:10 +02:00
|
|
|
children = [exmpp_xml:cdata(ListString)]}),
|
2003-08-03 21:57:08 +02:00
|
|
|
Res;
|
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
{error, 'internal_server_error'}
|
2003-08-03 21:57:08 +02:00
|
|
|
end
|
2008-10-06 17:01:36 +02:00
|
|
|
end.
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parse_items([]) ->
|
|
|
|
remove;
|
|
|
|
parse_items(Els) ->
|
|
|
|
parse_items(Els, []).
|
|
|
|
|
|
|
|
parse_items([], Res) ->
|
2009-01-19 16:27:07 +01:00
|
|
|
%% Sort the items by their 'order' attribute
|
2010-07-22 18:44:33 +02:00
|
|
|
%% lists:keysort(#listitem.order, Res);
|
|
|
|
lists:reverse(Res);
|
2008-10-06 17:01:36 +02:00
|
|
|
parse_items([El = #xmlel{name = item} | Els], Res) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Type = exmpp_xml:get_attribute_as_list(El, <<"type">>, false),
|
|
|
|
Value = exmpp_xml:get_attribute_as_binary(El, <<"value">>, false),
|
|
|
|
SAction =exmpp_xml:get_attribute_as_list(El, <<"action">>, false),
|
|
|
|
SOrder = exmpp_xml:get_attribute_as_list(El, <<"order">>, false),
|
2008-10-06 17:01:36 +02:00
|
|
|
Action = case catch list_to_action(SAction) of
|
2003-08-03 21:57:08 +02:00
|
|
|
{'EXIT', _} -> false;
|
|
|
|
Val -> Val
|
|
|
|
end,
|
2008-10-06 17:01:36 +02:00
|
|
|
Order = case catch list_to_integer(SOrder) of
|
2003-08-03 21:57:08 +02:00
|
|
|
{'EXIT', _} ->
|
|
|
|
false;
|
|
|
|
IntVal ->
|
|
|
|
if
|
|
|
|
IntVal >= 0 ->
|
|
|
|
IntVal;
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
(Action /= false) and (Order /= false) ->
|
|
|
|
I1 = #listitem{action = Action, order = Order},
|
|
|
|
I2 = case {Type, Value} of
|
2010-07-22 18:49:12 +02:00
|
|
|
{T, V} when is_list(T), is_binary(V) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case T of
|
|
|
|
"jid" ->
|
2010-07-22 18:49:12 +02:00
|
|
|
I1#listitem{type = jid,
|
|
|
|
value = V};
|
2003-08-03 21:57:08 +02:00
|
|
|
"group" ->
|
|
|
|
I1#listitem{type = group,
|
|
|
|
value = V};
|
|
|
|
"subscription" ->
|
2010-07-22 18:49:12 +02:00
|
|
|
I1#listitem{type = subscription,
|
|
|
|
value = V}
|
2003-08-03 21:57:08 +02:00
|
|
|
end;
|
2008-10-06 17:01:36 +02:00
|
|
|
{T, false} when is_list(T) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
I1
|
|
|
|
end,
|
|
|
|
case I2 of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
_ ->
|
2008-10-06 17:01:36 +02:00
|
|
|
case parse_matches(I2, exmpp_xml:get_child_elements(El)) of
|
2003-08-03 21:57:08 +02:00
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
I3 ->
|
|
|
|
parse_items(Els, [I3 | Res])
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
parse_items(_, _Res) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
false.
|
|
|
|
|
|
|
|
|
|
|
|
parse_matches(Item, []) ->
|
|
|
|
Item#listitem{match_all = true};
|
|
|
|
parse_matches(Item, Els) ->
|
|
|
|
parse_matches1(Item, Els).
|
|
|
|
|
|
|
|
parse_matches1(Item, []) ->
|
|
|
|
Item;
|
2008-10-06 17:01:36 +02:00
|
|
|
parse_matches1(Item, [#xmlel{name = message} | Els]) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
parse_matches1(Item#listitem{match_message = true}, Els);
|
2008-10-06 17:01:36 +02:00
|
|
|
parse_matches1(Item, [#xmlel{name = iq} | Els]) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
parse_matches1(Item#listitem{match_iq = true}, Els);
|
2008-10-06 17:01:36 +02:00
|
|
|
parse_matches1(Item, [#xmlel{name = 'presence-in'} | Els]) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
parse_matches1(Item#listitem{match_presence_in = true}, Els);
|
2008-10-06 17:01:36 +02:00
|
|
|
parse_matches1(Item, [#xmlel{name = 'presence-out'} | Els]) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
parse_matches1(Item#listitem{match_presence_out = true}, Els);
|
2008-10-06 17:01:36 +02:00
|
|
|
parse_matches1(_Item, [#xmlel{} | _Els]) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
false.
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-22 18:44:33 +02:00
|
|
|
%% storage representation to ejabberd representation
|
|
|
|
list_data_to_items(Data) ->
|
|
|
|
List =
|
|
|
|
lists:map(
|
|
|
|
fun(Data1) ->
|
|
|
|
#listitem{type = Data1#privacy_list_data.type,
|
|
|
|
value = Data1#privacy_list_data.value,
|
|
|
|
action = Data1#privacy_list_data.action,
|
|
|
|
order = Data1#privacy_list_data.order,
|
|
|
|
match_all = Data1#privacy_list_data.match_all,
|
|
|
|
match_iq = Data1#privacy_list_data.match_iq,
|
|
|
|
match_message = Data1#privacy_list_data.match_message,
|
|
|
|
match_presence_in = Data1#privacy_list_data.match_presence_in,
|
|
|
|
match_presence_out = Data1#privacy_list_data.match_presence_out}
|
|
|
|
end, Data),
|
|
|
|
SortedList = lists:keysort(#listitem.order, List),
|
|
|
|
SortedList.
|
|
|
|
|
|
|
|
%% ejabberd representation to storage representation
|
|
|
|
list_items_to_data(LUser, LServer, Name, List) ->
|
|
|
|
lists:map(
|
|
|
|
fun(Item) ->
|
2010-07-22 18:49:12 +02:00
|
|
|
#privacy_list_data{user_host = {LUser, LServer},
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name,
|
|
|
|
type = Item#listitem.type,
|
|
|
|
value = Item#listitem.value,
|
|
|
|
action = Item#listitem.action,
|
|
|
|
order = Item#listitem.order,
|
|
|
|
match_all = Item#listitem.match_all,
|
|
|
|
match_iq = Item#listitem.match_iq,
|
|
|
|
match_message = Item#listitem.match_message,
|
|
|
|
match_presence_in = Item#listitem.match_presence_in,
|
|
|
|
match_presence_out = Item#listitem.match_presence_out}
|
|
|
|
end, List).
|
|
|
|
|
2009-03-03 20:26:24 +01:00
|
|
|
is_list_needdb(Items) ->
|
|
|
|
lists:any(
|
|
|
|
fun(X) ->
|
|
|
|
case X#listitem.type of
|
|
|
|
subscription -> true;
|
|
|
|
group -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end, Items).
|
|
|
|
|
2010-10-07 00:14:28 +02:00
|
|
|
get_user_list(_, User, LServerB)
|
|
|
|
when is_binary(User), is_binary(LServerB) ->
|
2010-07-22 18:44:33 +02:00
|
|
|
LUser = binary_to_list(User),
|
2010-10-07 00:14:28 +02:00
|
|
|
LServer = binary_to_list(LServerB),
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:read(LServerB,
|
2010-07-22 18:44:33 +02:00
|
|
|
{privacy_default_list, {LUser, LServer}}) of
|
|
|
|
[] ->
|
2008-10-06 17:01:36 +02:00
|
|
|
#userlist{};
|
2010-07-22 18:44:33 +02:00
|
|
|
[#privacy_default_list{name = Default}] ->
|
2010-10-07 00:14:28 +02:00
|
|
|
Data = gen_storage:select(LServerB, privacy_list_data,
|
2010-07-22 18:49:12 +02:00
|
|
|
[{'=', user_host, {LUser, LServer}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{'=', name, Default}]),
|
|
|
|
List = list_data_to_items(Data),
|
|
|
|
NeedDb = is_list_needdb(List),
|
|
|
|
#userlist{name = Default,
|
|
|
|
needdb = NeedDb,
|
|
|
|
list = List}
|
|
|
|
end
|
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
{atomic, Res} = gen_storage:transaction(LServerB, privacy_default_list, F),
|
2010-07-22 18:44:33 +02:00
|
|
|
Res.
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
2009-03-03 20:32:48 +01:00
|
|
|
%% From is the sender, To is the destination.
|
|
|
|
%% If Dir = out, User@Server is the sender account (From).
|
|
|
|
%% If Dir = in, User@Server is the destination account (To).
|
2006-10-05 05:17:41 +02:00
|
|
|
check_packet(_, User, Server,
|
2009-03-03 20:26:24 +01:00
|
|
|
#userlist{list = List, needdb = NeedDb},
|
2009-07-30 19:57:56 +02:00
|
|
|
{From, To, #xmlel{name = PName} = El},
|
2009-01-03 16:15:38 +01:00
|
|
|
Dir) when
|
|
|
|
PName =:= message ;
|
2008-10-06 17:01:36 +02:00
|
|
|
PName =:= iq ;
|
|
|
|
PName =:= presence ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case List of
|
|
|
|
[] ->
|
|
|
|
allow;
|
|
|
|
_ ->
|
2009-03-03 20:32:48 +01:00
|
|
|
PType = case PName of
|
|
|
|
'message' -> message;
|
|
|
|
'iq' -> iq;
|
|
|
|
'presence' ->
|
2010-11-29 20:44:31 +01:00
|
|
|
case exmpp_xml:get_attribute(El, <<"type">>, '') of
|
2009-03-03 20:32:48 +01:00
|
|
|
%% notification
|
|
|
|
'' -> presence;
|
|
|
|
'unavailable' -> presence;
|
|
|
|
%% subscribe, subscribed, unsubscribe,
|
|
|
|
%% unsubscribed, error, probe, or other
|
|
|
|
_ -> other
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
PType2 = case {PType, Dir} of
|
|
|
|
{message, in} -> message;
|
|
|
|
{iq, in} -> iq;
|
|
|
|
{presence, in} -> presence_in;
|
|
|
|
{presence, out} -> presence_out;
|
|
|
|
{_, _} -> other
|
|
|
|
end,
|
|
|
|
LJID = case Dir of
|
|
|
|
in -> From;
|
|
|
|
out -> To
|
|
|
|
end,
|
|
|
|
{Subscription, Groups} =
|
|
|
|
case NeedDb of
|
|
|
|
true -> ejabberd_hooks:run_fold(roster_get_jid_info,
|
|
|
|
exmpp_stringprep:nameprep(Server),
|
|
|
|
{none, []},
|
|
|
|
[User, Server, LJID]);
|
|
|
|
false -> {[], []}
|
|
|
|
end,
|
|
|
|
check_packet_aux(List, PType2, LJID, Subscription, Groups)
|
2003-08-03 21:57:08 +02:00
|
|
|
end.
|
|
|
|
|
2009-03-03 20:32:48 +01:00
|
|
|
%% Ptype = mesage | iq | presence_in | presence_out | other
|
2003-12-17 21:13:21 +01:00
|
|
|
check_packet_aux([], _PType, _JID, _Subscription, _Groups) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
allow;
|
|
|
|
check_packet_aux([Item | List], PType, JID, Subscription, Groups) ->
|
|
|
|
#listitem{type = Type, value = Value, action = Action} = Item,
|
2005-06-20 05:18:13 +02:00
|
|
|
case is_ptype_match(Item, PType) of
|
|
|
|
true ->
|
|
|
|
case Type of
|
|
|
|
none ->
|
|
|
|
Action;
|
|
|
|
_ ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case is_type_match(Type, Value,
|
|
|
|
JID, Subscription, Groups) of
|
|
|
|
true ->
|
|
|
|
Action;
|
|
|
|
false ->
|
|
|
|
check_packet_aux(List, PType,
|
|
|
|
JID, Subscription, Groups)
|
2005-06-20 05:18:13 +02:00
|
|
|
end
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
check_packet_aux(List, PType, JID, Subscription, Groups)
|
2003-08-03 21:57:08 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
is_ptype_match(Item, PType) ->
|
|
|
|
case Item#listitem.match_all of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
false ->
|
|
|
|
case PType of
|
|
|
|
message ->
|
|
|
|
Item#listitem.match_message;
|
|
|
|
iq ->
|
|
|
|
Item#listitem.match_iq;
|
|
|
|
presence_in ->
|
|
|
|
Item#listitem.match_presence_in;
|
|
|
|
presence_out ->
|
2009-03-03 20:32:48 +01:00
|
|
|
Item#listitem.match_presence_out;
|
|
|
|
other ->
|
|
|
|
false
|
2003-08-03 21:57:08 +02:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2009-03-07 09:53:34 +01:00
|
|
|
%% TODO: Investigate this: sometimes Value has binaries, other times has strings
|
2010-07-22 18:49:12 +02:00
|
|
|
is_type_match(jid, Value, JID, _Subscription, _Groups) ->
|
|
|
|
{User, Server, Resource} = exmpp_jid:to_lower(exmpp_jid:parse(Value)),
|
2010-10-07 00:14:28 +02:00
|
|
|
((User == undefined) orelse (User == exmpp_jid:prep_node(JID)))
|
|
|
|
andalso ((Server == undefined) orelse (Server == exmpp_jid:prep_domain(JID)))
|
|
|
|
andalso ((Resource == undefined) orelse (Resource == exmpp_jid:prep_resource(JID)));
|
2010-07-22 18:49:12 +02:00
|
|
|
is_type_match(subscription, Value, _JID, Subscription, _Groups) ->
|
|
|
|
Value == Subscription;
|
|
|
|
is_type_match(group, Value, _JID, _Subscription, Groups) ->
|
|
|
|
lists:member(Value, Groups).
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
2010-11-03 13:24:40 +01:00
|
|
|
%% The ejabberd hook provides the arguments as binaries,
|
|
|
|
%% but the mod_privacy internal functions provide them as strings.
|
|
|
|
%% Once this module stores information as binaries, this incoherence will be solved.
|
|
|
|
remove_user(User, Server) when is_binary(User) and is_binary(Server) ->
|
|
|
|
remove_user(binary_to_list(User), binary_to_list(Server));
|
|
|
|
remove_user(User, Server) when is_list(User) and is_list(Server) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2010-10-07 00:14:28 +02:00
|
|
|
LServerB = list_to_binary(LServer),
|
2009-01-19 11:14:04 +01:00
|
|
|
F = fun() ->
|
2010-10-07 00:14:28 +02:00
|
|
|
gen_storage:delete(LServerB, {privacy_list, {LUser, LServer}})
|
2010-07-22 18:44:33 +02:00
|
|
|
end,
|
2010-10-07 00:14:28 +02:00
|
|
|
case gen_storage:transaction(LServerB, privacy_list, F) of
|
2010-07-22 18:44:33 +02:00
|
|
|
{atomic, _} ->
|
|
|
|
{result, []};
|
|
|
|
_ ->
|
|
|
|
{error, 'internal-server-error'}
|
|
|
|
end.
|
2009-01-19 11:14:04 +01:00
|
|
|
|
|
|
|
|
2006-10-05 05:17:41 +02:00
|
|
|
updated_list(_,
|
|
|
|
#userlist{name = OldName} = Old,
|
2003-08-03 21:57:08 +02:00
|
|
|
#userlist{name = NewName} = New) ->
|
|
|
|
if
|
|
|
|
OldName == NewName ->
|
|
|
|
New;
|
|
|
|
true ->
|
|
|
|
Old
|
|
|
|
end.
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2003-08-03 21:57:08 +02:00
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
update_tables(Host, mnesia) ->
|
2010-08-13 17:01:08 +02:00
|
|
|
HostB = list_to_binary(Host),
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage_migration:migrate_mnesia(
|
|
|
|
Host, privacy_default_list,
|
|
|
|
[{privacy, [us, default, lists],
|
|
|
|
fun({privacy, US, Default, Lists}) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Name, List}) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(#listitem{type = Type,
|
|
|
|
value = Value,
|
|
|
|
action = Action,
|
|
|
|
order = Order,
|
|
|
|
match_all = MatchAll,
|
|
|
|
match_iq = MatchIq,
|
|
|
|
match_message = MatchMessage,
|
|
|
|
match_presence_in = MatchPresenceIn,
|
|
|
|
match_presence_out = MatchPresenceOut}) ->
|
2010-07-22 18:49:12 +02:00
|
|
|
ValueBin = convert_value_to_binary(Value),
|
2010-08-13 17:01:08 +02:00
|
|
|
gen_storage:write(HostB,
|
2010-07-22 18:49:12 +02:00
|
|
|
#privacy_list_data{user_host = US,
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name,
|
|
|
|
type = Type,
|
2010-07-22 18:49:12 +02:00
|
|
|
value = ValueBin,
|
2010-07-22 18:44:33 +02:00
|
|
|
action = Action,
|
|
|
|
order = Order,
|
|
|
|
match_all = MatchAll,
|
|
|
|
match_iq = MatchIq,
|
|
|
|
match_message = MatchMessage,
|
|
|
|
match_presence_in = MatchPresenceIn,
|
|
|
|
match_presence_out = MatchPresenceOut})
|
|
|
|
end, List),
|
2010-08-13 17:01:08 +02:00
|
|
|
gen_storage:write(HostB,
|
2010-07-22 18:49:12 +02:00
|
|
|
#privacy_list{user_host = US,
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name})
|
|
|
|
end, Lists),
|
|
|
|
if
|
|
|
|
is_list(Default) ->
|
2010-07-22 18:49:12 +02:00
|
|
|
#privacy_default_list{user_host = US,
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Default};
|
|
|
|
true -> null
|
|
|
|
end
|
2010-07-22 18:49:12 +02:00
|
|
|
end}]);
|
|
|
|
|
|
|
|
update_tables(Host, odbc) ->
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage_migration:migrate_odbc(
|
|
|
|
Host, [privacy_default_list, privacy_list, privacy_list_data],
|
|
|
|
[{[{"privacy_list", ["username", "name", "id"]},
|
2010-07-22 18:49:12 +02:00
|
|
|
{"privacy_list_data", ["id","t","value","action","ord","match_all","match_iq","match_message",
|
|
|
|
"match_presence_in","match_presence_out"]}],
|
2010-07-22 18:44:33 +02:00
|
|
|
fun(SELECT, Username, Name, Id) ->
|
|
|
|
US = {Username, Host},
|
2010-07-22 18:49:12 +02:00
|
|
|
DefaultLists = [#privacy_default_list{user_host = US,
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name}
|
|
|
|
|| [_, _] <- SELECT(["username", "name"],
|
|
|
|
"privacy_default_list",
|
|
|
|
[{"username", Username},
|
|
|
|
{"name", Name}])],
|
|
|
|
ListData = lists:map(
|
|
|
|
fun([SType, SValue, SAction, SOrder, SMatchAll,
|
|
|
|
SMatchIQ, SMatchMessage, SMatchPresenceIn,
|
|
|
|
SMatchPresenceOut]) ->
|
|
|
|
{Type, Value} =
|
|
|
|
case SType of
|
|
|
|
"n" ->
|
|
|
|
{none, none};
|
|
|
|
"j" ->
|
|
|
|
case exmpp_jid:parse(SValue) of
|
|
|
|
JID ->
|
|
|
|
{jid, jlib:short_prepd_jid(JID)}
|
|
|
|
end;
|
|
|
|
"g" ->
|
|
|
|
{group, SValue};
|
|
|
|
"s" ->
|
|
|
|
case SValue of
|
|
|
|
"none" ->
|
|
|
|
{subscription, none};
|
|
|
|
"both" ->
|
|
|
|
{subscription, both};
|
|
|
|
"from" ->
|
|
|
|
{subscription, from};
|
|
|
|
"to" ->
|
|
|
|
{subscription, to}
|
|
|
|
end
|
|
|
|
end,
|
2010-07-22 18:49:12 +02:00
|
|
|
ValueBin = convert_value_to_binary(Value),
|
2010-07-22 18:44:33 +02:00
|
|
|
Action =
|
|
|
|
case SAction of
|
|
|
|
"a" -> allow;
|
|
|
|
"d" -> deny
|
|
|
|
end,
|
|
|
|
Order = list_to_integer(SOrder),
|
2010-07-22 18:49:12 +02:00
|
|
|
MatchAll = ejabberd_odbc:to_bool(SMatchAll),
|
|
|
|
MatchIQ = ejabberd_odbc:to_bool(SMatchIQ),
|
|
|
|
MatchMessage = ejabberd_odbc:to_bool(SMatchMessage),
|
|
|
|
MatchPresenceIn = ejabberd_odbc:to_bool(SMatchPresenceIn),
|
|
|
|
MatchPresenceOut = ejabberd_odbc:to_bool(SMatchPresenceOut),
|
|
|
|
#privacy_list_data{user_host = US,
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name,
|
|
|
|
type = Type,
|
2010-07-22 18:49:12 +02:00
|
|
|
value = ValueBin,
|
2010-07-22 18:44:33 +02:00
|
|
|
action = Action,
|
|
|
|
order = Order,
|
|
|
|
match_all = MatchAll,
|
|
|
|
match_iq = MatchIQ,
|
|
|
|
match_message = MatchMessage,
|
|
|
|
match_presence_in = MatchPresenceIn,
|
|
|
|
match_presence_out = MatchPresenceOut}
|
|
|
|
end, SELECT(["t", "value", "action", "ord", "match_all",
|
|
|
|
"match_iq", "match_message", "match_presence_in",
|
|
|
|
"match_presence_out"],
|
|
|
|
"privacy_list_data",
|
|
|
|
[{"id", Id}])),
|
2010-07-22 18:49:12 +02:00
|
|
|
[#privacy_list{user_host = US,
|
2010-07-22 18:44:33 +02:00
|
|
|
name = Name} | DefaultLists ++ ListData]
|
2010-07-22 18:49:12 +02:00
|
|
|
end},
|
2010-07-22 18:44:33 +02:00
|
|
|
{"privacy_default_list", ["username", "name"],
|
2010-07-22 18:49:12 +02:00
|
|
|
fun(_, Username, Name) ->
|
|
|
|
US = {Username, Host},
|
|
|
|
[#privacy_default_list{user_host = US,
|
|
|
|
name = Name}]
|
2010-07-22 18:44:33 +02:00
|
|
|
end}
|
|
|
|
]).
|
2003-08-03 21:57:08 +02:00
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
convert_value_to_binary({U, H, R}) ->
|
|
|
|
exmpp_jid:to_binary(U, H, R);
|
|
|
|
convert_value_to_binary(Value) when is_list(Value) ->
|
|
|
|
list_to_binary(Value);
|
|
|
|
convert_value_to_binary(none) ->
|
|
|
|
<<"none">>;
|
|
|
|
convert_value_to_binary(both) ->
|
|
|
|
<<"both">>;
|
|
|
|
convert_value_to_binary(from) ->
|
|
|
|
<<"from">>;
|
|
|
|
convert_value_to_binary(to) ->
|
|
|
|
<<"to">>.
|