1 %%%----------------------------------------------------------------------
3 %%% Author : Jason Allen <jason@ohloh.net> - kinda - just repurposing random code
4 %%% Purpose : Test out the erlang parsing
5 %%% Created : 1/17/2007 by Jason Allen <jason@ohloh.net>
6 %%% Id : $Id: erl1.erl 1 2007-17-01 jason $
7 %%%----------------------------------------------------------------------
10 -author('jason@ohloh.net').
11 -vsn('$Revision: 1 $ ').
14 -export([import_file/1,
17 -include("random.hrl").
18 -include("more_random.hrl").
20 %%%----------------------------------------------------------------------
22 %%%----------------------------------------------------------------------
25 User = filename:rootname(filename:basename(File)),
26 Server = filename:basename(filename:dirname(File)),
27 case (jlib:nodeprep(User) /= error) andalso
28 (jlib:nameprep(Server) /= error) of
30 case file:read_file(File) of
32 case xml_stream:parse_element(Text) of
33 El when element(1, El) == xmlelement ->
34 case catch process_xdb(User, Server, El) of
37 "Error while processing file \"~s\": ~p~n",
44 ?ERROR_MSG("Can't parse file \"~s\": ~p~n",
49 ?ERROR_MSG("Can't read file \"~s\": ~p~n", [File, Reason]),
53 ?ERROR_MSG("Illegal user/server name in file \"~s\"~n", [File]),
54 {error, "illegal user/server"}
59 {ok, Files} = file:list_dir(Dir),
60 MsgFiles = lists:filter(
62 case string:len(FN) > 4 of
65 string:len(FN) - 3) == ".xml";
72 Res = import_file(filename:join([Dir, FN])),
75 {ok, _} -> {error, "see aoabberd log for details"};
80 %%%----------------------------------------------------------------------
81 %%% Internal functions
82 %%%----------------------------------------------------------------------
84 process_xdb(User, Server, {xmlelement, Name, _Attrs, Els}) ->
89 xdb_data(User, Server, El)
96 xdb_data(User, Server, {xmlcdata, _CData}) ->
98 xdb_data(User, Server, {xmlelement, _Name, Attrs, _Els} = El) ->
99 From = jlib:make_jid(User, Server, ""),
100 LServer = jlib:nameprep(Server),
101 case xml:get_attr_s("xmlns", Attrs) of
103 Password = xml:get_tag_cdata(El),
104 ejabberd_auth:set_password(User, Server, Password),
107 case lists:member(mod_roster_odbc,
108 gen_mod:loaded_modules(LServer)) of
110 catch mod_roster_odbc:set_items(User, Server, El);
112 catch mod_roster:set_items(User, Server, El)
116 TimeStamp = xml:get_attr_s("last", Attrs),
117 Status = xml:get_tag_cdata(El),
118 case lists:member(mod_last_odbc,
119 gen_mod:loaded_modules(LServer)) of
121 catch mod_last_odbc:store_last_info(
124 list_to_integer(TimeStamp),
127 catch mod_last:store_last_info(
130 list_to_integer(TimeStamp),
135 case lists:member(mod_vcard_odbc,
136 gen_mod:loaded_modules(LServer)) of
138 catch mod_vcard_odbc:process_sm_iq(
140 jlib:make_jid("", Server, ""),
141 #iq{type = set, xmlns = ?NS_VCARD, sub_el = El});
143 catch mod_vcard:process_sm_iq(
145 jlib:make_jid("", Server, ""),
146 #iq{type = set, xmlns = ?NS_VCARD, sub_el = El})
149 "jabber:x:offline" ->
150 process_offline(Server, From, El),
153 case xml:get_attr_s("j_private_flag", Attrs) of
155 catch mod_private:process_sm_iq(
157 jlib:make_jid("", Server, ""),
158 #iq{type = set, xmlns = ?NS_PRIVATE,
159 sub_el = {xmlelement, "query", [],
162 jlib:remove_attr("xdbns", El))]}});
164 ?DEBUG("jd2ejd: Unknown namespace \"~s\"~n", [XMLNS])
170 process_offline(Server, To, {xmlelement, _, _, Els}) ->
171 LServer = jlib:nameprep(Server),
172 lists:foreach(fun({xmlelement, _, Attrs, _} = El) ->
173 FromS = xml:get_attr_s("from", Attrs),
176 jlib:make_jid("", Server, "");
178 jlib:string_to_jid(FromS)
184 ejabberd_hooks:run(offline_message_hook,