1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
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 pcall = pcall;
local mtools = require "migrator.mtools";
local next = next;
local pairs = pairs;
local json = require "util.json";
local os_getenv = os.getenv;
local error = error;
prosody = {};
local dm = require "util.datamanager"
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("//+", "/"):gsub("^~", os_getenv("HOME") or "~");
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
local 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();
while x do
dm.set_data_path(path);
local err;
x.data, err = dm.load(x.user, x.host, x.store);
if x.data == nil and err then
local p = dm.getpath(x.user, x.host, x.store);
print(("Error loading data at path %s for %s@%s (%s store): %s")
:format(p, x.user or "<nil>", x.host or "<nil>", x.store or "<nil>", err or "<nil>"));
else
return x;
end
x = iter();
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
local 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 {
reader = reader;
writer = writer;
}
|