Comparison

mod_firewall/conditions.lib.lua @ 2545:9b46d24edf0d

mod_firewall: Add and document COUNT condition
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Feb 2017 22:41:58 +0000
parent 2537:acdc1767a715
child 2552:18b6a55dd5d6
comparison
equal deleted inserted replaced
2544:223eea31588d 2545:9b46d24edf0d
277 error("Error parsing SCAN expression, syntax: SEARCH for PATTERN in LIST"); 277 error("Error parsing SCAN expression, syntax: SEARCH for PATTERN in LIST");
278 end 278 end
279 return ("scan_list(list_%s, %s)"):format(list_name, "tokens_"..search_name.."_"..pattern_name), { "scan_list", "tokens:"..search_name.."_"..pattern_name, "list:"..list_name }; 279 return ("scan_list(list_%s, %s)"):format(list_name, "tokens_"..search_name.."_"..pattern_name), { "scan_list", "tokens:"..search_name.."_"..pattern_name, "list:"..list_name };
280 end 280 end
281 281
282 local valid_comp_ops = { [">"] = ">", ["<"] = "<", ["="] = "==", ["=="] = "==", ["<="] = "<=", [">="] = ">=" };
283 function condition_handlers.COUNT(count_expression)
284 local pattern_name, search_name, comparator_expression = count_expression:match("(%S+) in (%S+) (.+)$");
285 if not (pattern_name) then
286 error("Error parsing COUNT expression, syntax: PATTERN in SEARCH COMPARATOR");
287 end
288 local value;
289 comparator_expression = comparator_expression:gsub("%d+", function (value_string)
290 value = tonumber(value_string);
291 return "";
292 end);
293 if not value then
294 error("Error parsing COUNT expression, expected value");
295 end
296 local comp_op = comparator_expression:gsub("%s+", "");
297 assert(valid_comp_ops[comp_op], "Error parsing COUNT expression, unknown comparison operator: "..comp_op);
298 return ("it_count(search_%s:gmatch(pattern_%s)) %s %d"):format(search_name, pattern_name, comp_op, value), { "it_count", "search:"..search_name, "pattern:"..pattern_name };
299 end
300
282 return condition_handlers; 301 return condition_handlers;