adhearsion-cw 1.0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/CHANGELOG +109 -0
  2. data/EVENTS +11 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE +456 -0
  5. data/Rakefile +134 -0
  6. data/adhearsion.gemspec +174 -0
  7. data/app_generators/ahn/USAGE +5 -0
  8. data/app_generators/ahn/ahn_generator.rb +97 -0
  9. data/app_generators/ahn/templates/.ahnrc +34 -0
  10. data/app_generators/ahn/templates/Gemfile +7 -0
  11. data/app_generators/ahn/templates/README +8 -0
  12. data/app_generators/ahn/templates/Rakefile +27 -0
  13. data/app_generators/ahn/templates/components/ami_remote/ami_remote.rb +15 -0
  14. data/app_generators/ahn/templates/components/disabled/HOW_TO_ENABLE +7 -0
  15. data/app_generators/ahn/templates/components/disabled/restful_rpc/README.markdown +11 -0
  16. data/app_generators/ahn/templates/components/disabled/restful_rpc/example-client.rb +48 -0
  17. data/app_generators/ahn/templates/components/disabled/restful_rpc/restful_rpc.rb +91 -0
  18. data/app_generators/ahn/templates/components/disabled/restful_rpc/restful_rpc.yml +34 -0
  19. data/app_generators/ahn/templates/components/disabled/restful_rpc/spec/restful_rpc_spec.rb +251 -0
  20. data/app_generators/ahn/templates/components/disabled/sandbox/sandbox.rb +104 -0
  21. data/app_generators/ahn/templates/components/disabled/sandbox/sandbox.yml +2 -0
  22. data/app_generators/ahn/templates/components/disabled/stomp_gateway/README.markdown +47 -0
  23. data/app_generators/ahn/templates/components/disabled/stomp_gateway/stomp_gateway.rb +34 -0
  24. data/app_generators/ahn/templates/components/disabled/stomp_gateway/stomp_gateway.yml +12 -0
  25. data/app_generators/ahn/templates/components/disabled/xmpp_gateway/README.markdown +3 -0
  26. data/app_generators/ahn/templates/components/disabled/xmpp_gateway/xmpp_gateway.rb +11 -0
  27. data/app_generators/ahn/templates/components/disabled/xmpp_gateway/xmpp_gateway.yml +0 -0
  28. data/app_generators/ahn/templates/components/simon_game/simon_game.rb +56 -0
  29. data/app_generators/ahn/templates/config/startup.rb +74 -0
  30. data/app_generators/ahn/templates/dialplan.rb +3 -0
  31. data/app_generators/ahn/templates/events.rb +32 -0
  32. data/bin/ahn +29 -0
  33. data/bin/ahnctl +68 -0
  34. data/bin/jahn +43 -0
  35. data/examples/asterisk_manager_interface/standalone.rb +51 -0
  36. data/lib/adhearsion/cli.rb +296 -0
  37. data/lib/adhearsion/component_manager/component_tester.rb +53 -0
  38. data/lib/adhearsion/component_manager/spec_framework.rb +18 -0
  39. data/lib/adhearsion/component_manager.rb +272 -0
  40. data/lib/adhearsion/events_support.rb +84 -0
  41. data/lib/adhearsion/foundation/all.rb +15 -0
  42. data/lib/adhearsion/foundation/blank_slate.rb +3 -0
  43. data/lib/adhearsion/foundation/custom_daemonizer.rb +45 -0
  44. data/lib/adhearsion/foundation/event_socket.rb +205 -0
  45. data/lib/adhearsion/foundation/future_resource.rb +36 -0
  46. data/lib/adhearsion/foundation/metaprogramming.rb +17 -0
  47. data/lib/adhearsion/foundation/numeric.rb +13 -0
  48. data/lib/adhearsion/foundation/pseudo_guid.rb +10 -0
  49. data/lib/adhearsion/foundation/relationship_properties.rb +42 -0
  50. data/lib/adhearsion/foundation/string.rb +26 -0
  51. data/lib/adhearsion/foundation/synchronized_hash.rb +96 -0
  52. data/lib/adhearsion/foundation/thread_safety.rb +7 -0
  53. data/lib/adhearsion/host_definitions.rb +67 -0
  54. data/lib/adhearsion/initializer/asterisk.rb +87 -0
  55. data/lib/adhearsion/initializer/configuration.rb +321 -0
  56. data/lib/adhearsion/initializer/database.rb +60 -0
  57. data/lib/adhearsion/initializer/drb.rb +31 -0
  58. data/lib/adhearsion/initializer/freeswitch.rb +22 -0
  59. data/lib/adhearsion/initializer/ldap.rb +57 -0
  60. data/lib/adhearsion/initializer/rails.rb +41 -0
  61. data/lib/adhearsion/initializer/xmpp.rb +42 -0
  62. data/lib/adhearsion/initializer.rb +394 -0
  63. data/lib/adhearsion/logging.rb +92 -0
  64. data/lib/adhearsion/tasks/components.rb +32 -0
  65. data/lib/adhearsion/tasks/database.rb +5 -0
  66. data/lib/adhearsion/tasks/deprecations.rb +59 -0
  67. data/lib/adhearsion/tasks/generating.rb +20 -0
  68. data/lib/adhearsion/tasks/lint.rb +4 -0
  69. data/lib/adhearsion/tasks/testing.rb +37 -0
  70. data/lib/adhearsion/tasks.rb +17 -0
  71. data/lib/adhearsion/version.rb +35 -0
  72. data/lib/adhearsion/voip/asterisk/agi_server.rb +115 -0
  73. data/lib/adhearsion/voip/asterisk/commands.rb +1581 -0
  74. data/lib/adhearsion/voip/asterisk/config_generators/agents.conf.rb +140 -0
  75. data/lib/adhearsion/voip/asterisk/config_generators/config_generator.rb +102 -0
  76. data/lib/adhearsion/voip/asterisk/config_generators/queues.conf.rb +250 -0
  77. data/lib/adhearsion/voip/asterisk/config_generators/voicemail.conf.rb +240 -0
  78. data/lib/adhearsion/voip/asterisk/config_manager.rb +71 -0
  79. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rb +1681 -0
  80. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rl.rb +341 -0
  81. data/lib/adhearsion/voip/asterisk/manager_interface/ami_messages.rb +78 -0
  82. data/lib/adhearsion/voip/asterisk/manager_interface/ami_protocol_lexer_machine.rl +87 -0
  83. data/lib/adhearsion/voip/asterisk/manager_interface.rb +705 -0
  84. data/lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb +80 -0
  85. data/lib/adhearsion/voip/asterisk/super_manager.rb +19 -0
  86. data/lib/adhearsion/voip/asterisk.rb +4 -0
  87. data/lib/adhearsion/voip/call.rb +498 -0
  88. data/lib/adhearsion/voip/call_routing.rb +64 -0
  89. data/lib/adhearsion/voip/commands.rb +9 -0
  90. data/lib/adhearsion/voip/constants.rb +39 -0
  91. data/lib/adhearsion/voip/conveniences.rb +18 -0
  92. data/lib/adhearsion/voip/dial_plan.rb +250 -0
  93. data/lib/adhearsion/voip/dsl/dialing_dsl/dialing_dsl_monkey_patches.rb +37 -0
  94. data/lib/adhearsion/voip/dsl/dialing_dsl.rb +151 -0
  95. data/lib/adhearsion/voip/dsl/dialplan/control_passing_exception.rb +27 -0
  96. data/lib/adhearsion/voip/dsl/dialplan/dispatcher.rb +124 -0
  97. data/lib/adhearsion/voip/dsl/dialplan/parser.rb +69 -0
  98. data/lib/adhearsion/voip/dsl/dialplan/thread_mixin.rb +16 -0
  99. data/lib/adhearsion/voip/dsl/numerical_string.rb +128 -0
  100. data/lib/adhearsion/voip/freeswitch/basic_connection_manager.rb +48 -0
  101. data/lib/adhearsion/voip/freeswitch/event_handler.rb +58 -0
  102. data/lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb +129 -0
  103. data/lib/adhearsion/voip/freeswitch/inbound_connection_manager.rb +38 -0
  104. data/lib/adhearsion/voip/freeswitch/oes_server.rb +195 -0
  105. data/lib/adhearsion/voip/menu_state_machine/calculated_match.rb +80 -0
  106. data/lib/adhearsion/voip/menu_state_machine/matchers.rb +123 -0
  107. data/lib/adhearsion/voip/menu_state_machine/menu_builder.rb +57 -0
  108. data/lib/adhearsion/voip/menu_state_machine/menu_class.rb +149 -0
  109. data/lib/adhearsion/xmpp/connection.rb +61 -0
  110. data/lib/adhearsion.rb +46 -0
  111. data/lib/theatre/README.markdown +64 -0
  112. data/lib/theatre/callback_definition_loader.rb +84 -0
  113. data/lib/theatre/guid.rb +23 -0
  114. data/lib/theatre/invocation.rb +121 -0
  115. data/lib/theatre/namespace_manager.rb +153 -0
  116. data/lib/theatre/version.rb +2 -0
  117. data/lib/theatre.rb +151 -0
  118. metadata +323 -0
