comparison mod_pastebin/mod_pastebin.lua @ 24:72bcc0475e2f

mod_pastebin: Expire pastes after 'pastebin_expire_after' hours, 24 by default
author Matthew Wild <mwild1@gmail.com>
date Fri, 02 Oct 2009 22:15:14 +0100
parents 92b1e6592d36
children ea59a8d98b03
comparison
equal deleted inserted replaced
23:92b1e6592d36 24:72bcc0475e2f
1 1
2 local st = require "util.stanza"; 2 local st = require "util.stanza";
3 local httpserver = require "net.httpserver"; 3 local httpserver = require "net.httpserver";
4 local uuid_new = require "util.uuid".generate; 4 local uuid_new = require "util.uuid".generate;
5 local os_time = os.time; 5 local os_time = os.time;
6 local t_insert, t_remove = table.insert, table.remove;
7 local add_task = require "util.timer".add_task;
6 8
7 local length_threshold = config.get(module.host, "core", "pastebin_threshold") or 500; 9 local length_threshold = config.get(module.host, "core", "pastebin_threshold") or 500;
8 10
9 local base_url = config.get(module.host, "core", "pastebin_url"); 11 local base_url = config.get(module.host, "core", "pastebin_url");
12
13 -- Seconds a paste should live for in seconds (config is in hours), default 24 hours
14 local expire_after = math.floor((config.get(module.host, "core", "pastebin_expire_after") or 24) * 3600);
10 15
11 local pastes = {}; 16 local pastes = {};
12 17
13 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im"; 18 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im";
14 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"; 19 local xmlns_xhtml = "http://www.w3.org/1999/xhtml";
15 20
16 local function pastebin_message(text) 21 local function pastebin_message(text)
17 local uuid = uuid_new(); 22 local uuid = uuid_new();
18 pastes[uuid] = { text = text, time = os_time() }; 23 pastes[uuid] = { text = text, time = os_time() };
24 pastes[#pastes+1] = uuid;
25 if not pastes[2] then -- No other pastes, give the timer a kick
26 add_task(expire_after, expire_pastes);
27 end
19 return base_url..uuid; 28 return base_url..uuid;
20 end 29 end
21 30
22 function handle_request(method, body, request) 31 function handle_request(method, body, request)
23 local pasteid = request.url.path:match("[^/]+$"); 32 local pasteid = request.url.path:match("[^/]+$");
59 end 68 end
60 end 69 end
61 70
62 module:hook("message/bare", check_message); 71 module:hook("message/bare", check_message);
63 72
73 function expire_pastes(time)
74 time = time or os_time(); -- COMPAT with 0.5
75 if pastes[1] then
76 pastes[pastes[1]] = nil;
77 t_remove(pastes, 1);
78 if pastes[1] then
79 return (expire_after - (time - pastes[pastes[1]].time)) + 1;
80 end
81 end
82 end
83
84
64 local ports = config.get(module.host, "core", "pastebin_ports") or { 5280 }; 85 local ports = config.get(module.host, "core", "pastebin_ports") or { 5280 };
65 for _, options in ipairs(ports) do 86 for _, options in ipairs(ports) do
66 local port, base, ssl, interface = 5280, "pastebin", false, nil; 87 local port, base, ssl, interface = 5280, "pastebin", false, nil;
67 if type(options) == "number" then 88 if type(options) == "number" then
68 port = options; 89 port = options;