Merge Tobias SCRAM-PLUS work
[prosody.git] / tools / migration / migrator / prosody_files.lua
1
2 local print = print;
3 local assert = assert;
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;
10 local pcall = pcall;
11 local mtools = require "migrator.mtools";
12 local next = next;
13 local pairs = pairs;
14 local json = require "util.json";
15 local os_getenv = os.getenv;
16
17 prosody = {};
18 local dm = require "util.datamanager"
19
20 module "prosody_files"
21
22 local function is_dir(path) return lfs.attributes(path, "mode") == "directory"; end
23 local function is_file(path) return lfs.attributes(path, "mode") == "file"; end
24 local function clean_path(path)
25         return path:gsub("\\", "/"):gsub("//+", "/"):gsub("^~", os_getenv("HOME") or "~");
26 end
27 local encode, decode; do
28         local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
29         decode = function (s) return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); end
30         encode = function (s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
31 end
32 local function decode_dir(x)
33         if x:gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
34                 return decode(x);
35         end
36 end
37 local function decode_file(x)
38         if x:match(".%.dat$") and x:gsub("%.dat$", ""):gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
39                 return decode(x:gsub("%.dat$", ""));
40         end
41 end
42 local function prosody_dir(path, ondir, onfile, ...)
43         for x in lfs.dir(path) do
44                 local xpath = path.."/"..x;
45                 if decode_dir(x) and is_dir(xpath) then
46                         ondir(xpath, x, ...);
47                 elseif decode_file(x) and is_file(xpath) then
48                         onfile(xpath, x, ...);
49                 end
50         end
51 end
52
53 local function handle_root_file(path, name)
54         --print("root file: ", decode_file(name))
55         coroutine.yield { user = nil, host = nil, store = decode_file(name) };
56 end
57 local function handle_host_file(path, name, host)
58         --print("host file: ", decode_dir(host).."/"..decode_file(name))
59         coroutine.yield { user = nil, host = decode_dir(host), store = decode_file(name) };
60 end
61 local function handle_store_file(path, name, store, host)
62         --print("store file: ", decode_file(name).."@"..decode_dir(host).."/"..decode_dir(store))
63         coroutine.yield { user = decode_file(name), host = decode_dir(host), store = decode_dir(store) };
64 end
65 local function handle_host_store(path, name, host)
66         prosody_dir(path, function() end, handle_store_file, name, host);
67 end
68 local function handle_host_dir(path, name)
69         prosody_dir(path, handle_host_store, handle_host_file, name);
70 end
71 local function handle_root_dir(path)
72         prosody_dir(path, handle_host_dir, handle_root_file);
73 end
74
75 local function decode_user(item)
76         local userdata = {
77                 user = item[1].user;
78                 host = item[1].host;
79                 stores = {};
80         };
81         for i=1,#item do -- loop over stores
82                 local result = {};
83                 local store = item[i];
84                 userdata.stores[store.store] = store.data;
85                 store.user = nil; store.host = nil; store.store = nil;
86         end
87         return userdata;
88 end
89
90 function reader(input)
91         local path = clean_path(assert(input.path, "no input.path specified"));
92         assert(is_dir(path), "input.path is not a directory");
93         local iter = coroutine.wrap(function()handle_root_dir(path);end);
94         -- get per-user stores, sorted
95         local iter = mtools.sorted {
96                 reader = function()
97                         local x = iter();
98                         if x then
99                                 dm.set_data_path(path);
100                                 local err;
101                                 x.data, err = dm.load(x.user, x.host, x.store);
102                                 if x.data == nil and err then
103                                         error(("Error loading data at path %s for %s@%s (%s store)")
104                                                 :format(path, x.user or "<nil>", x.host or "<nil>", x.store or "<nil>"), 0);
105                                 end
106                                 return x;
107                         end
108                 end;
109                 sorter = function(a, b)
110                         local a_host, a_user, a_store = a.host or "", a.user or "", a.store or "";
111                         local b_host, b_user, b_store = b.host or "", b.user or "", b.store or "";
112                         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);
113                 end;
114         };
115         -- merge stores to get users
116         iter = mtools.merged(iter, function(a, b)
117                 return (a.host == b.host and a.user == b.user);
118         end);
119
120         return function()
121                 local x = iter();
122                 return x and decode_user(x);
123         end
124 end
125
126 function writer(output)
127         local path = clean_path(assert(output.path, "no output.path specified"));
128         assert(is_dir(path), "output.path is not a directory");
129         return function(item)
130                 if not item then return; end -- end of input
131                 dm.set_data_path(path);
132                 for store, data in pairs(item.stores) do
133                         assert(dm.store(item.user, item.host, store, data));
134                 end
135         end
136 end
137
138 return _M;