2003-02-02 20:49:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : jd2ejd.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2009-01-12 19:41:46 +01:00
|
|
|
%%% Purpose : Import of jabberd14 user spool file
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 2 Feb 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2007-12-24 12:41:41 +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
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2003-02-02 20:49:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(jd2ejd).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
%% External exports
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([import_file/1, import_dir/1]).
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-08-04 11:34:12 +02:00
|
|
|
-include("xmpp.hrl").
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
import_file(File) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
User = filename:rootname(filename:basename(File)),
|
2005-04-17 20:08:34 +02:00
|
|
|
Server = filename:basename(filename:dirname(File)),
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nodeprep(User) /= error andalso
|
|
|
|
jid:nameprep(Server) /= error
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
true ->
|
|
|
|
case file:read_file(File) of
|
|
|
|
{ok, Text} ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml_stream:parse_element(Text) of
|
2013-03-14 10:33:02 +01:00
|
|
|
El when is_record(El, xmlel) ->
|
|
|
|
case catch process_xdb(User, Server, El) of
|
|
|
|
{'EXIT', Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Error while processing file \"~ts\": "
|
2013-03-14 10:33:02 +01:00
|
|
|
"~p~n",
|
2005-05-23 21:47:57 +02:00
|
|
|
[File, Reason]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Reason};
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Can't parse file \"~ts\": ~p~n",
|
2013-03-14 10:33:02 +01:00
|
|
|
[File, Reason]),
|
|
|
|
{error, Reason}
|
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Can't read file \"~ts\": ~p~n",
|
2013-03-14 10:33:02 +01:00
|
|
|
[File, Reason]),
|
|
|
|
{error, Reason}
|
|
|
|
end;
|
|
|
|
false ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Illegal user/server name in file \"~ts\"~n",
|
2013-03-14 10:33:02 +01:00
|
|
|
[File]),
|
|
|
|
{error, <<"illegal user/server">>}
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
import_dir(Dir) ->
|
|
|
|
{ok, Files} = file:list_dir(Dir),
|
2013-03-14 10:33:02 +01:00
|
|
|
MsgFiles = lists:filter(fun (FN) ->
|
|
|
|
case length(FN) > 4 of
|
|
|
|
true ->
|
|
|
|
string:substr(FN, length(FN) - 3) ==
|
|
|
|
".xml";
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Files),
|
|
|
|
lists:foldl(fun (FN, A) ->
|
|
|
|
Res = import_file(filename:join([Dir, FN])),
|
|
|
|
case {A, Res} of
|
|
|
|
{ok, ok} -> ok;
|
|
|
|
{ok, _} ->
|
|
|
|
{error, <<"see ejabberd log for details">>};
|
|
|
|
_ -> A
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
ok, MsgFiles).
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
2005-04-17 20:08:34 +02:00
|
|
|
%%% Internal functions
|
2003-02-02 20:49:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_xdb(User, Server,
|
|
|
|
#xmlel{name = Name, children = Els}) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
case Name of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"xdb">> ->
|
|
|
|
lists:foreach(fun (El) -> xdb_data(User, Server, El)
|
|
|
|
end,
|
|
|
|
Els);
|
|
|
|
_ -> ok
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
xdb_data(_User, _Server, {xmlcdata, _CData}) -> ok;
|
|
|
|
xdb_data(User, Server, #xmlel{attrs = Attrs} = El) ->
|
2016-08-04 11:34:12 +02:00
|
|
|
From = jid:make(User, Server),
|
|
|
|
LServer = From#jid.lserver,
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_AUTH ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Password = fxml:get_tag_cdata(El),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_auth:set_password(User, Server, Password),
|
|
|
|
ok;
|
|
|
|
?NS_ROSTER ->
|
2016-08-04 12:37:07 +02:00
|
|
|
catch mod_roster:set_items(User, Server, xmpp:decode(El)),
|
|
|
|
ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_LAST ->
|
2016-02-03 19:03:17 +01:00
|
|
|
TimeStamp = fxml:get_attr_s(<<"last">>, Attrs),
|
|
|
|
Status = fxml:get_tag_cdata(El),
|
2013-03-14 10:33:02 +01:00
|
|
|
catch mod_last:store_last_info(User, Server,
|
2016-09-24 22:34:28 +02:00
|
|
|
binary_to_integer(TimeStamp),
|
2013-03-14 10:33:02 +01:00
|
|
|
Status),
|
|
|
|
ok;
|
|
|
|
?NS_VCARD ->
|
2016-08-04 11:34:12 +02:00
|
|
|
catch mod_vcard:set_vcard(User, LServer, El),
|
2013-03-14 10:33:02 +01:00
|
|
|
ok;
|
|
|
|
<<"jabber:x:offline">> ->
|
|
|
|
process_offline(Server, From, El), ok;
|
|
|
|
XMLNS ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"j_private_flag">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"1">> ->
|
2016-08-04 11:34:12 +02:00
|
|
|
NewAttrs = lists:filter(
|
|
|
|
fun({<<"j_private_flag">>, _}) -> false;
|
|
|
|
({<<"xdbns">>, _}) -> false;
|
|
|
|
(_) -> true
|
|
|
|
end, Attrs),
|
|
|
|
catch mod_private:set_data(
|
2018-11-23 11:33:29 +01:00
|
|
|
From,
|
2016-08-04 11:34:12 +02:00
|
|
|
[{XMLNS, El#xmlel{attrs = NewAttrs}}]);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Unknown namespace \"~ts\"~n", [XMLNS])
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
ok
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_offline(Server, To, #xmlel{children = Els}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2016-08-04 11:34:12 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(#xmlel{} = El) ->
|
2016-09-24 13:17:21 +02:00
|
|
|
try xmpp:decode(El, ?NS_CLIENT, [ignore_els]) of
|
2017-02-16 09:00:26 +01:00
|
|
|
#message{from = JID} = Msg ->
|
2016-08-04 11:34:12 +02:00
|
|
|
From = case JID of
|
|
|
|
undefined -> jid:make(Server);
|
|
|
|
_ -> JID
|
|
|
|
end,
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
offline_message_hook,
|
2017-02-23 16:55:35 +01:00
|
|
|
LServer, {pass, xmpp:set_from_to(Msg, From, To)}, []);
|
2016-08-04 11:34:12 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
Txt = xmpp:format_error(Why),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to decode XML '~ts': ~ts",
|
2016-08-04 11:34:12 +02:00
|
|
|
[fxml:element_to_binary(El), Txt])
|
|
|
|
end
|
|
|
|
end, Els).
|