comparison mod_pastebin/mod_pastebin.lua @ 444:82ccfba5ac2f

mod_pastebin: Support for changing the summary length (pastebin_summary_length), and including it even in the plaintext version of the generated message
author Matthew Wild <mwild1@gmail.com>
date Fri, 23 Sep 2011 20:03:22 +0100
parents 7f0cdde1e42a
children 66ca5e4d9f7b
comparison
equal deleted inserted replaced
443:7679b8f6b886 444:82ccfba5ac2f
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; 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 pastebin_private_messages = module:get_option_boolean("pastebin_private_messages", hosts[module.host].type ~= "component"); 9 local utf8_pattern = "[\194-\244][\128-\191]*$";
10
11 local function drop_invalid_utf8(seq) 10 local function drop_invalid_utf8(seq)
12 local start = seq:byte(); 11 local start = seq:byte();
13 module:log("utf8: %d, %d", start, #seq); 12 module:log("utf8: %d, %d", start, #seq);
14 if (start <= 223 and #seq < 2) 13 if (start <= 223 and #seq < 2)
15 or (start >= 224 and start <= 239 and #seq < 3) 14 or (start >= 224 and start <= 239 and #seq < 3)
18 return ""; 17 return "";
19 end 18 end
20 return seq; 19 return seq;
21 end 20 end
22 21
22 local pastebin_private_messages = module:get_option_boolean("pastebin_private_messages", hosts[module.host].type ~= "component");
23 local max_summary_length = module:get_option_number("pastebin_summary_length", 150);
23 local length_threshold = config.get(module.host, "core", "pastebin_threshold") or 500; 24 local length_threshold = config.get(module.host, "core", "pastebin_threshold") or 500;
24 local line_threshold = config.get(module.host, "core", "pastebin_line_threshold") or 4; 25 local line_threshold = config.get(module.host, "core", "pastebin_line_threshold") or 4;
25 26
26 local base_url = config.get(module.host, "core", "pastebin_url"); 27 local base_url = config.get(module.host, "core", "pastebin_url");
27 28
84 body = body:gsub("^" .. trigger_string, "", 1); 85 body = body:gsub("^" .. trigger_string, "", 1);
85 end 86 end
86 local url = pastebin_text(body); 87 local url = pastebin_text(body);
87 module:log("debug", "Pasted message as %s", url); 88 module:log("debug", "Pasted message as %s", url);
88 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex])); 89 --module:log("debug", " stanza[bodyindex] = %q", tostring( stanza[bodyindex]));
89 stanza[bodyindex][1] = url; 90 local summary = body:sub(1, max_summary_length):gsub(utf8_pattern, drop_invalid_utf8) or "";
91 stanza[bodyindex][1] = summary:match("^([^\n:]*:?)").." "..url;
90 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml }); 92 local html = st.stanza("html", { xmlns = xmlns_xhtmlim }):tag("body", { xmlns = xmlns_xhtml });
91 html:tag("p"):text(body:sub(1,150):gsub("[\194-\244][\128-\191]*$", drop_invalid_utf8)):up(); 93 html:tag("p"):text(summary):up();
92 html:tag("a", { href = url }):text("[...]"):up(); 94 html:tag("a", { href = url }):text("[...]"):up();
93 stanza[htmlindex or #stanza+1] = html; 95 stanza[htmlindex or #stanza+1] = html;
94 end 96 end
95 end 97 end
96 98