eric-adhearsion 0.7.999 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/CHANGELOG +8 -2
  2. data/EVENTS +11 -0
  3. data/Rakefile +96 -24
  4. data/adhearsion.gemspec +148 -0
  5. data/app_generators/ahn/ahn_generator.rb +24 -9
  6. data/app_generators/ahn/templates/.ahnrc +25 -3
  7. data/app_generators/ahn/templates/Rakefile +22 -2
  8. data/app_generators/ahn/templates/components/ami_remote/ami_remote.rb +15 -0
  9. data/app_generators/ahn/templates/components/disabled/HOW_TO_ENABLE +7 -0
  10. data/app_generators/ahn/templates/components/disabled/stomp_gateway/README.markdown +47 -0
  11. data/app_generators/ahn/templates/components/disabled/stomp_gateway/config.yml +12 -0
  12. data/app_generators/ahn/templates/components/disabled/stomp_gateway/stomp_gateway.rb +34 -0
  13. data/app_generators/ahn/templates/components/simon_game/{lib/simon_game.rb → simon_game.rb} +14 -19
  14. data/app_generators/ahn/templates/config/startup.rb +3 -6
  15. data/app_generators/ahn/templates/dialplan.rb +2 -3
  16. data/app_generators/ahn/templates/events.rb +32 -0
  17. data/bin/jahn +10 -0
  18. data/examples/asterisk_manager_interface/standalone.rb +51 -0
  19. data/lib/adhearsion.rb +17 -11
  20. data/lib/adhearsion/cli.rb +141 -24
  21. data/lib/adhearsion/component_manager.rb +169 -238
  22. data/lib/adhearsion/component_manager/component_tester.rb +55 -0
  23. data/lib/adhearsion/component_manager/spec_framework.rb +24 -0
  24. data/lib/adhearsion/events_support.rb +84 -0
  25. data/lib/adhearsion/{core_extensions → foundation}/all.rb +0 -0
  26. data/lib/adhearsion/{blank_slate.rb → foundation/blank_slate.rb} +0 -0
  27. data/lib/adhearsion/{core_extensions → foundation}/custom_daemonizer.rb +0 -0
  28. data/lib/adhearsion/foundation/event_socket.rb +203 -0
  29. data/lib/adhearsion/foundation/future_resource.rb +36 -0
  30. data/lib/adhearsion/{core_extensions → foundation}/global.rb +0 -0
  31. data/lib/adhearsion/{core_extensions → foundation}/metaprogramming.rb +0 -0
  32. data/lib/adhearsion/foundation/numeric.rb +13 -0
  33. data/lib/adhearsion/foundation/pseudo_guid.rb +10 -0
  34. data/lib/adhearsion/{core_extensions → foundation}/relationship_properties.rb +2 -0
  35. data/lib/adhearsion/foundation/string.rb +26 -0
  36. data/lib/adhearsion/foundation/synchronized_hash.rb +96 -0
  37. data/lib/adhearsion/{core_extensions → foundation}/thread_safety.rb +0 -0
  38. data/lib/adhearsion/host_definitions.rb +5 -1
  39. data/lib/adhearsion/initializer.rb +229 -73
  40. data/lib/adhearsion/initializer/asterisk.rb +33 -11
  41. data/lib/adhearsion/initializer/configuration.rb +58 -6
  42. data/lib/adhearsion/initializer/database.rb +3 -46
  43. data/lib/adhearsion/initializer/drb.rb +9 -3
  44. data/lib/adhearsion/initializer/freeswitch.rb +3 -3
  45. data/lib/adhearsion/initializer/rails.rb +1 -1
  46. data/lib/adhearsion/tasks.rb +2 -1
  47. data/lib/adhearsion/tasks/deprecations.rb +59 -0
  48. data/lib/adhearsion/version.rb +3 -3
  49. data/lib/adhearsion/voip/asterisk.rb +2 -2
  50. data/lib/adhearsion/voip/asterisk/agi_server.rb +9 -6
  51. data/lib/adhearsion/voip/asterisk/commands.rb +106 -4
  52. data/lib/adhearsion/voip/asterisk/manager_interface.rb +562 -0
  53. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rb +1754 -0
  54. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rl.rb +286 -0
  55. data/lib/adhearsion/voip/asterisk/manager_interface/ami_messages.rb +78 -0
  56. data/lib/adhearsion/voip/asterisk/manager_interface/ami_protocol_lexer_machine.rl +87 -0
  57. data/lib/adhearsion/voip/asterisk/super_manager.rb +19 -0
  58. data/lib/adhearsion/voip/call.rb +51 -2
  59. data/lib/adhearsion/voip/dial_plan.rb +74 -61
  60. data/lib/adhearsion/voip/dsl/dialing_dsl.rb +1 -1
  61. data/lib/adhearsion/voip/dsl/dialplan/parser.rb +2 -6
  62. data/lib/adhearsion/voip/dsl/numerical_string.rb +2 -2
  63. data/lib/adhearsion/voip/freeswitch/oes_server.rb +2 -2
  64. data/lib/theatre.rb +151 -0
  65. data/lib/theatre/README.markdown +64 -0
  66. data/lib/theatre/callback_definition_loader.rb +84 -0
  67. data/lib/theatre/guid.rb +23 -0
  68. data/lib/theatre/invocation.rb +121 -0
  69. data/lib/theatre/namespace_manager.rb +153 -0
  70. data/lib/theatre/version.rb +2 -0
  71. metadata +63 -138
  72. data/Manifest.txt +0 -149
  73. data/README.txt +0 -6
  74. data/ahn_generators/component/USAGE +0 -5
  75. data/ahn_generators/component/component_generator.rb +0 -57
  76. data/ahn_generators/component/templates/configuration.rb +0 -0
  77. data/ahn_generators/component/templates/lib/lib.rb.erb +0 -3
  78. data/ahn_generators/component/templates/test/test.rb.erb +0 -12
  79. data/ahn_generators/component/templates/test/test_helper.rb +0 -14
  80. data/app_generators/ahn/templates/components/simon_game/configuration.rb +0 -0
  81. data/app_generators/ahn/templates/components/simon_game/test/test_helper.rb +0 -14
  82. data/app_generators/ahn/templates/components/simon_game/test/test_simon_game.rb +0 -31
  83. data/lib/adhearsion/core_extensions/array.rb +0 -0
  84. data/lib/adhearsion/core_extensions/guid.rb +0 -5
  85. data/lib/adhearsion/core_extensions/hash.rb +0 -0
  86. data/lib/adhearsion/core_extensions/numeric.rb +0 -4
  87. data/lib/adhearsion/core_extensions/proc.rb +0 -0
  88. data/lib/adhearsion/core_extensions/pseudo_uuid.rb +0 -11
  89. data/lib/adhearsion/core_extensions/publishable.rb +0 -73
  90. data/lib/adhearsion/core_extensions/string.rb +0 -26
  91. data/lib/adhearsion/core_extensions/thread.rb +0 -13
  92. data/lib/adhearsion/core_extensions/time.rb +0 -0
  93. data/lib/adhearsion/distributed/gateways/dbus_gateway.rb +0 -0
  94. data/lib/adhearsion/distributed/gateways/osa_gateway.rb +0 -0
  95. data/lib/adhearsion/distributed/gateways/rest_gateway.rb +0 -9
  96. data/lib/adhearsion/distributed/gateways/soap_gateway.rb +0 -9
  97. data/lib/adhearsion/distributed/gateways/xmlrpc_gateway.rb +0 -9
  98. data/lib/adhearsion/distributed/peer_finder.rb +0 -0
  99. data/lib/adhearsion/distributed/remote_cli.rb +0 -0
  100. data/lib/adhearsion/hooks.rb +0 -57
  101. data/lib/adhearsion/initializer/paths.rb +0 -55
  102. data/lib/adhearsion/services/scheduler.rb +0 -5
  103. data/lib/adhearsion/voip/asterisk/ami.rb +0 -147
  104. data/lib/adhearsion/voip/asterisk/ami/actions.rb +0 -238
  105. data/lib/adhearsion/voip/asterisk/ami/machine.rb +0 -871
  106. data/lib/adhearsion/voip/asterisk/ami/machine.rl +0 -109
  107. data/lib/adhearsion/voip/asterisk/ami/parser.rb +0 -262
  108. data/script/destroy +0 -14
  109. data/script/generate +0 -14
  110. data/spec/fixtures/dialplan.rb +0 -3
  111. data/spec/initializer/test_configuration.rb +0 -267
  112. data/spec/initializer/test_loading.rb +0 -162
  113. data/spec/initializer/test_paths.rb +0 -43
  114. data/spec/silence.rb +0 -10
  115. data/spec/test_ahn_command.rb +0 -149
  116. data/spec/test_code_quality.rb +0 -87
  117. data/spec/test_component_manager.rb +0 -97
  118. data/spec/test_constants.rb +0 -8
  119. data/spec/test_drb.rb +0 -104
  120. data/spec/test_helper.rb +0 -94
  121. data/spec/test_hooks.rb +0 -37
  122. data/spec/test_host_definitions.rb +0 -79
  123. data/spec/test_initialization.rb +0 -105
  124. data/spec/test_logging.rb +0 -80
  125. data/spec/test_relationship_properties.rb +0 -54
  126. data/spec/voip/asterisk/ami_response_definitions.rb +0 -23
  127. data/spec/voip/asterisk/config_file_generators/test_agents.rb +0 -253
  128. data/spec/voip/asterisk/config_file_generators/test_queues.rb +0 -325
  129. data/spec/voip/asterisk/config_file_generators/test_voicemail.rb +0 -306
  130. data/spec/voip/asterisk/menu_command/test_calculated_match.rb +0 -111
  131. data/spec/voip/asterisk/menu_command/test_matchers.rb +0 -98
  132. data/spec/voip/asterisk/mock_ami_server.rb +0 -176
  133. data/spec/voip/asterisk/test_agi_server.rb +0 -451
  134. data/spec/voip/asterisk/test_ami.rb +0 -227
  135. data/spec/voip/asterisk/test_commands.rb +0 -2006
  136. data/spec/voip/asterisk/test_config_manager.rb +0 -129
  137. data/spec/voip/dsl/dispatcher_spec_helper.rb +0 -45
  138. data/spec/voip/dsl/test_dialing_dsl.rb +0 -268
  139. data/spec/voip/dsl/test_dispatcher.rb +0 -82
  140. data/spec/voip/dsl/test_parser.rb +0 -87
  141. data/spec/voip/freeswitch/test_basic_connection_manager.rb +0 -39
  142. data/spec/voip/freeswitch/test_inbound_connection_manager.rb +0 -39
  143. data/spec/voip/freeswitch/test_oes_server.rb +0 -9
  144. data/spec/voip/test_call_routing.rb +0 -127
  145. data/spec/voip/test_dialplan_manager.rb +0 -372
  146. data/spec/voip/test_numerical_string.rb +0 -48
  147. data/spec/voip/test_phone_number.rb +0 -36
  148. data/test/test_ahn_generator.rb +0 -59
  149. data/test/test_component_generator.rb +0 -52
  150. data/test/test_generator_helper.rb +0 -20
