From fef2eaf0c8f1ce6a5d9f3b98eaedd8e157e1a43e Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Fri, 23 Nov 2018 14:40:31 +0000 Subject: [PATCH 1/4] Support inbound event filters --- README.md | 6 ++++++ TODO | 2 -- lib/librevox/listener/inbound.rb | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eeecbdf..b04de10 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,12 @@ techniques: require 'librevox' class MyInbound < Librevox::Listener::Inbound + # Events to listen to (default is ALL") + events ['CHANNEL_EXECUTE', 'CUSTOM foo'] + + # Filters to apply (optional) + filters 'Caller-Context' => ['default'] + def on_event e puts "Got event: #{e.content[:event_name]}" end diff --git a/TODO b/TODO index c18d42f..3e09ef6 100644 --- a/TODO +++ b/TODO @@ -5,8 +5,6 @@ - check command/application input and raise ArgumentError, possibly. -- filter events - - move test suite from bacon -> test/unit. - test how this works in async mode - I imagine that it might mess with diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index 96165ae..08cf3f1 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -12,11 +12,29 @@ def initialize args={} EventMachine.add_shutdown_hook {@shutdown = true} end + @@filters = {} + def self.filters(filters) + @@filters = filters + end + + @@events = ['ALL'] + def self.events(events) + @@events = [*events] + end + def connection_completed Librevox.logger.info "Connected." super + send_data "auth #{@auth}\n\n" - send_data "event plain ALL\n\n" + + send_data "event plain #{@@events.join(' ')}\n\n" + + @@filters.each do |header, filters| + [*filters].each do |filter| + send_data "filter #{header} #{filter}\n\n" + end + end end def unbind From be2f771588d8553a1ceca937e7c365a3d29645af Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Mon, 26 Nov 2018 10:20:02 +0000 Subject: [PATCH 2/4] Different approach --- README.md | 8 ++++--- lib/librevox/listener/inbound.rb | 39 ++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b04de10..f60cc8d 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,13 @@ techniques: require 'librevox' class MyInbound < Librevox::Listener::Inbound - # Events to listen to (default is ALL") - events ['CHANNEL_EXECUTE', 'CUSTOM foo'] + # Events to listen for (default is ALL") + event 'CHANNEL_EXECUTE' + event 'CUSTOM foo' # Filters to apply (optional) - filters 'Caller-Context' => ['default'] + filter 'Caller-Privacy-Hide-Name', 'no' + filter 'Caller-Context', ['default', 'example'] def on_event e puts "Got event: #{e.content[:event_name]}" diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index 08cf3f1..8bc881d 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -3,6 +3,27 @@ module Librevox module Listener class Inbound < Base + class << self + + def filters + @filters ||= {} + end + + def filter(header, values) + @filters ||= {} + @filters[header] = [*values] + end + + def events + @events || ['ALL'] + end + + def event(event) + @events ||= [] + @events << event + end + end + def initialize args={} super @@ -12,27 +33,17 @@ def initialize args={} EventMachine.add_shutdown_hook {@shutdown = true} end - @@filters = {} - def self.filters(filters) - @@filters = filters - end - - @@events = ['ALL'] - def self.events(events) - @@events = [*events] - end - def connection_completed Librevox.logger.info "Connected." super send_data "auth #{@auth}\n\n" - send_data "event plain #{@@events.join(' ')}\n\n" + send_data "event plain #{self.class.events.join(' ')}\n\n" - @@filters.each do |header, filters| - [*filters].each do |filter| - send_data "filter #{header} #{filter}\n\n" + self.class.filters.each do |header, values| + values.each do |value| + send_data "filter #{header} #{value}\n\n" end end end From 363b44658cd0ada92577b2e377cbfc5678ed874d Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Mon, 26 Nov 2018 11:45:28 +0100 Subject: [PATCH 3/4] Revert to old approach --- lib/librevox/listener/inbound.rb | 28 ++++++++++---------------- spec/librevox/listener/spec_inbound.rb | 26 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/lib/librevox/listener/inbound.rb b/lib/librevox/listener/inbound.rb index 8bc881d..08f6228 100644 --- a/lib/librevox/listener/inbound.rb +++ b/lib/librevox/listener/inbound.rb @@ -4,23 +4,15 @@ module Librevox module Listener class Inbound < Base class << self + attr_reader :subscribe_events + attr_reader :subscribe_filters - def filters - @filters ||= {} + def events(events) + @subscribe_events = events end - def filter(header, values) - @filters ||= {} - @filters[header] = [*values] - end - - def events - @events || ['ALL'] - end - - def event(event) - @events ||= [] - @events << event + def filters(filters) + @subscribe_filters = filters end end @@ -39,10 +31,12 @@ def connection_completed send_data "auth #{@auth}\n\n" - send_data "event plain #{self.class.events.join(' ')}\n\n" + events = self.class.subscribe_events || ['ALL'] + send_data "event plain #{events.join(' ')}\n\n" - self.class.filters.each do |header, values| - values.each do |value| + filters = self.class.subscribe_filters || {} + filters.each do |header, values| + [*values].each do |value| send_data "filter #{header} #{value}\n\n" end end diff --git a/spec/librevox/listener/spec_inbound.rb b/spec/librevox/listener/spec_inbound.rb index 03429af..7da7540 100644 --- a/spec/librevox/listener/spec_inbound.rb +++ b/spec/librevox/listener/spec_inbound.rb @@ -6,6 +6,12 @@ class InboundTestListener < Librevox::Listener::Inbound end +class InboundFilterTestListener < Librevox::Listener::Inbound + events ['CUSTOM', 'CHANNEL_EXECUTE'] + + filters 'Caller-Context' => ['default', 'example'], 'Caller-Privacy-Hide-Name' => 'no' +end + describe "Inbound listener" do before do @listener = InboundTestListener.new(nil) @@ -18,5 +24,25 @@ class InboundTestListener < Librevox::Listener::Inbound @listener.connection_completed @listener.outgoing_data.shift.should == "auth ClueCon\n\n" @listener.outgoing_data.shift.should == "event plain ALL\n\n" + @listener.outgoing_data.shift.should == nil + end +end + +describe "Inbound listener with filtering" do + before do + @listener = InboundFilterTestListener.new(nil) + end + + behaves_like "events" + behaves_like "api commands" + + should "authorize and subscribe to events" do + @listener.connection_completed + @listener.outgoing_data.shift.should == "auth ClueCon\n\n" + @listener.outgoing_data.shift.should == "event plain CUSTOM CHANNEL_EXECUTE\n\n" + @listener.outgoing_data.shift.should == "filter Caller-Context default\n\n" + @listener.outgoing_data.shift.should == "filter Caller-Context example\n\n" + @listener.outgoing_data.shift.should == "filter Caller-Privacy-Hide-Name no\n\n" + @listener.outgoing_data.shift.should == nil end end From 73c3433d8add5c45dbeb072b9fbf901761fa08f7 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Mon, 26 Nov 2018 11:57:18 +0100 Subject: [PATCH 4/4] Update readme --- README.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f60cc8d..6f1015c 100644 --- a/README.md +++ b/README.md @@ -39,18 +39,16 @@ techniques: require 'librevox' class MyInbound < Librevox::Listener::Inbound - # Events to listen for (default is ALL") - event 'CHANNEL_EXECUTE' - event 'CUSTOM foo' + # Events to listen for (default is ALL) + events ['CHANNEL_EXECUTE', 'CUSTOM foo'] # Filters to apply (optional) - filter 'Caller-Privacy-Hide-Name', 'no' - filter 'Caller-Context', ['default', 'example'] + filter 'Caller-Context' => ['default', 'example'], 'Caller-Privacy-Hide-Name' => 'no' def on_event e puts "Got event: #{e.content[:event_name]}" end - + # You can add a hook for a certain event: event :channel_hangup do # It is instance_eval'ed, so you can use your instance methods etc: @@ -62,7 +60,7 @@ class MyInbound < Librevox::Listener::Inbound event :channel_bridge do |e| ... end - + def do_something ... end