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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-12 15:44:42 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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-12 15:44:42 +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
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-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,
|
|
|
|
check_packet/6,
|
2008-12-23 01:48:09 +01:00
|
|
|
remove_user/2,
|
2006-10-05 05:17:41 +02:00
|
|
|
updated_list/3]).
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("jlib.hrl").
|
2007-07-26 11:13:31 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
|
|
|
mnesia:create_table(privacy, [{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, privacy)}]),
|
2005-04-17 20:08:34 +02:00
|
|
|
update_table(),
|
2006-10-05 05:17:41 +02:00
|
|
|
ejabberd_hooks:add(privacy_iq_get, Host,
|
|
|
|
?MODULE, process_iq_get, 50),
|
|
|
|
ejabberd_hooks:add(privacy_iq_set, Host,
|
|
|
|
?MODULE, process_iq_set, 50),
|
|
|
|
ejabberd_hooks:add(privacy_get_user_list, Host,
|
|
|
|
?MODULE, get_user_list, 50),
|
|
|
|
ejabberd_hooks:add(privacy_check_packet, Host,
|
|
|
|
?MODULE, check_packet, 50),
|
|
|
|
ejabberd_hooks:add(privacy_updated_list, Host,
|
|
|
|
?MODULE, updated_list, 50),
|
2008-12-23 01:48:09 +01:00
|
|
|
ejabberd_hooks:add(remove_user, Host,
|
|
|
|
?MODULE, remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_PRIVACY,
|
2003-08-03 21:57:08 +02:00
|
|
|
?MODULE, process_iq, IQDisc).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2006-10-05 05:17:41 +02:00
|
|
|
ejabberd_hooks:delete(privacy_iq_get, Host,
|
|
|
|
?MODULE, process_iq_get, 50),
|
|
|
|
ejabberd_hooks:delete(privacy_iq_set, Host,
|
|
|
|
?MODULE, process_iq_set, 50),
|
|
|
|
ejabberd_hooks:delete(privacy_get_user_list, Host,
|
|
|
|
?MODULE, get_user_list, 50),
|
|
|
|
ejabberd_hooks:delete(privacy_check_packet, Host,
|
|
|
|
?MODULE, check_packet, 50),
|
|
|
|
ejabberd_hooks:delete(privacy_updated_list, Host,
|
|
|
|
?MODULE, updated_list, 50),
|
2008-12-23 01:48:09 +01:00
|
|
|
ejabberd_hooks:delete(remove_user, Host,
|
|
|
|
?MODULE, remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PRIVACY).
|
2003-12-17 21:13:21 +01:00
|
|
|
|
2006-10-05 05:17:41 +02:00
|
|
|
process_iq(_From, _To, IQ) ->
|
|
|
|
SubEl = IQ#iq.sub_el,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}.
|
2003-08-03 21:57:08 +02:00
|
|
|
|
|
|
|
|
2006-10-05 05:17:41 +02:00
|
|
|
process_iq_get(_, From, _To, #iq{sub_el = SubEl},
|
2003-08-03 21:57:08 +02:00
|
|
|
#userlist{name = Active}) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
2003-08-03 21:57:08 +02:00
|
|
|
{xmlelement, _, _, Els} = SubEl,
|
|
|
|
case xml:remove_cdata(Els) of
|
|
|
|
[] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_lists_get(LUser, LServer, Active);
|
2003-12-17 21:13:21 +01:00
|
|
|
[{xmlelement, Name, Attrs, _SubEls}] ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case Name of
|
|
|
|
"list" ->
|
|
|
|
ListName = xml:get_attr("name", Attrs),
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_get(LUser, LServer, ListName);
|
2003-08-03 21:57:08 +02:00
|
|
|
_ ->
|
|
|
|
{error, ?ERR_BAD_REQUEST}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_BAD_REQUEST}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_lists_get(LUser, LServer, Active) ->
|
|
|
|
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
|
2003-12-17 21:13:21 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2003-08-03 21:57:08 +02:00
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR};
|
|
|
|
[] ->
|
|
|
|
{result, [{xmlelement, "query", [{"xmlns", ?NS_PRIVACY}], []}]};
|
|
|
|
[#privacy{default = Default, lists = Lists}] ->
|
|
|
|
case Lists of
|
|
|
|
[] ->
|
|
|
|
{result, [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_PRIVACY}], []}]};
|
|
|
|
_ ->
|
|
|
|
LItems = lists:map(
|
|
|
|
fun({N, _}) ->
|
|
|
|
{xmlelement, "list",
|
|
|
|
[{"name", N}], []}
|
|
|
|
end, Lists),
|
|
|
|
DItems =
|
|
|
|
case Default of
|
|
|
|
none ->
|
|
|
|
LItems;
|
|
|
|
_ ->
|
|
|
|
[{xmlelement, "default",
|
|
|
|
[{"name", Default}], []} | LItems]
|
|
|
|
end,
|
|
|
|
ADItems =
|
|
|
|
case Active of
|
|
|
|
none ->
|
|
|
|
DItems;
|
|
|
|
_ ->
|
|
|
|
[{xmlelement, "active",
|
|
|
|
[{"name", Active}], []} | DItems]
|
|
|
|
end,
|
|
|
|
{result,
|
|
|
|
[{xmlelement, "query", [{"xmlns", ?NS_PRIVACY}],
|
|
|
|
ADItems}]}
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_get(LUser, LServer, {value, Name}) ->
|
|
|
|
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
|
2003-12-17 21:13:21 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2003-08-03 21:57:08 +02:00
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR};
|
|
|
|
[] ->
|
2003-10-07 22:31:44 +02:00
|
|
|
{error, ?ERR_ITEM_NOT_FOUND};
|
|
|
|
%{result, [{xmlelement, "query", [{"xmlns", ?NS_PRIVACY}], []}]};
|
2003-08-03 21:57:08 +02:00
|
|
|
[#privacy{lists = Lists}] ->
|
|
|
|
case lists:keysearch(Name, 1, Lists) of
|
|
|
|
{value, {_, List}} ->
|
|
|
|
LItems = lists:map(fun item_to_xml/1, List),
|
|
|
|
{result,
|
|
|
|
[{xmlelement, "query", [{"xmlns", ?NS_PRIVACY}],
|
|
|
|
[{xmlelement, "list",
|
|
|
|
[{"name", Name}], LItems}]}]};
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_get(_LUser, _LServer, false) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
{error, ?ERR_BAD_REQUEST}.
|
|
|
|
|
|
|
|
item_to_xml(Item) ->
|
|
|
|
Attrs1 = [{"action", action_to_list(Item#listitem.action)},
|
|
|
|
{"order", order_to_list(Item#listitem.order)}],
|
|
|
|
Attrs2 = case Item#listitem.type of
|
|
|
|
none ->
|
|
|
|
Attrs1;
|
|
|
|
Type ->
|
|
|
|
[{"type", type_to_list(Item#listitem.type)},
|
|
|
|
{"value", value_to_list(Type, Item#listitem.value)} |
|
|
|
|
Attrs1]
|
|
|
|
end,
|
|
|
|
SubEls = case Item#listitem.match_all of
|
|
|
|
true ->
|
|
|
|
[];
|
|
|
|
false ->
|
|
|
|
SE1 = case Item#listitem.match_iq of
|
|
|
|
true ->
|
|
|
|
[{xmlelement, "iq", [], []}];
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
SE2 = case Item#listitem.match_message of
|
|
|
|
true ->
|
|
|
|
[{xmlelement, "message", [], []} | SE1];
|
|
|
|
false ->
|
|
|
|
SE1
|
|
|
|
end,
|
|
|
|
SE3 = case Item#listitem.match_presence_in of
|
|
|
|
true ->
|
|
|
|
[{xmlelement, "presence-in", [], []} | SE2];
|
|
|
|
false ->
|
|
|
|
SE2
|
|
|
|
end,
|
|
|
|
SE4 = case Item#listitem.match_presence_out of
|
|
|
|
true ->
|
|
|
|
[{xmlelement, "presence-out", [], []} | SE3];
|
|
|
|
false ->
|
|
|
|
SE3
|
|
|
|
end,
|
|
|
|
SE4
|
|
|
|
end,
|
|
|
|
{xmlelement, "item", Attrs2, SubEls}.
|
|
|
|
|
|
|
|
|
|
|
|
action_to_list(Action) ->
|
|
|
|
case Action of
|
|
|
|
allow -> "allow";
|
|
|
|
deny -> "deny"
|
|
|
|
end.
|
|
|
|
|
|
|
|
order_to_list(Order) ->
|
|
|
|
integer_to_list(Order).
|
|
|
|
|
|
|
|
type_to_list(Type) ->
|
|
|
|
case Type of
|
|
|
|
jid -> "jid";
|
|
|
|
group -> "group";
|
|
|
|
subscription -> "subscription"
|
|
|
|
end.
|
|
|
|
|
|
|
|
value_to_list(Type, Val) ->
|
|
|
|
case Type of
|
|
|
|
jid -> jlib:jid_to_string(Val);
|
|
|
|
group -> Val;
|
|
|
|
subscription ->
|
|
|
|
case Val of
|
|
|
|
both -> "both";
|
|
|
|
to -> "to";
|
|
|
|
from -> "from";
|
|
|
|
none -> "none"
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
list_to_action(S) ->
|
|
|
|
case S of
|
|
|
|
"allow" -> allow;
|
|
|
|
"deny" -> deny
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-10-05 05:17:41 +02:00
|
|
|
process_iq_set(_, From, _To, #iq{sub_el = SubEl}) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
2003-08-03 21:57:08 +02:00
|
|
|
{xmlelement, _, _, Els} = SubEl,
|
|
|
|
case xml:remove_cdata(Els) of
|
|
|
|
[{xmlelement, Name, Attrs, SubEls}] ->
|
|
|
|
ListName = xml:get_attr("name", Attrs),
|
|
|
|
case Name of
|
|
|
|
"list" ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_set(LUser, LServer, ListName,
|
|
|
|
xml:remove_cdata(SubEls));
|
2003-08-03 21:57:08 +02:00
|
|
|
"active" ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_active_set(LUser, LServer, ListName);
|
2003-08-03 21:57:08 +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
|
|
|
_ ->
|
|
|
|
{error, ?ERR_BAD_REQUEST}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_BAD_REQUEST}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_default_set(LUser, LServer, {value, Name}) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:read({privacy, {LUser, LServer}}) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND};
|
|
|
|
[#privacy{lists = Lists} = P] ->
|
|
|
|
case lists:keymember(Name, 1, Lists) of
|
|
|
|
true ->
|
|
|
|
mnesia:write(P#privacy{default = Name,
|
|
|
|
lists = Lists}),
|
|
|
|
{result, []};
|
|
|
|
false ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, {result, _} = Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
end;
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_default_set(LUser, LServer, false) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:read({privacy, {LUser, LServer}}) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
|
|
|
{result, []};
|
|
|
|
[R] ->
|
|
|
|
mnesia:write(R#privacy{default = none}),
|
|
|
|
{result, []}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, {result, _} = Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_active_set(LUser, LServer, {value, Name}) ->
|
|
|
|
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND};
|
2003-12-17 21:13:21 +01:00
|
|
|
[#privacy{lists = Lists}] ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case lists:keysearch(Name, 1, Lists) of
|
|
|
|
{value, {_, List}} ->
|
2009-01-20 18:34:58 +01:00
|
|
|
NeedDb = is_list_needdb(List),
|
|
|
|
{result, [], #userlist{name = Name, list = List, needdb = NeedDb}};
|
2003-08-03 21:57:08 +02:00
|
|
|
false ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_active_set(_LUser, _LServer, false) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
{result, [], #userlist{}}.
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_set(LUser, LServer, {value, Name}, Els) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
case parse_items(Els) of
|
|
|
|
false ->
|
|
|
|
{error, ?ERR_BAD_REQUEST};
|
|
|
|
remove ->
|
|
|
|
F =
|
|
|
|
fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:read({privacy, {LUser, LServer}}) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
|
|
|
{result, []};
|
|
|
|
[#privacy{default = Default, lists = Lists} = P] ->
|
|
|
|
% TODO: check active
|
|
|
|
if
|
|
|
|
Name == Default ->
|
|
|
|
{error, ?ERR_CONFLICT};
|
|
|
|
true ->
|
|
|
|
NewLists =
|
|
|
|
lists:keydelete(Name, 1, Lists),
|
|
|
|
mnesia:write(
|
|
|
|
P#privacy{lists = NewLists}),
|
|
|
|
{result, []}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, {result, _} = Res} ->
|
|
|
|
ejabberd_router:route(
|
2006-10-09 22:32:41 +02:00
|
|
|
jlib:make_jid(LUser, LServer, ""),
|
|
|
|
jlib:make_jid(LUser, LServer, ""),
|
2003-08-03 21:57:08 +02:00
|
|
|
{xmlelement, "broadcast", [],
|
2006-03-17 05:35:03 +01:00
|
|
|
[{privacy_list,
|
|
|
|
#userlist{name = Name, list = []},
|
|
|
|
Name}]}),
|
2003-08-03 21:57:08 +02:00
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
end;
|
|
|
|
List ->
|
|
|
|
F =
|
|
|
|
fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:wread({privacy, {LUser, LServer}}) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
|
|
|
NewLists = [{Name, List}],
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:write(#privacy{us = {LUser, LServer},
|
2003-08-03 21:57:08 +02:00
|
|
|
lists = NewLists}),
|
|
|
|
{result, []};
|
|
|
|
[#privacy{lists = Lists} = P] ->
|
|
|
|
NewLists1 = lists:keydelete(Name, 1, Lists),
|
|
|
|
NewLists = [{Name, List} | NewLists1],
|
|
|
|
mnesia:write(P#privacy{lists = NewLists}),
|
|
|
|
{result, []}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, {result, _} = Res} ->
|
|
|
|
ejabberd_router:route(
|
2007-10-17 04:19:44 +02:00
|
|
|
jlib:make_jid(LUser, LServer, ""),
|
|
|
|
jlib:make_jid(LUser, LServer, ""),
|
2003-08-03 21:57:08 +02:00
|
|
|
{xmlelement, "broadcast", [],
|
2006-03-17 05:35:03 +01:00
|
|
|
[{privacy_list,
|
|
|
|
#userlist{name = Name, list = List},
|
|
|
|
Name}]}),
|
2003-08-03 21:57:08 +02:00
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_list_set(_LUser, _LServer, false, _Els) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
{error, ?ERR_BAD_REQUEST}.
|
|
|
|
|
|
|
|
|
|
|
|
parse_items([]) ->
|
|
|
|
remove;
|
|
|
|
parse_items(Els) ->
|
|
|
|
parse_items(Els, []).
|
|
|
|
|
|
|
|
parse_items([], Res) ->
|
2009-01-16 17:06:21 +01:00
|
|
|
%% Sort the items by their 'order' attribute
|
2009-01-20 18:34:58 +01:00
|
|
|
lists:keysort(#listitem.order, Res);
|
2003-08-03 21:57:08 +02:00
|
|
|
parse_items([{xmlelement, "item", Attrs, SubEls} | Els], Res) ->
|
|
|
|
Type = xml:get_attr("type", Attrs),
|
|
|
|
Value = xml:get_attr("value", Attrs),
|
|
|
|
SAction = xml:get_attr("action", Attrs),
|
|
|
|
SOrder = xml:get_attr("order", Attrs),
|
|
|
|
Action = case catch list_to_action(element(2, SAction)) of
|
|
|
|
{'EXIT', _} -> false;
|
|
|
|
Val -> Val
|
|
|
|
end,
|
|
|
|
Order = case catch list_to_integer(element(2, SOrder)) of
|
|
|
|
{'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
|
|
|
|
{{value, T}, {value, V}} ->
|
|
|
|
case T of
|
|
|
|
"jid" ->
|
|
|
|
case jlib:string_to_jid(V) of
|
|
|
|
error ->
|
|
|
|
false;
|
|
|
|
JID ->
|
2003-10-10 21:01:11 +02:00
|
|
|
I1#listitem{
|
|
|
|
type = jid,
|
|
|
|
value = jlib:jid_tolower(JID)}
|
2003-08-03 21:57:08 +02:00
|
|
|
end;
|
|
|
|
"group" ->
|
|
|
|
I1#listitem{type = group,
|
|
|
|
value = V};
|
|
|
|
"subscription" ->
|
|
|
|
case V of
|
|
|
|
"none" ->
|
|
|
|
I1#listitem{type = subscription,
|
|
|
|
value = none};
|
|
|
|
"both" ->
|
|
|
|
I1#listitem{type = subscription,
|
|
|
|
value = both};
|
|
|
|
"from" ->
|
|
|
|
I1#listitem{type = subscription,
|
|
|
|
value = from};
|
|
|
|
"to" ->
|
|
|
|
I1#listitem{type = subscription,
|
|
|
|
value = to};
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
{{value, _}, false} ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
I1
|
|
|
|
end,
|
|
|
|
case I2 of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
case parse_matches(I2, xml:remove_cdata(SubEls)) of
|
|
|
|
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;
|
|
|
|
parse_matches1(Item, [{xmlelement, "message", _, _} | Els]) ->
|
|
|
|
parse_matches1(Item#listitem{match_message = true}, Els);
|
|
|
|
parse_matches1(Item, [{xmlelement, "iq", _, _} | Els]) ->
|
|
|
|
parse_matches1(Item#listitem{match_iq = true}, Els);
|
|
|
|
parse_matches1(Item, [{xmlelement, "presence-in", _, _} | Els]) ->
|
|
|
|
parse_matches1(Item#listitem{match_presence_in = true}, Els);
|
|
|
|
parse_matches1(Item, [{xmlelement, "presence-out", _, _} | Els]) ->
|
|
|
|
parse_matches1(Item#listitem{match_presence_out = true}, Els);
|
2003-12-17 21:13:21 +01:00
|
|
|
parse_matches1(_Item, [{xmlelement, _, _, _} | _Els]) ->
|
2003-08-03 21:57:08 +02:00
|
|
|
false.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-20 18:34:58 +01:00
|
|
|
is_list_needdb(Items) ->
|
|
|
|
lists:any(
|
|
|
|
fun(X) ->
|
|
|
|
case X#listitem.type of
|
|
|
|
subscription -> true;
|
|
|
|
group -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end, Items).
|
|
|
|
|
2006-10-05 05:17:41 +02:00
|
|
|
get_user_list(_, User, Server) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
|
2003-08-03 21:57:08 +02:00
|
|
|
[] ->
|
|
|
|
#userlist{};
|
|
|
|
[#privacy{default = Default, lists = Lists}] ->
|
|
|
|
case Default of
|
|
|
|
none ->
|
|
|
|
#userlist{};
|
|
|
|
_ ->
|
|
|
|
case lists:keysearch(Default, 1, Lists) of
|
|
|
|
{value, {_, List}} ->
|
2009-01-20 18:34:58 +01:00
|
|
|
NeedDb = is_list_needdb(List),
|
|
|
|
#userlist{name = Default, list = List, needdb = NeedDb};
|
2003-08-03 21:57:08 +02:00
|
|
|
_ ->
|
|
|
|
#userlist{}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
#userlist{}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2009-02-16 17:24:08 +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-01-20 18:34:58 +01:00
|
|
|
#userlist{list = List, needdb = NeedDb},
|
2009-02-16 17:24:08 +01:00
|
|
|
{From, To, {xmlelement, PName, Attrs, _}},
|
2003-08-03 21:57:08 +02:00
|
|
|
Dir) ->
|
|
|
|
case List of
|
|
|
|
[] ->
|
|
|
|
allow;
|
|
|
|
_ ->
|
|
|
|
PType = case PName of
|
|
|
|
"message" -> message;
|
|
|
|
"iq" -> iq;
|
2009-02-16 17:24:08 +01:00
|
|
|
"presence" ->
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
%% notification
|
|
|
|
"" -> presence;
|
|
|
|
"unavailable" -> presence;
|
|
|
|
%% subscribe, subscribed, unsubscribe,
|
|
|
|
%% unsubscribed, error, probe, or other
|
|
|
|
_ -> other
|
|
|
|
end
|
2003-08-03 21:57:08 +02:00
|
|
|
end,
|
2009-02-16 17:24:08 +01:00
|
|
|
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 -> jlib:jid_tolower(From);
|
|
|
|
out -> jlib:jid_tolower(To)
|
|
|
|
end,
|
|
|
|
{Subscription, Groups} =
|
|
|
|
case NeedDb of
|
|
|
|
true -> ejabberd_hooks:run_fold(roster_get_jid_info,
|
|
|
|
jlib: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-02-16 17:24:08 +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-02-16 17:24:08 +01:00
|
|
|
Item#listitem.match_presence_out;
|
|
|
|
other ->
|
|
|
|
false
|
2003-08-03 21:57:08 +02:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
is_type_match(Type, Value, JID, Subscription, Groups) ->
|
|
|
|
case Type of
|
|
|
|
jid ->
|
|
|
|
case Value of
|
|
|
|
{"", Server, ""} ->
|
|
|
|
case JID of
|
|
|
|
{_, Server, _} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
{User, Server, ""} ->
|
|
|
|
case JID of
|
|
|
|
{User, Server, _} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Value == JID
|
|
|
|
end;
|
|
|
|
subscription ->
|
|
|
|
Value == Subscription;
|
|
|
|
group ->
|
|
|
|
lists:member(Value, Groups)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2008-12-23 01:48:09 +01:00
|
|
|
remove_user(User, Server) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({privacy,
|
|
|
|
{LUser, LServer}})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, privacy),
|
|
|
|
case mnesia:table_info(privacy, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[user, default, lists] ->
|
|
|
|
?INFO_MSG("Converting privacy table from "
|
|
|
|
"{user, default, lists} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_privacy_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, privacy},
|
|
|
|
{attributes, record_info(fields, privacy)}]),
|
|
|
|
mnesia:transform_table(privacy, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_privacy_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#privacy{us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_privacy_tmp_table,
|
|
|
|
R#privacy{us = {U, Host}})
|
|
|
|
end, ok, privacy)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(privacy),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(privacy),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_privacy_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_privacy_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating privacy table", []),
|
|
|
|
mnesia:transform_table(privacy, ignore, Fields)
|
|
|
|
end.
|
|
|
|
|
2003-08-03 21:57:08 +02:00
|
|
|
|