Software / code / prosody
Annotate
util/datamanager.lua @ 177:606c433955e7
Bug fixes and checks for presence subscriptions, etc
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Sun, 26 Oct 2008 00:22:18 +0500 |
| parent | 129:0f119bece309 |
| child | 182:f5cb6b5a6eb7 |
| 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; | |
| 88 | 8 local tostring = tostring; |
|
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
129
diff
changeset
|
9 local error = error; |
| 0 | 10 |
| 11 module "datamanager" | |
| 12 | |
| 13 | |
| 14 ---- utils ----- | |
| 15 local encode, decode; | |
| 16 | |
| 17 local log = function (type, msg) return log(type, "datamanager", msg); end | |
| 18 | |
| 19 do | |
| 20 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); | |
| 21 | |
| 22 decode = function (s) | |
| 23 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes)); | |
| 24 end | |
| 25 | |
| 26 encode = function (s) | |
| 27 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end)); | |
| 28 end | |
| 29 end | |
| 30 | |
| 31 local function basicSerialize (o) | |
| 32 if type(o) == "number" or type(o) == "boolean" then | |
| 33 return tostring(o) | |
| 34 else -- assume it is a string | |
| 88 | 35 return format("%q", tostring(o)) |
| 0 | 36 end |
| 37 end | |
| 38 | |
| 39 | |
| 40 local function simplesave (f, o) | |
| 41 if type(o) == "number" then | |
| 42 f:write(o) | |
| 43 elseif type(o) == "string" then | |
| 44 f:write(format("%q", o)) | |
| 45 elseif type(o) == "table" then | |
| 46 f:write("{\n") | |
| 47 for k,v in pairs(o) do | |
|
82
cbf387f29d56
Fix for saving tables with non-string keys
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
48 f:write(" [", basicSerialize(k), "] = ") |
| 0 | 49 simplesave(f, v) |
| 50 f:write(",\n") | |
| 51 end | |
| 52 f:write("}\n") | |
|
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
129
diff
changeset
|
53 elseif type(o) == "boolean" then |
|
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
129
diff
changeset
|
54 f:write(o and "true" or "false"); |
| 0 | 55 else |
| 56 error("cannot serialize a " .. type(o)) | |
| 57 end | |
| 58 end | |
| 59 | |
| 60 ------- API ------------- | |
| 61 | |
| 62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 else |
|
d0a0bac6815e
Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents:
0
diff
changeset
|
68 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
|
69 end |
| 0 | 70 end |
| 71 | |
| 72 function load(username, host, datastore) | |
| 73 local data, ret = loadfile(getpath(username, host, datastore)); | |
|
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
74 if not data then |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
75 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or nil).."@"..(host or nil)); |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
76 return nil; |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
77 end |
| 0 | 78 setfenv(data, {}); |
| 79 local success, ret = pcall(data); | |
|
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
80 if not success then |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
81 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or nil).."@"..(host or nil)); |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
82 return nil; |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
83 end |
| 0 | 84 return ret; |
| 85 end | |
| 86 | |
| 87 function store(username, host, datastore, data) | |
| 88 local f, msg = io_open(getpath(username, host, datastore), "w+"); | |
|
117
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
89 if not f then |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
90 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or nil).."@"..(host or nil)); |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
91 return nil; |
|
8e5c5e6a3240
Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents:
88
diff
changeset
|
92 end |
| 0 | 93 f:write("return "); |
| 94 simplesave(f, data); | |
| 95 f:close(); | |
| 96 return true; | |
| 97 end | |
| 98 | |
|
129
0f119bece309
Fixed: Some modules did not return anything
Waqas Hussain <waqas20@gmail.com>
parents:
117
diff
changeset
|
99 return _M; |