@@ -0,0 +1,39 @@
1
+ # Adhearsion, open source collaboration framework
2
+ # Copyright (C) 2006,2007,2008 Jay Phillips
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Adhearsion
19
+ module VoIP
20
+
21
+ # Please help adjust these if they may be inaccurate!
22
+ module Constants
23
+ US_LOCAL_NUMBER = /^[1-9]\d{6}$/
24
+ US_NATIONAL_NUMBER = /^1?[1-9]\d{2}[1-9]\d{6}$/
25
+ ISN = /^\d+\*\d+$/ # See http://freenum.org
26
+ SIP_URI = /^sip:[\w\._%+-]+@[\w\.-]+\.[a-zA-Z]{2,4}$/
27
+
28
+ # Type of Number definitions given over PRI. Taken from the Q.931-ITU spec, page 68.
29
+ Q931_TYPE_OF_NUMBER = Hash.new(:unknown).merge 0b001 => :international,
30
+ 0b010 => :national,
31
+ 0b011 => :network_specific,
32
+ 0b100 => :subscriber,
33
+ 0b110 => :abbreviated_number
34
+ end
35
+
36
+ include Constants
37
+
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module Conveniences
4
+
5
+ # Compiles the provided Asterisk dialplan pattern into a Ruby regular
6
+ # expression. For more usage of Asterisk's pattern syntax, see
7
+ # http://www.voip-info.org/wiki/view/Asterisk+Dialplan+Patterns
8
+ def _(pattern)
9
+ # Uncomment the following code fragment for complete compatibility.
10
+ # The fragment handles the seldom-used hyphen number spacer with no
11
+ # meaning.
12
+ Regexp.new '^' << pattern.# gsub(/(?!\[[\w+-]+)-(?![\w-]+\])/,'').
13
+ gsub('X', '[0-9]').gsub('Z', '[1-9]').gsub('N','[2-9]').
14
+ gsub('.','.+').gsub('!','.*') << '$'
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,250 @@
1
+ # Hardcoding require for now since for some reason it's not being loaded
2
+ require 'adhearsion/voip/dsl/dialplan/control_passing_exception'
3
+
4
+ require 'adhearsion/version'
5
+ # JRuby contains a bug that breaks some of the menu functionality
6
+ # See: https://adhearsion.lighthouseapp.com/projects/5871/tickets/92-menu-method-under-jruby-does-not-appear-to-work
7
+ begin
8
+ curver = Adhearsion::PkgVersion.new(JRUBY_VERSION)
9
+ minver = Adhearsion::PkgVersion.new("1.6.0")
10
+ if curver < minver
11
+ puts "****************************************************************************"
12
+ puts "Versions of JRuby prior to 1.6.0 contain a bug that impacts"
13
+ puts "using the \"+\" operator to jump from one context to another."
14
+ puts "Adhearsion has detected JRuby version #{JRUBY_VERSION}. For more information see:"
15
+ puts "https://adhearsion.lighthouseapp.com/projects/5871/tickets/92-menu-method-under-jruby-does-not-appear-to-work"
16
+ puts "****************************************************************************"
17
+ end
18
+ rescue NameError
19
+ # In case JRUBY_VERSION is not defined.
20
+ rescue ArgumentError
21
+ # Needed to handle ActiveSupport's handling of missing constants
22
+ # with anonymous modules under Ruby 1.9
23
+ end
24
+
25
+ module Adhearsion
26
+ class DialPlan
27
+ attr_accessor :loader, :entry_points
28
+ def initialize(loader = Loader)
29
+ @loader = loader
30
+ @entry_points = @loader.load_dialplans.contexts
31
+ end
32
+
33
+ ##
34
+ # Lookup and return an entry point by context name
35
+ #
36
+ def lookup(context_name)
37
+ entry_points[context_name]
38
+ end
39
+
40
+ ##
41
+ # Executable environment for a dial plan in the scope of a call. This class has all the dialplan methods mixed into it.
42
+ #
43
+ class ExecutionEnvironment
44
+
45
+ class << self
46
+ def create(*args)
47
+ new(*args).tap { |instance| instance.stage! }
48
+ end
49
+ end
50
+
51
+ attr_reader :call
52
+ def initialize(call, entry_point)
53
+ @call, @entry_point = call, entry_point
54
+ end
55
+
56
+ ##
57
+ # Adds the methods to this ExecutionEnvironment which make it useful. e.g. dialplan-related methods, call variables,
58
+ # and component methods.
59
+ #
60
+ def stage!
61
+ extend_with_voip_commands!
62
+ extend_with_call_variables!
63
+ extend_with_dialplan_component_methods!
64
+ end
65
+
66
+ def run
67
+ raise "Cannot run ExecutionEnvironment without an entry point!" unless entry_point
68
+ current_context = entry_point
69
+ answer if AHN_CONFIG.automatically_answer_incoming_calls
70
+ begin
71
+ instance_eval(&current_context)
72
+ rescue Adhearsion::VoIP::DSL::Dialplan::ControlPassingException => exception
73
+ current_context = exception.target
74
+ retry
75
+ end
76
+ end
77
+
78
+ protected
79
+
80
+ attr_reader :entry_point
81
+
82
+
83
+ def extend_with_voip_commands!
84
+ extend Adhearsion::VoIP::Conveniences
85
+ extend Adhearsion::VoIP::Commands.for(call.originating_voip_platform)
86
+ end
87
+
88
+ def extend_with_call_variables!
89
+ call.define_variable_accessors self
90
+ end
91
+
92
+ def extend_with_dialplan_component_methods!
93
+ Components.component_manager.extend_object_with(self, :dialplan) if Components.component_manager
94
+ end
95
+
96
+ end
97
+
98
+ class Manager
99
+
100
+ class NoContextError < StandardError; end
101
+
102
+ class << self
103
+ def handle(call)
104
+ new.handle(call)
105
+ end
106
+ end
107
+
108
+ attr_accessor :dial_plan, :context
109
+ def initialize
110
+ @dial_plan = DialPlan.new
111
+ end
112
+
113
+ def handle(call)
114
+ if call.failed_call?
115
+ environment = ExecutionEnvironment.create(call, nil)
116
+ call.extract_failed_reason_from environment
117
+ raise FailedExtensionCallException.new(environment)
118
+ end
119
+
120
+ if call.hungup_call?
121
+ raise HungupExtensionCallException.new(ExecutionEnvironment.new(call, nil))
122
+ end
123
+
124
+ starting_entry_point = entry_point_for call
125
+ raise NoContextError, "No dialplan entry point for call context '#{call.context}' -- Ignoring call!" unless starting_entry_point
126
+ @context = ExecutionEnvironment.create(call, starting_entry_point)
127
+ inject_context_names_into_environment @context
128
+ @context.run
129
+ end
130
+
131
+ # Find the dialplan by the context name from the call or from the
132
+ # first path entry in the AGI URL
133
+ def entry_point_for(call)
134
+
135
+ # Try the request URI for an entry point first
136
+ if call.respond_to?(:request) && m = call.request.path.match(%r{/([^/]+)})
137
+ if entry_point = dial_plan.lookup(m[1].to_sym)
138
+ return entry_point
139
+ else
140
+ ahn_log.warn "AGI URI requested context \"#{m[1]}\" but matching Adhearsion context not found! Falling back to Asterisk context."
141
+ end
142
+ end
143
+
144
+ # Fall back to the matching Asterisk context name
145
+ if entry_point = dial_plan.lookup(call.context.to_sym)
146
+ return entry_point
147
+ end
148
+ end
149
+
150
+ protected
151
+
152
+ def inject_context_names_into_environment(environment)
153
+ return unless dial_plan.entry_points
154
+ dial_plan.entry_points.each do |name, context|
155
+ environment.meta_def(name) { context }
156
+ end
157
+ end
158
+
159
+ end
160
+
161
+ class Loader
162
+ class << self
163
+ attr_accessor :default_dial_plan_file_name
164
+
165
+ def load(dial_plan_as_string)
166
+ string_io = StringIO.new dial_plan_as_string
167
+ def string_io.path
168
+ "(eval)"
169
+ end
170
+ load_dialplans string_io
171
+ end
172
+
173
+ def load_dialplans(*files)
174
+ files = Adhearsion::AHN_CONFIG.files_from_setting("paths", "dialplan") if files.empty?
175
+ files = Array files
176
+ files.map! do |file|
177
+ case file
178
+ when File, StringIO
179
+ file
180
+ when String
181
+ File.new file
182
+ else
183
+ raise ArgumentError, "Unrecognized type of file #{file.inspect}"
184
+ end
185
+ end
186
+ new.tap do |loader|
187
+ files.each do |file|
188
+ loader.load file
189
+ end
190
+ end
191
+ end
192
+
193
+ end
194
+
195
+ self.default_dial_plan_file_name ||= 'dialplan.rb'
196
+
197
+ def initialize
198
+ @context_collector = ContextNameCollector.new
199
+ end
200
+
201
+ def contexts
202
+ @context_collector.contexts
203
+ end
204
+
205
+ def load(dialplan_file)
206
+ dialplan_code = dialplan_file.read
207
+ @context_collector.instance_eval(dialplan_code, dialplan_file.path)
208
+ nil
209
+ end
210
+
211
+ class ContextNameCollector# < ::BlankSlate
212
+
213
+ class << self
214
+
215
+ def const_missing(name)
216
+ super
217
+ rescue ArgumentError
218
+ raise NameError, %(undefined constant "#{name}")
219
+ end
220
+
221
+ end
222
+
223
+ attr_reader :contexts
224
+ def initialize
225
+ @contexts = {}
226
+ end
227
+
228
+ def method_missing(name, *args, &block)
229
+ super if !block_given? || args.any?
230
+ contexts[name] = DialplanContextProc.new(name, &block)
231
+ end
232
+
233
+ end
234
+ end
235
+ class DialplanContextProc < Proc
236
+
237
+ attr_reader :name
238
+
239
+ def initialize(name, &block)
240
+ super(&block)
241
+ @name = name
242
+ end
243
+
244
+ def +@
245
+ raise Adhearsion::VoIP::DSL::Dialplan::ControlPassingException.new(self)
246
+ end
247
+
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,37 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module DSL
4
+ class DialingDSL
5
+ module MonkeyPatches
6
+ module RegexpMonkeyPatch
7
+
8
+ def |(other)
9
+ case other
10
+ when RouteRule
11
+ other.unshift_pattern self
12
+ other
13
+ when Regexp
14
+ RouteRule.new :patterns => [self, other]
15
+ else
16
+ raise ArgumentError, "Unsupported pattern type #{other.inspect}"
17
+ end
18
+ end
19
+
20
+ def >>(other)
21
+ case other
22
+ when ProviderDefinition
23
+ RouteRule.new :patterns => self, :providers => other
24
+ when RouteRule
25
+ other.tap do |route|
26
+ route.unshift_pattern self
27
+ end
28
+ else raise ArgumentError, "Unsupported route definition #{other.inspect}"
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,151 @@
1
+ # Adhearsion, open source collaboration framework
2
+ # Copyright (C) 2006,2007,2008 Jay Phillips
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ require 'adhearsion/foundation/metaprogramming'
19
+ require 'adhearsion/voip/conveniences'
20
+ require 'adhearsion/voip/constants'
21
+ require 'adhearsion/voip/dsl/dialing_dsl/dialing_dsl_monkey_patches'
22
+
23
+ module Adhearsion
24
+ module VoIP
25
+ module DSL
26
+ class DialingDSL
27
+
28
+ extend Conveniences
29
+ include Constants
30
+
31
+ Regexp.class_eval do
32
+ include Adhearsion::VoIP::DSL::DialingDSL::MonkeyPatches::RegexpMonkeyPatch
33
+ end
34
+
35
+ def self.inherited(klass)
36
+ klass.class_eval do
37
+ [:@@providers, :@@routes].each do |var|
38
+ class_variable_set(var, [])
39
+ cattr_reader var.to_s.gsub('@@', '')
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.calculate_routes_for(destination)
45
+ destination = destination.to_s
46
+ routes.select { |defined_route| defined_route === destination }.map &:providers
47
+ end
48
+
49
+ class ProviderDefinition < OpenStruct
50
+
51
+ def initialize(name)
52
+ super()
53
+ self.name = name.to_s.to_sym
54
+ end
55
+
56
+ def >>(other)
57
+ RouteRule.new :providers => [self, other]
58
+ end
59
+
60
+ def format_number_for_platform(number, platform=:asterisk)
61
+ case platform
62
+ when :asterisk
63
+ [protocol || "SIP", name || "default", number].join '/'
64
+ else
65
+ raise "Unsupported platform #{platform}!"
66
+ end
67
+ end
68
+
69
+ def format_number_for_platform(number, platform=:asterisk)
70
+ case platform
71
+ when :asterisk
72
+ [protocol || "SIP", name || "default", number].join '/'
73
+ else
74
+ raise "Unsupported platform #{platform}!"
75
+ end
76
+ end
77
+
78
+ def defined_properties_without_name
79
+ @table.clone.tap do |copy|
80
+ copy.delete :name
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ protected
87
+
88
+ def self.provider(name, &block)
89
+ raise ArgumentError, "no block given" unless block_given?
90
+
91
+ options = ProviderDefinition.new name
92
+ yield options
93
+
94
+ providers << options
95
+ meta_def(name) { options }
96
+ end
97
+
98
+ def self.route(route)
99
+ routes << route
100
+ end
101
+
102
+ class RouteRule
103
+
104
+ attr_reader :patterns, :providers
105
+
106
+ def initialize(hash={})
107
+ @patterns = Array hash[:patterns]
108
+ @providers = Array hash[:providers]
109
+ end
110
+
111
+ def merge!(other)
112
+ providers.concat other.providers
113
+ patterns.concat other.patterns
114
+ self
115
+ end
116
+
117
+ def >>(other)
118
+ case other
119
+ when RouteRule then merge! other
120
+ when ProviderDefinition then providers << other
121
+ else raise RouteException, "Unrecognized object in route definition: #{other.inspect}"
122
+ end
123
+ self
124
+ end
125
+
126
+ def |(other)
127
+ case other
128
+ when RouteRule then merge! other
129
+ when Regexp
130
+ patterns << other
131
+ self
132
+ else raise other.inspect
133
+ end
134
+ end
135
+
136
+ def ===(other)
137
+ patterns.each { |pattern| return true if pattern === other }
138
+ false
139
+ end
140
+
141
+ def unshift_pattern(pattern)
142
+ patterns.unshift pattern
143
+ end
144
+
145
+ class RouteException < StandardError; end
146
+
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,27 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module DSL
4
+ module Dialplan
5
+ # A ControlPassingException is used internally to stop execution of one
6
+ # dialplan context and begin execution of another proc instead. It is
7
+ # most notably used by the ~@ unary operator that can be called on a
8
+ # context name within a dialplan to transfer control entirely to that
9
+ # particular context. The serve() method in the servlet_container actually
10
+ # rescues these exceptions specifically and then does +e.target to execute
11
+ # that code.
12
+ class ControlPassingException < StandardError
13
+
14
+ attr_reader :target
15
+
16
+ def initialize(target)
17
+ super()
18
+ @target = target
19
+ end
20
+
21
+ end
22
+
23
+ class ContextNotFoundException < StandardError; end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,124 @@
1
+ module Adhearsion
2
+ module VoIP
3
+ module DSL
4
+ module Dialplan
5
+
6
+ class ReturnValue < StandardError
7
+ attr_reader :obj
8
+ def initialize(obj)
9
+ @obj = obj
10
+ super
11
+ end
12
+ end
13
+
14
+ class Hangup < StandardError; end
15
+
16
+ # Instantiated and returned in every dialplan command
17
+ class EventCommand
18
+
19
+ attr_accessor :app, :args, :response_block, :returns, :on_keypress
20
+
21
+ def initialize(app, *args, &block)
22
+ @hash = args.pop if args.last.kind_of?(Hash)
23
+ @app, @args = app, args
24
+
25
+ if @hash
26
+ @returns = @hash[:returns] || :raw
27
+ @on_keypress = @hash[:on_keypress]
28
+ end
29
+
30
+ @response_block = block if block_given?
31
+ end
32
+
33
+ def on_keypress(&block)
34
+ block_given? ? @on_keypress = block : @on_keypress
35
+ end
36
+
37
+ def on_break(&block)
38
+ block_given? ? @on_break = block : @on_break
39
+ end
40
+
41
+ end
42
+
43
+ class NoOpEventCommand < EventCommand
44
+ attr_reader :timeout, :on_keypress
45
+ def initialize(timeout=nil, hash={})
46
+ @timeout = timeout
47
+ @on_keypress = hash[:on_keypress]
48
+ end
49
+ end
50
+
51
+ class ExitingEventCommand < EventCommand; end
52
+
53
+ # Serves as a EventCommand proxy for handling EventCommands. Useful
54
+ # if doing any pre-processing.
55
+ #
56
+ # For example, when sending commands over the event socket to FreeSwitch,
57
+ # every command must start with "api", but, when doing an originate, it
58
+ # must begin with an ampersand. These two pre-processors would be developed
59
+ # as separate CommandDispatchers.
60
+ class CommandDispatcher
61
+
62
+ attr_reader :factory, :context
63
+
64
+ def initialize(factory, context=nil)
65
+ @context = context
66
+ @factory = factory.new context
67
+ end
68
+
69
+ def dispatch!(event_command)
70
+ raise NotImplementedError, "Must subclass #{self.class} and override this!"
71
+ end
72
+
73
+ def method_missing(name, *args, &block)
74
+ *commands = *@factory.send(name, *args, &block)
75
+ commands.map do |command|
76
+ if command.kind_of? Proc
77
+ instance_eval(&command)
78
+ elsif command.kind_of? EventCommand
79
+ dispatched_command = dispatch! command
80
+ if command.response_block
81
+ while (new_cmd = command.response_block.call(dispatched_command)).kind_of? EventCommand
82
+ dispatched_command = dispatch! new_cmd
83
+ end
84
+ end
85
+ dispatched_command
86
+ else
87
+ command
88
+ end
89
+ end.last
90
+ rescue ReturnValue => r
91
+ return r.obj
92
+ end
93
+
94
+ def return!(obj)
95
+ raise DSL::Dialplan::ReturnValue.new(obj)
96
+ end
97
+
98
+ def break!(uuid=@context)
99
+ raise NotImplementedError, "Must subclass #{self.class} and override this!"
100
+ end
101
+
102
+ # Takes a Hash and meta_def()'s a method for each key that returns
103
+ # the key's value in the Hash.
104
+ def def_keys!(hash)
105
+ hash.each_pair do |k,v|
106
+ meta_def(k) { v } rescue nil
107
+ end
108
+ end
109
+
110
+ def clone
111
+ super.tap do |nemesis|
112
+ instance_variables.each do |iv|
113
+ value = instance_variable_get(iv)
114
+ nemesis.instance_variable_set iv, value.clone if value
115
+ end
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+ end
123
+ end
124
+ end