X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=tools%2Fejabberdsql2prosody.lua;h=4372064386c37ba8d0494faf0fca6037fb3ff7b1;hb=99b003b1917a8ab0b561f7fb98d28d1fad8f8dc8;hp=f652af5ba034a4caa0c20a3a142826c8acc9d291;hpb=95c64b70f7cb28745222146784181e866b515069;p=prosody.git diff --git a/tools/ejabberdsql2prosody.lua b/tools/ejabberdsql2prosody.lua index f652af5b..43720643 100644 --- a/tools/ejabberdsql2prosody.lua +++ b/tools/ejabberdsql2prosody.lua @@ -1,15 +1,18 @@ #!/usr/bin/env lua -- Prosody IM --- Copyright (C) 2008-2009 Matthew Wild --- Copyright (C) 2008-2009 Waqas Hussain +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain -- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. -- +prosody = {}; + package.path = package.path ..";../?.lua"; local serialize = require "util.serialization".serialize; local st = require "util.stanza"; +local parse_xml = require "util.xml".parse; package.loaded["util.logger"] = {init = function() return function() end; end} local dm = require "util.datamanager" dm.set_data_path("data"); @@ -19,12 +22,16 @@ function parseFile(filename) local file = nil; local last = nil; +local line = 1; local function read(expected) local ch; if last then ch = last; last = nil; - else ch = file:read(1); end - if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil")); end + else + ch = file:read(1); + if ch == "\n" then line = line + 1; end + end + if expected and ch ~= expected then error("expected: "..expected.."; got: "..(ch or "nil").." on line "..line); end return ch; end local function pushback(ch) @@ -123,7 +130,12 @@ local function readInsert() end end local tname = readTableName(); - for ch in ("` VALUES "):gmatch(".") do read(ch); end -- expect this + read("`"); read(" ") -- expect this + if peek() == "(" then -- skip column list + repeat until read() == ")"; + read(" "); + end + for ch in ("VALUES "):gmatch(".") do read(ch); end -- expect this local tuples = readTuples(); read(";"); read("\n"); return tname, tuples; @@ -156,58 +168,6 @@ return readFile(filename); ------ end --- XML parser -local parse_xml = (function() - local entity_map = setmetatable({ - ["amp"] = "&"; - ["gt"] = ">"; - ["lt"] = "<"; - ["apos"] = "'"; - ["quot"] = "\""; - }, {__index = function(_, s) - if s:sub(1,1) == "#" then - if s:sub(2,2) == "x" then - return string.char(tonumber(s:sub(3), 16)); - else - return string.char(tonumber(s:sub(2))); - end - end - end - }); - local function xml_unescape(str) - return (str:gsub("&(.-);", entity_map)); - end - local function parse_tag(s) - local name,sattr=(s):gmatch("([^%s]+)(.*)")(); - local attr = {}; - for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end - return name, attr; - end - return function(xml) - local stanza = st.stanza("root"); - local regexp = "<([^>]*)>([^<]*)"; - for elem, text in xml:gmatch(regexp) do - if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions - elseif elem:sub(1,1) == "/" then -- end tag - elem = elem:sub(2); - stanza:up(); -- TODO check for start-end tag name match - elseif elem:sub(-1,-1) == "/" then -- empty tag - elem = elem:sub(1,-2); - local name,attr = parse_tag(elem); - stanza:tag(name, attr):up(); - else -- start tag - local name,attr = parse_tag(elem); - stanza:tag(name, attr); - end - if #text ~= 0 then -- text - stanza:text(xml_unescape(text)); - end - end - return stanza.tags[1]; - end -end)(); --- end of XML parser - local arg, host = ...; local help = "/? -? ? /h -h /help -help --help"; if not(arg and host) or help:find(arg, 1, true) then @@ -254,7 +214,7 @@ end for i, row in ipairs(t["users"] or NULL) do local node, password = row.username, row.password; local ret, err = dm.store(node, host, "accounts", {password = password}); - print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password); + print("["..(err or "success").."] accounts: "..node.."@"..host); end function roster(node, host, jid, item)