From 7bbf59625a83ccd1cff50742cc2cc60f5724e9fd Mon Sep 17 00:00:00 2001 From: Alain Zscheile Date: Sun, 27 Nov 2022 00:40:44 +0100 Subject: [PATCH] add ability to supply arbitrary filters --- lib/floof/application.ex | 4 +++- lib/floof/distributor.ex | 23 +++++++++++++++++++---- mix.exs | 3 +++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/floof/application.ex b/lib/floof/application.ex index 378c8ad..6d4572d 100644 --- a/lib/floof/application.ex +++ b/lib/floof/application.ex @@ -14,6 +14,7 @@ defmodule Floof.Application do markers = Application.fetch_env!(:floof, :markers) attrs = Application.fetch_env!(:floof, :attrs) set_markers = Application.fetch_env!(:floof, :set_markers) + extra_filters = Application.fetch_env!(:floof, :extra_filters) spool_dir = Application.fetch_env!(:floof, :spool_dir) sessions_file = Application.fetch_env!(:floof, :sessions_file) @@ -28,7 +29,8 @@ defmodule Floof.Application do Floof.Distributor.fconfig( markers: markers, attrs: attrs, - set_markers: set_markers + set_markers: set_markers, + extra_filters: extra_filters )}, {Floof.SessionManager, {:sessions, [file: to_charlist(sessions_file)]}} ] ++ diff --git a/lib/floof/distributor.ex b/lib/floof/distributor.ex index bf9de86..6e5e472 100644 --- a/lib/floof/distributor.ex +++ b/lib/floof/distributor.ex @@ -12,7 +12,8 @@ defmodule Floof.Distributor do Record.defrecord(:fconfig, markers: [], attrs: [], - set_markers: [] + set_markers: [], + extra_filters: [] ) Record.defrecord(:dstate, fconf: {}, trgs: MapSet.new()) @@ -72,13 +73,27 @@ defmodule Floof.Distributor do {:XferBlob, ttl, old_markers, signature, data} = dcd set_markers = fconfig(fconf, :set_markers) new_markers = Enum.uniq(set_markers ++ old_markers) - dcd2 = {:XferBlob, ttl - 1, new_markers, signature, data} + dcd = {:XferBlob, ttl - 1, new_markers, signature, data} + + dcd = + Enum.reduce( + fconfig(fconf, :extra_filters), + dcd, + fn f, dcdx -> + case dcdx do + nil -> nil + dcdx -> f.(dcdx) + end + end + ) for trg <- trgs do - send(trg, {:fwdxfer, {dcdhash, dcd2}}) + send(trg, {:fwdxfer, {dcdhash, dcd}}) end - Floof.SessionManager.set_for_all(dcdhash, dcd2, origin) + if ttl > 1 do + Floof.SessionManager.set_for_all(dcdhash, dcd, origin) + end end) end diff --git a/mix.exs b/mix.exs index ca3212d..35e4a75 100644 --- a/mix.exs +++ b/mix.exs @@ -34,6 +34,9 @@ defmodule Floof.MixProject do # these are the markers of form {:RelativeDistinguishedName, oid-as-list(), value()} # which get added to all processed messages set_markers: [], + # a list of functions which can be used to further filter packets + # (by returning nil if unwanted) and modify them + extra_filters: [], # file locations # the spool dir saves messages in-transit, and is used to prevent RAM OOM spool_dir: "mock_spool",