erichmond-ruby-agi 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,239 @@
1
+ #
2
+ # File: asterisk_variables.rb
3
+ #
4
+ # ruby-agi: Ruby Language API for Asterisk
5
+ #
6
+ # Copyright (C) <2005> Mohammad Khan <info@beeplove.com>
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
+ #
22
+
23
+ # == Overview
24
+ #
25
+
26
+ # Followings are the list of AGI variables
27
+ # -- ruby-agi >> agi_request
28
+ # -- ruby-agi >> agi_channel
29
+ # -- ruby-agi >> agi_language
30
+ # -- ruby-agi >> agi_type
31
+ # -- ruby-agi >> agi_uniqueid
32
+ # -- ruby-agi >> agi_callerid
33
+ # -- ruby-agi >> agi_dnid
34
+ # -- ruby-agi >> agi_rdnis
35
+ # -- ruby-agi >> agi_context
36
+ # -- ruby-agi >> agi_extension
37
+ # -- ruby-agi >> agi_priority
38
+ # -- ruby-agi >> agi_enhanced
39
+ # -- ruby-agi >> agi_accountcode
40
+
41
+
42
+ require 'ruby-agi/agi.rb'
43
+ require 'ruby-agi/error.rb'
44
+
45
+ class AGI
46
+ end
47
+
48
+ class AsteriskVariable < AGI
49
+
50
+ def initialize
51
+ env
52
+ end
53
+
54
+ protected
55
+ def env
56
+ if @asterisk_variable.nil?
57
+ @asterisk_variable = Hash.new
58
+ semaphore do
59
+ $stdin.each_line do | line |
60
+ line.strip!
61
+ break if line.nil? or line.size.zero? or line == "\n"
62
+ key, value = line.split(':')
63
+ value.strip! if not value.nil?
64
+ if ((not key.nil?) and (not key.to_s.size.zero?))
65
+ @asterisk_variable[key.to_s] = value
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ return @asterisk_variable
72
+ end
73
+
74
+ protected
75
+ def read_env(name)
76
+ if debug?
77
+ semaphore do
78
+ $stderr.puts " -- ruby-agi << Read asterisk variable '#{name}'"
79
+ $stderr.puts " -- ruby-agi >> Asterisk variable #{name} = #{env[name].to_s}"
80
+ end
81
+ end
82
+
83
+ return env[name]
84
+ end
85
+
86
+ public
87
+ def request
88
+ return read_env('agi_request')
89
+ end
90
+
91
+ public
92
+ def channel
93
+ return read_env('agi_channel')
94
+ end
95
+
96
+ public
97
+ def language
98
+ return read_env('agi_language')
99
+ end
100
+
101
+ public
102
+ def type
103
+ return read_env('agi_type')
104
+ end
105
+
106
+ public
107
+ def uniqueid
108
+ return read_env('agi_uniqueid')
109
+ end
110
+
111
+ public
112
+ def callerid
113
+ if @callerid.nil?
114
+ init_caller_variable
115
+ end
116
+
117
+ return @callerid
118
+ end
119
+
120
+ public
121
+ def dnid
122
+ return read_env('agi_dnid')
123
+ end
124
+
125
+ public
126
+ def rdnid
127
+ return read_env('agi_rdnid')
128
+ end
129
+
130
+ public
131
+ def context
132
+ return read_env('agi_context')
133
+ end
134
+
135
+ public
136
+ def extension
137
+ return read_env('agi_extension')
138
+ end
139
+
140
+ public
141
+ def priority
142
+ return read_env('agi_priority')
143
+ end
144
+
145
+ public
146
+ def enhanced
147
+ return read_env('agi_enhanced')
148
+ end
149
+
150
+ public
151
+ def accountcode
152
+ return read_env('agi_accountcode')
153
+ end
154
+
155
+ ####################################################################
156
+ #### additional methods generated from existing basic methods ####
157
+ ####################################################################
158
+
159
+ public
160
+ def calleridname
161
+ if @calleridname.nil?
162
+ init_caller_variable
163
+ end
164
+
165
+ return @calleridname
166
+ end
167
+
168
+ public
169
+ def calleridnumber
170
+ if @calleridnumber.nil?
171
+ init_caller_variable
172
+ end
173
+
174
+ return @calleridnumber
175
+ end
176
+
177
+ # following methods are not confirm that they are AGI variables
178
+ public
179
+ def callingpres
180
+ return read_env('agi_callingpres')
181
+ end
182
+
183
+ public
184
+ def callingani2
185
+ return read_env('agi_callingani2')
186
+ end
187
+
188
+ public
189
+ def callington
190
+ return read_env('agi_callington')
191
+ end
192
+
193
+ public
194
+ def callingtns
195
+ return read_env('agi_callingtns')
196
+ end
197
+
198
+ # asterisk-1.0.9 : agi_callerid (default value is 'unknown')
199
+ # asterisk-1.0.10 : agi_callerid (default value is 'unknown')
200
+ # asterisk-1.0.10 : agi_callerid, agi_calleridname (default value for both is 'unknown')
201
+ protected
202
+ def init_caller_variable
203
+ @callerid = read_env('agi_callerid').to_s.strip
204
+ if @callerid == "unknown"
205
+ @callerid = ""
206
+ @calleridname = ""
207
+ @calleridnumber = ""
208
+ elsif Regexp.new(/^\d+\d$/).match(@callerid)
209
+ @calleridname = ""
210
+ @calleridnumber = @callerid
211
+ else
212
+ @calleridname = read_env('agi_calleridname')
213
+ if @calleridname.nil? # for asterisk that don't have agi variable 'agi_calleridname'
214
+ name = Regexp.new(/\".+\"/).match(@callerid)
215
+ number = Regexp.new(/\<\d+\>/).match(@callerid)
216
+
217
+ if name.nil?
218
+ @calleridname = ""
219
+ else
220
+ name = name.to_s
221
+ name.gsub!(/\"/, '')
222
+ @calleridname = name.to_s.strip
223
+ end
224
+
225
+ if number.nil?
226
+ @calleridnumber = ""
227
+ else
228
+ number = number.to_s
229
+ number.sub!(/\</, '')
230
+ number.sub!(/\>/, '')
231
+ @calleridnumber = number.to_s.strip
232
+ end
233
+ else # for asterisk that have agi variable 'agi_calleridname'
234
+ @calleridnumber = @callerid
235
+ @callerid = "\"#{@calleridname}\" <#{@calleridnumber}>"
236
+ end
237
+ end
238
+ end
239
+ end