util/datamanager.lua

Sun, 05 Oct 2008 19:10:21 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 05 Oct 2008 19:10:21 +0100
branch
tls
changeset 66
018705d57f09
parent 0
3e3171b59028
child 82
cbf387f29d56
child 84
d0a0bac6815e
permissions
-rw-r--r--

Working TLS!

0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
1 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
2 local setmetatable, type = setmetatable, type;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
3 local pairs = pairs;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
4 local char = string.char;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
6 local log = log;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
7 local io_open = io.open;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
8
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
9 module "datamanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
10
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
11
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
12 ---- utils -----
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
13 local encode, decode;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
14
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
15 local log = function (type, msg) return log(type, "datamanager", msg); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
16
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
17 do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
18 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
19
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
20 decode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
22 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
23
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24 encode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
26 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
28
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
29 local function basicSerialize (o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
30 if type(o) == "number" or type(o) == "boolean" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
31 return tostring(o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
32 else -- assume it is a string
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
33 return string.format("%q", tostring(o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
34 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
35 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
36
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
37
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
38 local function simplesave (f, o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
39 if type(o) == "number" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
40 f:write(o)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
41 elseif type(o) == "string" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
42 f:write(format("%q", o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
43 elseif type(o) == "table" then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
44 f:write("{\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
45 for k,v in pairs(o) do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
46 f:write(" [", format("%q", k), "] = ")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
47 simplesave(f, v)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
48 f:write(",\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
49 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
50 f:write("}\n")
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
51 else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
52 error("cannot serialize a " .. type(o))
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
53 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
54 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
55
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
56 ------- API -------------
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
57
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
58 function getpath(username, host, datastore)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
59 return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
60 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
61
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
62 function load(username, host, datastore)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
63 local data, ret = loadfile(getpath(username, host, datastore));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
64 if not data then log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..username.."@"..host); return nil; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
65 setfenv(data, {});
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
66 local success, ret = pcall(data);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
67 if not success then log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..username.."@"..host); return nil; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
68 return ret;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
69 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
70
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
71 function store(username, host, datastore, data)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
72 local f, msg = io_open(getpath(username, host, datastore), "w+");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
73 if not f then log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..username.."@"..host); return nil; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
74 f:write("return ");
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
75 simplesave(f, data);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
76 f:close();
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
77 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
78 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
79

mercurial