Software /
code /
prosody
Annotate
util/datamanager.lua @ 84:d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 09 Oct 2008 01:20:43 +0500 |
parent | 0:3e3171b59028 |
child | 87:059ef1c30844 |
rev | line source |
---|---|
0 | 1 local format = string.format; |
2 local setmetatable, type = setmetatable, type; | |
3 local pairs = pairs; | |
4 local char = string.char; | |
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; | |
6 local log = log; | |
7 local io_open = io.open; | |
8 | |
9 module "datamanager" | |
10 | |
11 | |
12 ---- utils ----- | |
13 local encode, decode; | |
14 | |
15 local log = function (type, msg) return log(type, "datamanager", msg); end | |
16 | |
17 do | |
18 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); | |
19 | |
20 decode = function (s) | |
21 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); | |
22 end | |
23 | |
24 encode = function (s) | |
25 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end)); | |
26 end | |
27 end | |
28 | |
29 local function basicSerialize (o) | |
30 if type(o) == "number" or type(o) == "boolean" then | |
31 return tostring(o) | |
32 else -- assume it is a string | |
33 return string.format("%q", tostring(o)) | |
34 end | |
35 end | |
36 | |
37 | |
38 local function simplesave (f, o) | |
39 if type(o) == "number" then | |
40 f:write(o) | |
41 elseif type(o) == "string" then | |
42 f:write(format("%q", o)) | |
43 elseif type(o) == "table" then | |
44 f:write("{\n") | |
45 for k,v in pairs(o) do | |
46 f:write(" [", format("%q", k), "] = ") | |
47 simplesave(f, v) | |
48 f:write(",\n") | |
49 end | |
50 f:write("}\n") | |
51 else | |
52 error("cannot serialize a " .. type(o)) | |
53 end | |
54 end | |
55 | |
56 ------- API ------------- | |
57 | |
58 function getpath(username, host, datastore) | |
84
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
59 if username then |
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
60 return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username)); |
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
61 elseif host then |
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
62 return format("data/%s/%s.dat", encode(host), datastore); |
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
63 else |
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
64 return format("data/%s.dat", datastore); |
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
65 end |
0 | 66 end |
67 | |
68 function load(username, host, datastore) | |
69 local data, ret = loadfile(getpath(username, host, datastore)); | |
70 if not data then log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..username.."@"..host); return nil; end | |
71 setfenv(data, {}); | |
72 local success, ret = pcall(data); | |
73 if not success then log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..username.."@"..host); return nil; end | |
74 return ret; | |
75 end | |
76 | |
77 function store(username, host, datastore, data) | |
78 local f, msg = io_open(getpath(username, host, datastore), "w+"); | |
79 if not f then log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..username.."@"..host); return nil; end | |
80 f:write("return "); | |
81 simplesave(f, data); | |
82 f:close(); | |
83 return true; | |
84 end | |
85 |