From 953de697e10ff960b535ef02ff358cdffe97c951 Mon Sep 17 00:00:00 2001 From: chris-teague Date: Thu, 22 Nov 2012 17:15:57 +1030 Subject: [PATCH 1/4] Added xml_status switch to sofia status command - makes it easier to work with response body --- lib/fsr/cmd/sofia/status.rb | 6 ++++-- spec/fsr/cmd/sofia.rb | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/fsr/cmd/sofia/status.rb b/lib/fsr/cmd/sofia/status.rb index 22ad5bb..a857f6a 100644 --- a/lib/fsr/cmd/sofia/status.rb +++ b/lib/fsr/cmd/sofia/status.rb @@ -9,6 +9,7 @@ def initialize(fs_socket = nil, args = {}) @fs_socket = fs_socket # FSR::CommandSocket object @status = args[:status] # Status type; profile or gateway @name = args[:name] # Name of profile or gateway + @xml_status = !!args[:xml_status] # Return results in xml rather than text # If status is given, make sure it's profile or gateway unless @status.nil? raise "status must be profile or gateway" unless @status =~ /profile|gateway/i @@ -27,10 +28,11 @@ def run(api_method = :api) # This method builds the API command to send to the freeswitch event socket def raw + status_type = @xml_status ? 'xmlstatus' : 'status' if @status and @name - orig_command = "sofia status #{@status} #{@name}" + orig_command = "sofia #{status_type} #{@status} #{@name}" else - orig_command = "sofia status" + orig_command = "sofia #{status_type}" end end end diff --git a/spec/fsr/cmd/sofia.rb b/spec/fsr/cmd/sofia.rb index 2e0bdfa..9674fd0 100644 --- a/spec/fsr/cmd/sofia.rb +++ b/spec/fsr/cmd/sofia.rb @@ -28,6 +28,28 @@ status = sofia.status(:status => 'gateway', :name => 'server') status.raw.should == "sofia status gateway server" end + ## Sofia XML Status ## + it "FSR::Cmd::Sofia should allow xmlstatus" do + sofia = FSR::Cmd::Sofia.new + status = sofia.status(:xml_status => true) + status.raw.should == "sofia xmlstatus" + end + # Sofia XML Status profile internal + it "FSR::Cmd::Sofia should allow status profile internal" do + sofia = FSR::Cmd::Sofia.new + status = sofia.status(:status => 'profile', + :name => 'internal', + :xml_status => true) + status.raw.should == "sofia xmlstatus profile internal" + end + # Sofia XML Status gateway server + it "FSR::Cmd::Sofia should allow status gateway server" do + sofia = FSR::Cmd::Sofia.new + status = sofia.status(:status => 'gateway', + :name => 'server', + :xml_status => true) + status.raw.should == "sofia xmlstatus gateway server" + end ## Sofia profile ## it "FSR::Cmd::Sofia should allow profile" do From 0bfb9d61c8117f6897af59f43c58494a3aa00aac Mon Sep 17 00:00:00 2001 From: chris-teague Date: Fri, 23 Nov 2012 14:22:39 +1030 Subject: [PATCH 2/4] Adding chat functionality --- lib/fsr/cmd/chat.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/fsr/cmd/chat.rb diff --git a/lib/fsr/cmd/chat.rb b/lib/fsr/cmd/chat.rb new file mode 100644 index 0000000..66d16f6 --- /dev/null +++ b/lib/fsr/cmd/chat.rb @@ -0,0 +1,31 @@ +require "fsr/app" +module FSR + module Cmd + class Chat < Command + + attr_reader :fs_socket + + def initialize(fs_socket = nil, args = {}) + @fs_socket = fs_socket # FSR::CommandSocket obj + @protocol = args[:protocol] ? args[:protocol] : 'sip' + @from = '1001' + @to = '1003@192.168.0.6' + @message = 'Oh, hello!' + end + + # Send the command to the event socket, using api by default. + def run(api_method = :api) + orig_command = "%s %s" % [api_method, raw] + Log.debug "saying #{orig_command}" + @fs_socket.say(orig_command) + end + + # This method builds the API command to send to the freeswitch event socket + def raw + %Q(chat #{@protocol}|#{@from}|#{@to}|#{@message}) + end + end + + register(:chat, Chat) + end +end \ No newline at end of file From e88a5ad08e627f3bed1622099b57be279707c5cb Mon Sep 17 00:00:00 2001 From: chris-teague Date: Fri, 23 Nov 2012 15:16:15 +1030 Subject: [PATCH 3/4] Added some chat command test coverage. --- spec/fsr/cmd/chat.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 spec/fsr/cmd/chat.rb diff --git a/spec/fsr/cmd/chat.rb b/spec/fsr/cmd/chat.rb new file mode 100644 index 0000000..1f9144a --- /dev/null +++ b/spec/fsr/cmd/chat.rb @@ -0,0 +1,58 @@ +require 'spec/helper' +require "fsr/cmd" +FSR::Cmd.load_command("chat") + +describe "Testing FSR::Cmd::Chat" do + # Invalid originates + it "Must be passed an argument hash" do + lambda { FSR::Cmd::Chat.new(nil, :endpoint) }.should.raise(ArgumentError). + message.should.match(/args \(Passed: <<<.*?>>>\) must be a hash/) + end + + it "Can not send a chat without a message" do + lambda { FSR::Cmd::Chat.new(nil, :to => '1000', :from => '1001') }.should.raise(ArgumentError). + message.should.match(/Cannot send chat without :message set/) + end + + it "Can not send a chat without to set" do + lambda { FSR::Cmd::Chat.new(nil, :message => 'Hello', :from => '1001') }.should.raise(ArgumentError). + message.should.match(/Cannot send chat without :to set/) + end + + it "Can not send a chat without from set" do + lambda { FSR::Cmd::Chat.new(nil, :message => 'Hello', :to => '1000') }.should.raise(ArgumentError). + message.should.match(/Cannot send chat without :from set/) + end + + it "Can not send a chat with invalid protocol" do + lambda { FSR::Cmd::Chat.new(nil, :message => 'Hello', :to => '1000', :from => '1001', :protocol => '') }.should.raise(ArgumentError). + message.should.match(/Cannot send chat with invalid protocol/) + end + + it "Creates a valid chat message with sensible default protocol" do + chat = FSR::Cmd::Chat.new(nil, :message => 'Hello', :to => '1000', :from => '1001') + chat.raw.should == "chat sip|1001|1000|Hello" + end + + # Different options choices + it "Honours different protocols" do + chat = FSR::Cmd::Chat.new(nil, :message => 'Hello', :to => '1000', :from => '1001', :protocol => 'jingle') + chat.raw.should == "chat jingle|1001|1000|Hello" + end + + it "Honours a more realistic to" do + chat = FSR::Cmd::Chat.new(nil, :message => 'Hello', :to => '1000@192.168.1.1', :from => '1001') + chat.raw.should == "chat sip|1001|1000@192.168.1.1|Hello" + end + + it "Honours a more complex message" do + chat = FSR::Cmd::Chat.new(nil, :message => 'Hello, friendly sir. fƒø£0"f', :to => '1000@192.168.1.1', :from => '1001') + chat.raw.should == %Q(chat sip|1001|1000@192.168.1.1|Hello, friendly sir. fƒø£0"f) + end + + it "Escapes pipes from messages" do + chat = FSR::Cmd::Chat.new(nil, :message => 'He||o there |ove|y', :to => '1000@192.168.1.1', :from => '1001') + chat.raw.should == "chat sip|1001|1000@192.168.1.1|He\|\|o there \|ove\|y" + end + +end \ No newline at end of file From d3f6910dd7e6c3a71b25a79482cdf1e259933261 Mon Sep 17 00:00:00 2001 From: chris-teague Date: Fri, 23 Nov 2012 15:33:35 +1030 Subject: [PATCH 4/4] Added test coverage to chat command --- lib/fsr/cmd/chat.rb | 16 ++++++++++++---- spec/fsr/cmd/chat.rb | 6 +++--- spec/fsr/loading.rb | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/fsr/cmd/chat.rb b/lib/fsr/cmd/chat.rb index 66d16f6..f54187d 100644 --- a/lib/fsr/cmd/chat.rb +++ b/lib/fsr/cmd/chat.rb @@ -6,11 +6,19 @@ class Chat < Command attr_reader :fs_socket def initialize(fs_socket = nil, args = {}) + + raise(ArgumentError, "args (Passed: <<<#{args}>>>) must be a hash") unless args.kind_of?(Hash) + @fs_socket = fs_socket # FSR::CommandSocket obj @protocol = args[:protocol] ? args[:protocol] : 'sip' - @from = '1001' - @to = '1003@192.168.0.6' - @message = 'Oh, hello!' + raise(ArgumentError, "Cannot send chat with invalid protocol") unless @protocol.to_s.size > 0 + @from = args[:from] # i.e. 1000@192.168.1.1 + raise(ArgumentError, "Cannot send chat without :from set") unless @from.to_s.size > 0 + @to = args[:to] # i.e. 1001@192.168.1.1 + raise(ArgumentError, "Cannot send chat without :to set") unless @to.to_s.size > 0 + @message = args[:message] + raise(ArgumentError, "Cannot send chat without :message set") unless @message.to_s.size > 0 + @message.gsub!('|', '\|') end # Send the command to the event socket, using api by default. @@ -26,6 +34,6 @@ def raw end end - register(:chat, Chat) + register(:chat, Chat) end end \ No newline at end of file diff --git a/spec/fsr/cmd/chat.rb b/spec/fsr/cmd/chat.rb index 1f9144a..dbef89f 100644 --- a/spec/fsr/cmd/chat.rb +++ b/spec/fsr/cmd/chat.rb @@ -46,13 +46,13 @@ end it "Honours a more complex message" do - chat = FSR::Cmd::Chat.new(nil, :message => 'Hello, friendly sir. fƒø£0"f', :to => '1000@192.168.1.1', :from => '1001') - chat.raw.should == %Q(chat sip|1001|1000@192.168.1.1|Hello, friendly sir. fƒø£0"f) + chat = FSR::Cmd::Chat.new(nil, :message => 'Hello, friendly sir. "*/;{(\|/;,@#,/', :to => '1000@192.168.1.1', :from => '1001') + chat.raw.should == 'chat sip|1001|1000@192.168.1.1|Hello, friendly sir. "*/;{(\\\|/;,@#,/' end it "Escapes pipes from messages" do chat = FSR::Cmd::Chat.new(nil, :message => 'He||o there |ove|y', :to => '1000@192.168.1.1', :from => '1001') - chat.raw.should == "chat sip|1001|1000@192.168.1.1|He\|\|o there \|ove\|y" + chat.raw.should == %Q(chat sip|1001|1000@192.168.1.1|He\\|\\|o there \\|ove\\|y) end end \ No newline at end of file diff --git a/spec/fsr/loading.rb b/spec/fsr/loading.rb index b63f9c1..941ebad 100644 --- a/spec/fsr/loading.rb +++ b/spec/fsr/loading.rb @@ -16,7 +16,7 @@ # When you add commands you must modify the expected cmds_loaded behavior it "Loads all commands" do - all_commands = [:kill, :uuid_dump, :originate, :sofia, :fsctl, :sofia_contact, :status, :calls, :call_center, :channels, :enum, :sched_hangup, :sched_transfer, :uuid_transfer, :uuid_send_dtmf, :conference, :valet_info] # If you add a command add it to this set + all_commands = [:kill, :uuid_dump, :originate, :chat, :sofia, :fsctl, :sofia_contact, :status, :calls, :call_center, :channels, :enum, :sched_hangup, :sched_transfer, :uuid_transfer, :uuid_send_dtmf, :conference, :valet_info] # If you add a command add it to this set cmds_loaded = FSR.load_all_commands cmds_loaded.kind_of?(Array).should == true all_commands.each do |cmd|