tools/migration/migrator/prosody_files: Fix undefined global access of ?error?, print...
[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 local error = error;
17
18 prosody = {};
19 local dm = require "util.datamanager"
20
21 module "prosody_files"
22
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 "~");
27 end
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
32 end
33 local function decode_dir(x)
34         if x:gsub("%%%x%x", ""):gsub("[a-zA-Z0-9]", "") == "" then
35                 return decode(x);
36         end
37 end
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$", ""));
41         end
42 end
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
47                         ondir(xpath, x, ...);
48                 elseif decode_file(x) and is_file(xpath) then
49                         onfile(xpath, x, ...);
50                 end
51         end
52 end
53
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) };
57 end
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) };
61 end
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) };
65 end
66 local function handle_host_store(path, name, host)
67         prosody_dir(path, function() end, handle_store_file, name, host);
68 end
69 local function handle_host_dir(path, name)
70         prosody_dir(path, handle_host_store, handle_host_file, name);
71 end
72 local function handle_root_dir(path)
73         prosody_dir(path, handle_host_dir, handle_root_file);
74 end
75
76 local function decode_user(item)
77         local userdata = {
78                 user = item[1].user;
79                 host = item[1].host;
80                 stores = {};
81         };
82         for i=1,#item do -- loop over stores
83                 local result = {};
84                 local store = item[i];
85                 userdata.stores[store.store] = store.data;
86                 store.user = nil; store.host = nil; store.store = nil;
87         end
88         return userdata;
89 end
90
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 {
97                 reader = function()
98                         local x = iter();
99                         while x do
100                                 dm.set_data_path(path);
101                                 local err;
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>"));
107                                 else
108                                         return x;
109                                 end
110                                 x = iter();
111                         end
112                 end;
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);
117                 end;
118         };
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);
122         end);
123
124         return function()
125                 local x = iter();
126                 return x and decode_user(x);
127         end
128 end
129
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));
138                 end
139         end
140 end
141
142 return _M;