comparison mod_pastebin/mod_pastebin.lua @ 3031:040eaa3844f4

mod_pastebin: Check line length via preconstructed pattern instead of gsub Optimization on the theory that this check scales with the configuration option instead of with the size of the user-supplied message
author Kim Alvefur <zash@zash.se>
date Sun, 20 May 2018 17:55:53 +0200
parents 4a7f08fe793c
children 0f2dd6397569
comparison
equal deleted inserted replaced
3030:4a7f08fe793c 3031:040eaa3844f4
87 if not once then 87 if not once then
88 s:add_child(replacement); 88 s:add_child(replacement);
89 end 89 end
90 end 90 end
91 91
92 local line_count_pattern = string.rep("[^\n]\n", line_threshold):sub(1, -2);
93
92 function check_message(data) 94 function check_message(data)
93 local origin, stanza = data.origin, data.stanza; 95 local origin, stanza = data.origin, data.stanza;
94 96
95 -- Only check for MUC presence when loaded on a component. 97 -- Only check for MUC presence when loaded on a component.
96 if is_component then 98 if is_component then
107 109
108 --module:log("debug", "Body(%s) length: %d", type(body), #(body or "")); 110 --module:log("debug", "Body(%s) length: %d", type(body), #(body or ""));
109 111
110 if ( #body > length_threshold and utf8_length(body) > length_threshold ) or 112 if ( #body > length_threshold and utf8_length(body) > length_threshold ) or
111 (trigger_string and body:find(trigger_string, 1, true) == 1) or 113 (trigger_string and body:find(trigger_string, 1, true) == 1) or
112 (select(2, body:gsub("\n", "%0")) >= line_threshold) 114 body:find(line_count_pattern) then
113 then
114 if trigger_string and body:sub(1, #trigger_string) == trigger_string then 115 if trigger_string and body:sub(1, #trigger_string) == trigger_string then
115 body = body:sub(#trigger_string+1); 116 body = body:sub(#trigger_string+1);
116 end 117 end
117 local url = pastebin_text(body); 118 local url = pastebin_text(body);
118 module:log("debug", "Pasted message as %s", url); 119 module:log("debug", "Pasted message as %s", url);