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