changeset 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
files mod_pastebin/mod_pastebin.lua
diffstat 1 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_pastebin/mod_pastebin.lua	Fri Oct 02 13:52:37 2009 +0100
+++ b/mod_pastebin/mod_pastebin.lua	Fri Oct 02 22:15:14 2009 +0100
@@ -3,11 +3,16 @@
 local httpserver = require "net.httpserver";
 local uuid_new = require "util.uuid".generate;
 local os_time = os.time;
+local t_insert, t_remove = table.insert, table.remove;
+local add_task = require "util.timer".add_task;
 
 local length_threshold = config.get(module.host, "core", "pastebin_threshold") or 500;
 
 local base_url = config.get(module.host, "core", "pastebin_url");
 
+-- Seconds a paste should live for in seconds (config is in hours), default 24 hours
+local expire_after = math.floor((config.get(module.host, "core", "pastebin_expire_after") or 24) * 3600);
+
 local pastes = {};
 
 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im";
@@ -16,6 +21,10 @@
 local function pastebin_message(text)
 	local uuid = uuid_new();
 	pastes[uuid] = { text = text, time = os_time() };
+	pastes[#pastes+1] = uuid;
+	if not pastes[2] then -- No other pastes, give the timer a kick
+		add_task(expire_after, expire_pastes);
+	end
 	return base_url..uuid;
 end
 
@@ -61,6 +70,18 @@
 
 module:hook("message/bare", check_message);
 
+function expire_pastes(time)
+	time = time or os_time(); -- COMPAT with 0.5
+	if pastes[1] then
+		pastes[pastes[1]] = nil;
+		t_remove(pastes, 1);
+		if pastes[1] then
+			return (expire_after - (time - pastes[pastes[1]].time)) + 1;
+		end
+	end
+end
+
+
 local ports = config.get(module.host, "core", "pastebin_ports") or { 5280 };
 for _, options in ipairs(ports) do
 	local port, base, ssl, interface = 5280, "pastebin", false, nil;