changeset 2125:edf5cf3c474b

mod_firewall: Move meta() function to main module, and make it a global so libs can use it
author Matthew Wild <mwild1@gmail.com>
date Thu, 17 Mar 2016 21:29:07 +0000
parents 89363766202c
children b0d711cd3da5
files mod_firewall/actions.lib.lua mod_firewall/conditions.lib.lua mod_firewall/mod_firewall.lua
diffstat 3 files changed, 18 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mod_firewall/actions.lib.lua	Thu Mar 17 21:27:24 2016 +0000
+++ b/mod_firewall/actions.lib.lua	Thu Mar 17 21:29:07 2016 +0000
@@ -1,13 +1,7 @@
+--luacheck: globals meta idsafe
 local action_handlers = {};
 
 
--- Run code through this to allow strings to contain code. e.g.: LOG=Received: $(stanza:top_tag())
-local function meta(s, extra)
-	return (s:gsub("$(%b())", [["..tostring(%1).."]])
-		:gsub("$(%b<>)", [["..stanza:find("%1").."]])
-		:gsub("$$(%a+)", extra or {}));
-end
-
 -- Takes an XML string and returns a code string that builds that stanza
 -- using st.stanza()
 local function compile_xml(data)
--- a/mod_firewall/conditions.lib.lua	Thu Mar 17 21:27:24 2016 +0000
+++ b/mod_firewall/conditions.lib.lua	Thu Mar 17 21:29:07 2016 +0000
@@ -1,3 +1,4 @@
+--luacheck: globals meta idsafe
 local condition_handlers = {};
 
 local jid = require "util.jid";
--- a/mod_firewall/mod_firewall.lua	Thu Mar 17 21:27:24 2016 +0000
+++ b/mod_firewall/mod_firewall.lua	Thu Mar 17 21:29:07 2016 +0000
@@ -42,6 +42,22 @@
 	return name:match("^%a[%w_]*$")
 end
 
+-- Run quoted (%q) strings through this to allow them to contain code. e.g.: LOG=Received: $(stanza:top_tag())
+function meta(s, extra)
+	return (s:gsub("$(%b())", [["..tostring(%1).."]])
+		:gsub("$(%b<>)", function (expr)
+			expr = expr:sub(2,-2);
+			if expr:match("^@") then
+				return "\"..stanza.attr["..(%q):format(expr:sub(2)).."]..\"";
+			end
+			return "\"..stanza:find("..("%q"):format(expr:sub(2, -2))..")..\"";
+		end)
+		:gsub("$$(%a+)", extra or {})
+		:gsub([[^""%.%.]], "")
+		:gsub([[%.%.""$]], ""));
+end
+
+
 -- Dependency locations:
 -- <type lib>
 -- <type global>