Software /
code /
prosody
Comparison
tools/jabberd14sql2prosody.lua @ 5216:63db6f281708
tools/jabberd14sql2prosody: Use util.xml.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 03 Dec 2012 05:41:18 +0500 |
parent | 5109:12fb961c8f27 |
child | 5696:9fba74a28e0c |
comparison
equal
deleted
inserted
replaced
5215:b3d837c1ccf1 | 5216:63db6f281708 |
---|---|
443 local dm = require "util.datamanager"; | 443 local dm = require "util.datamanager"; |
444 dm.set_data_path("data"); | 444 dm.set_data_path("data"); |
445 | 445 |
446 local datetime = require "util.datetime"; | 446 local datetime = require "util.datetime"; |
447 | 447 |
448 local lxp = require "lxp"; | |
449 local st = require "util.stanza"; | 448 local st = require "util.stanza"; |
450 | 449 local parse_xml = require "util.xml".parse; |
451 local parse_xml = (function() | |
452 local ns_prefixes = { | |
453 ["http://www.w3.org/XML/1998/namespace"] = "xml"; | |
454 }; | |
455 local ns_separator = "\1"; | |
456 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$"; | |
457 return function(xml) | |
458 local handler = {}; | |
459 local stanza = st.stanza("root"); | |
460 function handler:StartElement(tagname, attr) | |
461 local curr_ns,name = tagname:match(ns_pattern); | |
462 if name == "" then | |
463 curr_ns, name = "", curr_ns; | |
464 end | |
465 if curr_ns ~= "" then | |
466 attr.xmlns = curr_ns; | |
467 end | |
468 for i=1,#attr do | |
469 local k = attr[i]; | |
470 attr[i] = nil; | |
471 local ns, nm = k:match(ns_pattern); | |
472 if nm ~= "" then | |
473 ns = ns_prefixes[ns]; | |
474 if ns then | |
475 attr[ns..":"..nm] = attr[k]; | |
476 attr[k] = nil; | |
477 end | |
478 end | |
479 end | |
480 stanza:tag(name, attr); | |
481 end | |
482 function handler:CharacterData(data) | |
483 stanza:text(data); | |
484 end | |
485 function handler:EndElement(tagname) | |
486 stanza:up(); | |
487 end | |
488 local parser = lxp.new(handler, "\1"); | |
489 local ok, err, line, col = parser:parse(xml); | |
490 if ok then ok, err, line, col = parser:parse(); end | |
491 --parser:close(); | |
492 if ok then | |
493 return stanza.tags[1]; | |
494 else | |
495 return ok, err.." (line "..line..", col "..col..")"; | |
496 end | |
497 end; | |
498 end)(); | |
499 | 450 |
500 function store_password(username, host, password) | 451 function store_password(username, host, password) |
501 -- create or update account for username@host | 452 -- create or update account for username@host |
502 local ret, err = dm.store(username, host, "accounts", {password = password}); | 453 local ret, err = dm.store(username, host, "accounts", {password = password}); |
503 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password); | 454 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password); |