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-19 16:27:07 +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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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-02-02 20:49:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(jd2ejd).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
%% 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
|
|
|
|
2008-10-13 11:39:18 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-02-02 20:49:19 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% 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)),
|
2008-10-13 11:39:18 +02:00
|
|
|
case exmpp_stringprep:is_node(User) andalso
|
|
|
|
exmpp_stringprep:is_name(Server) of
|
2005-04-17 20:08:34 +02:00
|
|
|
true ->
|
|
|
|
case file:read_file(File) of
|
|
|
|
{ok, Text} ->
|
2008-10-13 11:39:18 +02:00
|
|
|
try
|
|
|
|
[El] = exmpp_xml:parse_document(Text,
|
2008-12-05 16:06:49 +01:00
|
|
|
[names_as_atom]),
|
2008-10-13 11:39:18 +02:00
|
|
|
case catch process_xdb(User, Server, El) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG(
|
|
|
|
"Error while processing file \"~s\": ~p~n",
|
|
|
|
[File, Reason]),
|
|
|
|
{error, Reason};
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_:Reason1 ->
|
2005-04-17 20:08:34 +02:00
|
|
|
?ERROR_MSG("Can't parse file \"~s\": ~p~n",
|
2008-10-13 11:39:18 +02:00
|
|
|
[File, Reason1]),
|
|
|
|
{error, Reason1}
|
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
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2008-10-13 11:39:18 +02:00
|
|
|
process_xdb(User, Server, #xmlel{name = "xdb", children = Els}) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
|
|
|
xdb_data(User, Server, El)
|
|
|
|
end, Els);
|
|
|
|
process_xdb(_User, _Server, _El) ->
|
|
|
|
ok.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
|
2008-10-13 11:39:18 +02:00
|
|
|
xdb_data(_User, _Server, #xmlcdata{}) ->
|
2005-04-21 16:06:15 +02:00
|
|
|
ok;
|
2008-10-13 11:39:18 +02:00
|
|
|
xdb_data(User, Server, #xmlel{ns = NS} = El) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
From = exmpp_jid:make(User, Server),
|
2008-10-13 11:39:18 +02:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
case NS of
|
|
|
|
?NS_LEGACY_AUTH ->
|
|
|
|
Password = exmpp_xml:get_cdata(El),
|
2005-04-17 20:08:34 +02:00
|
|
|
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;
|
2008-10-13 11:39:18 +02:00
|
|
|
?NS_LAST_ACTIVITY ->
|
2009-01-21 14:34:26 +01:00
|
|
|
TimeStamp = exmpp_xml:get_attribute_as_list(El, 'last', ""),
|
2008-10-13 11:39:18 +02:00
|
|
|
Status = exmpp_xml:get_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,
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Server),
|
2008-10-13 11:39:18 +02:00
|
|
|
#iq{kind = request, type = set, ns = ?NS_VCARD, payload = El, iq_ns = ?NS_JABBER_CLIENT});
|
2006-02-15 05:15:54 +01:00
|
|
|
false ->
|
|
|
|
catch mod_vcard:process_sm_iq(
|
|
|
|
From,
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Server),
|
2008-10-13 11:39:18 +02:00
|
|
|
#iq{kind = request, type = set, ns = ?NS_VCARD, payload = El, iq_ns = ?NS_JABBER_CLIENT})
|
2006-02-15 05:15:54 +01:00
|
|
|
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 ->
|
2009-01-21 14:34:26 +01:00
|
|
|
case exmpp_xml:get_attribute_as_list(El, "j_private_flag", "") of
|
2005-04-17 20:08:34 +02:00
|
|
|
"1" ->
|
2005-05-16 01:03:25 +02:00
|
|
|
catch mod_private:process_sm_iq(
|
2005-04-17 20:08:34 +02:00
|
|
|
From,
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Server),
|
2008-10-13 11:39:18 +02:00
|
|
|
#iq{kind = request, type = set, ns = ?NS_PRIVATE,
|
|
|
|
iq_ns = ?NS_JABBER_CLIENT,
|
|
|
|
payload = #xmlel{name = 'query', children =
|
|
|
|
[exmpp_xml:remove_attribute(
|
|
|
|
exmpp_xml:remove_attribute(El, "xdbns"), "j_private_flag")]}});
|
2005-04-17 20:08:34 +02:00
|
|
|
_ ->
|
|
|
|
?DEBUG("jd2ejd: Unknown namespace \"~s\"~n", [XMLNS])
|
|
|
|
end,
|
|
|
|
ok
|
|
|
|
end.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
|
2008-10-13 11:39:18 +02:00
|
|
|
process_offline(Server, To, #xmlel{children = Els}) ->
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
lists:foreach(fun(#xmlel{} = El) ->
|
|
|
|
FromS = exmpp_stanza:get_sender(El),
|
2003-02-04 21:45:23 +01:00
|
|
|
From = case FromS of
|
2008-10-13 11:39:18 +02:00
|
|
|
undefined ->
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Server);
|
2003-02-04 21:45:23 +01:00
|
|
|
_ ->
|
2008-10-13 11:39:18 +02:00
|
|
|
try
|
2009-06-01 18:35:55 +02:00
|
|
|
exmpp_jid:parse(FromS)
|
2008-10-13 11:39:18 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end
|
2003-02-04 21:45:23 +01:00
|
|
|
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).
|
|
|
|
|