X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=tools%2Fejabberdsql2prosody.lua;h=69c8cfe813fed6f0880d3f246e13898b50fa3cf5;hb=6ffc2add3cf85b642f91be991d083398e536a816;hp=958cf0e2d1c97ec027e7bb71ab4dcab684f6f890;hpb=79d3c09c2c0089f3a2b97529e43120e2057f2ff5;p=prosody.git diff --git a/tools/ejabberdsql2prosody.lua b/tools/ejabberdsql2prosody.lua index 958cf0e2..69c8cfe8 100644 --- a/tools/ejabberdsql2prosody.lua +++ b/tools/ejabberdsql2prosody.lua @@ -2,7 +2,7 @@ -- Prosody IM -- 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. -- @@ -10,8 +10,17 @@ prosody = {}; package.path = package.path ..";../?.lua"; + +local my_name = arg[0]; +if my_name:match("[/\\]") then + package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua"); + package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so"); +end + + 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"); @@ -21,12 +30,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) @@ -125,7 +138,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; @@ -158,58 +176,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 @@ -325,11 +291,21 @@ for i, row in ipairs(t["rostergroups"] or NULL) do roster_group(row.username, host, row.jid, row.grp); end for i, row in ipairs(t["vcard"] or NULL) do - local ret, err = dm.store(row.username, host, "vcard", st.preserialize(parse_xml(row.vcard))); - print("["..(err or "success").."] vCard: "..row.username.."@"..host); + local stanza, err = parse_xml(row.vcard); + if stanza then + local ret, err = dm.store(row.username, host, "vcard", st.preserialize(stanza)); + print("["..(err or "success").."] vCard: "..row.username.."@"..host); + else + print("[error] vCard XML parse failed: "..row.username.."@"..host); + end end for i, row in ipairs(t["private_storage"] or NULL) do - private_storage(row.username, host, row.namespace, parse_xml(row.data)); + local stanza, err = parse_xml(row.data); + if stanza then + private_storage(row.username, host, row.namespace, stanza); + else + print("[error] Private XML parse failed: "..row.username.."@"..host); + end end table.sort(t["spool"] or NULL, function(a,b) return a.seq < b.seq; end); -- sort by sequence number, just in case local time_offset = os.difftime(os.time(os.date("!*t")), os.time(os.date("*t"))) -- to deal with timezones @@ -338,11 +314,15 @@ local date_parse = function(s) return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec-time_offset}); end for i, row in ipairs(t["spool"] or NULL) do - local stanza = parse_xml(row.xml); - local last_child = stanza.tags[#stanza.tags]; - if not last_child or last_child ~= stanza[#stanza] then error("Last child of offline message is not a tag"); end - if last_child.name ~= "x" and last_child.attr.xmlns ~= "jabber:x:delay" then error("Last child of offline message is not a timestamp"); end - stanza[#stanza], stanza.tags[#stanza.tags] = nil, nil; - local t = date_parse(last_child.attr.stamp); - offline_msg(row.username, host, t, stanza); + local stanza, err = parse_xml(row.xml); + if stanza then + local last_child = stanza.tags[#stanza.tags]; + if not last_child or last_child ~= stanza[#stanza] then error("Last child of offline message is not a tag"); end + if last_child.name ~= "x" and last_child.attr.xmlns ~= "jabber:x:delay" then error("Last child of offline message is not a timestamp"); end + stanza[#stanza], stanza.tags[#stanza.tags] = nil, nil; + local t = date_parse(last_child.attr.stamp); + offline_msg(row.username, host, t, stanza); + else + print("[error] Offline message XML parsing failed: "..row.username.."@"..host); + end end