aboutsummaryrefslogtreecommitdiffstats
path: root/tools/migration/migrator/prosody_files.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2011-02-26 00:24:23 +0000
committerMatthew Wild <mwild1@gmail.com>2011-02-26 00:24:23 +0000
commit05b3139a2ca2d784ac07066d8773ca07b5113159 (patch)
treeaa68c7dba38183a45322f3356d08c73e8c45b599 /tools/migration/migrator/prosody_files.lua
parent48f32e711317c95d6d4b32de85ae8b8fe1590533 (diff)
parent2b4811c8430dcbec09c67b73e8f939b4047f1aec (diff)
downloadprosody-05b3139a2ca2d784ac07066d8773ca07b5113159.tar.gz
prosody-05b3139a2ca2d784ac07066d8773ca07b5113159.zip
Merge 0.8->trunk
Diffstat (limited to 'tools/migration/migrator/prosody_files.lua')
-rw-r--r--tools/migration/migrator/prosody_files.lua133
1 files changed, 133 insertions, 0 deletions
diff --git a/tools/migration/migrator/prosody_files.lua b/tools/migration/migrator/prosody_files.lua
new file mode 100644
index 00000000..0a610d0e
--- /dev/null
+++ b/tools/migration/migrator/prosody_files.lua
@@ -0,0 +1,133 @@
+
+local print = print;
+local assert = assert;
+local setmetatable = setmetatable;
+local tonumber = tonumber;
+local char = string.char;
+local coroutine = coroutine;
+local lfs = require "lfs";
+local loadfile = loadfile;
+local setfenv = setfenv;
+local pcall = pcall;
+local mtools = require "migrator.mtools";
+local next = next;
+local pairs = pairs;
+local json = require "util.json";
+
+prosody = {};
+local dm = require "util.datamanager"
+
+module "prosody_files"
+
+local function is_dir(path) return lfs.attributes(path, "mode") == "directory"; end
+local function is_file(path) return lfs.attributes(path, "mode") == "file"; end
+local function clean_path(path)
+ return path:gsub("\\", "/"):gsub("//+", "/");
+end
+local encode, decode; do
+ local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
+ decode = function (s) return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); end
+ encode = function (s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
+end
+local function decode_dir(x)
+ if x:gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
+ return decode(x);
+ end
+end
+local function decode_file(x)
+ if x:match(".%.dat$") and x:gsub("%.dat$", ""):gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
+ return decode(x:gsub("%.dat$", ""));
+ end
+end
+local function prosody_dir(path, ondir, onfile, ...)
+ for x in lfs.dir(path) do
+ local xpath = path.."/"..x;
+ if decode_dir(x) and is_dir(xpath) then
+ ondir(xpath, x, ...);
+ elseif decode_file(x) and is_file(xpath) then
+ onfile(xpath, x, ...);
+ end
+ end
+end
+
+local function handle_root_file(path, name)
+ --print("root file: ", decode_file(name))
+ coroutine.yield { user = nil, host = nil, store = decode_file(name) };
+end
+local function handle_host_file(path, name, host)
+ --print("host file: ", decode_dir(host).."/"..decode_file(name))
+ coroutine.yield { user = nil, host = decode_dir(host), store = decode_file(name) };
+end
+local function handle_store_file(path, name, store, host)
+ --print("store file: ", decode_file(name).."@"..decode_dir(host).."/"..decode_dir(store))
+ coroutine.yield { user = decode_file(name), host = decode_dir(host), store = decode_dir(store) };
+end
+local function handle_host_store(path, name, host)
+ prosody_dir(path, function() end, handle_store_file, name, host);
+end
+local function handle_host_dir(path, name)
+ prosody_dir(path, handle_host_store, handle_host_file, name);
+end
+local function handle_root_dir(path)
+ prosody_dir(path, handle_host_dir, handle_root_file);
+end
+
+local function decode_user(item)
+ local userdata = {
+ user = item[1].user;
+ host = item[1].host;
+ stores = {};
+ };
+ for i=1,#item do -- loop over stores
+ local result = {};
+ local store = item[i];
+ userdata.stores[store.store] = store.data;
+ store.user = nil; store.host = nil; store.store = nil;
+ end
+ return userdata;
+end
+
+function reader(input)
+ local path = clean_path(assert(input.path, "no input.path specified"));
+ assert(is_dir(path), "input.path is not a directory");
+ local iter = coroutine.wrap(function()handle_root_dir(path);end);
+ -- get per-user stores, sorted
+ local iter = mtools.sorted {
+ reader = function()
+ local x = iter();
+ if x then
+ dm.set_data_path(path);
+ x.data = assert(dm.load(x.user, x.host, x.store));
+ return x;
+ end
+ end;
+ sorter = function(a, b)
+ local a_host, a_user, a_store = a.host or "", a.user or "", a.store or "";
+ local b_host, b_user, b_store = b.host or "", b.user or "", b.store or "";
+ 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);
+ end;
+ };
+ -- merge stores to get users
+ iter = mtools.merged(iter, function(a, b)
+ return (a.host == b.host and a.user == b.user);
+ end);
+
+ return function()
+ local x = iter();
+ return x and decode_user(x);
+ end
+end
+
+function writer(output)
+ local path = clean_path(assert(output.path, "no output.path specified"));
+ assert(is_dir(path), "output.path is not a directory");
+ return function(item)
+ if not item then return; end -- end of input
+ dm.set_data_path(path);
+ for store, data in pairs(item.stores) do
+ assert(dm.store(item.user, item.host, store, data));
+ end
+ end
+end
+
+return _M;