2016-01-28 12:23:51 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : prosody2ejabberd.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-01-28 12:23:51 +01:00
|
|
|
%%% Created : 20 Jan 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2016-12-27 10:44:07 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-01-28 12:23:51 +01:00
|
|
|
-module(prosody2ejabberd).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([from_dir/1]).
|
|
|
|
|
2018-06-14 13:00:47 +02:00
|
|
|
-include("scram.hrl").
|
2016-07-26 10:29:17 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-01-28 12:23:51 +01:00
|
|
|
-include("logger.hrl").
|
|
|
|
-include("mod_roster.hrl").
|
|
|
|
-include("mod_offline.hrl").
|
2016-01-29 12:14:08 +01:00
|
|
|
-include("mod_privacy.hrl").
|
2016-01-28 12:23:51 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
from_dir(ProsodyDir) ->
|
2018-03-19 18:09:35 +01:00
|
|
|
case code:ensure_loaded(luerl) of
|
|
|
|
{module, _} ->
|
|
|
|
case file:list_dir(ProsodyDir) of
|
|
|
|
{ok, HostDirs} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(HostDir) ->
|
|
|
|
Host = list_to_binary(HostDir),
|
|
|
|
lists:foreach(
|
|
|
|
fun(SubDir) ->
|
|
|
|
Path = filename:join(
|
|
|
|
[ProsodyDir, HostDir, SubDir]),
|
|
|
|
convert_dir(Path, Host, SubDir)
|
|
|
|
end, ["vcard", "accounts", "roster",
|
|
|
|
"private", "config", "offline",
|
|
|
|
"privacy", "pep", "pubsub"])
|
|
|
|
end, HostDirs);
|
|
|
|
{error, Why} = Err ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to list ~s: ~s",
|
2018-03-19 18:09:35 +01:00
|
|
|
[ProsodyDir, file:format_error(Why)]),
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
{error, _} = Err ->
|
2018-09-19 22:12:14 +02:00
|
|
|
?ERROR_MSG("The file 'luerl.beam' is not found: maybe "
|
|
|
|
"ejabberd is not compiled with Lua support", []),
|
2016-01-28 12:23:51 +01:00
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
convert_dir(Path, Host, Type) ->
|
|
|
|
case file:list_dir(Path) of
|
|
|
|
{ok, Files} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(File) ->
|
|
|
|
FilePath = filename:join(Path, File),
|
2017-08-10 19:49:20 +02:00
|
|
|
case Type of
|
|
|
|
"pep" ->
|
|
|
|
case filelib:is_dir(FilePath) of
|
|
|
|
true ->
|
|
|
|
JID = list_to_binary(File ++ "@" ++ Host),
|
|
|
|
convert_dir(FilePath, JID, "pubsub");
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
case eval_file(FilePath) of
|
|
|
|
{ok, Data} ->
|
|
|
|
Name = iolist_to_binary(filename:rootname(File)),
|
2017-08-13 18:18:19 +02:00
|
|
|
convert_data(url_decode(Host), Type,
|
|
|
|
url_decode(Name), Data);
|
2017-08-10 19:49:20 +02:00
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end
|
2016-01-28 12:23:51 +01:00
|
|
|
end
|
|
|
|
end, Files);
|
|
|
|
{error, enoent} ->
|
|
|
|
ok;
|
|
|
|
{error, Why} = Err ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to list ~s: ~s",
|
2016-01-28 12:23:51 +01:00
|
|
|
[Path, file:format_error(Why)]),
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
eval_file(Path) ->
|
|
|
|
case file:read_file(Path) of
|
|
|
|
{ok, Data} ->
|
|
|
|
State0 = luerl:init(),
|
|
|
|
State1 = luerl:set_table([item],
|
|
|
|
fun([X], State) -> {[X], State} end,
|
|
|
|
State0),
|
|
|
|
NewData = case filename:extension(Path) of
|
|
|
|
".list" ->
|
|
|
|
<<"return {", Data/binary, "};">>;
|
|
|
|
_ ->
|
|
|
|
Data
|
|
|
|
end,
|
|
|
|
case luerl:eval(NewData, State1) of
|
|
|
|
{ok, _} = Res ->
|
|
|
|
Res;
|
|
|
|
{error, Why} = Err ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to eval ~s: ~p", [Path, Why]),
|
2016-01-28 12:23:51 +01:00
|
|
|
Err
|
|
|
|
end;
|
|
|
|
{error, Why} = Err ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to read file ~s: ~s",
|
2016-01-28 12:23:51 +01:00
|
|
|
[Path, file:format_error(Why)]),
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
2017-02-20 12:47:56 +01:00
|
|
|
maybe_get_scram_auth(Data) ->
|
|
|
|
case proplists:get_value(<<"iteration_count">>, Data, no_ic) of
|
|
|
|
IC when is_float(IC) -> %% A float like 4096.0 is read
|
|
|
|
#scram{
|
2017-04-11 12:13:58 +02:00
|
|
|
storedkey = misc:hex_to_base64(proplists:get_value(<<"stored_key">>, Data, <<"">>)),
|
|
|
|
serverkey = misc:hex_to_base64(proplists:get_value(<<"server_key">>, Data, <<"">>)),
|
2017-06-29 10:28:44 +02:00
|
|
|
salt = base64:encode(proplists:get_value(<<"salt">>, Data, <<"">>)),
|
2017-02-20 12:47:56 +01:00
|
|
|
iterationcount = round(IC)
|
|
|
|
};
|
|
|
|
_ -> <<"">>
|
|
|
|
end.
|
|
|
|
|
2016-01-28 12:23:51 +01:00
|
|
|
convert_data(Host, "accounts", User, [Data]) ->
|
2017-02-20 12:47:56 +01:00
|
|
|
Password = case proplists:get_value(<<"password">>, Data, no_pass) of
|
|
|
|
no_pass ->
|
|
|
|
maybe_get_scram_auth(Data);
|
|
|
|
Pass when is_binary(Pass) ->
|
|
|
|
Pass
|
|
|
|
end,
|
2016-01-28 12:23:51 +01:00
|
|
|
case ejabberd_auth:try_register(User, Host, Password) of
|
2017-05-11 13:37:21 +02:00
|
|
|
ok ->
|
2016-01-28 12:23:51 +01:00
|
|
|
ok;
|
|
|
|
Err ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to register user ~s@~s: ~p",
|
2016-01-28 12:23:51 +01:00
|
|
|
[User, Host, Err]),
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
convert_data(Host, "roster", User, [Data]) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Host),
|
|
|
|
Rosters =
|
|
|
|
lists:flatmap(
|
|
|
|
fun({<<"pending">>, L}) ->
|
|
|
|
convert_pending_item(LUser, LServer, L);
|
|
|
|
({S, L}) when is_binary(S) ->
|
|
|
|
convert_roster_item(LUser, LServer, S, L);
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Data),
|
|
|
|
lists:foreach(fun mod_roster:set_roster/1, Rosters);
|
|
|
|
convert_data(Host, "private", User, [Data]) ->
|
|
|
|
PrivData = lists:flatmap(
|
|
|
|
fun({_TagXMLNS, Raw}) ->
|
|
|
|
case deserialize(Raw) of
|
|
|
|
[El] ->
|
2016-02-03 19:03:17 +01:00
|
|
|
XMLNS = fxml:get_tag_attr_s(<<"xmlns">>, El),
|
2016-01-28 12:23:51 +01:00
|
|
|
[{XMLNS, El}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Data),
|
2018-11-23 11:33:29 +01:00
|
|
|
mod_private:set_data(jid:make(User, Host), PrivData);
|
2016-01-28 12:23:51 +01:00
|
|
|
convert_data(Host, "vcard", User, [Data]) ->
|
|
|
|
LServer = jid:nameprep(Host),
|
|
|
|
case deserialize(Data) of
|
|
|
|
[VCard] ->
|
|
|
|
mod_vcard:set_vcard(User, LServer, VCard);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
convert_data(_Host, "config", _User, [Data]) ->
|
2018-12-19 12:10:07 +01:00
|
|
|
RoomJID1 = case proplists:get_value(<<"jid">>, Data, not_found) of
|
|
|
|
not_found -> proplists:get_value(<<"_jid">>, Data, room_jid_not_found);
|
|
|
|
A when is_binary(A) -> A
|
|
|
|
end,
|
|
|
|
RoomJID = jid:decode(RoomJID1),
|
2016-01-28 12:23:51 +01:00
|
|
|
Config = proplists:get_value(<<"_data">>, Data, []),
|
|
|
|
RoomCfg = convert_room_config(Data),
|
|
|
|
case proplists:get_bool(<<"persistent">>, Config) of
|
|
|
|
true when RoomJID /= error ->
|
2019-05-02 20:22:46 +02:00
|
|
|
mod_muc:store_room(find_serverhost(RoomJID#jid.lserver), RoomJID#jid.lserver,
|
2016-01-28 12:23:51 +01:00
|
|
|
RoomJID#jid.luser, RoomCfg);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
convert_data(Host, "offline", User, [Data]) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Host),
|
2017-05-21 22:21:13 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({_, RawXML}) ->
|
|
|
|
case deserialize(RawXML) of
|
|
|
|
[El] ->
|
2018-03-02 11:43:50 +01:00
|
|
|
case el_to_offline_msg(LUser, LServer, El) of
|
|
|
|
[Msg] -> ok = mod_offline:store_offline_msg(Msg);
|
|
|
|
[] -> ok
|
|
|
|
end;
|
2017-05-21 22:21:13 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Data);
|
2016-01-29 12:14:08 +01:00
|
|
|
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)},
|
2017-05-20 21:36:32 +02:00
|
|
|
mod_privacy:set_list(Priv);
|
2017-08-13 18:18:19 +02:00
|
|
|
convert_data(HostStr, "pubsub", Node, [Data]) ->
|
2017-08-10 19:49:20 +02:00
|
|
|
case decode_pubsub_host(HostStr) of
|
|
|
|
Host when is_binary(Host);
|
|
|
|
is_tuple(Host) ->
|
|
|
|
Type = node_type(Host),
|
|
|
|
NodeData = convert_node_config(HostStr, Data),
|
|
|
|
DefaultConfig = mod_pubsub:config(Host, default_node_config, []),
|
|
|
|
Owner = proplists:get_value(owner, NodeData),
|
|
|
|
Options = lists:foldl(
|
|
|
|
fun({_Opt, undefined}, Acc) ->
|
|
|
|
Acc;
|
|
|
|
({Opt, Val}, Acc) ->
|
|
|
|
lists:keystore(Opt, 1, Acc, {Opt, Val})
|
|
|
|
end, DefaultConfig, proplists:get_value(options, NodeData)),
|
|
|
|
case mod_pubsub:tree_action(Host, create_node, [Host, Node, Type, Owner, Options, []]) of
|
|
|
|
{ok, Nidx} ->
|
|
|
|
case mod_pubsub:node_action(Host, Type, create_node, [Nidx, Owner]) of
|
|
|
|
{result, _} ->
|
|
|
|
Access = open, % always allow subscriptions proplists:get_value(access_model, Options),
|
|
|
|
Publish = open, % always allow publications proplists:get_value(publish_model, Options),
|
|
|
|
MaxItems = proplists:get_value(max_items, Options),
|
|
|
|
Affiliations = proplists:get_value(affiliations, NodeData),
|
|
|
|
Subscriptions = proplists:get_value(subscriptions, NodeData),
|
|
|
|
Items = proplists:get_value(items, NodeData),
|
|
|
|
[mod_pubsub:node_action(Host, Type, set_affiliation,
|
|
|
|
[Nidx, Entity, Aff])
|
|
|
|
|| {Entity, Aff} <- Affiliations, Entity =/= Owner],
|
|
|
|
[mod_pubsub:node_action(Host, Type, subscribe_node,
|
|
|
|
[Nidx, jid:make(Entity), Entity, Access, never, [], [], []])
|
|
|
|
|| Entity <- Subscriptions],
|
|
|
|
[mod_pubsub:node_action(Host, Type, publish_item,
|
|
|
|
[Nidx, Publisher, Publish, MaxItems, ItemId, Payload, []])
|
|
|
|
|| {ItemId, Publisher, Payload} <- Items];
|
|
|
|
Error ->
|
|
|
|
Error
|
|
|
|
end;
|
2017-06-15 09:56:05 +02:00
|
|
|
Error ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to import pubsub node ~s on ~p:~n~p",
|
2017-08-10 19:49:20 +02:00
|
|
|
[Node, Host, NodeData]),
|
2017-06-15 09:56:05 +02:00
|
|
|
Error
|
|
|
|
end;
|
|
|
|
Error ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to import pubsub node: ~p", [Error]),
|
2017-06-15 09:56:05 +02:00
|
|
|
Error
|
|
|
|
end;
|
2016-01-28 12:23:51 +01:00
|
|
|
convert_data(_Host, _Type, _User, _Data) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
convert_pending_item(LUser, LServer, LuaList) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({S, true}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(S) of
|
|
|
|
J ->
|
2016-01-28 12:23:51 +01:00
|
|
|
LJID = jid:tolower(J),
|
|
|
|
[#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
|
|
|
jid = LJID,
|
2017-02-26 08:07:12 +01:00
|
|
|
ask = in}]
|
|
|
|
catch _:{bad_jid, _} ->
|
2016-01-28 12:23:51 +01:00
|
|
|
[]
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, LuaList).
|
|
|
|
|
|
|
|
convert_roster_item(LUser, LServer, JIDstring, LuaList) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(JIDstring) of
|
|
|
|
JID ->
|
2016-01-28 12:23:51 +01:00
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
InitR = #roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
|
|
|
jid = LJID},
|
2018-12-21 14:46:54 +01:00
|
|
|
lists:foldl(
|
|
|
|
fun({<<"groups">>, Val}, [R]) ->
|
2016-01-28 12:23:51 +01:00
|
|
|
Gs = lists:flatmap(
|
|
|
|
fun({G, true}) -> [G];
|
|
|
|
(_) -> []
|
|
|
|
end, Val),
|
2018-12-21 14:46:54 +01:00
|
|
|
[R#roster{groups = Gs}];
|
|
|
|
({<<"subscription">>, Sub}, [R]) ->
|
|
|
|
[R#roster{subscription = misc:binary_to_atom(Sub)}];
|
|
|
|
({<<"ask">>, <<"subscribe">>}, [R]) ->
|
|
|
|
[R#roster{ask = out}];
|
|
|
|
({<<"name">>, Name}, [R]) ->
|
|
|
|
[R#roster{name = Name}];
|
|
|
|
({<<"persist">>, false}, _) ->
|
|
|
|
[];
|
|
|
|
(_, []) ->
|
|
|
|
[]
|
|
|
|
end, [InitR], LuaList)
|
2017-02-26 08:07:12 +01:00
|
|
|
catch _:{bad_jid, _} ->
|
2016-01-28 12:23:51 +01:00
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
convert_room_affiliations(Data) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({J, Aff}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(J) of
|
2016-01-28 12:23:51 +01:00
|
|
|
#jid{luser = U, lserver = S} ->
|
2017-04-11 12:13:58 +02:00
|
|
|
[{{U, S, <<>>}, misc:binary_to_atom(Aff)}]
|
2017-02-26 08:07:12 +01:00
|
|
|
catch _:{bad_jid, _} ->
|
2016-01-28 12:23:51 +01:00
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, proplists:get_value(<<"_affiliations">>, Data, [])).
|
|
|
|
|
|
|
|
convert_room_config(Data) ->
|
|
|
|
Config = proplists:get_value(<<"_data">>, Data, []),
|
|
|
|
Pass = case proplists:get_value(<<"password">>, Config, <<"">>) of
|
|
|
|
<<"">> ->
|
|
|
|
[];
|
|
|
|
Password ->
|
|
|
|
[{password_protected, true},
|
|
|
|
{password, Password}]
|
|
|
|
end,
|
2017-02-26 08:07:12 +01:00
|
|
|
Subj = try jid:decode(
|
2016-01-28 12:23:51 +01:00
|
|
|
proplists:get_value(
|
|
|
|
<<"subject_from">>, Config, <<"">>)) of
|
|
|
|
#jid{lresource = Nick} when Nick /= <<"">> ->
|
|
|
|
[{subject, proplists:get_value(<<"subject">>, Config, <<"">>)},
|
2017-02-26 08:07:12 +01:00
|
|
|
{subject_author, Nick}]
|
|
|
|
catch _:{bad_jid, _} ->
|
2016-01-28 12:23:51 +01:00
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Anonymous = case proplists:get_value(<<"whois">>, Config, <<"moderators">>) of
|
|
|
|
<<"moderators">> -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
[{affiliations, convert_room_affiliations(Data)},
|
|
|
|
{allow_change_subj, proplists:get_bool(<<"changesubject">>, Config)},
|
2018-12-19 11:59:52 +01:00
|
|
|
{mam, proplists:get_bool(<<"archiving">>, Config)},
|
2016-01-28 12:23:51 +01:00
|
|
|
{description, proplists:get_value(<<"description">>, Config, <<"">>)},
|
|
|
|
{members_only, proplists:get_bool(<<"members_only">>, Config)},
|
|
|
|
{moderated, proplists:get_bool(<<"moderated">>, Config)},
|
2018-12-19 11:59:52 +01:00
|
|
|
{persistent, proplists:get_bool(<<"persistent">>, Config)},
|
2016-01-28 12:23:51 +01:00
|
|
|
{anonymous, Anonymous}] ++ Pass ++ Subj.
|
|
|
|
|
2016-01-29 12:14:08 +01:00
|
|
|
convert_privacy_item({_, Item}) ->
|
|
|
|
Action = proplists:get_value(<<"action">>, Item, <<"allow">>),
|
|
|
|
Order = proplists:get_value(<<"order">>, Item, 0),
|
2017-04-11 12:13:58 +02:00
|
|
|
T = misc:binary_to_atom(proplists:get_value(<<"type">>, Item, <<"none">>)),
|
2016-01-29 12:14:08 +01:00
|
|
|
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};
|
2017-02-26 08:07:12 +01:00
|
|
|
jid -> {T, jid:tolower(jid:decode(V))};
|
2017-04-11 12:13:58 +02:00
|
|
|
subscription -> {T, misc:binary_to_atom(V)}
|
2016-01-29 12:14:08 +01:00
|
|
|
end
|
|
|
|
catch _:_ ->
|
|
|
|
{none, none}
|
|
|
|
end,
|
|
|
|
#listitem{type = Type,
|
|
|
|
value = Value,
|
2017-04-11 12:13:58 +02:00
|
|
|
action = misc:binary_to_atom(Action),
|
2016-01-29 12:14:08 +01:00
|
|
|
order = erlang:trunc(Order),
|
|
|
|
match_all = MatchAll,
|
|
|
|
match_iq = MatchIQ,
|
|
|
|
match_message = MatchMsg,
|
|
|
|
match_presence_in = MatchPresIn,
|
|
|
|
match_presence_out = MatchPresOut}.
|
|
|
|
|
2017-06-15 09:56:05 +02:00
|
|
|
url_decode(Encoded) ->
|
|
|
|
url_decode(Encoded, <<>>).
|
|
|
|
url_decode(<<$%, Hi, Lo, Tail/binary>>, Acc) ->
|
|
|
|
Hex = list_to_integer([Hi, Lo], 16),
|
|
|
|
url_decode(Tail, <<Acc/binary, Hex>>);
|
|
|
|
url_decode(<<H, Tail/binary>>, Acc) ->
|
|
|
|
url_decode(Tail, <<Acc/binary, H>>);
|
|
|
|
url_decode(<<>>, Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
2017-08-10 19:49:20 +02:00
|
|
|
decode_pubsub_host(Host) ->
|
|
|
|
try jid:decode(Host) of
|
|
|
|
#jid{luser = <<>>, lserver = LServer} -> LServer;
|
|
|
|
#jid{luser = LUser, lserver = LServer} -> {LUser, LServer, <<>>}
|
|
|
|
catch _:{bad_jid, _} -> bad_jid
|
|
|
|
end.
|
|
|
|
|
|
|
|
node_type({_U, _S, _R}) -> <<"pep">>;
|
|
|
|
node_type(Host) -> hd(mod_pubsub:plugins(Host)).
|
2017-06-15 09:56:05 +02:00
|
|
|
|
|
|
|
max_items(Config, Default) ->
|
|
|
|
case round(proplists:get_value(<<"max_items">>, Config, Default)) of
|
|
|
|
I when I =< 0 -> Default;
|
|
|
|
I -> I
|
|
|
|
end.
|
|
|
|
|
|
|
|
convert_node_affiliations(Data) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({J, Aff}) ->
|
|
|
|
try jid:decode(J) of
|
|
|
|
JID ->
|
|
|
|
[{JID, misc:binary_to_atom(Aff)}]
|
|
|
|
catch _:{bad_jid, _} ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, proplists:get_value(<<"affiliations">>, Data, [])).
|
|
|
|
|
|
|
|
convert_node_subscriptions(Data) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({J, true}) ->
|
|
|
|
try jid:decode(J) of
|
|
|
|
JID ->
|
|
|
|
[jid:tolower(JID)]
|
|
|
|
catch _:{bad_jid, _} ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, proplists:get_value(<<"subscribers">>, Data, [])).
|
|
|
|
|
|
|
|
convert_node_items(Host, Data) ->
|
|
|
|
Authors = proplists:get_value(<<"data_author">>, Data, []),
|
|
|
|
lists:flatmap(
|
|
|
|
fun({ItemId, Item}) ->
|
2017-08-10 18:54:00 +02:00
|
|
|
try jid:decode(proplists:get_value(ItemId, Authors, Host)) of
|
2017-06-15 09:56:05 +02:00
|
|
|
JID ->
|
|
|
|
[El] = deserialize(Item),
|
|
|
|
[{ItemId, JID, El#xmlel.children}]
|
|
|
|
catch _:{bad_jid, _} ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, proplists:get_value(<<"data">>, Data, [])).
|
|
|
|
|
|
|
|
convert_node_config(Host, Data) ->
|
|
|
|
Config = proplists:get_value(<<"config">>, Data, []),
|
|
|
|
[{affiliations, convert_node_affiliations(Data)},
|
|
|
|
{subscriptions, convert_node_subscriptions(Data)},
|
|
|
|
{owner, jid:decode(proplists:get_value(<<"creator">>, Config, Host))},
|
|
|
|
{items, convert_node_items(Host, Data)},
|
|
|
|
{options, [
|
|
|
|
{deliver_notifications,
|
|
|
|
proplists:get_value(<<"deliver_notifications">>, Config, true)},
|
|
|
|
{deliver_payloads,
|
|
|
|
proplists:get_value(<<"deliver_payloads">>, Config, true)},
|
|
|
|
{persist_items,
|
|
|
|
proplists:get_value(<<"persist_items">>, Config, true)},
|
|
|
|
{max_items,
|
|
|
|
max_items(Config, 10)},
|
|
|
|
{access_model,
|
|
|
|
misc:binary_to_atom(proplists:get_value(<<"access_model">>, Config, <<"open">>))},
|
|
|
|
{publish_model,
|
|
|
|
misc:binary_to_atom(proplists:get_value(<<"publish_model">>, Config, <<"publishers">>))},
|
|
|
|
{title,
|
|
|
|
proplists:get_value(<<"title">>, Config, <<"">>)}
|
|
|
|
]}
|
|
|
|
].
|
|
|
|
|
2016-01-28 12:23:51 +01:00
|
|
|
el_to_offline_msg(LUser, LServer, #xmlel{attrs = Attrs} = El) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try
|
|
|
|
TS = xmpp_util:decode_timestamp(
|
|
|
|
fxml:get_attr_s(<<"stamp">>, Attrs)),
|
|
|
|
Attrs1 = lists:filter(
|
2017-03-21 18:23:55 +01:00
|
|
|
fun({<<"stamp">>, _}) -> false;
|
|
|
|
({<<"stamp_legacy">>, _}) -> false;
|
2017-02-26 08:07:12 +01:00
|
|
|
(_) -> true
|
|
|
|
end, Attrs),
|
2017-03-21 18:35:07 +01:00
|
|
|
El1 = El#xmlel{attrs = Attrs1},
|
|
|
|
case xmpp:decode(El1, ?NS_CLIENT, [ignore_els]) of
|
|
|
|
#message{from = #jid{} = From, to = #jid{} = To} = Packet ->
|
|
|
|
[#offline_msg{
|
|
|
|
us = {LUser, LServer},
|
|
|
|
timestamp = TS,
|
|
|
|
expire = never,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
2016-11-13 08:44:53 +01:00
|
|
|
catch _:{bad_timestamp, _} ->
|
2017-02-26 08:07:12 +01:00
|
|
|
[];
|
|
|
|
_:{bad_jid, _} ->
|
2017-03-21 18:35:07 +01:00
|
|
|
[];
|
|
|
|
_:{xmpp_codec, _} ->
|
2016-01-28 12:23:51 +01:00
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2019-05-02 20:22:46 +02:00
|
|
|
find_serverhost(Host) ->
|
|
|
|
[ServerHost] =
|
|
|
|
lists:filter(
|
|
|
|
fun(ServerHost) ->
|
|
|
|
case gen_mod:is_loaded(ServerHost, mod_muc) of
|
|
|
|
true ->
|
2019-06-14 11:33:26 +02:00
|
|
|
lists:member(Host, gen_mod:get_module_opt_hosts(ServerHost, mod_muc));
|
2019-05-02 20:22:46 +02:00
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts()),
|
2019-05-02 20:22:46 +02:00
|
|
|
ServerHost.
|
|
|
|
|
2016-01-28 12:23:51 +01:00
|
|
|
deserialize(L) ->
|
|
|
|
deserialize(L, #xmlel{}, []).
|
|
|
|
|
2018-12-21 15:00:54 +01:00
|
|
|
deserialize([{Other, _}|T], El, Acc)
|
|
|
|
when (Other == <<"key">>)
|
|
|
|
or (Other == <<"when">>)
|
|
|
|
or (Other == <<"with">>) ->
|
|
|
|
deserialize(T, El, Acc);
|
2016-01-28 12:23:51 +01:00
|
|
|
deserialize([{<<"attr">>, Attrs}|T], El, Acc) ->
|
|
|
|
deserialize(T, El#xmlel{attrs = Attrs ++ El#xmlel.attrs}, Acc);
|
|
|
|
deserialize([{<<"name">>, Name}|T], El, Acc) ->
|
|
|
|
deserialize(T, El#xmlel{name = Name}, Acc);
|
|
|
|
deserialize([{_, S}|T], #xmlel{children = Els} = El, Acc) when is_binary(S) ->
|
|
|
|
deserialize(T, El#xmlel{children = [{xmlcdata, S}|Els]}, Acc);
|
|
|
|
deserialize([{_, L}|T], #xmlel{children = Els} = El, Acc) when is_list(L) ->
|
|
|
|
deserialize(T, El#xmlel{children = deserialize(L) ++ Els}, Acc);
|
2017-08-18 09:20:27 +02:00
|
|
|
deserialize([], #xmlel{children = Els} = El, Acc) ->
|
|
|
|
[El#xmlel{children = lists:reverse(Els)}|Acc].
|