@@ -24,8 +24,11 @@ module Adhearsion
24
24
 
25
25
  class << self
26
26
  def configure(&block)
27
- Adhearsion.module_eval { remove_const(:AHN_CONFIG) } if Adhearsion.const_defined?(:AHN_CONFIG)
28
- Adhearsion.const_set(:AHN_CONFIG, new(&block))
27
+ if Adhearsion.const_defined?(:AHN_CONFIG)
28
+ yield AHN_CONFIG if block_given?
29
+ else
30
+ Adhearsion.const_set(:AHN_CONFIG, new(&block))
31
+ end
29
32
  end
30
33
  end
31
34
 
@@ -33,10 +36,6 @@ module Adhearsion
33
36
  attr_accessor :end_call_on_hangup
34
37
  attr_accessor :end_call_on_error
35
38
 
36
- def logging(options)
37
- Adhearsion::Logging.logging_level = options[:level]
38
- end
39
-
40
39
  def initialize
41
40
  @automatically_answer_incoming_calls = true
42
41
  @end_call_on_hangup = true
@@ -44,6 +43,59 @@ module Adhearsion
44
43
  yield self if block_given?
45
44
  end
46
45
 
46
+ def ahnrc
47
+ @ahnrc
48
+ end
49
+
50
+ ##
51
+ # Load the contents of an .ahnrc file into this Configuration.
52
+ #
53
+ # @param [String, Hash] ahnrc String of YAML .ahnrc data or a Hash of the pre-loaded YAML data structure
54
+ #
55
+ def ahnrc=(new_ahnrc)
56
+ case new_ahnrc
57
+ when Hash
58
+ @raw_ahnrc = new_ahnrc.to_yaml.freeze
59
+ @ahnrc = new_ahnrc.clone.freeze
60
+ when String
61
+ @raw_ahnrc = new_ahnrc.clone.freeze
62
+ @ahnrc = YAML.load(new_ahnrc).freeze
63
+ end
64
+ end
65
+
66
+ def logging(options)
67
+ Adhearsion::Logging.logging_level = options[:level]
68
+ end
69
+
70
+ ##
71
+ # Adhearsion's .ahnrc file is used to define paths to certain parts of the framework. For example, the name dialplan.rb
72
+ # is actually specified in .ahnrc. This file can actually be just a filename, a filename with a glob (.e.g "*.rb"), an
73
+ # Array of filenames or even an Array of globs.
74
+ #
75
+ # @param [String,Array] String segments which convey the nesting of Hash keys through .ahnrc
76
+ # @raise [RuntimeError] If ahnrc has not been set yet with #ahnrc=()
77
+ # @raise [NameError] If the path through the ahnrc is invalid
78
+ #
79
+ def files_from_setting(*path_through_config)
80
+ raise RuntimeError, "No ahnrc has been set yet!" unless @ahnrc
81
+ queried_nested_setting = path_through_config.flatten.inject(@ahnrc) do |hash,key_name|
82
+ if hash.kind_of?(Hash) && hash.has_key?(key_name)
83
+ hash[key_name]
84
+ else
85
+ raise NameError, "Paths #{path_through_config.inspect} not found in .ahnrc!"
86
+ end
87
+ end
88
+ raise NameError, "Paths #{path_through_config.inspect} not found in .ahnrc!" unless queried_nested_setting
89
+ queried_nested_setting = Array queried_nested_setting
90
+ queried_nested_setting.map { |filename| files_from_glob(filename) }.flatten.uniq
91
+ end
92
+
93
+ private
94
+
95
+ def files_from_glob(glob)
96
+ Dir.glob "#{AHN_ROOT}/#{glob}"
97
+ end
98
+
47
99
  class AbstractConfiguration
