Annotate

tools/ejabberdsql2prosody.lua @ 1598:d82e13d57821

ejabberdsql2prosody: Added support for the escape sequence '\\' in strings
author Waqas Hussain <waqas20@gmail.com>
date Sun, 26 Jul 2009 17:29:56 +0500
parent 1597:84328a4df655
child 1599:308986a3e66a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Prosody IM
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Matthew Wild
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 -- Copyright (C) 2008-2009 Waqas Hussain
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 --
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 --
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 package.path = package.path ..";../?.lua";
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local serialize = require "util.serialization".serialize;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local st = require "util.stanza";
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 package.loaded["util.logger"] = {init = function() return function() end; end}
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local dm = require "util.datamanager"
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 dm.set_data_path("data");
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 function parseFile(filename)
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 ------
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local file = nil;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local last = nil;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local function read(expected)
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local ch;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 if last then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 ch = last; last = nil;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 else ch = file:read(1); end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil")); end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 return ch;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local function pushback(ch)
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 if last then error(); end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 last = ch;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local function peek()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 if not last then last = read(); end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 return last;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local function unescape(s)
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 if s == "\\'" then return "'"; end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 if s == "\\n" then return "\n"; end
1598
d82e13d57821 ejabberdsql2prosody: Added support for the escape sequence '\\' in strings
Waqas Hussain <waqas20@gmail.com>
parents: 1597
diff changeset
42 if s == "\\\\" then return "\\"; end
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 error("Unknown escape sequence: "..s);
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 local function readString()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 read("'");
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 local s = "";
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 while true do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 local ch = peek();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 if ch == "\\" then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 s = s..unescape(read()..read());
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 elseif ch == "'" then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 break;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 else
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 s = s..read();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 read("'");
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 return s;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 local function readNonString()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 local s = "";
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 while true do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 if peek() == "," or peek() == ")" then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 break;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 else
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 s = s..read();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 return tonumber(s);
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 local function readItem()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 if peek() == "'" then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 return readString();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 else
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 return readNonString();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 local function readTuple()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 local items = {}
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 read("(");
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 while peek() ~= ")" do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 table.insert(items, readItem());
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 if peek() == ")" then break; end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 read(",");
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 read(")");
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 return items;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 local function readTuples()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 if peek() ~= "(" then read("("); end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 local tuples = {};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 while true do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 table.insert(tuples, readTuple());
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 if peek() == "," then read() end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 if peek() == ";" then break; end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 return tuples;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 local function readTableName()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 local tname = "";
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 while peek() ~= "`" do tname = tname..read(); end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 return tname;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 local function readInsert()
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 if peek() == nil then return nil; end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 if peek() == ch then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 read(); -- found
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 else -- match failed, skip line
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 while peek() and read() ~= "\n" do end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 return nil;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 local tname = readTableName();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 local tuples = readTuples();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 read(";"); read("\n");
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 return tname, tuples;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 local function readFile(filename)
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 file = io.open(filename);
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 if not file then error("File not found: "..filename); os.exit(0); end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 local t = {};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 while true do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 local tname, tuples = readInsert();
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 if tname then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 t[tname] = tuples;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 elseif peek() == nil then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 break;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 return t;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 return readFile(filename);
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 ------
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141
1594
6e639a34e503 ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1593
diff changeset
142 local arg, host = ...;
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 local help = "/? -? ? /h -h /help -help --help";
1594
6e639a34e503 ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1593
diff changeset
144 if not(arg and host) or help:find(arg, 1, true) then
1593
4e44aa858a4c ejabberdsql2prosody: Improved help message
Waqas Hussain <waqas20@gmail.com>
parents: 1592
diff changeset
145 print([[ejabberd SQL DB dump importer for Prosody
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146
1594
6e639a34e503 ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1593
diff changeset
147 Usage: ejabberdsql2prosody.lua filename.txt hostname
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148
1593
4e44aa858a4c ejabberdsql2prosody: Improved help message
Waqas Hussain <waqas20@gmail.com>
parents: 1592
diff changeset
149 The file can be generated using mysqldump:
1594
6e639a34e503 ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1593
diff changeset
150 mysqldump db_name > filename.txt]]);
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 os.exit(1);
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 local map = {
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 ["last"] = {"username", "seconds", "state"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 ["privacy_default_list"] = {"username", "name"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 ["privacy_list"] = {"username", "name", "id"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 ["privacy_list_data"] = {"id", "t", "value", "action", "ord", "match_all", "match_iq", "match_message", "match_presence_in", "match_presence_out"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 ["private_storage"] = {"username", "namespace", "data"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 ["rostergroups"] = {"username", "jid", "grp"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 ["spool"] = {"username", "xml", "seq"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 ["users"] = {"username", "password"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 ["vcard"] = {"username", "vcard"};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 --["vcard_search"] = {};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 }
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 local NULL = {};
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 local t = parseFile(arg);
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 for name, data in pairs(t) do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 local m = map[name];
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 if m then
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 for i=1,#data do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 local row = data[i];
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 for j=1,#row do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 row[m[j]] = row[j];
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 row[j] = nil;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 end
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 end
1595
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
180 --print(serialize(t));
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 for i, row in ipairs(t["users"] or NULL) do
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 local node, password = row.username, row.password;
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 local ret, err = dm.store(node, host, "accounts", {password = password});
1594
6e639a34e503 ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents: 1593
diff changeset
185 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 end
1595
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
187
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
188 function roster(node, host, jid, item)
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
189 local roster = dm.load(node, host, "roster") or {};
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
190 roster[jid] = item;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
191 local ret, err = dm.store(node, host, "roster", roster);
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
192 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
193 end
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
194 function roster_pending(node, host, jid)
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
195 local roster = dm.load(node, host, "roster") or {};
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
196 roster.pending = roster.pending or {};
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
197 roster.pending[jid] = true;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
198 local ret, err = dm.store(node, host, "roster", roster);
1596
4fcb2df97141 ejabberdsql2prosody: Fixed: pending-in subscriptions could halt processing
Waqas Hussain <waqas20@gmail.com>
parents: 1595
diff changeset
199 print("["..(err or "success").."] roster-pending: " ..node.."@"..host.." - "..jid);
1595
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
200 end
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
201 function roster_group(node, host, jid, group)
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
202 local roster = dm.load(node, host, "roster") or {};
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
203 local item = roster[jid];
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
204 if not item then print("Warning: No roster item "..jid.." for user "..user..", can't put in group "..group); return; end
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
205 item.groups[group] = true;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
206 local ret, err = dm.store(node, host, "roster", roster);
1596
4fcb2df97141 ejabberdsql2prosody: Fixed: pending-in subscriptions could halt processing
Waqas Hussain <waqas20@gmail.com>
parents: 1595
diff changeset
207 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group);
1592
a7c140fc672b ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 end
1595
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
209 for i, row in ipairs(t["rosterusers"] or NULL) do
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
210 local node, contact = row.username, row.jid;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
211 local name = row.nick;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
212 if name == "" then name = nil; end
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
213 local subscription = row.subscription;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
214 if subscription == "N" then
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
215 subscription = "none"
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
216 elseif subscription == "B" then
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
217 subscription = "both"
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
218 elseif subscription == "F" then
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
219 subscription = "from"
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
220 elseif subscription == "T" then
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
221 subscription = "to"
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
222 else error("Unknown subscription type: "..subscription) end;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
223 local ask = row.ask;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
224 if ask == "N" then
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
225 ask = nil;
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
226 elseif ask == "O" then
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
227 ask = "subscribe";
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
228 elseif ask == "I" then
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
229 roster_pending(node, host, contact);
1597
84328a4df655 ejabberdsql2prosody: Fixed: 'ask' value should be nil for pending-in subscriptions
Waqas Hussain <waqas20@gmail.com>
parents: 1596
diff changeset
230 ask = nil;
1595
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
231 else error("Unknown ask type: "..ask); end
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
232 local item = {name = name, ask = ask, subscription = subscription, groups = {}};
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
233 roster(node, host, contact, item);
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
234 end
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
235 for i, row in ipairs(t["rostergroups"] or NULL) do
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
236 roster_group(row.username, host, row.jid, row.grp);
9551055b5344 ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents: 1594
diff changeset
237 end