Software / code / prosody
Comparison
tools/ejabberd2prosody.lua @ 482:b86082df0bc0
ejabberd db dump importer for Prosody
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Sat, 29 Nov 2008 23:59:27 +0500 |
| child | 484:af499a5ee32f |
comparison
equal
deleted
inserted
replaced
| 478:3abf90751a8f | 482:b86082df0bc0 |
|---|---|
| 1 | |
| 2 require "erlparse"; | |
| 3 require "serialize"; | |
| 4 | |
| 5 package.path = package.path ..";../?.lua"; | |
| 6 local st = require "util.stanza"; | |
| 7 package.loaded["util.logger"] = {init = function() return function() end; end} | |
| 8 local dm = require "util.datamanager" | |
| 9 local data_path = "data"; | |
| 10 dm.set_data_path(data_path); | |
| 11 | |
| 12 local _mkdir = {} | |
| 13 function mkdir(path) | |
| 14 path = path:gsub("/", "\\"); | |
| 15 --print("mkdir",path); | |
| 16 local x = io.popen("mkdir "..path.." 2>&1"):read("*a"); | |
| 17 end | |
| 18 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end | |
| 19 function getpath(username, host, datastore, ext) | |
| 20 ext = ext or "dat"; | |
| 21 if username then | |
| 22 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext); | |
| 23 elseif host then | |
| 24 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext); | |
| 25 else | |
| 26 return format("%s/%s.%s", data_path, datastore, ext); | |
| 27 end | |
| 28 end | |
| 29 function mkdirs(host) | |
| 30 if not _mkdir[host] then | |
| 31 local host_dir = string.format("%s/%s", data_path, encode(host)); | |
| 32 mkdir(host_dir); | |
| 33 mkdir(host_dir.."/accounts"); | |
| 34 mkdir(host_dir.."/vcard"); | |
| 35 mkdir(host_dir.."/roster"); | |
| 36 mkdir(host_dir.."/private"); | |
| 37 mkdir(host_dir.."/offline"); | |
| 38 _mkdir[host] = true; | |
| 39 end | |
| 40 end | |
| 41 mkdir(data_path); | |
| 42 | |
| 43 function build_stanza(tuple, stanza) | |
| 44 if tuple[1] == "xmlelement" then | |
| 45 local name = tuple[2]; | |
| 46 local attr = {}; | |
| 47 for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end | |
| 48 local up; | |
| 49 if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end | |
| 50 for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end | |
| 51 if up then stanza:up(); else return stanza end | |
| 52 elseif tuple[1] == "xmlcdata" then | |
| 53 stanza:text(tuple[2]); | |
| 54 else | |
| 55 error("unknown element type: "..serialize.serialize(tuple)); | |
| 56 end | |
| 57 end | |
| 58 function build_time(tuple) | |
| 59 local Megaseconds,Seconds,Microseconds = unpack(tuple); | |
| 60 return Megaseconds * 1000000 + Seconds; | |
| 61 end | |
| 62 | |
| 63 function vcard(node, host, stanza) | |
| 64 mkdirs(host); | |
| 65 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza)); | |
| 66 print("["..(err or "success").."] vCard: "..node.."@"..host); | |
| 67 end | |
| 68 function password(node, host, password) | |
| 69 mkdirs(host); | |
| 70 local ret, err = dm.store(node, host, "accounts", {password = password}); | |
| 71 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password); | |
| 72 end | |
| 73 function roster(node, host, jid, item) | |
| 74 mkdirs(host); | |
| 75 local roster = dm.load(node, host, "roster") or {}; | |
| 76 roster[jid] = item; | |
| 77 local ret, err = dm.store(node, host, "roster", roster); | |
| 78 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid); | |
| 79 end | |
| 80 function private_storage(node, host, xmlns, stanza) | |
| 81 mkdirs(host); | |
| 82 local private = dm.load(node, host, "private") or {}; | |
| 83 private[xmlns] = st.preserialize(stanza); | |
| 84 local ret, err = dm.store(node, host, "private", private); | |
| 85 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns); | |
| 86 end | |
| 87 function offline_msg(node, host, t, stanza) | |
| 88 mkdirs(host); | |
| 89 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t); | |
| 90 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t); | |
| 91 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza)); | |
| 92 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t)); | |
| 93 end | |
| 94 | |
| 95 | |
| 96 local filters = { | |
| 97 passwd = function(tuple) | |
| 98 password(tuple[2][1], tuple[2][2], tuple[3]); | |
| 99 end; | |
| 100 vcard = function(tuple) | |
| 101 vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3])); | |
| 102 end; | |
| 103 roster = function(tuple) | |
| 104 local node = tuple[3][1]; local host = tuple[3][2]; | |
| 105 local contact = tuple[4][1].."@"..tuple[4][2]; | |
| 106 local name = tuple[5]; local subscription = tuple[6]; | |
| 107 local ask = tuple[7]; local groups = tuple[8]; | |
| 108 if type(name) ~= type("") then name = nil; end | |
| 109 if ask == "none" then ask = nil; elseif ask == "out" then ask = "subscribe" else error(ask) end | |
| 110 if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end | |
| 111 local item = {name = name, ask = ask, subscription = subscription, groups = {}}; | |
| 112 for _, g in ipairs(groups) do item.groups[g] = true; end | |
| 113 roster(node, host, contact, item); | |
| 114 end; | |
| 115 private_storage = function(tuple) | |
| 116 private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3])); | |
| 117 end; | |
| 118 offline_msg = function(tuple) | |
| 119 offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7])); | |
| 120 end; | |
| 121 config = function(tuple) | |
| 122 if tuple[2] == "hosts" then | |
| 123 local output = io.output(); io.output("prosody.cfg.lua"); | |
| 124 io.write("-- Configuration imported from ejabberd --\n"); | |
| 125 io.write([[Host "*" | |
| 126 modules_enabled = { | |
| 127 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in. | |
| 128 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots. | |
| 129 "roster"; -- Allow users to have a roster. Recommended ;) | |
| 130 "register"; -- Allow users to register on this server using a client | |
| 131 "tls"; -- Add support for secure TLS on c2s/s2s connections | |
| 132 "vcard"; -- Allow users to set vCards | |
| 133 "private"; -- Private XML storage (for room bookmarks, etc.) | |
| 134 "version"; -- Replies to server version requests | |
| 135 "dialback"; -- s2s dialback support | |
| 136 "uptime"; | |
| 137 "disco"; | |
| 138 "time"; | |
| 139 "ping"; | |
| 140 --"selftests"; | |
| 141 }; | |
| 142 ]]); | |
| 143 for _, h in ipairs(tuple[3]) do | |
| 144 io.write("Host \"" .. h .. "\"\n"); | |
| 145 end | |
| 146 io.output(output); | |
| 147 print("prosody.cfg.lua created"); | |
| 148 end | |
| 149 end; | |
| 150 }; | |
| 151 | |
| 152 local arg = ...; | |
| 153 local help = "/? -? ? /h -h /help -help --help"; | |
| 154 if not arg or help:find(arg, 1, true) then | |
| 155 print([[ejabberd db dump importer for Prosody | |
| 156 | |
| 157 Usage: ejabberd2prosody.lua filename.txt | |
| 158 | |
| 159 The file can be generated from ejabberd using: | |
| 160 sudo ./bin/ejabberdctl dump filename.txt | |
| 161 | |
| 162 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]); | |
| 163 os.exit(1); | |
| 164 end | |
| 165 local count = 0; | |
| 166 local t = {}; | |
| 167 for item in erlparse.parseFile(arg) do | |
| 168 count = count + 1; | |
| 169 local name = item[1]; | |
| 170 t[name] = (t[name] or 0) + 1; | |
| 171 --print(count, serialize.serialize(item)); | |
| 172 if filters[name] then filters[name](item); end | |
| 173 end | |
| 174 --print(serialize.serialize(t)); |