From a89fa5148e8e69c2581edfd7b8a9da644030c3a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 1 Oct 2026 12:08:16 +0200 Subject: [PATCH 1/4] Let filters take a block, evaluated on connect filters 'Header' => value runs when the class body loads, before an app may know a value it filters on. filters { { 'Header' => value } } now works too: the block runs on the listener when the connection starts. --- lib/librevox/listener/inbound.rb | 11 ++++-- .../librevox/listener/inbound_filter_test.rb | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index 7517bef..a6c94c7 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -11,8 +11,14 @@ def events(events) @subscribe_events = events end - def filters(filters) - @subscribe_filters = filters + # The header filters to add on connecting, as a hash, or a block that + # returns one when the connection starts (for values not known when + # the class loads). + # @example + # filters "Event-Name" => "CHANNEL_PARK" + # filters { { "variable_app" => App.current.name } } + def filters(filters = nil, &block) + @subscribe_filters = block || filters end end @@ -35,6 +41,7 @@ def run_session # connection has no filters, so subscribing first would let the whole # node's events through until the filters land. filters = self.class.subscribe_filters || {} + filters = instance_exec(&filters) if filters.is_a?(Proc) filters.each do |header, values| [*values].each do |value| send_message "filter #{header} #{value}" diff --git a/test/functional/librevox/listener/inbound_filter_test.rb b/test/functional/librevox/listener/inbound_filter_test.rb index cf770fc..06ad891 100644 --- a/test/functional/librevox/listener/inbound_filter_test.rb +++ b/test/functional/librevox/listener/inbound_filter_test.rb @@ -46,3 +46,38 @@ def test_sends_auth_then_filters_then_events assert_nil @listener.outgoing_data.shift end end + +class InboundBlockFilterTestListener < Librevox::Listener::Inbound + events ['CUSTOM'] + + filters { { 'Caller-Context' => context } } + + def context + 'evaluated-on-connect' + end +end + +class TestInboundListenerWithBlockFilters < Minitest::Test + prepend Librevox::Test::AsyncTest + include Librevox::Test::ListenerHelpers + + def setup + @listener = InboundBlockFilterTestListener.new(MockConnection.new) + @session_task = Async { @listener.run_session } + command_reply "Reply-Text" => "+OK accepted" + command_reply "Reply-Text" => "+OK filter added" + command_reply "Reply-Text" => "+OK event listener enabled plain" + end + + def teardown + @session_task&.stop + super + end + + # The block runs on the listener when the connection starts. + def test_a_filter_block_is_evaluated_on_connect + assert_equal "auth ClueCon", @listener.outgoing_data.shift + assert_equal "filter Caller-Context evaluated-on-connect", @listener.outgoing_data.shift + assert_equal "event plain CUSTOM", @listener.outgoing_data.shift + end +end From fd44cc68be21b566f699e70fb2c62fa1308d6c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 1 Oct 2026 12:10:07 +0200 Subject: [PATCH 2/4] Take a lambda filter value instead of a filters block filters { { 'Header' => value } } read oddly. A lambda value, the way Rails defers a scope, keeps the hash form: filters 'Header' => -> { value }. The lambda runs on the listener when the connection starts. --- lib/librevox/listener/inbound.rb | 14 +++++++------- .../librevox/listener/inbound_filter_test.rb | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index a6c94c7..b568797 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -11,14 +11,14 @@ def events(events) @subscribe_events = events end - # The header filters to add on connecting, as a hash, or a block that - # returns one when the connection starts (for values not known when - # the class loads). + # The header filters to add on connecting. A value can be a lambda, + # run on the listener when the connection starts, for a value not + # known when the class loads. # @example # filters "Event-Name" => "CHANNEL_PARK" - # filters { { "variable_app" => App.current.name } } - def filters(filters = nil, &block) - @subscribe_filters = block || filters + # filters "variable_app" => -> { App.current.name } + def filters(filters) + @subscribe_filters = filters end end @@ -41,8 +41,8 @@ def run_session # connection has no filters, so subscribing first would let the whole # node's events through until the filters land. filters = self.class.subscribe_filters || {} - filters = instance_exec(&filters) if filters.is_a?(Proc) filters.each do |header, values| + values = instance_exec(&values) if values.is_a?(Proc) [*values].each do |value| send_message "filter #{header} #{value}" end diff --git a/test/functional/librevox/listener/inbound_filter_test.rb b/test/functional/librevox/listener/inbound_filter_test.rb index 06ad891..4fd2a3d 100644 --- a/test/functional/librevox/listener/inbound_filter_test.rb +++ b/test/functional/librevox/listener/inbound_filter_test.rb @@ -47,22 +47,22 @@ def test_sends_auth_then_filters_then_events end end -class InboundBlockFilterTestListener < Librevox::Listener::Inbound +class InboundLazyFilterTestListener < Librevox::Listener::Inbound events ['CUSTOM'] - filters { { 'Caller-Context' => context } } + filters 'Caller-Context' => -> { context } def context 'evaluated-on-connect' end end -class TestInboundListenerWithBlockFilters < Minitest::Test +class TestInboundListenerWithLazyFilters < Minitest::Test prepend Librevox::Test::AsyncTest include Librevox::Test::ListenerHelpers def setup - @listener = InboundBlockFilterTestListener.new(MockConnection.new) + @listener = InboundLazyFilterTestListener.new(MockConnection.new) @session_task = Async { @listener.run_session } command_reply "Reply-Text" => "+OK accepted" command_reply "Reply-Text" => "+OK filter added" @@ -74,8 +74,8 @@ def teardown super end - # The block runs on the listener when the connection starts. - def test_a_filter_block_is_evaluated_on_connect + # The lambda runs on the listener when the connection starts. + def test_a_lambda_filter_value_is_evaluated_on_connect assert_equal "auth ClueCon", @listener.outgoing_data.shift assert_equal "filter Caller-Context evaluated-on-connect", @listener.outgoing_data.shift assert_equal "event plain CUSTOM", @listener.outgoing_data.shift From 4d4ea8b46b95aabb3760bad1a67a16e0bbd221cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 1 Oct 2026 12:11:51 +0200 Subject: [PATCH 3/4] Revert "Take a lambda filter value instead of a filters block" This reverts commit fd44cc68be21b566f699e70fb2c62fa1308d6c65. --- lib/librevox/listener/inbound.rb | 14 +++++++------- .../librevox/listener/inbound_filter_test.rb | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index b568797..a6c94c7 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -11,14 +11,14 @@ def events(events) @subscribe_events = events end - # The header filters to add on connecting. A value can be a lambda, - # run on the listener when the connection starts, for a value not - # known when the class loads. + # The header filters to add on connecting, as a hash, or a block that + # returns one when the connection starts (for values not known when + # the class loads). # @example # filters "Event-Name" => "CHANNEL_PARK" - # filters "variable_app" => -> { App.current.name } - def filters(filters) - @subscribe_filters = filters + # filters { { "variable_app" => App.current.name } } + def filters(filters = nil, &block) + @subscribe_filters = block || filters end end @@ -41,8 +41,8 @@ def run_session # connection has no filters, so subscribing first would let the whole # node's events through until the filters land. filters = self.class.subscribe_filters || {} + filters = instance_exec(&filters) if filters.is_a?(Proc) filters.each do |header, values| - values = instance_exec(&values) if values.is_a?(Proc) [*values].each do |value| send_message "filter #{header} #{value}" end diff --git a/test/functional/librevox/listener/inbound_filter_test.rb b/test/functional/librevox/listener/inbound_filter_test.rb index 4fd2a3d..06ad891 100644 --- a/test/functional/librevox/listener/inbound_filter_test.rb +++ b/test/functional/librevox/listener/inbound_filter_test.rb @@ -47,22 +47,22 @@ def test_sends_auth_then_filters_then_events end end -class InboundLazyFilterTestListener < Librevox::Listener::Inbound +class InboundBlockFilterTestListener < Librevox::Listener::Inbound events ['CUSTOM'] - filters 'Caller-Context' => -> { context } + filters { { 'Caller-Context' => context } } def context 'evaluated-on-connect' end end -class TestInboundListenerWithLazyFilters < Minitest::Test +class TestInboundListenerWithBlockFilters < Minitest::Test prepend Librevox::Test::AsyncTest include Librevox::Test::ListenerHelpers def setup - @listener = InboundLazyFilterTestListener.new(MockConnection.new) + @listener = InboundBlockFilterTestListener.new(MockConnection.new) @session_task = Async { @listener.run_session } command_reply "Reply-Text" => "+OK accepted" command_reply "Reply-Text" => "+OK filter added" @@ -74,8 +74,8 @@ def teardown super end - # The lambda runs on the listener when the connection starts. - def test_a_lambda_filter_value_is_evaluated_on_connect + # The block runs on the listener when the connection starts. + def test_a_filter_block_is_evaluated_on_connect assert_equal "auth ClueCon", @listener.outgoing_data.shift assert_equal "filter Caller-Context evaluated-on-connect", @listener.outgoing_data.shift assert_equal "event plain CUSTOM", @listener.outgoing_data.shift From 2500688c62bd66706269e96390912dda1a6257b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 1 Oct 2026 12:19:23 +0200 Subject: [PATCH 4/4] Return sched_del's count sched_del now returns how many tasks FreeSWITCH deleted, read from its "+OK Deleted: " reply, so callers check a number instead of matching the reply text. --- lib/librevox/commands.rb | 7 ++++--- test/unit/librevox/commands_test.rb | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/librevox/commands.rb b/lib/librevox/commands.rb index 40b7fff..4b8fa14 100644 --- a/lib/librevox/commands.rb +++ b/lib/librevox/commands.rb @@ -85,12 +85,13 @@ def sched_hangup(time, uuid, cause = nil) # Delete a scheduled task by its id, or every task in a group. A call's # scheduled tasks (sched_hangup, sched_transfer, …) are grouped by its uuid. - # FreeSWITCH replies "+OK Deleted: ". + # Returns how many tasks were deleted, from FreeSWITCH's + # "+OK Deleted: ". # @example - # socket.sched_del "592567a2-1be4-11df-a036-19bfdab2092f" + # socket.sched_del "592567a2-1be4-11df-a036-19bfdab2092f" # => 1 # @see http://wiki.freeswitch.org/wiki/Mod_commands#sched_del def sched_del(id) - command "sched_del", id + command("sched_del", id).content[/\A\+OK Deleted: (\d+)/, 1].to_i end # Bridge two call legs together. At least one leg must be answered. diff --git a/test/unit/librevox/commands_test.rb b/test/unit/librevox/commands_test.rb index f7e65d0..2c17686 100644 --- a/test/unit/librevox/commands_test.rb +++ b/test/unit/librevox/commands_test.rb @@ -110,9 +110,22 @@ def test_sched_hangup assert_equal "+3600 1234-abcd ALLOTTED_TIMEOUT", cmd[:args] end - def test_sched_del - cmd = C.sched_del "1234-abcd" - assert_equal "sched_del", cmd[:name] - assert_equal "1234-abcd", cmd[:args] + # sched_del reads its count from FreeSWITCH's reply. + module SchedDelReply + include Librevox::Commands + + extend self + + attr_accessor :sent + + def command(name, args = "") + self.sent = [name, args] + Librevox::Protocol::Response.new("Content-Type: api/response", "+OK Deleted: 1\n") + end + end + + def test_sched_del_returns_how_many_tasks_it_deleted + assert_equal 1, SchedDelReply.sched_del("1234-abcd") + assert_equal ["sched_del", "1234-abcd"], SchedDelReply.sent end end