X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=tools%2Fxep227toprosody.lua;h=b5156f45f8193085b4ff5334ea747c1d18e4e510;hb=927c7d3d686607dbda65e7ae041f3c2d532debee;hp=313b2194f19731105cf75cce1fe5f4b63556500e;hpb=095d291f5356ff2450c11608fda6bdabc4286f91;p=prosody.git diff --git a/tools/xep227toprosody.lua b/tools/xep227toprosody.lua old mode 100644 new mode 100755 index 313b2194..b5156f45 --- a/tools/xep227toprosody.lua +++ b/tools/xep227toprosody.lua @@ -36,13 +36,15 @@ end local lxp = require "lxp"; local st = require "util.stanza"; -local init_xmlhandlers = require "core.xmlhandlers"; +local xmppstream = require "util.xmppstream"; +local new_xmpp_handlers = xmppstream.new_sax_handlers; local dm = require "util.datamanager" dm.set_data_path("data"); -local ns_separator = "\1"; -local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$"; -local ns_xep227 = "http://www.xmpp.org/extensions/xep-0227.html#ns"; +local ns_separator = xmppstream.ns_separator; +local ns_pattern = xmppstream.ns_pattern; + +local xmlns_xep227 = "http://www.xmpp.org/extensions/xep-0227.html#ns"; ----------------------------------------------------------------------- @@ -62,11 +64,11 @@ function store_roster(username, host, roster_items) -- fetch current roster-table for username@host if he already has one local roster = dm.load(username, host, "roster") or {}; -- merge imported roster-items with loaded roster - for item_tag in roster_items:childtags() do + for item_tag in roster_items:childtags("item") do -- jid for this roster-item local item_jid = item_tag.attr.jid -- validate item stanzas - if (item_tag.name == "item") and (item_jid ~= "") then + if (item_jid ~= "") then -- prepare roster item -- TODO: is the subscription attribute optional? local item = {subscription = item_tag.attr.subscription, groups = {}}; @@ -75,9 +77,9 @@ function store_roster(username, host, roster_items) item.name = item_tag.attr.name; end -- optional: iterate over group stanzas inside item stanza - for group_tag in item_tag:childtags() do + for group_tag in item_tag:childtags("group") do local group_name = group_tag:get_text(); - if (group_tag.name == "group") and (group_name ~= "") then + if (group_name ~= "") then item.groups[group_name] = true; else print("[error] invalid group stanza: "..group_tag:pretty_print()); @@ -98,7 +100,7 @@ end function store_private(username, host, private_items) local private = dm.load(username, host, "private") or {}; - for ch in private_items:childtags() do + for _, ch in ipairs(private_items.tags) do --print("private :"..ch:pretty_print()); private[ch.name..":"..ch.attr.xmlns] = st.preserialize(ch); print("[success] private item: " ..username.."@"..host.." - "..ch.name); @@ -110,11 +112,11 @@ end function store_offline_messages(username, host, offline_messages) -- TODO: maybe use list_load(), append and list_store() instead -- of constantly reopening the file with list_append()? - for ch in offline_messages:childtags() do + for ch in offline_messages:childtags("message", "jabber:client") do --print("message :"..ch:pretty_print()); local ret, err = dm.list_append(username, host, "offline", st.preserialize(ch)); print("["..(err or "success").."] stored offline message: " ..username.."@"..host.." - "..ch.attr.from); - end + end end @@ -146,7 +148,7 @@ local user_name = ""; local cb = { stream_tag = "user", - stream_ns = ns_xep227, + stream_ns = xmlns_xep227, }; function cb.streamopened(session, attr) session.notopen = false; @@ -176,7 +178,7 @@ function cb.handlestanza(session, stanza) end end -local user_handlers = init_xmlhandlers({ notopen = true, }, cb); +local user_handlers = new_xmpp_handlers({ notopen = true }, cb); ----------------------------------------------------------------------- @@ -195,10 +197,10 @@ function lxp_handlers.StartElement(parser, elementname, attributes) if curr_host ~= "" then -- forward to xmlhandlers user_handlers:StartElement(elementname, attributes); - elseif (curr_ns == ns_xep227) and (name == "host") then + elseif (curr_ns == xmlns_xep227) and (name == "host") then curr_host = attributes["jid"]; -- start of host element print("Begin parsing host "..curr_host); - elseif (curr_ns ~= ns_xep227) or (name ~= "server-data") then + elseif (curr_ns ~= xmlns_xep227) or (name ~= "server-data") then io.stderr:write("Unhandled XML element: ", name, "\n"); os.exit(1); end @@ -213,14 +215,14 @@ function lxp_handlers.EndElement(parser, elementname) --count = count - 1; --io.write("- ", string.rep(" ", count), name, " (", curr_ns, ")", "\n") if curr_host ~= "" then - if (curr_ns == ns_xep227) and (name == "host") then + if (curr_ns == xmlns_xep227) and (name == "host") then print("End parsing host "..curr_host); curr_host = "" -- end of host element else -- forward to xmlhandlers user_handlers:EndElement(elementname); end - elseif (curr_ns ~= ns_xep227) or (name ~= "server-data") then + elseif (curr_ns ~= xmlns_xep227) or (name ~= "server-data") then io.stderr:write("Unhandled XML element: ", name, "\n"); os.exit(1); end