Software /
code /
prosody
Annotate
tools/openfire2prosody.lua @ 5694:7aec065d78a0
Merge 0.9->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 13 Jun 2013 23:21:24 +0100 |
parent | 5217:f2becd36d1d0 |
child | 5696:9fba74a28e0c |
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 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 -- |
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 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 -- ugly workaround for getting datamanager to work outside of prosody :( |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 prosody = { }; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 prosody.platform = "unknown"; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 if os.getenv("WINDIR") then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 prosody.platform = "windows"; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 elseif package.config:sub(1,1) == "/" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 prosody.platform = "posix"; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 end |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 |
5217
f2becd36d1d0
tools/openfire2prosody: Use util.xml.
Waqas Hussain <waqas20@gmail.com>
parents:
5089
diff
changeset
|
21 local parse_xml = require "util.xml".parse; |
5089
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 ----------------------------------------------------------------------- |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 package.loaded["util.logger"] = {init = function() return function() end; end} |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local dm = require "util.datamanager" |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 dm.set_data_path("data"); |
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 local arg = ...; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 local help = "/? -? ? /h -h /help -help --help"; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 if not arg or help:find(arg, 1, true) then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 print([[Openfire importer for Prosody |
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 Usage: openfire2prosody.lua filename.xml hostname |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 ]]); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 os.exit(1); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 end |
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 local host = select(2, ...) or "localhost"; |
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 local file = assert(io.open(arg)); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 local data = assert(file:read("*a")); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 file:close(); |
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 xml = assert(parse_xml(data)); |
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 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
|
49 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 local substatus_mapping = { ["0"] = "none", ["1"] = "to", ["2"] = "from", ["3"] = "both" }; |
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 for _,tag in ipairs(xml.tags) do |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 if tag.name == "User" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 local username, password, roster; |
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 for _,tag in ipairs(tag.tags) do |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 if tag.name == "Username" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 username = tag:get_text(); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 elseif tag.name == "Password" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 password = tag:get_text(); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 elseif tag.name == "Roster" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 roster = {}; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 local pending = {}; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 for _,tag in ipairs(tag.tags) do |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 if tag.name == "Item" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 local jid = assert(tag.attr.jid, "Roster item has no JID"); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 if tag.attr.substatus ~= "-1" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 local item = {}; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 item.name = tag.attr.name; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 item.subscription = assert(substatus_mapping[tag.attr.substatus], "invalid substatus"); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 item.ask = tag.attr.askstatus == "0" and "subscribe" or nil; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 local groups = {}; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 for _,tag in ipairs(tag) do |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 if tag.name == "Group" then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 groups[tag:get_text()] = true; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 end |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 end |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 item.groups = groups; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 roster[jid] = item; |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 end |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 if tag.attr.recvstatus == "1" then pending[jid] = true; end |
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 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 if next(pending) then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 roster[false] = { pending = pending }; |
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 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 assert(username and password, "No username or password"); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 local ret, err = dm.store(username, host, "accounts", {password = password}); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 if roster then |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 local ret, err = dm.store(username, host, "roster", roster); |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 print("["..(err or "success").."] stored roster: "..username.."@"..host.." = "..password); |
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 end |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 end |
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 |