48
100
  extend ConfigurationEntryPoint
49
101
 
@@ -23,7 +23,7 @@ module Adhearsion
23
23
  private
24
24
 
25
25
  def create_call_hook_for_connection_cleanup
26
- Hooks::BeforeCall.create_hook do
26
+ Events.register_callback([:asterisk, :before_call]) do
27
27
  ActiveRecord::Base.verify_active_connections!
28
28
  end
29
29
  end
@@ -33,7 +33,7 @@ module Adhearsion
33
33
  end
34
34
 
35
35
  def require_models
36
- all_models.each do |model|
36
+ AHN_CONFIG.files_from_setting("paths", "models").each do |model|
37
37
  load model
38
38
  end
39
39
  end
@@ -46,47 +46,4 @@ module Adhearsion
46
46
  end
47
47
 
48
48
  end
49
- end
50
-
51
- =begin
52
- db_config = Adhearsion::Configuration.core.database
53
- if db_config
54
- unless Adhearsion::Paths.manager_for? "models"
55
- raise "No paths specified for the database 'models' in .ahnrc! Aborting."
56
- end
57
- require 'active_record'
58
-
59
- ActiveRecord::Base.verification_timeout = 14400
60
- ActiveRecord::Base.logger = Logger.new("log/database.log")
61
- ActiveRecord::Base.establish_connection db_config
62
-
63
- all_models.each { |model| require model }
64
-
65
- # Below is a monkey patch for keeping ActiveRecord connections alive.
66
- # http://www.sparecycles.org/2007/7/2/saying-goodbye-to-lost-connections-in-rails
67
-
68
- module ActiveRecord
69
- module ConnectionAdapters
70
- class MysqlAdapter
71
- def execute(sql, name = nil) #:nodoc:
72
- reconnect_lost_connections = true
73
- begin
74
- log(sql, name) { @connection.query(sql) }
75
- rescue ActiveRecord::StatementInvalid => exception
76
- if reconnect_lost_connections and exception.message =~ /(Lost connection to MySQL server during query|MySQL server has gone away)/
77
- reconnect_lost_connections = false
78
- reconnect!
79
- retry
80
- elsif exception.message.split(":").first =~ /Packets out of order/
81
- raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
82
- else
83
- raise
84
- end
85
- end
86
- end
87
- end
88
- end
89
- end
90
-
91
- end
92
- =end
49
+ end
@@ -8,17 +8,23 @@ module Adhearsion
8
8
  class DrbInitializer
