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').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2003-02-05 21:49:26 +01:00
|
|
|
-export([start/1,
|
|
|
|
start/2,
|
2003-02-04 21:45:23 +01:00
|
|
|
import_file/1,
|
|
|
|
import_dir/1]).
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
|
|
|
-export([init/1,
|
|
|
|
wait_for_xdb/2,
|
|
|
|
xdb_data/2,
|
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
code_change/4,
|
|
|
|
handle_info/3,
|
|
|
|
terminate/3]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2003-10-17 21:15:38 +02:00
|
|
|
-record(state, {socket, pid, xml_stream_pid,
|
2003-02-02 20:49:19 +01:00
|
|
|
user = "", server = ?MYNAME, resource = ""
|
|
|
|
}).
|
|
|
|
|
|
|
|
%-define(DBGFSM, true).
|
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-02-05 21:49:26 +01:00
|
|
|
start(File) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
User = filename:rootname(filename:basename(File)),
|
2003-02-05 21:49:26 +01:00
|
|
|
start(File, User).
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2003-02-05 21:49:26 +01:00
|
|
|
start(File, User) ->
|
|
|
|
gen_fsm:start(?MODULE, [File, User, self()], ?FSMOPTS).
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, StopReason}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-02-05 21:28:49 +01:00
|
|
|
init([File, User, Pid]) ->
|
2003-10-17 21:15:38 +02:00
|
|
|
% Profiling
|
|
|
|
%eprof:start(),
|
|
|
|
%eprof:profile([self()]),
|
2003-02-02 20:49:19 +01:00
|
|
|
XMLStreamPid = xml_stream:start(self()),
|
|
|
|
{ok, Text} = file:read_file(File),
|
|
|
|
xml_stream:send_text(XMLStreamPid, Text),
|
2003-10-17 21:15:38 +02:00
|
|
|
{ok, wait_for_xdb, #state{user = User, pid = Pid,
|
|
|
|
xml_stream_pid = XMLStreamPid}}.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
wait_for_xdb({xmlstreamstart, Name, _Attrs}, StateData) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
case Name of
|
|
|
|
"xdb" ->
|
|
|
|
{next_state, xdb_data, StateData};
|
|
|
|
_ ->
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_xdb(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
xdb_data({xmlstreamelement, El}, StateData) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
{xmlelement, _Name, Attrs, _Els} = El,
|
2003-02-02 20:49:19 +01:00
|
|
|
Server = StateData#state.server,
|
2003-10-09 20:09:05 +02:00
|
|
|
From = jlib:make_jid(StateData#state.user, Server, ""),
|
2003-02-02 20:49:19 +01:00
|
|
|
NewState =
|
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
?NS_AUTH ->
|
|
|
|
Password = xml:get_tag_cdata(El),
|
|
|
|
ejabberd_auth:set_password(StateData#state.user, Password),
|
|
|
|
StateData;
|
|
|
|
?NS_ROSTER ->
|
|
|
|
%mod_roster:process_iq(From,
|
|
|
|
% {"", ?MYNAME, ""},
|
2003-12-17 21:13:21 +01:00
|
|
|
% #iq{type = set, xmlns = ?NS_ROSTER, sub_el = El}),
|
2003-02-02 20:49:19 +01:00
|
|
|
mod_roster:set_items(StateData#state.user, El),
|
|
|
|
StateData;
|
2003-02-04 21:45:23 +01:00
|
|
|
?NS_VCARD ->
|
2003-12-17 21:13:21 +01:00
|
|
|
mod_vcard:process_sm_iq(
|
|
|
|
From,
|
|
|
|
jlib:make_jid("", ?MYNAME, ""),
|
|
|
|
#iq{type = set, xmlns = ?NS_VCARD, sub_el = El}),
|
2003-02-04 21:45:23 +01:00
|
|
|
StateData;
|
|
|
|
"jabber:x:offline" ->
|
|
|
|
process_offline(From, El),
|
|
|
|
StateData;
|
2003-02-02 20:49:19 +01:00
|
|
|
%?NS_REGISTER ->
|
|
|
|
% mod_register:process_iq(
|
|
|
|
% {"", "", ""}, {"", ?MYNAME, ""},
|
2003-12-17 21:13:21 +01:00
|
|
|
% #iq{type =set, xmlns = ?NS_REGISTER, xub_el = El}),
|
2003-02-02 20:49:19 +01:00
|
|
|
% User = xml:get_path_s(El, [{elem, "username"}, cdata]),
|
|
|
|
% io:format("user ~s~n", [User]),
|
|
|
|
% StateData;
|
|
|
|
XMLNS ->
|
2003-10-09 20:09:05 +02:00
|
|
|
case xml:get_attr_s("j_private_flag", Attrs) of
|
|
|
|
"1" ->
|
|
|
|
mod_private:process_local_iq(
|
|
|
|
From,
|
|
|
|
jlib:make_jid("", ?MYNAME, ""),
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = set, xmlns = ?NS_PRIVATE,
|
|
|
|
sub_el = {xmlelement, "query", [],
|
|
|
|
[jlib:remove_attr(
|
|
|
|
"j_private_flag",
|
|
|
|
jlib:remove_attr("xdbns", El))]}}),
|
2003-10-09 20:09:05 +02:00
|
|
|
StateData;
|
|
|
|
_ ->
|
|
|
|
io:format("jd2ejd: Unknown namespace \"~s\"~n",
|
|
|
|
[XMLNS]),
|
|
|
|
StateData
|
|
|
|
end
|
2003-02-02 20:49:19 +01:00
|
|
|
end,
|
|
|
|
{next_state, xdb_data, NewState};
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
xdb_data({xmlstreamend, _Name}, StateData) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
xdb_data(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%state_name(Event, From, StateData) ->
|
|
|
|
% Reply = ok,
|
|
|
|
% {reply, Reply, state_name, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-12-17 21:13:21 +01:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-12-17 21:13:21 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName, StateData) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, StateData}.
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_info(_, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
2003-12-17 21:13:21 +01:00
|
|
|
terminate(Reason, _StateName, StateData) ->
|
2003-10-17 21:15:38 +02:00
|
|
|
exit(StateData#state.xml_stream_pid, closed),
|
2003-10-09 20:09:05 +02:00
|
|
|
StateData#state.pid ! {jd2ejd, Reason},
|
2003-10-17 21:15:38 +02:00
|
|
|
% Profiling
|
|
|
|
%eprof:log("/tmp/eprof"),
|
|
|
|
%eprof:analyse(),
|
|
|
|
%eprof:stop(),
|
2003-02-02 20:49:19 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-02-04 21:45:23 +01:00
|
|
|
process_offline(To, {xmlelement, _, _, Els}) ->
|
|
|
|
lists:foreach(fun({xmlelement, _, Attrs, _} = El) ->
|
|
|
|
FromS = xml:get_attr_s("from", Attrs),
|
|
|
|
From = case FromS of
|
|
|
|
"" ->
|
|
|
|
{"", ?MYNAME, ""};
|
|
|
|
_ ->
|
|
|
|
jlib:string_to_jid(FromS)
|
|
|
|
end,
|
|
|
|
case From of
|
|
|
|
error ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
mod_offline:store_packet(From, To, El)
|
|
|
|
end
|
|
|
|
end, Els).
|
|
|
|
|
|
|
|
|
|
|
|
import_file(File) ->
|
2003-10-09 20:09:05 +02:00
|
|
|
clear_queue(),
|
2003-02-05 21:49:26 +01:00
|
|
|
start(File),
|
2003-02-05 21:47:20 +01:00
|
|
|
receive
|
2003-10-09 20:09:05 +02:00
|
|
|
{jd2ejd, Result} -> Result
|
2003-10-17 21:15:38 +02:00
|
|
|
%after 4000 -> timeout
|
2003-02-05 21:47:20 +01:00
|
|
|
end.
|
2003-02-04 21:45:23 +01:00
|
|
|
|
2003-10-09 20:09:05 +02:00
|
|
|
clear_queue() ->
|
|
|
|
receive
|
|
|
|
{jd2ejd, _Result} -> clear_queue
|
|
|
|
after 0 -> ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2003-02-04 21:45:23 +01: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),
|
|
|
|
lists:foreach(
|
|
|
|
fun(FN) ->
|
|
|
|
import_file(filename:join([Dir, FN]))
|
|
|
|
end, MsgFiles),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
2003-02-02 20:49:19 +01:00
|
|
|
|