Software /
code /
prosody
Annotate
tools/ejabberdsql2prosody.lua @ 13270:14bbfb2cc8dd default tip
lint: Teach luacheck about module:once
Silence warning for using this introduced in 9c62ffbdf2ae
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 15 Oct 2023 16:43:14 +0200 |
parent | 13142:879a6a33c21b |
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 |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5696
diff
changeset
|
5 -- |
1592
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"; |
5696
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
13 |
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
14 local my_name = arg[0]; |
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
15 if my_name:match("[/\\]") then |
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
16 package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua"); |
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
17 package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so"); |
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
18 end |
13142
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
19 if not pcall(require, "prosody.loader") then |
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
20 pcall(require, "loader"); |
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
21 end |
5696
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
22 |
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5215
diff
changeset
|
23 |
13142
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
24 local serialize = require "prosody.util.serialization".serialize; |
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
25 local st = require "prosody.util.stanza"; |
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
26 local parse_xml = require "prosody.util.xml".parse; |
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
27 package.loaded["prosody.util.logger"] = {init = function() return function() end; end} |
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
7568
diff
changeset
|
28 local dm = require "prosody.util.datamanager" |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 dm.set_data_path("data"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 function parseFile(filename) |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 ------ |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 local file = nil; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 local last = nil; |
4384
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
36 local line = 1; |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 local function read(expected) |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local ch; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 if last then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 ch = last; last = nil; |
4384
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
41 else |
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
42 ch = file:read(1); |
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
43 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
|
44 end |
7fbcb7e6a6a0
tools/ejabberdsql2prosody: Track current line for error reporting
Matthew Wild <mwild1@gmail.com>
parents:
3541
diff
changeset
|
45 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
|
46 return ch; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 local function peek() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 if not last then last = read(); end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 return last; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 |
1599
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
53 local escapes = { |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
54 ["\\0"] = "\0"; |
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 ["\\b"] = "\b"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
58 ["\\n"] = "\n"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
59 ["\\r"] = "\r"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
60 ["\\t"] = "\t"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
61 ["\\Z"] = "\26"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
62 ["\\\\"] = "\\"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
63 ["\\%"] = "%"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
64 ["\\_"] = "_"; |
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
65 } |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 local function unescape(s) |
1599
308986a3e66a
ejabberdsql2prosody: Added support for all mysql escape sequences
Waqas Hussain <waqas20@gmail.com>
parents:
1598
diff
changeset
|
67 return escapes[s] or error("Unknown escape sequence: "..s); |
1592
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 local function readString() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 read("'"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 local s = ""; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 local ch = peek(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 if ch == "\\" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 s = s..unescape(read()..read()); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 elseif ch == "'" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 break; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 else |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 s = s..read(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 read("'"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 return s; |
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 local function readNonString() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 local s = ""; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 if peek() == "," or peek() == ")" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 break; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 else |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 s = s..read(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 end |
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 return tonumber(s); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 local function readItem() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 if peek() == "'" then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 return readString(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 else |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 return readNonString(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 end |
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 local function readTuple() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 local items = {} |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 read("("); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 while peek() ~= ")" do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 table.insert(items, readItem()); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 if peek() == ")" then break; end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 read(","); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 read(")"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 return items; |
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 local function readTuples() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 if peek() ~= "(" then read("("); end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 local tuples = {}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 table.insert(tuples, readTuple()); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 if peek() == "," then read() end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 if peek() == ";" then break; end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 return tuples; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 local function readTableName() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 local tname = ""; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 while peek() ~= "`" do tname = tname..read(); end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 return tname; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 local function readInsert() |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 if peek() == nil then return nil; end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 for ch in ("INSERT INTO `"):gmatch(".") do -- find line starting with this |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 if peek() == ch then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 read(); -- found |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 else -- match failed, skip line |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 while peek() and read() ~= "\n" do end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 return nil; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 local tname = readTableName(); |
4496
11983a1e92c0
tools/ejabberdsql2prosody: Handle INSERT statement form where column list is specified (by skipping the column list).
Waqas Hussain <waqas20@gmail.com>
parents:
4384
diff
changeset
|
140 read("`"); read(" ") -- expect this |
11983a1e92c0
tools/ejabberdsql2prosody: Handle INSERT statement form where column list is specified (by skipping the column list).
Waqas Hussain <waqas20@gmail.com>
parents:
4384
diff
changeset
|
141 if peek() == "(" then -- skip column list |
11983a1e92c0
tools/ejabberdsql2prosody: Handle INSERT statement form where column list is specified (by skipping the column list).
Waqas Hussain <waqas20@gmail.com>
parents:
4384
diff
changeset
|
142 repeat until read() == ")"; |
11983a1e92c0
tools/ejabberdsql2prosody: Handle INSERT statement form where column list is specified (by skipping the column list).
Waqas Hussain <waqas20@gmail.com>
parents:
4384
diff
changeset
|
143 read(" "); |
11983a1e92c0
tools/ejabberdsql2prosody: Handle INSERT statement form where column list is specified (by skipping the column list).
Waqas Hussain <waqas20@gmail.com>
parents:
4384
diff
changeset
|
144 end |
11983a1e92c0
tools/ejabberdsql2prosody: Handle INSERT statement form where column list is specified (by skipping the column list).
Waqas Hussain <waqas20@gmail.com>
parents:
4384
diff
changeset
|
145 for ch in ("VALUES "):gmatch(".") do read(ch); end -- expect this |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 local tuples = readTuples(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 read(";"); read("\n"); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 return tname, tuples; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 local function readFile(filename) |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 file = io.open(filename); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 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
|
154 local t = {}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 while true do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 local tname, tuples = readInsert(); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 if tname then |
1647
066cd784d97b
ejabberdsql2prosody: Fix access of undefined global
Waqas Hussain <waqas20@gmail.com>
parents:
1630
diff
changeset
|
158 if t[tname] then |
066cd784d97b
ejabberdsql2prosody: Fix access of undefined global
Waqas Hussain <waqas20@gmail.com>
parents:
1630
diff
changeset
|
159 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
|
160 for i=1,#tuples do |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
161 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
|
162 end |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
163 else |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
164 t[tname] = tuples; |
e20f90743863
ejabberdsql2prosody: Allow for multiple INSERTs to the same table
Waqas Hussain <waqas20@gmail.com>
parents:
1603
diff
changeset
|
165 end |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 elseif peek() == nil then |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 break; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 return t; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 return readFile(filename); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 ------ |
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 |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
178 local arg, hostname = ...; |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 local help = "/? -? ? /h -h /help -help --help"; |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
180 if not(arg and hostname) or help:find(arg, 1, true) then |
1593
4e44aa858a4c
ejabberdsql2prosody: Improved help message
Waqas Hussain <waqas20@gmail.com>
parents:
1592
diff
changeset
|
181 print([[ejabberd SQL DB dump importer for Prosody |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 |
1594
6e639a34e503
ejabberdsql2prosody: Added a second required command line parameter: hostname
Waqas Hussain <waqas20@gmail.com>
parents:
1593
diff
changeset
|
183 Usage: ejabberdsql2prosody.lua filename.txt hostname |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 |
1593
4e44aa858a4c
ejabberdsql2prosody: Improved help message
Waqas Hussain <waqas20@gmail.com>
parents:
1592
diff
changeset
|
185 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
|
186 mysqldump db_name > filename.txt]]); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 os.exit(1); |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 local map = { |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 ["last"] = {"username", "seconds", "state"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 ["privacy_default_list"] = {"username", "name"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 ["privacy_list"] = {"username", "name", "id"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 ["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
|
194 ["private_storage"] = {"username", "namespace", "data"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 ["rostergroups"] = {"username", "jid", "grp"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 ["rosterusers"] = {"username", "jid", "nick", "subscription", "ask", "askmessage", "server", "subscribe", "type"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 ["spool"] = {"username", "xml", "seq"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 ["users"] = {"username", "password"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 ["vcard"] = {"username", "vcard"}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 --["vcard_search"] = {}; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 } |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 local NULL = {}; |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
203 local parsed = parseFile(arg); |
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
204 for name, data in pairs(parsed) do |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 local m = map[name]; |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 if m then |
1601
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
207 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
|
208 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
|
209 end |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 for i=1,#data do |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 local row = data[i]; |
1601
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
212 for j=1,#m do |
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
213 row[m[j]] = row[j]; |
a3ffc27b6207
ejabberdsql2prosody: Fix typo, and improve the warning message
Waqas Hussain <waqas20@gmail.com>
parents:
1600
diff
changeset
|
214 row[j] = nil; |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 end |
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 end |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
219 --print(serialize(t)); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
221 for _, row in ipairs(parsed["users"] or NULL) do |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 local node, password = row.username, row.password; |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
223 local ret, err = dm.store(node, hostname, "accounts", {password = password}); |
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
224 print("["..(err or "success").."] accounts: "..node.."@"..hostname); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 end |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
226 |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
227 function roster(node, host, jid, item) |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
228 local roster = dm.load(node, host, "roster") or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
229 roster[jid] = item; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
230 local ret, err = dm.store(node, host, "roster", roster); |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
231 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid); |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
232 end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
233 function roster_pending(node, host, jid) |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
234 local roster = dm.load(node, host, "roster") or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
235 roster.pending = roster.pending or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
236 roster.pending[jid] = true; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
237 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
|
238 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
|
239 end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
240 function roster_group(node, host, jid, group) |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
241 local roster = dm.load(node, host, "roster") or {}; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
242 local item = roster[jid]; |
1603
d11606859400
ejabberdsql2prosody: Fixed a nil global access error
Waqas Hussain <waqas20@gmail.com>
parents:
1602
diff
changeset
|
243 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
|
244 item.groups[group] = true; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
245 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
|
246 print("["..(err or "success").."] roster-group: " ..node.."@"..host.." - "..jid.." - "..group); |
1592
a7c140fc672b
ejabberdsql2prosody: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
247 end |
1630
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
248 function private_storage(node, host, xmlns, stanza) |
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
249 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
|
250 private[stanza.name..":"..xmlns] = st.preserialize(stanza); |
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
251 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
|
252 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
|
253 end |
1648
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
254 function offline_msg(node, host, t, stanza) |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 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
|
259 end |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
260 for _, row in ipairs(parsed["rosterusers"] or NULL) do |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
261 local node, contact = row.username, row.jid; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
262 local name = row.nick; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
263 if name == "" then name = nil; end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
264 local subscription = row.subscription; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
265 if subscription == "N" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
266 subscription = "none" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
267 elseif subscription == "B" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
268 subscription = "both" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
269 elseif subscription == "F" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
270 subscription = "from" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
271 elseif subscription == "T" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
272 subscription = "to" |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
273 else error("Unknown subscription type: "..subscription) end; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
274 local ask = row.ask; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
275 if ask == "N" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
276 ask = nil; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
277 elseif ask == "O" then |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
278 ask = "subscribe"; |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
279 elseif ask == "I" then |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
280 roster_pending(node, hostname, contact); |
1597
84328a4df655
ejabberdsql2prosody: Fixed: 'ask' value should be nil for pending-in subscriptions
Waqas Hussain <waqas20@gmail.com>
parents:
1596
diff
changeset
|
281 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
|
282 elseif ask == "B" then |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
283 roster_pending(node, hostname, contact); |
1602
1056369a29df
ejabberdsql2prosody: Add support for rosterusers.ask == "B" (both pending in and out subscriptions)
Waqas Hussain <waqas20@gmail.com>
parents:
1601
diff
changeset
|
284 ask = "subscribe"; |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
285 else error("Unknown ask type: "..ask); end |
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
286 local item = {name = name, ask = ask, subscription = subscription, groups = {}}; |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
287 roster(node, hostname, contact, item); |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
288 end |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
289 for _, row in ipairs(parsed["rostergroups"] or NULL) do |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
290 roster_group(row.username, hostname, row.jid, row.grp); |
1595
9551055b5344
ejabberdsql2prosody: Added support for rosters
Waqas Hussain <waqas20@gmail.com>
parents:
1594
diff
changeset
|
291 end |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
292 for _, row in ipairs(parsed["vcard"] or NULL) do |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
293 local stanza, err = parse_xml(row.vcard); |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
294 if stanza then |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
295 local ret, err = dm.store(row.username, hostname, "vcard", st.preserialize(stanza)); |
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
296 print("["..(err or "success").."] vCard: "..row.username.."@"..hostname); |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
297 else |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
298 print("[error] vCard XML parse failed: "..row.username.."@"..hostname); |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
299 end |
1629
f6af348cf497
ejabberdsql2prosody: Added support for vCards
Waqas Hussain <waqas20@gmail.com>
parents:
1628
diff
changeset
|
300 end |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
301 for _, row in ipairs(parsed["private_storage"] or NULL) do |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
302 local stanza, err = parse_xml(row.data); |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
303 if stanza then |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
304 private_storage(row.username, hostname, row.namespace, stanza); |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
305 else |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
306 print("[error] Private XML parse failed: "..row.username.."@"..hostname); |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
307 end |
1630
cb3d937dd1da
ejabberdsql2prosody: Added support for Private XML Storage
Waqas Hussain <waqas20@gmail.com>
parents:
1629
diff
changeset
|
308 end |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
309 table.sort(parsed["spool"] or NULL, function(a,b) return a.seq < b.seq; end); -- sort by sequence number, just in case |
1648
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
310 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
|
311 local date_parse = function(s) |
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
312 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
|
313 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
|
314 end |
7568
4f7990902874
ejabberdsql2prosody: rename variable 't' to prevent shadowing upvalues [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7567
diff
changeset
|
315 for _, row in ipairs(parsed["spool"] or NULL) do |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
316 local stanza, err = parse_xml(row.xml); |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
317 if stanza then |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
318 local last_child = stanza.tags[#stanza.tags]; |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
319 if not last_child or last_child ~= stanza[#stanza] then error("Last child of offline message is not a tag"); end |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
320 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 |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
321 stanza[#stanza], stanza.tags[#stanza.tags] = nil, nil; |
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
322 local t = date_parse(last_child.attr.stamp); |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
323 offline_msg(row.username, hostname, t, stanza); |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
324 else |
7567
495de404a8ae
ejabberdsql2prosody: rename variable 'host' to prevent shadowing upvalue [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents:
7566
diff
changeset
|
325 print("[error] Offline message XML parsing failed: "..row.username.."@"..hostname); |
5991
a4b4b152a7d6
tools/ejabberdsql2prosody: Skip invalid XML in data, and print out errors.
Waqas Hussain <waqas20@gmail.com>
parents:
5696
diff
changeset
|
326 end |
1648
b05371e6f058
ejabberdsql2prosody: Added support for offline messages
Waqas Hussain <waqas20@gmail.com>
parents:
1647
diff
changeset
|
327 end |