forked from bougyman/freeswitcher
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from chris-teague/chat_command
Added support for FreeSwitch Chat command
- Loading branch information
Showing
5 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "fsr/app" | ||
module FSR | ||
module Cmd | ||
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' | ||
raise(ArgumentError, "Cannot send chat with invalid protocol") unless @protocol.to_s.size > 0 | ||
@from = args[:from] # i.e. [email protected] | ||
raise(ArgumentError, "Cannot send chat without :from set") unless @from.to_s.size > 0 | ||
@to = args[:to] # i.e. [email protected] | ||
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. | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 => '[email protected]', :from => '1001') | ||
chat.raw.should == "chat sip|1001|[email protected]|Hello" | ||
end | ||
|
||
it "Honours a more complex message" do | ||
chat = FSR::Cmd::Chat.new(nil, :message => 'Hello, friendly sir. "*/;{(\|/;,@#,/', :to => '[email protected]', :from => '1001') | ||
chat.raw.should == 'chat sip|1001|[email protected]|Hello, friendly sir. "*/;{(\\\|/;,@#,/' | ||
end | ||
|
||
it "Escapes pipes from messages" do | ||
chat = FSR::Cmd::Chat.new(nil, :message => 'He||o there |ove|y', :to => '[email protected]', :from => '1001') | ||
chat.raw.should == %Q(chat sip|1001|[email protected]|He\\|\\|o there \\|ove\\|y) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters