From f2408e750369dddfeccd937691ec467574cd1890 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:02:10 +0200 Subject: [PATCH 1/2] Add sendevent to listeners sendevent is an event socket command like api and filter, so listeners now send it themselves: sendevent(name, headers) fires the event and returns the Event-UUID FreeSWITCH gives it (its reply is "+OK "), which every listener sees. Headers with a nil value are left out. --- lib/librevox/listener/base.rb | 11 +++++++++++ test/functional/librevox/listener/inbound_test.rb | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/librevox/listener/base.rb b/lib/librevox/listener/base.rb index 9e97d3d..9c898c0 100644 --- a/lib/librevox/listener/base.rb +++ b/lib/librevox/listener/base.rb @@ -59,6 +59,17 @@ def send_message(msg) reply end + # Fire an event into FreeSWITCH, for every ESL listener subscribed to it. + # Returns the Event-UUID FreeSWITCH gives the event, which every listener + # sees. + # @example + # sendevent "CUSTOM", "Event-Subclass" => "my::event", "Some-Header" => "value" + def sendevent(name, headers = {}) + lines = headers.compact.map { |header, value| "#{header}: #{value}" } + reply = send_message(["sendevent #{name}", *lines].join("\n")) + reply.headers[:reply_text].delete_prefix("+OK ") + end + def execute_app(app, uuid, args = nil, **params) event_uuid = SecureRandom.uuid diff --git a/test/functional/librevox/listener/inbound_test.rb b/test/functional/librevox/listener/inbound_test.rb index 407ce77..7c19069 100644 --- a/test/functional/librevox/listener/inbound_test.rb +++ b/test/functional/librevox/listener/inbound_test.rb @@ -34,4 +34,13 @@ def test_sends_auth_and_event_subscription assert_equal "event plain ALL", @listener.outgoing_data.shift assert_nil @listener.outgoing_data.shift end + + def test_sendevent_sends_the_event_and_returns_its_event_uuid + @listener.outgoing_data.clear + sending = Async { @listener.sendevent("CUSTOM", "Event-Subclass" => "my::event", "Some-Header" => "value", "Skipped" => nil) } + command_reply "Reply-Text" => "+OK 2488abe2-c494-4d71-a83f-f3ab40c75f44" + + assert_equal "2488abe2-c494-4d71-a83f-f3ab40c75f44", sending.wait + assert_equal "sendevent CUSTOM\nEvent-Subclass: my::event\nSome-Header: value", @listener.outgoing_data.shift + end end From e5be901aec6d88b1fa0c6fa0549a03a04e607ee9 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:05:16 +0200 Subject: [PATCH 2/2] Refuse line breaks in sendevent headers A line break in an event name, header name or value would end the sendevent command early and send the rest as a command of its own, e.g. a value of "x\n\napi uuid_kill all". sendevent now raises ArgumentError instead of sending anything. --- lib/librevox/listener/base.rb | 10 ++++++++-- test/functional/librevox/listener/inbound_test.rb | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/librevox/listener/base.rb b/lib/librevox/listener/base.rb index 9c898c0..e0a851a 100644 --- a/lib/librevox/listener/base.rb +++ b/lib/librevox/listener/base.rb @@ -61,11 +61,17 @@ def send_message(msg) # Fire an event into FreeSWITCH, for every ESL listener subscribed to it. # Returns the Event-UUID FreeSWITCH gives the event, which every listener - # sees. + # sees. A line break in a header would end the command early and send + # the rest as another command, so one raises ArgumentError. # @example # sendevent "CUSTOM", "Event-Subclass" => "my::event", "Some-Header" => "value" def sendevent(name, headers = {}) - lines = headers.compact.map { |header, value| "#{header}: #{value}" } + headers = headers.compact + if [name, *headers.flatten].any? { |part| part.to_s.match?(/[\r\n]/) } + raise ArgumentError, "sendevent headers can't contain line breaks" + end + + lines = headers.map { |header, value| "#{header}: #{value}" } reply = send_message(["sendevent #{name}", *lines].join("\n")) reply.headers[:reply_text].delete_prefix("+OK ") end diff --git a/test/functional/librevox/listener/inbound_test.rb b/test/functional/librevox/listener/inbound_test.rb index 7c19069..ca89bca 100644 --- a/test/functional/librevox/listener/inbound_test.rb +++ b/test/functional/librevox/listener/inbound_test.rb @@ -43,4 +43,15 @@ def test_sendevent_sends_the_event_and_returns_its_event_uuid assert_equal "2488abe2-c494-4d71-a83f-f3ab40c75f44", sending.wait assert_equal "sendevent CUSTOM\nEvent-Subclass: my::event\nSome-Header: value", @listener.outgoing_data.shift end + + # A line break would end the command early and send the rest as a command + # of its own. + def test_sendevent_refuses_a_line_break_in_a_header + @listener.outgoing_data.clear + + assert_raises(ArgumentError) { @listener.sendevent("CUSTOM", "Some-Header" => "x\n\napi uuid_kill all") } + assert_raises(ArgumentError) { @listener.sendevent("CUSTOM", "Some\r\nHeader" => "x") } + assert_raises(ArgumentError) { @listener.sendevent("CUSTOM\nbgapi status") } + assert_nil @listener.outgoing_data.shift, "nothing was sent" + end end