9
9
 
10
10
  class << self
11
-
11
+
12
12
  def start
13
13
  config = Adhearsion::AHN_CONFIG.drb
14
14
  DRb.install_acl ACL.new(config.acl) if config.acl
15
- DRb.start_service "druby://#{config.host}:#{config.port}", Adhearsion::DrbDoor.instance
15
+
16
+ drb_door = Object.new
17
+ Components.component_manager.extend_object_with(drb_door, :rpc)
18
+
19
+ DRb.start_service "druby://#{config.host}:#{config.port}", drb_door
20
+
16
21
  ahn_log "Starting DRb on #{config.host}:#{config.port}"
17
22
  end
18
-
23
+
19
24
  def stop
20
25
  DRb.stop_service
21
26
  end
27
+
22
28
  end
23
29
  end
24
30
  end
@@ -15,8 +15,8 @@ if oes_enabled
15
15
 
16
16
  server = Adhearsion::VoIP::FreeSwitch::OesServer.new port, host
17
17
 
18
- Adhearsion::Hooks::AfterInitialized.create_hook { server.start }
19
- Adhearsion::Hooks::ThreadsJoinedAfterInitialized.create_hook { server.join }
20
- Adhearsion::Hooks::TearDown.create_hook { server.stop }
18
+ Events.register_callback(:after_initialized) { server.start }
19
+ Events.register_callback(:shutdown) { server.stop }
20
+ IMPORTANT_THREADS << server
21
21
 
