2003-02-02 20:49:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : jd2ejd.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : Import of jabberd1.4 user spool file
|
|
|
|
%%% Created : 2 Feb 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(jd2ejd).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
|
|
|
|
%% External exports
|
2005-04-17 20:08:34 +02:00
|
|
|
-export([import_file/1,
|
2003-02-04 21:45:23 +01:00
|
|
|
import_dir/1]).
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.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)),
|
|
|
|
case (jlib:nodeprep(User) /= error) andalso
|
|
|
|
(jlib:nameprep(Server) /= error) of
|
|
|
|
true ->
|
|
|
|
case file:read_file(File) of
|
|
|
|
{ok, Text} ->
|
|
|
|
case xml_stream:parse_element(Text) of
|
|
|
|
El when element(1, El) == xmlelement ->
|
|
|
|
case catch process_xdb(User, Server, El) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG(
|
|
|
|
"Error while processing file \"~s\": ~p~n",
|
2005-05-23 21:47:57 +02:00
|
|
|
[File, Reason]),
|
2007-12-07 00:15:04 +01:00
|
|
|
{error, Reason};
|
2005-04-17 20:08:34 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("Can't parse file \"~s\": ~p~n",
|
2005-05-23 21:47:57 +02:00
|
|
|
[File, Reason]),
|
|
|
|
{error, Reason}
|
2005-04-17 20:08:34 +02:00
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
2005-05-23 21:47:57 +02:00
|
|
|
?ERROR_MSG("Can't read file \"~s\": ~p~n", [File, Reason]),
|
|
|
|
{error, Reason}
|
2005-04-17 20:08:34 +02:00
|
|
|
end;
|
|
|
|
false ->
|
2005-05-23 21:47:57 +02:00
|
|
|
?ERROR_MSG("Illegal user/server name in file \"~s\"~n", [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),
|
|
|
|
MsgFiles = lists:filter(
|
|
|
|
fun(FN) ->
|
|
|
|
case string:len(FN) > 4 of
|
|
|
|
true ->
|
|
|
|
string:substr(FN,
|
|
|
|
string:len(FN) - 3) == ".xml";
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end, Files),
|
2005-05-23 21:47:57 +02:00
|
|
|
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
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_xdb(User, Server, {xmlelement, Name, _Attrs, Els}) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
case Name of
|
|
|
|
"xdb" ->
|
2005-04-17 20:08:34 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
|
|
|
xdb_data(User, Server, El)
|
|
|
|
end, Els);
|
2003-02-02 20:49:19 +01:00
|
|
|
_ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
ok
|
|
|
|
end.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
xdb_data(_User, _Server, {xmlcdata, _CData}) ->
|
2005-04-21 16:06:15 +02:00
|
|
|
ok;
|
|
|
|
xdb_data(User, Server, {xmlelement, _Name, Attrs, _Els} = El) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
From = jlib:make_jid(User, Server, ""),
|
2005-07-26 05:04:26 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
?NS_AUTH ->
|
|
|
|
Password = xml:get_tag_cdata(El),
|
|
|
|
ejabberd_auth:set_password(User, Server, Password),
|
|
|
|
ok;
|
|
|
|
?NS_ROSTER ->
|
2005-07-26 05:04:26 +02:00
|
|
|
case lists:member(mod_roster_odbc,
|
|
|
|
gen_mod:loaded_modules(LServer)) of
|
|
|
|
true ->
|
|
|
|
catch mod_roster_odbc:set_items(User, Server, El);
|
|
|
|
false ->
|
|
|
|
catch mod_roster:set_items(User, Server, El)
|
|
|
|
end,
|
2005-04-17 20:08:34 +02:00
|
|
|
ok;
|
2005-05-23 21:47:57 +02:00
|
|
|
?NS_LAST ->
|
|
|
|
TimeStamp = xml:get_attr_s("last", Attrs),
|
|
|
|
Status = xml:get_tag_cdata(El),
|
2005-07-26 05:04:26 +02:00
|
|
|
case lists:member(mod_last_odbc,
|
|
|
|
gen_mod:loaded_modules(LServer)) of
|
|
|
|
true ->
|
|
|
|
catch mod_last_odbc:store_last_info(
|
|
|
|
User,
|
|
|
|
Server,
|
|
|
|
list_to_integer(TimeStamp),
|
|
|
|
Status);
|
|
|
|
false ->
|
|
|
|
catch mod_last:store_last_info(
|
|
|
|
User,
|
|
|
|
Server,
|
|
|
|
list_to_integer(TimeStamp),
|
|
|
|
Status)
|
|
|
|
end,
|
2005-05-23 21:47:57 +02:00
|
|
|
ok;
|
2005-04-17 20:08:34 +02:00
|
|
|
?NS_VCARD ->
|
2006-02-15 05:15:54 +01:00
|
|
|
case lists:member(mod_vcard_odbc,
|
|
|
|
gen_mod:loaded_modules(LServer)) of
|
|
|
|
true ->
|
|
|
|
catch mod_vcard_odbc:process_sm_iq(
|
|
|
|
From,
|
|
|
|
jlib:make_jid("", Server, ""),
|
|
|
|
#iq{type = set, xmlns = ?NS_VCARD, sub_el = El});
|
|
|
|
false ->
|
|
|
|
catch mod_vcard:process_sm_iq(
|
|
|
|
From,
|
|
|
|
jlib:make_jid("", Server, ""),
|
|
|
|
#iq{type = set, xmlns = ?NS_VCARD, sub_el = El})
|
|
|
|
end,
|
2005-04-17 20:08:34 +02:00
|
|
|
ok;
|
|
|
|
"jabber:x:offline" ->
|
2005-07-26 05:04:26 +02:00
|
|
|
process_offline(Server, From, El),
|
2005-04-17 20:08:34 +02:00
|
|
|
ok;
|
|
|
|
XMLNS ->
|
|
|
|
case xml:get_attr_s("j_private_flag", Attrs) of
|
|
|
|
"1" ->
|
2005-05-16 01:03:25 +02:00
|
|
|
catch mod_private:process_sm_iq(
|
2005-04-17 20:08:34 +02:00
|
|
|
From,
|
2005-07-26 05:04:26 +02:00
|
|
|
jlib:make_jid("", Server, ""),
|
2005-04-17 20:08:34 +02:00
|
|
|
#iq{type = set, xmlns = ?NS_PRIVATE,
|
|
|
|
sub_el = {xmlelement, "query", [],
|
|
|
|
[jlib:remove_attr(
|
|
|
|
"j_private_flag",
|
|
|
|
jlib:remove_attr("xdbns", El))]}});
|
|
|
|
_ ->
|
|
|
|
?DEBUG("jd2ejd: Unknown namespace \"~s\"~n", [XMLNS])
|
|
|
|
end,
|
|
|
|
ok
|
|
|
|
end.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
|
2005-07-26 05:04:26 +02:00
|
|
|
process_offline(Server, To, {xmlelement, _, _, Els}) ->
|
|
|
|
LServer = jlib:nameprep(Server),
|
2003-02-04 21:45:23 +01:00
|
|
|
lists:foreach(fun({xmlelement, _, Attrs, _} = El) ->
|
|
|
|
FromS = xml:get_attr_s("from", Attrs),
|
|
|
|
From = case FromS of
|
|
|
|
"" ->
|
2005-07-26 05:04:26 +02:00
|
|
|
jlib:make_jid("", Server, "");
|
2003-02-04 21:45:23 +01:00
|
|
|
_ ->
|
|
|
|
jlib:string_to_jid(FromS)
|
|
|
|
end,
|
|
|
|
case From of
|
|
|
|
error ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2005-07-26 05:04:26 +02:00
|
|
|
ejabberd_hooks:run(offline_message_hook,
|
|
|
|
LServer,
|
|
|
|
[From, To, El])
|
2003-02-04 21:45:23 +01:00
|
|
|
end
|
|
|
|
end, Els).
|
|
|
|
|