Automated merge with http://waqas.ath.cx:8000/
[prosody.git] / tools / ejabberd2prosody.lua
1 #!/usr/bin/env lua
2 -- Prosody IM v0.2
3 -- Copyright (C) 2008 Matthew Wild
4 -- Copyright (C) 2008 Waqas Hussain
5 -- 
6 -- This program is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU General Public License
8 -- as published by the Free Software Foundation; either version 2
9 -- of the License, or (at your option) any later version.
10 -- 
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 -- GNU General Public License for more details.
15 -- 
16 -- You should have received a copy of the GNU General Public License
17 -- along with this program; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 --
20
21
22 \r
23 require "erlparse";\r
24 \r
25 package.path = package.path ..";../?.lua";\r
26 local serialize = require "util.serialization".serialize;
27 local st = require "util.stanza";\r
28 package.loaded["util.logger"] = {init = function() return function() end; end}\r
29 local dm = require "util.datamanager"\r
30 dm.set_data_path("data");\r
31 \r
32 function build_stanza(tuple, stanza)\r
33         if tuple[1] == "xmlelement" then\r
34                 local name = tuple[2];\r
35                 local attr = {};\r
36                 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end\r
37                 local up;\r
38                 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end\r
39                 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end\r
40                 if up then stanza:up(); else return stanza end\r
41         elseif tuple[1] == "xmlcdata" then\r
42                 stanza:text(tuple[2]);\r
43         else\r
44                 error("unknown element type: "..serialize(tuple));\r
45         end\r
46 end\r
47 function build_time(tuple)\r
48         local Megaseconds,Seconds,Microseconds = unpack(tuple);\r
49         return Megaseconds * 1000000 + Seconds;\r
50 end\r
51 \r
52 function vcard(node, host, stanza)\r
53         local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));\r
54         print("["..(err or "success").."] vCard: "..node.."@"..host);\r
55 end\r
56 function password(node, host, password)\r
57         local ret, err = dm.store(node, host, "accounts", {password = password});\r
58         print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);\r
59 end\r
60 function roster(node, host, jid, item)\r
61         local roster = dm.load(node, host, "roster") or {};\r
62         roster[jid] = item;\r
63         local ret, err = dm.store(node, host, "roster", roster);\r
64         print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);\r
65 end\r
66 function private_storage(node, host, xmlns, stanza)\r
67         local private = dm.load(node, host, "private") or {};\r
68         private[xmlns] = st.preserialize(stanza);\r
69         local ret, err = dm.store(node, host, "private", private);\r
70         print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);\r
71 end\r
72 function offline_msg(node, host, t, stanza)\r
73         stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);\r
74         stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);\r
75         local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));\r
76         print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));\r
77 end\r
78 \r
79 \r
80 local filters = {\r
81         passwd = function(tuple)\r
82                 password(tuple[2][1], tuple[2][2], tuple[3]);\r
83         end;\r
84         vcard = function(tuple)\r
85                 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));\r
86         end;\r
87         roster = function(tuple)\r
88                 local node = tuple[3][1]; local host = tuple[3][2];\r
89                 local contact = (type(tuple[4][1]) == "table") and tuple[4][2] or tuple[4][1].."@"..tuple[4][2];\r
90                 local name = tuple[5]; local subscription = tuple[6];\r
91                 local ask = tuple[7]; local groups = tuple[8];\r
92                 if type(name) ~= type("") then name = nil; end\r
93                 if ask == "none" then ask = nil; elseif ask == "out" then ask = "subscribe" else error(ask) end\r
94                 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end\r
95                 local item = {name = name, ask = ask, subscription = subscription, groups = {}};\r
96                 for _, g in ipairs(groups) do item.groups[g] = true; end\r
97                 roster(node, host, contact, item);\r
98         end;\r
99         private_storage = function(tuple)\r
100                 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));\r
101         end;\r
102         offline_msg = function(tuple)\r
103                 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));\r
104         end;\r
105         config = function(tuple)\r
106                 if tuple[2] == "hosts" then\r
107                         local output = io.output(); io.output("prosody.cfg.lua");\r
108                         io.write("-- Configuration imported from ejabberd --\n");\r
109                         io.write([[Host "*"\r
110         modules_enabled = {\r
111                 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.\r
112                 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.\r
113                 "roster"; -- Allow users to have a roster. Recommended ;)\r
114                 "register"; -- Allow users to register on this server using a client\r
115                 "tls"; -- Add support for secure TLS on c2s/s2s connections\r
116                 "vcard"; -- Allow users to set vCards\r
117                 "private"; -- Private XML storage (for room bookmarks, etc.)\r
118                 "version"; -- Replies to server version requests\r
119                 "dialback"; -- s2s dialback support\r
120                 "uptime";\r
121                 "disco";\r
122                 "time";\r
123                 "ping";\r
124                 --"selftests";\r
125         };\r
126 ]]);\r
127                         for _, h in ipairs(tuple[3]) do\r
128                                 io.write("Host \"" .. h .. "\"\n");\r
129                         end\r
130                         io.output(output);\r
131                         print("prosody.cfg.lua created");\r
132                 end\r
133         end;\r
134 };\r
135 \r
136 local arg = ...;\r
137 local help = "/? -? ? /h -h /help -help --help";\r
138 if not arg or help:find(arg, 1, true) then\r
139         print([[ejabberd db dump importer for Prosody\r
140 \r
141   Usage: ejabberd2prosody.lua filename.txt\r
142 \r
143 The file can be generated from ejabberd using:\r
144   sudo ./bin/ejabberdctl dump filename.txt\r
145 \r
146 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);\r
147         os.exit(1);\r
148 end\r
149 local count = 0;\r
150 local t = {};\r
151 for item in erlparse.parseFile(arg) do\r
152         count = count + 1;\r
153         local name = item[1];\r
154         t[name] = (t[name] or 0) + 1;\r
155         --print(count, serialize(item));\r
156         if filters[name] then filters[name](item); end\r
157 end\r
158 --print(serialize(t));\r