changeset 4512:b88f05c878ac

mod_firewall: Add basic LIST backend for receiving items from PubSub Relies on mod_pubsub_subscription
author Kim Alvefur <zash@zash.se>
date Sun, 14 Mar 2021 23:31:32 +0100
parents 97fac0ba0469
children ade2064160e3
files mod_firewall/definitions.lib.lua
diffstat 1 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_firewall/definitions.lib.lua	Mon Mar 15 16:31:23 2021 +0100
+++ b/mod_firewall/definitions.lib.lua	Sun Mar 14 23:31:32 2021 +0100
@@ -188,6 +188,55 @@
 			return self.items and self.items[item] == true;
 		end;
 	};
+
+	-- %LIST: pubsub:pubsub.example.com/node
+	-- TODO or the actual URI scheme? Bit overkill maybe?
+	-- TODO Publish items back to the service?
+	-- Step 1: Receiving pubsub events and storing them in the list
+	-- We'll start by using only the item id.
+	-- TODO Invent some custom schema for this? Needed for just a set of strings?
+	pubsubitemid = {
+		init = function(self, pubsub_spec, opts)
+			local service_addr, node = pubsub_spec:match("^([^/]*)/(.*)");
+			module:depends("pubsub_subscription");
+			module:add_item("pubsub-subscription", {
+					service = service_addr;
+					node = node;
+					on_subscribed = function ()
+						self.items = {};
+					end;
+					on_item = function (event)
+						self:add(event.item.attr.id);
+					end;
+					on_retract = function (event)
+						self:remove(event.item.attr.id);
+					end;
+					on_purge = function ()
+						self.items = {};
+					end;
+					on_unsubscribed = function ()
+						self.items = nil;
+					end;
+					on_delete= function ()
+						self.items = nil;
+					end;
+				});
+			-- TODO Initial fetch? Or should mod_pubsub_subscription do this?
+		end;
+		add = function (self, item)
+			if self.items then
+				self.items[item] = true;
+			end
+		end;
+		remove = function (self, item)
+			if self.items then
+				self.items[item] = nil;
+			end
+		end;
+		contains = function (self, item)
+			return self.items and self.items[item] == true;
+		end;
+	};
 };
 list_backends.https = list_backends.http;