Comparison

mod_pastebin/mod_pastebin.lua @ 167:0d37d18ea073

mod_pastebin: Fix trigger_string matching when no trigger is set, and add support for counting lines (pastebin_line_threshold, default: 4)
author Matthew Wild <mwild1@gmail.com>
date Thu, 10 Jun 2010 00:46:46 +0100
parent 156:b51741b7e86d
child 189:16d367e3c04e
comparison
equal deleted inserted replaced
166:75a85eac3c27 167:0d37d18ea073
5 local os_time = os.time; 5 local os_time = os.time;
6 local t_insert, t_remove = table.insert, table.remove; 6 local t_insert, t_remove = table.insert, table.remove;
7 local add_task = require "util.timer".add_task; 7 local add_task = require "util.timer".add_task;
8 8
9 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;
10 local line_threshold = config.get(module.host, "core", "pastebin_line_threshold") or 4;
10 11
11 local base_url = config.get(module.host, "core", "pastebin_url"); 12 local base_url = config.get(module.host, "core", "pastebin_url");
12 13
13 -- Seconds a paste should live for in seconds (config is in hours), default 24 hours 14 -- 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); 15 local expire_after = math.floor((config.get(module.host, "core", "pastebin_expire_after") or 24) * 3600);
15 16
16 local trigger_string = config.get(module.host, "core", "pastebin_trigger"); 17 local trigger_string = config.get(module.host, "core", "pastebin_trigger");
17 trigger_string = (trigger_string and trigger_string .. " ") or ""; 18 trigger_string = (trigger_string and trigger_string .. " ");
18 19
19 local pastes = {}; 20 local pastes = {};
20 local default_headers = { ["Content-Type"] = "text/plain; charset=utf-8" }; 21 local default_headers = { ["Content-Type"] = "text/plain; charset=utf-8" };
21 22
22 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im"; 23 local xmlns_xhtmlim = "http://jabber.org/protocol/xhtml-im";
58 if not body then return; end 59 if not body then return; end
59 body = body:get_text(); 60 body = body:get_text();
60 61
61 --module:log("debug", "Body(%s) length: %d", type(body), #(body or "")); 62 --module:log("debug", "Body(%s) length: %d", type(body), #(body or ""));
62 63
63 if body and ((#body > length_threshold) or (body:find(trigger_string, 1, true) == 1)) then 64 if body and (
64 body = body:gsub("^" .. trigger_string, "", 1); 65 (#body > length_threshold) or
66 (trigger_string and body:find(trigger_string, 1, true) == 1) or
67 (select(2, body:gsub("\n", "%0")) >= line_threshold)
68 ) then
69 if trigger_string then
70 body = body:gsub("^" .. trigger_string, "", 1);
71 end
65 local url = pastebin_text(body); 72 local url = pastebin_text(body);
66 module:log("debug", "Pasted message as %s", url); 73 module:log("debug", "Pasted message as %s", url);
67 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex])); 74 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex]));
68 stanza[bodyindex][1] = url; 75 stanza[bodyindex][1] = url;
69 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml }); 76 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml });