2 local lfs = require "lfs";
3 local st = require "util.stanza";
4 local parse_xml = require "util.xml".parse;
5 local os_getenv = os.getenv;
6 local io_open = io.open;
9 local coroutine = coroutine;
14 local function is_dir(path) return lfs.attributes(path, "mode") == "directory"; end
15 local function is_file(path) return lfs.attributes(path, "mode") == "file"; end
16 local function clean_path(path)
17 return path:gsub("\\", "/"):gsub("//+", "/"):gsub("^~", os_getenv("HOME") or "~");
20 local function load_xml(path)
21 local f, err = io_open(path);
22 if not f then return f, err; end
23 local data = f:read("*a");
25 if not data then return; end
26 return parse_xml(data);
29 local function load_spool_file(host, filename, path)
30 local xml = load_xml(path);
31 if not xml then return; end
33 local register_element = xml:get_child("query", "jabber:iq:register");
34 local username_element = register_element and register_element:get_child("username", "jabber:iq:register");
35 local password_element = register_element and register_element:get_child("password", "jabber:iq:auth");
36 local username = username_element and username_element:get_text();
37 local password = password_element and password_element:get_text();
39 print("[warn] Missing /xdb/{jabber:iq:register}register/username> in file "..filename)
41 elseif username..".xml" ~= filename then
42 print("[warn] Missing /xdb/{jabber:iq:register}register/username does not match filename "..filename);
51 local stores = userdata.stores;
52 stores.accounts = { password = password };
55 local tag = xml.tags[i];
56 local xname = (tag.attr.xmlns or "")..":"..tag.name;
57 if tag.attr.j_private_flag == "1" and tag.attr.xmlns then
59 stores.private = stores.private or {};
60 tag.attr.j_private_flag = nil;
61 stores.private[tag.attr.xmlns] = st.preserialize(tag);
62 elseif xname == "jabber:iq:auth:password" then
63 if stores.accounts.password ~= tag:get_text() then
65 print("[warn] conflicting passwords")
67 stores.accounts.password = tag:get_text();
70 elseif xname == "jabber:iq:register:query" then
72 elseif xname == "jabber:xdb:nslist:foo" then
74 elseif xname == "jabber:iq:auth:0k:zerok" then
76 elseif xname == "jabber:iq:roster:query" then
79 local subscription_types = { from = true, to = true, both = true, none = true };
80 for _,item_element in ipairs(tag.tags) do
81 assert(item_element.name == "item");
82 assert(item_element.attr.jid);
83 assert(subscription_types[item_element.attr.subscription]);
84 assert((item_element.attr.ask or "subscribe") == "subscribe")
85 if item_element.name == "item" then
87 for _,group_element in ipairs(item_element.tags) do
88 assert(group_element.name == "group");
89 groups[group_element:get_text()] = true;
92 name = item_element.attr.name;
93 subscription = item_element.attr.subscription;
94 ask = item_element.attr.ask;
97 roster[item_element.attr.jid] = item;
100 stores.roster = roster;
101 elseif xname == "jabber:iq:last:query" then
103 elseif xname == "jabber:x:offline:foo" then
105 elseif xname == "vcard-temp:vCard" then
107 stores.vcard = st.preserialize(tag);
109 print("[warn] Unknown tag: "..xname);
115 local function loop_over_users(path, host, cb)
116 for file in lfs.dir(path) do
117 if file:match("%.xml$") then
118 local user = load_spool_file(host, file, path.."/"..file);
119 if user then cb(user); end
123 local function loop_over_hosts(path, cb)
124 for host in lfs.dir(path) do
125 if host ~= "." and host ~= ".." and is_dir(path.."/"..host) then
126 loop_over_users(path.."/"..host, host, cb);
131 function reader(input)
132 local path = clean_path(assert(input.path, "no input.path specified"));
133 assert(is_dir(path), "input.path is not a directory");
136 return coroutine.wrap(function() loop_over_users(input.path, input.host, coroutine.yield) end);
138 return coroutine.wrap(function() loop_over_hosts(input.path, coroutine.yield) end);