diff mod_firewall/conditions.lib.lua @ 2342:6848297cf40a

mod_firewall: Add conditions for testing whether a sender of a stanza is in the recipient's roster (or in a certain roster group)
author Matthew Wild <mwild1@gmail.com>
date Fri, 04 Nov 2016 12:46:11 +0000
parents 21bc4d7cddae
children c065ab67d807
line wrap: on
line diff
--- a/mod_firewall/conditions.lib.lua	Wed Nov 02 20:42:27 2016 +0100
+++ b/mod_firewall/conditions.lib.lua	Fri Nov 04 12:46:11 2016 +0000
@@ -3,6 +3,12 @@
 
 local jid = require "util.jid";
 
+-- Helper to convert user-input strings (yes/true//no/false) to a bool
+local function string_to_boolean(s)
+	s = s:lower();
+	return s == "yes" or s == "true";
+end
+
 -- Return a code string for a condition that checks whether the contents
 -- of variable with the name 'name' matches any of the values in the
 -- comma/space/pipe delimited list 'values'.
@@ -90,6 +96,15 @@
 	return zone_check(zone, "from");
 end
 
+function condition_handlers.IN_ROSTER(yes_no)
+	local in_roster_requirement = string_to_boolean(yes_no);
+	return "not "..(in_roster_requirement and "not" or "").." roster_entry", { "roster_entry" };
+end
+
+function condition_handlers.IN_ROSTER_GROUP(group)
+	return ("not not (roster_entry and roster_entry.groups[%q])"):format(group), { "roster_entry" };
+end
+
 function condition_handlers.PAYLOAD(payload_ns)
 	return ("stanza:get_child(nil, %q)"):format(payload_ns);
 end