Annotate

tools/openfire2prosody.lua @ 13146:771eb453e03a

util.sqlite3: Deduplicate query methods There were 3 very similar methods: - :execute() - :execute_query() - :execute_update() The first one returns the prepared statement and is mainly used internally in the library for CREATE statements. The later two only really differ in how the results are returned. Those two are one main method and one small one that only picks out the iterator.
author Kim Alvefur <zash@zash.se>
date Sat, 10 Jun 2023 22:02:15 +0200
parent 13142:879a6a33c21b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5089
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Prosody IM
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5696
diff changeset
4 --
5089
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 package.path = package.path..";../?.lua";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 package.cpath = package.cpath..";../?.so"; -- needed for util.pposix used in datamanager
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
5696
9fba74a28e0c package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 5217
diff changeset
12 local my_name = arg[0];
9fba74a28e0c package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 5217
diff changeset
13 if my_name:match("[/\\]") then
9fba74a28e0c package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 5217
diff changeset
14 package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua");
9fba74a28e0c package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 5217
diff changeset
15 package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so");
9fba74a28e0c package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 5217
diff changeset
16 end
9fba74a28e0c package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 5217
diff changeset
17
13142
879a6a33c21b tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
18 if not pcall(require, "prosody.loader") then
879a6a33c21b tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
19 pcall(require, "loader");
879a6a33c21b tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
20 end
879a6a33c21b tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
21
5089
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 -- ugly workaround for getting datamanager to work outside of prosody :(
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 prosody = { };
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 prosody.platform = "unknown";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 if os.getenv("WINDIR") then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 prosody.platform = "windows";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 elseif package.config:sub(1,1) == "/" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 prosody.platform = "posix";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30
13142
879a6a33c21b tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
31 local parse_xml = require "prosody.util.xml".parse;
5089
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 -----------------------------------------------------------------------
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 package.loaded["util.logger"] = {init = function() return function() end; end}
13142
879a6a33c21b tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
36 local dm = require "prosody.util.datamanager"
5089
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 dm.set_data_path("data");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local arg = ...;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local help = "/? -? ? /h -h /help -help --help";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 if not arg or help:find(arg, 1, true) then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 print([[Openfire importer for Prosody
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 Usage: openfire2prosody.lua filename.xml hostname
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 ]]);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 os.exit(1);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 local host = select(2, ...) or "localhost";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 local file = assert(io.open(arg));
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 local data = assert(file:read("*a"));
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 file:close();
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 local xml = assert(parse_xml(data));
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 assert(xml.name == "Openfire", "The input file is not an Openfire XML export");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 local substatus_mapping = { ["0"] = "none", ["1"] = "to", ["2"] = "from", ["3"] = "both" };
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 for _,tag in ipairs(xml.tags) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 if tag.name == "User" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 local username, password, roster;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 for _,tag in ipairs(tag.tags) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 if tag.name == "Username" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 username = tag:get_text();
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 elseif tag.name == "Password" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 password = tag:get_text();
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 elseif tag.name == "Roster" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 roster = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 local pending = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 for _,tag in ipairs(tag.tags) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 if tag.name == "Item" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 local jid = assert(tag.attr.jid, "Roster item has no JID");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 if tag.attr.substatus ~= "-1" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 local item = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 item.name = tag.attr.name;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 item.subscription = assert(substatus_mapping[tag.attr.substatus], "invalid substatus");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 item.ask = tag.attr.askstatus == "0" and "subscribe" or nil;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 local groups = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 for _,tag in ipairs(tag) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 if tag.name == "Group" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 groups[tag:get_text()] = true;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 item.groups = groups;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 roster[jid] = item;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 if tag.attr.recvstatus == "1" then pending[jid] = true; end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 if next(pending) then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 roster[false] = { pending = pending };
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 assert(username and password, "No username or password");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 local ret, err = dm.store(username, host, "accounts", {password = password});
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 if roster then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 local ret, err = dm.store(username, host, "roster", roster);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 print("["..(err or "success").."] stored roster: "..username.."@"..host.." = "..password);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113