From 54dc2f56c6170dc54b4f3ca64c9cc3f3921af6bd Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Fri, 29 Jan 2016 14:14:08 +0300 Subject: [PATCH] Import privacy lists from Prosody --- src/mod_privacy.erl | 32 ++++++++++++++++++++++- src/prosody2ejabberd.erl | 55 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/mod_privacy.erl b/src/mod_privacy.erl index 9c2af037b..62b043db0 100644 --- a/src/mod_privacy.erl +++ b/src/mod_privacy.erl @@ -35,7 +35,8 @@ process_iq_set/4, process_iq_get/5, get_user_list/3, check_packet/6, remove_user/2, item_to_raw/1, raw_to_item/1, is_list_needdb/1, updated_list/3, - item_to_xml/1, get_user_lists/2, import/3]). + item_to_xml/1, get_user_lists/2, import/3, + set_privacy_list/1]). -export([sql_add_privacy_list/2, sql_get_default_privacy_list/2, @@ -529,6 +530,35 @@ remove_privacy_list(LUser, LServer, Name, odbc) -> end, odbc_queries:sql_transaction(LServer, F). +set_privacy_list(#privacy{us = {_, LServer}} = Privacy) -> + DBType = gen_mod:db_type(LServer, ?MODULE), + set_privacy_list(Privacy, DBType). + +set_privacy_list(Privacy, mnesia) -> + mnesia:dirty_write(Privacy); +set_privacy_list(Privacy, riak) -> + ejabberd_riak:put(Privacy, privacy_schema()); +set_privacy_list(#privacy{us = {LUser, LServer}, + default = Default, + lists = Lists}, odbc) -> + F = fun() -> + lists:foreach( + fun({Name, List}) -> + sql_add_privacy_list(LUser, Name), + {selected, [<<"id">>], [[I]]} = + sql_get_privacy_list_id_t(LUser, Name), + RItems = lists:map(fun item_to_raw/1, List), + sql_set_privacy_list(I, RItems), + if is_binary(Default) -> + sql_set_default_privacy_list(LUser, Default), + ok; + true -> + ok + end + end, Lists) + end, + odbc_queries:sql_transaction(LServer, F). + set_privacy_list(LUser, LServer, Name, List, mnesia) -> F = fun () -> case mnesia:wread({privacy, {LUser, LServer}}) of diff --git a/src/prosody2ejabberd.erl b/src/prosody2ejabberd.erl index ce46e3991..d16437209 100644 --- a/src/prosody2ejabberd.erl +++ b/src/prosody2ejabberd.erl @@ -16,6 +16,7 @@ -include("logger.hrl"). -include("mod_roster.hrl"). -include("mod_offline.hrl"). +-include("mod_privacy.hrl"). %%%=================================================================== %%% API @@ -32,7 +33,8 @@ from_dir(ProsodyDir) -> [ProsodyDir, HostDir, SubDir]), convert_dir(Path, Host, SubDir) end, ["vcard", "accounts", "roster", - "private", "config", "offline"]) + "private", "config", "offline", + "privacy"]) end, HostDirs); {error, Why} = Err -> ?ERROR_MSG("failed to list ~s: ~s", @@ -159,6 +161,23 @@ convert_data(Host, "offline", User, [Data]) -> end, Data), mod_offline:store_offline_msg( LServer, {LUser, LServer}, Msgs, length(Msgs), infinity); +convert_data(Host, "privacy", User, [Data]) -> + LUser = jid:nodeprep(User), + LServer = jid:nameprep(Host), + Lists = proplists:get_value(<<"lists">>, Data, []), + Priv = #privacy{ + us = {LUser, LServer}, + default = proplists:get_value(<<"default">>, Data, none), + lists = lists:flatmap( + fun({Name, Vals}) -> + Items = proplists:get_value(<<"items">>, Vals, []), + case lists:map(fun convert_privacy_item/1, + Items) of + [] -> []; + ListItems -> [{Name, ListItems}] + end + end, Lists)}, + mod_privacy:set_privacy_list(Priv); convert_data(_Host, _Type, _User, _Data) -> ok. @@ -246,6 +265,40 @@ convert_room_config(Data) -> {moderated, proplists:get_bool(<<"moderated">>, Config)}, {anonymous, Anonymous}] ++ Pass ++ Subj. +convert_privacy_item({_, Item}) -> + Action = proplists:get_value(<<"action">>, Item, <<"allow">>), + Order = proplists:get_value(<<"order">>, Item, 0), + T = jlib:binary_to_atom(proplists:get_value(<<"type">>, Item, <<"none">>)), + V = proplists:get_value(<<"value">>, Item, <<"">>), + MatchIQ = proplists:get_bool(<<"iq">>, Item), + MatchMsg = proplists:get_bool(<<"message">>, Item), + MatchPresIn = proplists:get_bool(<<"presence-in">>, Item), + MatchPresOut = proplists:get_bool(<<"presence-out">>, Item), + MatchAll = if (MatchIQ == false) and (MatchMsg == false) and + (MatchPresIn == false) and (MatchPresOut == false) -> + true; + true -> + false + end, + {Type, Value} = try case T of + none -> {T, none}; + group -> {T, V}; + jid -> {T, jid:tolower(jid:from_string(V))}; + subscription -> {T, jlib:binary_to_atom(V)} + end + catch _:_ -> + {none, none} + end, + #listitem{type = Type, + value = Value, + action = jlib:binary_to_atom(Action), + order = erlang:trunc(Order), + match_all = MatchAll, + match_iq = MatchIQ, + match_message = MatchMsg, + match_presence_in = MatchPresIn, + match_presence_out = MatchPresOut}. + el_to_offline_msg(LUser, LServer, #xmlel{attrs = Attrs} = El) -> case jlib:datetime_string_to_timestamp( xml:get_attr_s(<<"stamp">>, Attrs)) of