Annotate

tools/openfire2prosody.lua @ 13078:6da83deb8d7f 0.12

util.error: Fix error on conversion of invalid error stanza, fix #1805 Error stanzas should have an <error> element, but if you pass a stanza without one to util.error.from_stanza() it triggers an attempt to index a nil value, which this patch avoids. In the conditional, it should be safe to assume error_tag is non-nil since condition can't have those values then.
author Kim Alvefur <zash@zash.se>
date Wed, 19 Apr 2023 11:32:53 +0200
parent 5776:bd0ff8ae98a8
child 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
5089
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 -- ugly workaround for getting datamanager to work outside of prosody :(
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 prosody = { };
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 prosody.platform = "unknown";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 if os.getenv("WINDIR") then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 prosody.platform = "windows";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 elseif package.config:sub(1,1) == "/" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 prosody.platform = "posix";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26
5217
f2becd36d1d0 tools/openfire2prosody: Use util.xml.
Waqas Hussain <waqas20@gmail.com>
parents: 5089
diff changeset
27 local parse_xml = require "util.xml".parse;
5089
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 -----------------------------------------------------------------------
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 package.loaded["util.logger"] = {init = function() return function() end; end}
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 local dm = require "util.datamanager"
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 dm.set_data_path("data");
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 local arg = ...;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 local help = "/? -? ? /h -h /help -help --help";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 if not arg or help:find(arg, 1, true) then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 print([[Openfire importer for Prosody
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 Usage: openfire2prosody.lua filename.xml hostname
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 ]]);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 os.exit(1);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 end
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 local host = select(2, ...) or "localhost";
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 local file = assert(io.open(arg));
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 local data = assert(file:read("*a"));
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 file:close();
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 xml = assert(parse_xml(data));
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 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
55
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 local substatus_mapping = { ["0"] = "none", ["1"] = "to", ["2"] = "from", ["3"] = "both" };
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 for _,tag in ipairs(xml.tags) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 if tag.name == "User" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 local username, password, roster;
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(tag.tags) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 if tag.name == "Username" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 username = tag:get_text();
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 elseif tag.name == "Password" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 password = tag:get_text();
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 elseif tag.name == "Roster" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 roster = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 local pending = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 for _,tag in ipairs(tag.tags) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 if tag.name == "Item" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 local jid = assert(tag.attr.jid, "Roster item has no JID");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 if tag.attr.substatus ~= "-1" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 local item = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 item.name = tag.attr.name;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 item.subscription = assert(substatus_mapping[tag.attr.substatus], "invalid substatus");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 item.ask = tag.attr.askstatus == "0" and "subscribe" or nil;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 local groups = {};
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 for _,tag in ipairs(tag) do
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 if tag.name == "Group" then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 groups[tag:get_text()] = true;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 item.groups = groups;
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 roster[jid] = item;
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 if tag.attr.recvstatus == "1" then pending[jid] = true; end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 if next(pending) then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 roster[false] = { pending = pending };
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 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 assert(username and password, "No username or password");
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 local ret, err = dm.store(username, host, "accounts", {password = password});
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 if roster then
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 local ret, err = dm.store(username, host, "roster", roster);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 print("["..(err or "success").."] stored roster: "..username.."@"..host.." = "..password);
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
a5b683909f79 tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109