comparison mod_pastebin/mod_pastebin.lua @ 516:3d3687fef751

mod_pastebin: Fix issue with metatable not being set when a reload changes expires_after to 0
author Matthew Wild <mwild1@gmail.com>
date Wed, 21 Dec 2011 10:25:17 +0000
parents 46e1983486e9
children f866325305ed
comparison
equal deleted inserted replaced
515:e98fe28c50b0 516:3d3687fef751
41 local pastes = {}; 41 local pastes = {};
42 local default_headers = { ["Content-Type"] = "text/plain; charset=utf-8" }; 42 local default_headers = { ["Content-Type"] = "text/plain; charset=utf-8" };
43 43
44 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im"; 44 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im";
45 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; 45 local xmlns_xhtml = "http://www.w3.org/1999/xhtml";
46
47 if expire_after == 0 then
48 local dm = require "util.datamanager";
49 setmetatable(pastes, {
50 __index = function (pastes, id)
51 if type(id) == "string" then
52 return dm.load(id, module.host, "pastebin");
53 end
54 end;
55 __newindex = function (pastes, id, data)
56 if type(id) == "string" then
57 dm.store(id, module.host, "pastebin", data);
58 end
59 end;
60 });
61 else
62 setmetatable(pastes, nil);
63 end
64 46
65 function pastebin_text(text) 47 function pastebin_text(text)
66 local uuid = uuid_new(); 48 local uuid = uuid_new();
67 pastes[uuid] = { body = text, time = os_time(), headers = default_headers }; 49 pastes[uuid] = { body = text, time = os_time(), headers = default_headers };
68 pastes[#pastes+1] = uuid; 50 pastes[#pastes+1] = uuid;
159 base_url = base_url or ("http://"..module:get_host()..(port ~= 80 and (":"..port) or "").."/"..base.."/"); 141 base_url = base_url or ("http://"..module:get_host()..(port ~= 80 and (":"..port) or "").."/"..base.."/");
160 142
161 httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl } 143 httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl }
162 end 144 end
163 145
146 function module.load()
147 if expire_after == 0 then
148 local dm = require "util.datamanager";
149 setmetatable(pastes, {
150 __index = function (pastes, id)
151 if type(id) == "string" then
152 return dm.load(id, module.host, "pastebin");
153 end
154 end;
155 __newindex = function (pastes, id, data)
156 if type(id) == "string" then
157 dm.store(id, module.host, "pastebin", data);
158 end
159 end;
160 });
161 else
162 setmetatable(pastes, nil);
163 end
164 end
165
164 function module.save() 166 function module.save()
165 return { pastes = pastes }; 167 return { pastes = pastes };
166 end 168 end
167 169
168 function module.restore(data) 170 function module.restore(data)