4 local setmetatable = setmetatable;
5 local tonumber = tonumber;
6 local char = string.char;
7 local coroutine = coroutine;
8 local lfs = require "lfs";
9 local loadfile = loadfile;
11 local mtools = require "migrator.mtools";
14 local json = require "util.json";
15 local os_getenv = os.getenv;
19 local dm = require "util.datamanager"
21 module "prosody_files"
23 local function is_dir(path) return lfs.attributes(path, "mode") == "directory"; end
24 local function is_file(path) return lfs.attributes(path, "mode") == "file"; end
25 local function clean_path(path)
26 return path:gsub("\\", "/"):gsub("//+", "/"):gsub("^~", os_getenv("HOME") or "~");
28 local encode, decode; do
29 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
30 decode = function (s) return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); end
31 encode = function (s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
33 local function decode_dir(x)
34 if x:gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
38 local function decode_file(x)
39 if x:match(".%.dat$") and x:gsub("%.dat$", ""):gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
40 return decode(x:gsub("%.dat$", ""));
43 local function prosody_dir(path, ondir, onfile, ...)
44 for x in lfs.dir(path) do
45 local xpath = path.."/"..x;
46 if decode_dir(x) and is_dir(xpath) then
48 elseif decode_file(x) and is_file(xpath) then
49 onfile(xpath, x, ...);
54 local function handle_root_file(path, name)
55 --print("root file: ", decode_file(name))
56 coroutine.yield { user = nil, host = nil, store = decode_file(name) };
58 local function handle_host_file(path, name, host)
59 --print("host file: ", decode_dir(host).."/"..decode_file(name))
60 coroutine.yield { user = nil, host = decode_dir(host), store = decode_file(name) };
62 local function handle_store_file(path, name, store, host)
63 --print("store file: ", decode_file(name).."@"..decode_dir(host).."/"..decode_dir(store))
64 coroutine.yield { user = decode_file(name), host = decode_dir(host), store = decode_dir(store) };
66 local function handle_host_store(path, name, host)
67 prosody_dir(path, function() end, handle_store_file, name, host);
69 local function handle_host_dir(path, name)
70 prosody_dir(path, handle_host_store, handle_host_file, name);
72 local function handle_root_dir(path)
73 prosody_dir(path, handle_host_dir, handle_root_file);
76 local function decode_user(item)
82 for i=1,#item do -- loop over stores
84 local store = item[i];
85 userdata.stores[store.store] = store.data;
86 store.user = nil; store.host = nil; store.store = nil;
91 function reader(input)
92 local path = clean_path(assert(input.path, "no input.path specified"));
93 assert(is_dir(path), "input.path is not a directory");
94 local iter = coroutine.wrap(function()handle_root_dir(path);end);
95 -- get per-user stores, sorted
96 local iter = mtools.sorted {
100 dm.set_data_path(path);
102 x.data, err = dm.load(x.user, x.host, x.store);
103 if x.data == nil and err then
104 local p = dm.getpath(x.user, x.host, x.store);
105 print(("Error loading data at path %s for %s@%s (%s store): %s")
106 :format(p, x.user or "<nil>", x.host or "<nil>", x.store or "<nil>", err or "<nil>"));
113 sorter = function(a, b)
114 local a_host, a_user, a_store = a.host or "", a.user or "", a.store or "";
115 local b_host, b_user, b_store = b.host or "", b.user or "", b.store or "";
116 return a_host > b_host or (a_host==b_host and a_user > b_user) or (a_host==b_host and a_user==b_user and a_store > b_store);
119 -- merge stores to get users
120 iter = mtools.merged(iter, function(a, b)
121 return (a.host == b.host and a.user == b.user);
126 return x and decode_user(x);
130 function writer(output)
131 local path = clean_path(assert(output.path, "no output.path specified"));
132 assert(is_dir(path), "output.path is not a directory");
133 return function(item)
134 if not item then return; end -- end of input
135 dm.set_data_path(path);
136 for store, data in pairs(item.stores) do
137 assert(dm.store(item.user, item.host, store, data));