22
22
  end
@@ -18,7 +18,7 @@ module Adhearsion
18
18
  load_rails
19
19
  if defined? ActiveRecord
20
20
  ActiveRecord::Base.allow_concurrency = true
21
- Hooks::BeforeCall.create_hook do
21
+ Events.register_callback([:asterisk, :before_call]) do
22
22
  ActiveRecord::Base.verify_active_connections!
23
23
  end
24
24
  end
@@ -4,6 +4,7 @@ require 'adhearsion/tasks/database'
4
4
  require 'adhearsion/tasks/testing'
5
5
  require 'adhearsion/tasks/generating'
6
6
  require 'adhearsion/tasks/lint'
7
+ require 'adhearsion/tasks/deprecations'
7
8
 
8
9
  namespace :adhearsion do
9
10
  desc "Dump useful information about this application's adhearsion environment"
@@ -12,4 +13,4 @@ namespace :adhearsion do
12
13
  end
13
14
  end
14
15
 
15
- task :default => "adhearsion:about"
16
+ task :default => "adhearsion:about"
@@ -0,0 +1,59 @@
1
+ namespace :deprecations do
2
+ desc <<-DESC
3
+ Older versions of Adhearsion had an .ahnrc "paths" section similar to the following...
4
+
5
+ paths:
6
+ models:
7
+ directory: models
8
+ pattern: *.rb
9
+
10
+ This has been deprecated. The new format is this:
11
+
12
+ paths:
13
+ models: {models,gui/app/models}/*.rb
14
+
15
+ This Rake task will fix your .ahnrc if you have
16
+ DESC
17
+ task :fix_ahnrc_path_format do
18
+ puts "\nThis will remove all comments from your .ahnrc file. A backup will be created as .ahnrc.backup."
19
+ puts "If you wish to do this manually to preserve your comments, simply overwrite .ahnrc with .ahnrc.backup"
20
+ puts "and apply the change manually."
21
+ puts
22
+
23
+ require 'fileutils'
24
+ require 'yaml'
25
+
26
+ ahnrc_file = File.expand_path(".ahnrc")
27
+
28
+ FileUtils.cp ahnrc_file, ahnrc_file + ".backup"
29
+ ahnrc_contents = YAML.load_file ahnrc_file
30
+
31
+ abort '.ahnrc does not have a "paths" section!' unless ahnrc_contents.has_key? "paths"
32
+
33
+ paths = ahnrc_contents["paths"]
34
+ paths.clone.each_pair do |key,value|
35
+ if value.kind_of?(Hash)
36
+ if value.has_key?("directory") || value.has_key?("pattern")
37
+ directory, pattern = value.values_at "directory", "pattern"
38
+ new_path = "#{directory}/#{pattern}"
39
+
40
+ puts "!!! CHANGING KEY #{key.inspect}!"
41
+ puts "!!! NEW: #{new_path.inspect}"
42
+ puts "!!! OLD:\n#{{key => value}.to_yaml.sub("---", "")}\n\n"
43
+
44
+ paths[key] = new_path
45
+ end
46
+ end
47
+ end
48
+
49
+ ahnrc_contents["paths"] = paths
50
+ new_yaml = ahnrc_contents.to_yaml.gsub("--- \n", "")
51
+
52
+ puts "New .ahnrc file:\n" + ("#" * 25) + "\n"
53
+ puts new_yaml
54
+ puts '#' * 25
55
+
56
+ File.open(ahnrc_file, "w") { |file| file.puts new_yaml }
57
+ puts "Wrote to .ahnrc. Done!"
58
+ end
59
+ end
@@ -1,8 +1,8 @@
1
1
  module Adhearsion #:nodoc:
2
2
  module VERSION #:nodoc:
3
- MAJOR = 0 unless defined? MAJOR
4
- MINOR = 7 unless defined? MINOR
5
- TINY = 999 unless defined? TINY
3
+ MAJOR = 0 unless defined? MAJOR
4
+ MINOR = 8 unless defined? MINOR
5
+ TINY = 0 unless defined? TINY
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.') unless defined? STRING
8
8
  end
@@ -1,4 +1,4 @@
1
1
  require File.dirname(__FILE__) + "/dsl/numerical_string"
2
2
  require File.dirname(__FILE__) + "/asterisk/agi_server"
3
- require File.dirname(__FILE__) + "/asterisk/ami"
4
- require File.dirname(__FILE__) + "/asterisk/commands"
3
+ require File.dirname(__FILE__) + "/asterisk/manager_interface"
4
+ require File.dirname(__FILE__) + "/asterisk/commands"
@@ -12,8 +12,8 @@ module Adhearsion
12
12
  end
13
13
 
14
14
  def serve(io)
15
- Hooks::BeforeCall.trigger_hooks
16
- call = Adhearsion.receive_call_from(io)
15
+ call = Adhearsion.receive_call_from(io)
16
+ Events.trigger_immediately([:asterisk, :before_call], call)
17
17
  ahn_log.agi "Handling call with variables #{call.variables.inspect}"
18
18
 
19
19
  return DialPlan::ConfirmationManager.handle(call) if DialPlan::ConfirmationManager.confirmation_call?(call)
@@ -26,16 +26,16 @@ module Adhearsion
26
26
  call.hangup!
27
27
  rescue FailedExtensionCallException => failed_call
28
28
  begin
29
- ahn_log.agi "Received \"failed\" meta-call with :failed_reason => #{failed_call.call.failed_reason.inspect}. Executing OnFailedCall hooks."
30
- Hooks::OnFailedCall.trigger_hooks(failed_call.call)
29
+ ahn_log.agi "Received \"failed\" meta-call with :failed_reason => #{failed_call.call.failed_reason.inspect}. Executing Executing /asterisk/failed_call event callbacks."
30
+ Events.trigger [:asterisk, :failed_call], failed_call.call
31
31
  call.hangup!
32
32
  rescue => e
33
33
  ahn_log.agi.error e
34
34
  end
35
35
  rescue HungupExtensionCallException => hungup_call
36
36
  begin
37
- ahn_log.agi "Received \"h\" meta-call. Executing OnHungupCall hooks."
38
- Hooks::OnHungupCall.trigger_hooks(hungup_call.call)
37
+ ahn_log.agi "Received \"h\" meta-call. Executing /asterisk/call_hangup event callbacks."
38
+ Events.trigger [:asterisk, :call_hangup], hungup_call.call
39
39
  call.hangup!
40
40
  rescue => e
41
41
  ahn_log.agi.error e
@@ -47,7 +47,10 @@ module Adhearsion
47
47
  rescue => e
48
48
  ahn_log.agi.error e.inspect
49
49
  ahn_log.agi.error e.backtrace.map { |s| " " * 5 + s }.join("\n")
50
+ ensure
51
+ Adhearsion.remove_inactive_call call rescue nil
50
52
  end
53
+
51
54
  end
52
55
 
53
56
  DEFAULT_OPTIONS = { :server_class => RubyServer, :port => 4573, :host => "0.0.0.0" } unless defined? DEFAULT_OPTIONS
@@ -7,6 +7,22 @@ module Adhearsion
7
7
  module Commands
8
8
 
9
9
  RESPONSE_PREFIX = "200 result=" unless defined? RESPONSE_PREFIX
10
+
11
+ # These are the status messages that asterisk will issue after a dial command is executed.
12
+ # More information here: http://www.voip-info.org/wiki/index.php?page=Asterisk+variable+DIALSTATUS
13
+ # Here is a current list of dial status messages which are not all necessarily supported by adhearsion:
14
+ #
15
+ # ANSWER: Call is answered. A successful dial. The caller reached the callee.
16
+ # BUSY: Busy signal. The dial command reached its number but the number is busy.
17
+ # NOANSWER: No answer. The dial command reached its number, the number rang for too long, then the dial timed out.
18
+ # CANCEL: Call is cancelled. The dial command reached its number but the caller hung up before the callee picked up.
19
+ # CONGESTION: Congestion. This status is usually a sign that the dialled number is not recognised.
20
+ # CHANUNAVAIL: Channel unavailable. On SIP, peer may not be registered.
21
+ # DONTCALL: Privacy mode, callee rejected the call
22
+ # TORTURE: Privacy mode, callee chose to send caller to torture menu
23
+ # INVALIDARGS: Error parsing Dial command arguments (added for Asterisk 1.4.1, SVN r53135-53136)
24
+ #
25
+ #
10
26
  DIAL_STATUSES = Hash.new(:unknown).merge(:answer => :answered,
11
27
  :congestion => :congested,
12
28
  :busy => :busy,
@@ -36,17 +52,47 @@ module Adhearsion
36
52
  end
37
53
  end
38
54
 
55
+ # This method is the underlying method executed by nearly all the command methods in this module.
56
+ # It is used to send the plaintext commands in the proper AGI format over TCP/IP back to an Asterisk server via the
57
+ # FAGI protocol.
58
+ # It is not recommended that you call this method directly unless you plan to write a new command method
59
+ # in which case use this method you to communicate directly with an Asterisk server via the FAGI protocol.
60
+ # For more information about FAGI visit: http://www.voip-info.org/wiki/view/Asterisk+FastAGI
39
61
  def raw_response(message = nil)
40
62
  ahn_log.agi.debug ">>> #{message}"
41
63
  write message if message
42
64
  read
43
65
  end
44
66
 
67
+ # The answer command must be called first before any other commands can be issued.
68
+ # In typical adhearsion applications the answer command is called by default as soon
69
+ # as a call is transfered to a valid context in dialplan.rb.
70
+ # If you do not want your adhearsion application to automatically issue an answer command,
71
+ # then you must edit your startup.rb file and configure this setting.
72
+ # Keep in mind that you should not need to issue another answer command after
73
+ # an answer command has already been issued either explicitly by your code or implicitly
74
+ # by the standard adhearsion configuration.
45
75
  def answer
46
76
  raw_response "ANSWER"
47
77
  true
48
78
  end
49
79
 
80
+ # This asterisk dialplan command allows you to instruct Asterisk to start applications
81
+ # which are typically run from extensions.conf. For a complete list of these commands
82
+ # please visit: http://www.voip-info.org/wiki/view/Asterisk+-+documentation+of+application+commands
83
+ #
84
+ # The most common commands are already made available through the FAGI interface provided
85
+ # by this code base. For commands that do not fall into this category, then exec is what you
86
+ # should use.
87
+ #
88
+ # For example, if there are specific asterisk modules you have loaded that will not
89
+ # available through the standard commands provided through FAGI - then you can used EXEC.
90
+ #
91
+ # Example:
92
+ # execute 'SIPAddHeader', '"Call-Info: answer-after=0"
93
+ #
94
+ # Using execute in this way will add a header to an existing SIP call.
95
+ #
50
96
  def execute(application, *arguments)
51
97
  result = raw_response("EXEC #{application} #{arguments * '|'}")
52
98
  return false if error?(result)
@@ -54,6 +100,10 @@ module Adhearsion
54
100
  end
55
101
 
56
102
  # Hangs up the current channel.
103
+ # After this command is issued, your application will stop executing.
104
+ # This should be used in the same way you would call the ruby exit() method to exit an application.
105
+ # If it is necessary to do some additional cleanup tasks before returning control back to asterisk, then
106
+ # make sure you have setup a begin...ensure block in the context of your adhearsion application dialplan.
57
107
  def hangup
58
108
  raw_response 'HANGUP'
59
109
  end
@@ -113,9 +163,15 @@ module Adhearsion
113
163
 
114
164
  def with_next_message(&block)
115
165
  raise LocalJumpError, "Must supply a block" unless block_given?
116
- block.call @call.inbox.pop
166
+ block.call(next_message)
117
167
  end
118
168
 
169
+ # This command shouled be used to advance to the next message in the Asterisk Comedian Voicemail application
170
+ def next_message
171
+ @call.inbox.pop
172
+ end
173
+
174
+ # This command should be used to check if a message is waiting on the Asterisk Comedian Voicemail application.
119
175
  def messages_waiting?
120
176
  not @call.inbox.empty?
121
177
  end
@@ -478,6 +534,10 @@ module Adhearsion
478
534
  execute "MeetMe", conference_id, command_flags, options[:pin]
479
535
  end
480
536
 
537
+ # Issue this command to access a channel variable that exists in the asterisk dialplan (i.e. extensions.conf)
538
+ # A complete description is available here: http://www.voip-info.org/wiki/view/get+variable
539
+ # Use get_variable to pass information from other modules or high level configurations from the asterisk dialplan
540
+ # to the adhearsion dialplan.
481
541
  def get_variable(variable_name)
482
542
  result = raw_response("GET VARIABLE #{variable_name}")
483
543
  case result
@@ -488,6 +548,12 @@ module Adhearsion
488
548
  end
489
549
  end
490
550
 
551
+ # Use set_variable to pass information back to the asterisk dial plan.
552
+ # A complete decription is available here: http://www.voip-info.org/wiki/view/set+variable
553
+ # Keep in mind that the variables are not global variables. These variables only exist for the channel
554
+ # related to the call that is being serviced by the particular instance of your adhearsion application.
555
+ # You will not be able to pass information back to the asterisk dialplan for other instances of your adhearsion
556
+ # application to share. Once the channel is "hungup" then the variables are cleared and their information is gone.
491
557
  def set_variable(variable_name, value)
492
558
  raw_response("SET VARIABLE %s %p" % [variable_name.to_s, value.to_s]) == "200 result=1"
493
559
  end
@@ -576,6 +642,42 @@ module Adhearsion
576
642
  voicemail_main
577
643
  end
578
644
 
645
+ # Use this command to dial an extension i.e. "phone number" in asterisk
646
+ # This command maps to the Asterisk DIAL command in the asterisk dialplan: http://www.voip-info.org/wiki-Asterisk+cmd+Dial
647
+ #
648
+ # The first parameter, number, must be a string that represents the extension or "number" that asterisk should dial.
649
+ # Be careful to not just specify a number like 5001, 9095551001
650
+ # You must specify a properly formatted string as Asterisk would expect to use in order to understand
651
+ # whether the call should be dialed using SIP, IAX, or some other means.
652
+ # Examples:
653
+ #
654
+ # Make a call to the PSTN using my SIP provider for VoIP termination:
655
+ # dial("SIP/19095551001@my.sip.voip.terminator.us")
656
+ #
657
+ # Make 3 Simulataneous calls to the SIP extensions separated by & symbols, try for 15 seconds and use the callerid
658
+ # for this call specified by the variable my_callerid
659
+ # dial "SIP/jay-desk-650&SIP/jay-desk-601&SIP/jay-desk-601-2", :for => 15.seconds, :caller_id => my_callerid
660
+ #
661
+ # Make a call using the IAX provider to the PSTN
662
+ # dial("IAX2/my.id@voipjet/19095551234", :name=>"John Doe", :caller_id=>"9095551234")
663
+ #
664
+ # Options Parameter:
665
+ # :caller_id - the caller id number to be used when the call is placed. It is advised you properly adhere to the
666
+ # policy of VoIP termination providers with respect to caller id values.
667
+ #
668
+ # :name - this is the name which should be passed with the caller ID information
669
+ # if :name=>"John Doe" and :caller_id => "444-333-1000" then the compelete CID and name would be "John Doe" <4443331000>
670
+ # support for caller id information varies from country to country and from one VoIP termination provider to another.
671
+ #
672
+ # :for - this option can be thought of best as a timeout. i.e. timeout after :for if no one answers the call
673
+ # For example, dial("SIP/jay-desk-650&SIP/jay-desk-601&SIP/jay-desk-601-2", :for => 15.seconds, :caller_id => callerid)
674
+ # this call will timeout after 15 seconds if 1 of the 3 extensions being dialed do not pick prior to the 15 second time limit
675
+ #
676
+ # :options - This is a string of options like "Tr" which are supported by the asterisk DIAL application.
677
+ # for a complete list of these options and their usage please visit: http://www.voip-info.org/wiki-Asterisk+cmd+Dial
678
+ #
679
+ # :confirm - ?
680
+ #
579
681
  def dial(number, options={})
580
682
  *recognized_options = :caller_id, :name, :for, :options, :confirm
581
683
 
@@ -635,19 +737,19 @@ module Adhearsion
635
737
  end
636
738
 
637
739
  def interruptable_play(*files)
638
- files.each do |file|
740
+ files.flatten.each do |file|
639
741
  result = result_digit_from raw_response("EXEC BACKGROUND #{file}")
640
742
  return result if result != 0.chr
641
743
  end
642
744
  nil
643
745
  end
644
-
746
+
645
747
  def set_caller_id_number(caller_id)
646
748
  return unless caller_id
647
749
  raise ArgumentError, "Caller ID must be numerical" if caller_id.to_s !~ /^\d+$/
648
750
  raw_response %(SET CALLERID %p) % caller_id
649
751
  end
650
-
752
+
651
753
  def set_caller_id_name(caller_id_name)
652
754
  return unless caller_id_name
653
755
  variable "CALLERID(name)" => caller_id_name