Mercurial > prosody-modules
comparison mod_firewall/conditions.lib.lua @ 2386:00eed68f63bf
mod_firewall: INSPECT: support for literal substring search and expressions
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 19 Nov 2016 15:52:32 +0000 |
parents | 12b78170b76c |
children | f96bdfd81eba |
comparison
equal
deleted
inserted
replaced
2385:c0c2f8665c3e | 2386:00eed68f63bf |
---|---|
109 return ("stanza:get_child(nil, %q)"):format(payload_ns); | 109 return ("stanza:get_child(nil, %q)"):format(payload_ns); |
110 end | 110 end |
111 | 111 |
112 function condition_handlers.INSPECT(path) | 112 function condition_handlers.INSPECT(path) |
113 if path:find("=") then | 113 if path:find("=") then |
114 local query, is_pattern_match, value = path:match("(.-)(~?)=(.*)"); | 114 local query, match_type, value = path:match("(.-)([~/$]*)=(.*)"); |
115 if not(query:match("#$") or query:match("@[^/]+")) then | 115 if not(query:match("#$") or query:match("@[^/]+")) then |
116 error("Stanza path does not return a string (append # for text content or @name for value of named attribute)", 0); | 116 error("Stanza path does not return a string (append # for text content or @name for value of named attribute)", 0); |
117 end | 117 end |
118 if is_pattern_match ~= "" then | 118 local quoted_value = ("%q"):format(value); |
119 return ("(stanza:find(%q) or ''):match(%q)"):format(path:match("(.-)~=(.*)")); | 119 if match_type:find("$", 1, true) then |
120 match_type = match_type:gsub("%$", ""); | |
121 quoted_value = meta(quoted_value); | |
122 end | |
123 if match_type == "~" then -- Lua pattern match | |
124 return ("(stanza:find(%q) or ''):match(%s)"):format(query, quoted_value); | |
125 elseif match_type == "/" then -- find literal substring | |
126 return ("(stanza:find(%q) or ''):find(%s, 1, true)"):format(query, quoted_value); | |
127 elseif match_type == "" then -- exact match | |
128 return ("stanza:find(%q) == %s"):format(query, quoted_value); | |
120 else | 129 else |
121 return ("stanza:find(%q) == %q"):format(path:match("(.-)=(.*)")); | 130 error("Unrecognised comparison '"..match_type.."='", 0); |
122 end | 131 end |
123 end | 132 end |
124 return ("stanza:find(%q)"):format(path); | 133 return ("stanza:find(%q)"):format(path); |
125 end | 134 end |
126 | 135 |