Software /
code /
prosody
Annotate
tools/ejabberdsql2prosody.lua @ 4506:6643c9753453
mod_saslauth: Remove useless import of, and call to nodeprep.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 12 Feb 2012 15:14:35 +0100 |
parent | 4384:7fbcb7e6a6a0 |
child | 4496:11983a1e92c0 |
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 |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2688
diff
changeset
|
3 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2688
diff
changeset
|
4 -- Copyright (C) 2008-2010 Waqas Hussain |
1592
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 |
3541
a72b88953bf2
ejabberdsql2prosody: Added a global 'prosody' table to fix a traceback.
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
10 prosody = {}; |
a72b88953bf2
ejabberdsql2prosody: Added a global 'prosody' table to fix a traceback.
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
11 |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 package.path = package.path ..";../?.lua"; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local serialize = require "util.serialization".serialize; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local st = require "util.stanza"; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 package.loaded["util.logger"] = {init = function() return function() end; end} |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local dm = require "util.datamanager" |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 dm.set_data_path("data"); |
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 function parseFile(filename) |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 ------ |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local file = nil; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 local last = nil; |
4384
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
24 local line = 1; |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local function read(expected) |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local ch; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 if last then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 ch = last; last = nil; |
4384
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
29 else |
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
30 ch = file:read(1); |
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
31 if ch == "\n" then line = line + 1; end |
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
32 end |
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
33 if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil").." on line "..line); end |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 return ch; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 local function pushback(ch) |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 if last then error(); end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 last = ch; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local function peek() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 if not last then last = read(); end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 return last; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 |
1599
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
45 local escapes = { |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
46 ["\\0"] = "\0"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
47 ["\\'"] = "'"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
48 ["\\\""] = "\""; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
49 ["\\b"] = "\b"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
50 ["\\n"] = "\n"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
51 ["\\r"] = "\r"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
52 ["\\t"] = "\t"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
53 ["\\Z"] = "\26"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
54 ["\\\\"] = "\\"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
55 ["\\%"] = "%"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
56 ["\\_"] = "_"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
57 } |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 local function unescape(s) |
1599
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
59 return escapes[s] or error("Unknown escape sequence: "..s); |
1592
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 readString() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 read("'"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 local s = ""; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 local ch = peek(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 if ch == "\\" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 s = s..unescape(read()..read()); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 elseif ch == "'" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 break; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 else |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 s = s..read(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 read("'"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 return s; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 local function readNonString() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 local s = ""; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 if peek() == "," or peek() == ")" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 break; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 else |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 s = s..read(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 return tonumber(s); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 local function readItem() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 if peek() == "'" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 return readString(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 else |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 return readNonString(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 local function readTuple() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 local items = {} |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 read("("); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 while peek() ~= ")" do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 table.insert(items, readItem()); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 if peek() == ")" then break; end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 read(","); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 read(")"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 return items; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 local function readTuples() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 if peek() ~= "(" then read("("); end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 local tuples = {}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 table.insert(tuples, readTuple()); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 if peek() == "," then read() end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 if peek() == ";" then break; end |
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 return tuples; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 local function readTableName() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 local tname = ""; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 while peek() ~= "`" do tname = tname..read(); end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 return tname; |
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 local function readInsert() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 if peek() == nil then return nil; end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 if peek() == ch then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 read(); -- found |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 else -- match failed, skip line |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 while peek() and read() ~= "\n" do end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 return nil; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 local tname = readTableName(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 local tuples = readTuples(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 read(";"); read("\n"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 return tname, tuples; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 local function readFile(filename) |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 file = io.open(filename); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 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
|
141 local t = {}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 local tname, tuples = readInsert(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 if tname then |
1647
066cd784d97b
ejabberdsql2prosody: Fix access of undefined global
Waqas Hussain <waqas20@gmail.com>
parents:
1630
diff
changeset
|
145 if t[tname] then |
066cd784d97b
ejabberdsql2prosody: Fix access of undefined global
Waqas Hussain <waqas20@gmail.com>
parents:
1630
diff
changeset
|
146 local t_name = t[tname]; |
1611
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
147 for i=1,#tuples do |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
148 table.insert(t_name, tuples[i]); |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
149 end |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
150 else |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
151 t[tname] = tuples; |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
152 end |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 elseif peek() == nil then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 break; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 return t; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 return readFile(filename); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 ------ |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 |
1628
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
165 -- XML parser |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
166 local parse_xml = (function() |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
167 local entity_map = setmetatable({ |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
168 ["amp"] = "&"; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
169 ["gt"] = ">"; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
170 ["lt"] = "<"; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
171 ["apos"] = "'"; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
172 ["quot"] = "\""; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
173 }, {__index = function(_, s) |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
174 if s:sub(1,1) == "#" then |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
175 if s:sub(2,2) == "x" then |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
176 return string.char(tonumber(s:sub(3), 16)); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
177 else |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
178 return string.char(tonumber(s:sub(2))); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
179 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
180 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
181 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
182 }); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
183 local function xml_unescape(str) |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
184 return (str:gsub("&(.-);", entity_map)); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
185 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
186 local function parse_tag(s) |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
187 local name,sattr=(s):gmatch("([^%s]+)(.*)")(); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
188 local attr = {}; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
189 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
190 return name, attr; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
191 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
192 return function(xml) |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
193 local stanza = st.stanza("root"); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
194 local regexp = "<([^>]*)>([^<]*)"; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
195 for elem, text in xml:gmatch(regexp) do |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
196 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
197 elseif elem:sub(1,1) == "/" then -- end tag |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
198 elem = elem:sub(2); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
199 stanza:up(); -- TODO check for start-end tag name match |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
200 elseif elem:sub(-1,-1) == "/" then -- empty tag |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
201 elem = elem:sub(1,-2); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
202 local name,attr = parse_tag(elem); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
203 stanza:tag(name, attr):up(); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
204 else -- start tag |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
205 local name,attr = parse_tag(elem); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
206 stanza:tag(name, attr); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
207 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
208 if #text ~= 0 then -- text |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
209 stanza:text(xml_unescape(text)); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
210 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
211 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
212 return stanza.tags[1]; |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
213 end |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
214 end)(); |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
215 -- end of XML parser |
c3ce7cbd123d
ejabberdsql2prosody: Added an XML parser
Waqas Hussain <waqas20@gmail.com>
parents:
1611
diff
changeset
|
216 |
1594
6e639a34e503
ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents:
1593
diff
changeset
|
217 local arg, host = ...; |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 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
|
219 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
|
220 print([[ejabberd SQL DB dump importer for Prosody |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 |
1594
6e639a34e503
ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents:
1593
diff
changeset
|
222 Usage: ejabberdsql2prosody.lua filename.txt hostname |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 |
1593
4e44aa858a4c
ejabberdsql2prosody: Improved help message
Waqas Hussain <waqas20@gmail.com>
parents:
1592
diff
changeset
|
224 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
|
225 mysqldump db_name > filename.txt]]); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 os.exit(1); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 local map = { |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 ["last"] = {"username", "seconds", "state"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 ["privacy_default_list"] = {"username", "name"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 ["privacy_list"] = {"username", "name", "id"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
232 ["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
|
233 ["private_storage"] = {"username", "namespace", "data"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
234 ["rostergroups"] = {"username", "jid", "grp"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
235 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
236 ["spool"] = {"username", "xml", "seq"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
237 ["users"] = {"username", "password"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
238 ["vcard"] = {"username", "vcard"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
239 --["vcard_search"] = {}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
240 } |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
241 local NULL = {}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
242 local t = parseFile(arg); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
243 for name, data in pairs(t) do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
244 local m = map[name]; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
245 if m then |
1601
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
246 if #data > 0 and #data[1] ~= #m then |
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
247 print("[warning] expected "..#m.." columns for table `"..name.."`, found "..#data[1]); |
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
248 end |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
249 for i=1,#data do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
250 local row = data[i]; |
1601
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
251 for j=1,#m do |
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
252 row[m[j]] = row[j]; |
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
253 row[j] = nil; |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
254 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
255 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
256 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
257 end |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
258 --print(serialize(t)); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
259 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
260 for i, row in ipairs(t["users"] or NULL) do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
261 local node, password = row.username, row.password; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
262 local ret, err = dm.store(node, host, "accounts", {password = password}); |
2688
95d665f20c71
ejabberdsql2prosody: Don't print password of imported accounts (thanks azerttyu)
Matthew Wild <mwild1@gmail.com>
parents:
1649
diff
changeset
|
263 print("["..(err or "success").."] accounts: "..node.."@"..host); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
264 end |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
265 |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
266 function roster(node, host, jid, item) |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
267 local roster = dm.load(node, host, "roster") or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
268 roster[jid] = item; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
269 local ret, err = dm.store(node, host, "roster", roster); |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
270 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid); |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
271 end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
272 function roster_pending(node, host, jid) |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
273 local roster = dm.load(node, host, "roster") or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
274 roster.pending = roster.pending or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
275 roster.pending[jid] = true; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
276 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
|
277 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
|
278 end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
279 function roster_group(node, host, jid, group) |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
280 local roster = dm.load(node, host, "roster") or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
281 local item = roster[jid]; |
1603
d11606859400
ejabberdsql2prosody: Fixed a nil global access error
Waqas Hussain <waqas20@gmail.com>
parents:
1602
diff
changeset
|
282 if not item then print("Warning: No roster item "..jid.." for user "..node..", can't put in group "..group); return; end |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
283 item.groups[group] = true; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
284 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
|
285 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
286 end |
1630
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
287 function private_storage(node, host, xmlns, stanza) |
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
288 local private = dm.load(node, host, "private") or {}; |
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
289 private[stanza.name..":"..xmlns] = st.preserialize(stanza); |
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
290 local ret, err = dm.store(node, host, "private", private); |
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
291 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns); |
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
292 end |
1648
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
293 function offline_msg(node, host, t, stanza) |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
294 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
295 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
296 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza)); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
297 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t)); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
298 end |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
299 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
|
300 local node, contact = row.username, row.jid; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
301 local name = row.nick; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
302 if name == "" then name = nil; end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
303 local subscription = row.subscription; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
304 if subscription == "N" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
305 subscription = "none" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
306 elseif subscription == "B" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
307 subscription = "both" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
308 elseif subscription == "F" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
309 subscription = "from" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
310 elseif subscription == "T" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
311 subscription = "to" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
312 else error("Unknown subscription type: "..subscription) end; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
313 local ask = row.ask; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
314 if ask == "N" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
315 ask = nil; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
316 elseif ask == "O" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
317 ask = "subscribe"; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
318 elseif ask == "I" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
319 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
|
320 ask = nil; |
1602
1056369a29df
ejabberdsql2prosody: Add support for rosterusers.ask == "B" (both pending in and out subscriptions)
Waqas Hussain <waqas20@gmail.com>
parents:
1601
diff
changeset
|
321 elseif ask == "B" then |
1056369a29df
ejabberdsql2prosody: Add support for rosterusers.ask == "B" (both pending in and out subscriptions)
Waqas Hussain <waqas20@gmail.com>
parents:
1601
diff
changeset
|
322 roster_pending(node, host, contact); |
1056369a29df
ejabberdsql2prosody: Add support for rosterusers.ask == "B" (both pending in and out subscriptions)
Waqas Hussain <waqas20@gmail.com>
parents:
1601
diff
changeset
|
323 ask = "subscribe"; |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
324 else error("Unknown ask type: "..ask); end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
325 local item = {name = name, ask = ask, subscription = subscription, groups = {}}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
326 roster(node, host, contact, item); |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
327 end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
328 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
|
329 roster_group(row.username, host, row.jid, row.grp); |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
330 end |
1629
f6af348cf497
ejabberdsql2prosody: Added support for vCards
Waqas Hussain <waqas20@gmail.com>
parents:
1628
diff
changeset
|
331 for i, row in ipairs(t["vcard"] or NULL) do |
f6af348cf497
ejabberdsql2prosody: Added support for vCards
Waqas Hussain <waqas20@gmail.com>
parents:
1628
diff
changeset
|
332 local ret, err = dm.store(row.username, host, "vcard", st.preserialize(parse_xml(row.vcard))); |
f6af348cf497
ejabberdsql2prosody: Added support for vCards
Waqas Hussain <waqas20@gmail.com>
parents:
1628
diff
changeset
|
333 print("["..(err or "success").."] vCard: "..row.username.."@"..host); |
f6af348cf497
ejabberdsql2prosody: Added support for vCards
Waqas Hussain <waqas20@gmail.com>
parents:
1628
diff
changeset
|
334 end |
1630
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
335 for i, row in ipairs(t["private_storage"] or NULL) do |
1649
a6698ab04039
ejabberdsql2prosody: Removed unnecessary stanza reserialization for private storage
Waqas Hussain <waqas20@gmail.com>
parents:
1648
diff
changeset
|
336 private_storage(row.username, host, row.namespace, parse_xml(row.data)); |
1630
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
337 end |
1648
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
338 table.sort(t["spool"] or NULL, function(a,b) return a.seq < b.seq; end); -- sort by sequence number, just in case |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
339 local time_offset = os.difftime(os.time(os.date("!*t")), os.time(os.date("*t"))) -- to deal with timezones |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
340 local date_parse = function(s) |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
341 local year, month, day, hour, min, sec = s:match("(....)-?(..)-?(..)T(..):(..):(..)"); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
342 return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec-time_offset}); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
343 end |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
344 for i, row in ipairs(t["spool"] or NULL) do |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
345 local stanza = parse_xml(row.xml); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
346 local last_child = stanza.tags[#stanza.tags]; |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
347 if not last_child or last_child ~= stanza[#stanza] then error("Last child of offline message is not a tag"); end |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
348 if last_child.name ~= "x" and last_child.attr.xmlns ~= "jabber:x:delay" then error("Last child of offline message is not a timestamp"); end |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
349 stanza[#stanza], stanza.tags[#stanza.tags] = nil, nil; |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
350 local t = date_parse(last_child.attr.stamp); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
351 offline_msg(row.username, host, t, stanza); |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
352 end |