Unit tests for Literate Haskell
[ohcount] / test / src_dir / erl1.erl
1 %%%----------------------------------------------------------------------
2 %%% File    : erl1.erl
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 %%%----------------------------------------------------------------------
8
9 -module(erl1).
10 -author('jason@ohloh.net').
11 -vsn('$Revision: 1 $ ').
12
13 %% External exports
14 -export([import_file/1,
15          import_dir/1]).
16
17 -include("random.hrl").
18 -include("more_random.hrl").
19
20 %%%----------------------------------------------------------------------
21 %%% API
22 %%%----------------------------------------------------------------------
23
24 import_file(File) ->
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
29         true ->
30             case file:read_file(File) of
31                 {ok, Text} ->
32                     case xml_stream:parse_element(Text) of
33                         El when element(1, El) == xmlelement ->
34                             case catch process_xdb(User, Server, El) of
35                                 {'EXIT', Reason} ->
36                                     ?ERROR_MSG(
37                                        "Error while processing file \"~s\": ~p~n",
38                                        [File, Reason]),
39                                        {error, Reason};
40                                 _ ->
41                                     ok
42                             end;
43                         {error, Reason} ->
44                             ?ERROR_MSG("Can't parse file \"~s\": ~p~n",
45                                        [File, Reason]),
46                             {error, Reason}
47                     end;
48                 {error, Reason} ->
49                     ?ERROR_MSG("Can't read file \"~s\": ~p~n", [File, Reason]),
50                     {error, Reason}
51             end;
52         false ->
53             ?ERROR_MSG("Illegal user/server name in file \"~s\"~n", [File]),
54             {error, "illegal user/server"}
55     end.
56
57
58 import_dir(Dir) ->
59     {ok, Files} = file:list_dir(Dir),
60     MsgFiles = lists:filter(
61                  fun(FN) ->
62                          case string:len(FN) > 4 of
63                              true ->
64                                  string:substr(FN,
65                                                string:len(FN) - 3) == ".xml";
66                              _ ->
67                                  false
68                          end
69                  end, Files),
70     lists:foldl(
71       fun(FN, A) ->
72               Res = import_file(filename:join([Dir, FN])),
73               case {A, Res} of
74                   {ok, ok} -> ok;
75                   {ok, _} -> {error, "see aoabberd log for details"};
76                   _ -> A
77               end
78       end, ok, MsgFiles).
79
80 %%%----------------------------------------------------------------------
81 %%% Internal functions
82 %%%----------------------------------------------------------------------
83
84 process_xdb(User, Server, {xmlelement, Name, _Attrs, Els}) ->
85     case Name of
86         "xdb" ->
87             lists:foreach(
88               fun(El) ->
89                       xdb_data(User, Server, El)
90               end, Els);
91         _ ->
92             ok
93     end.
94
95
96 xdb_data(User, Server, {xmlcdata, _CData}) ->
97     ok;
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
102         ?NS_AUTH ->
103             Password = xml:get_tag_cdata(El),
104             ejabberd_auth:set_password(User, Server, Password),
105             ok;
106         ?NS_ROSTER ->
107             case lists:member(mod_roster_odbc,
108                               gen_mod:loaded_modules(LServer)) of
109                 true ->
110                     catch mod_roster_odbc:set_items(User, Server, El);
111                 false ->
112                     catch mod_roster:set_items(User, Server, El)
113             end,
114             ok;
115         ?NS_LAST ->
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
120                 true ->
121                     catch mod_last_odbc:store_last_info(
122                             User,
123                             Server,
124                             list_to_integer(TimeStamp),
125                             Status);
126                 false ->
127                     catch mod_last:store_last_info(
128                             User,
129                             Server,
130                             list_to_integer(TimeStamp),
131                             Status)
132             end,
133             ok;
134         ?NS_VCARD ->
135             case lists:member(mod_vcard_odbc,
136                               gen_mod:loaded_modules(LServer)) of
137                 true ->
138                     catch mod_vcard_odbc:process_sm_iq(
139                             From,
140                             jlib:make_jid("", Server, ""),
141                             #iq{type = set, xmlns = ?NS_VCARD, sub_el = El});
142                 false ->
143                     catch mod_vcard:process_sm_iq(
144                             From,
145                             jlib:make_jid("", Server, ""),
146                             #iq{type = set, xmlns = ?NS_VCARD, sub_el = El})
147             end,
148             ok;
149         "jabber:x:offline" ->
150             process_offline(Server, From, El),
151             ok;
152         XMLNS ->
153             case xml:get_attr_s("j_private_flag", Attrs) of
154                 "1" ->
155                     catch mod_private:process_sm_iq(
156                             From,
157                             jlib:make_jid("", Server, ""),
158                             #iq{type = set, xmlns = ?NS_PRIVATE,
159                                 sub_el = {xmlelement, "query", [],
160                                           [jlib:remove_attr(
161                                              "j_private_flag",
162                                              jlib:remove_attr("xdbns", El))]}});
163                 _ ->
164                     ?DEBUG("jd2ejd: Unknown namespace \"~s\"~n", [XMLNS])
165             end,
166             ok
167     end.
168
169
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),
174                           From = case FromS of
175                                      "" ->
176                                          jlib:make_jid("", Server, "");
177                                      _ ->
178                                          jlib:string_to_jid(FromS)
179                                  end,
180                           case From of
181                               error ->
182                                   ok;
183                               _ ->
184                                   ejabberd_hooks:run(offline_message_hook,
185                                                      LServer,
186                                                      [From, To, El])
187                           end
188                   end, Els).
189