Welcome to mirror list, hosted at ThFree Co, Russian Federation.

jd2ejd.erl « src - github.com/processone/ejabberd.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 974b80540bc9a7a2d569e22d25c0f43d96cad548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
%%%----------------------------------------------------------------------
%%% 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$ ').

%% External exports
-export([import_file/1,
	 import_dir/1]).

-include("ejabberd.hrl").
-include("jlib.hrl").



%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------

import_file(File) ->
    User = filename:rootname(filename:basename(File)),
    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",
				       [File, Reason]);
				_ ->
				    ok
			    end;
			{error, Reason} ->
			    ?ERROR_MSG("Can't parse file \"~s\": ~p~n",
				       [File, Reason])
		    end;
		{error, Reason} ->
		    ?ERROR_MSG("Can't read file \"~s\": ~p~n", [File, Reason])
	    end;
	false ->
	    ?ERROR_MSG("Incorrect user/server name in file \"~s\"~n", [File])
    end.


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.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

process_xdb(User, Server, {xmlelement, Name, _Attrs, Els}) ->
    case Name of
	"xdb" ->
	    lists:foreach(
	      fun(El) ->
		      xdb_data(User, Server, El)
	      end, Els);
	_ ->
	    ok
    end.


xdb_data(User, Server, El) ->
    {xmlelement, _Name, Attrs, _Els} = El,
    From = jlib:make_jid(User, Server, ""),
    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 ->
	    catch mod_roster:set_items(User, Server, El),
	    ok;
	?NS_VCARD ->
	    catch mod_vcard:process_sm_iq(
		    From,
		    jlib:make_jid("", ?MYNAME, ""),
		    #iq{type = set, xmlns = ?NS_VCARD, sub_el = El}),
	    ok;
	"jabber:x:offline" ->
	    process_offline(From, El),
	    ok;
	XMLNS ->
	    case xml:get_attr_s("j_private_flag", Attrs) of
		"1" ->
		    catch mod_private:process_local_iq(
			    From,
			    jlib:make_jid("", ?MYNAME, ""),
			    #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.


process_offline(To, {xmlelement, _, _, Els}) ->
    lists:foreach(fun({xmlelement, _, Attrs, _} = El) ->
			  FromS = xml:get_attr_s("from", Attrs),
			  From = case FromS of
				     "" ->
					 jlib:make_jid("", ?MYNAME, "");
				     _ ->
					 jlib:string_to_jid(FromS)
				 end,
			  case From of
			      error ->
				  ok;
			      _ ->
				  catch mod_offline:store_packet(From, To, El)
			  end
		  end, Els).