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