ruby-agi 0.0.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/Release-Notes +2 -0
- data/extconf.rb +2 -0
- data/lib/ruby-agi/agi.rb +847 -0
- data/lib/{asterisk_variable.rb → ruby-agi/asterisk_variable.rb} +49 -34
- data/lib/ruby-agi/command.rb +897 -0
- data/lib/ruby-agi.rb +5 -0
- metadata +12 -32
- data/examples/call_log.rb +0 -0
- data/lib/agi.rb +0 -310
- data/lib/command.rb +0 -486
- data.tar.gz.sig +0 -1
- metadata.gz.sig +0 -0
- /data/lib/{error.rb → ruby-agi/error.rb} +0 -0
- /data/lib/{return_status.rb → ruby-agi/return_status.rb} +0 -0
data/lib/command.rb
DELETED
@@ -1,486 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# File: command.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
|
-
# this would be the most used class
|
24
|
-
|
25
|
-
# most common arguements
|
26
|
-
# timeout: nil=forever,
|
27
|
-
# escape_digit, by default #, if nil passed as argument there will not have any escape
|
28
|
-
# escape_digits, by default #, if nil passed as argument there will not have any escape
|
29
|
-
# channel, current channel otherwise mentioned
|
30
|
-
# max_digit, maximum number of digits would be accepted from user, always 255 otherwise mentioned
|
31
|
-
# filename, name of file to be streamed, recoreds ..
|
32
|
-
# beep, always yes, otherwise passed false or nil as argument
|
33
|
-
# format, always gsm otherwise mentioned
|
34
|
-
# secs, all methods need time as an argument are passed as seconds
|
35
|
-
|
36
|
-
require 'agi.rb'
|
37
|
-
require 'return_status.rb'
|
38
|
-
require 'error.rb'
|
39
|
-
require 'sync'
|
40
|
-
|
41
|
-
class AGI
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
class Command < AGI
|
46
|
-
|
47
|
-
# answer: Asserts answer
|
48
|
-
public
|
49
|
-
def answer
|
50
|
-
cmd = 'ANSWER'
|
51
|
-
rs = exec_command(cmd)
|
52
|
-
raise CommandError, rs.to_s if rs.command_error?
|
53
|
-
return rs
|
54
|
-
end
|
55
|
-
|
56
|
-
# set autohangup: Autohangup channel in some time
|
57
|
-
public
|
58
|
-
def set_auto_hangup(secs=0)
|
59
|
-
cmd = 'SET AUTOHANGUP ' + secs.to_s
|
60
|
-
rs = exec_command(cmd)
|
61
|
-
raise CommandError, rs.to_s if rs.command_error?
|
62
|
-
return rs
|
63
|
-
end
|
64
|
-
|
65
|
-
# channel status: Returns status of the connected channel
|
66
|
-
# we are returing channel object
|
67
|
-
public
|
68
|
-
def channel_status(channel=nil)
|
69
|
-
cmd = 'CHANNEL STATUS'
|
70
|
-
cmd = cmd + ' ' + channel.to_s if not channel.nil?
|
71
|
-
rs = exec_command(cmd)
|
72
|
-
raise CommandError, rs.to_s if rs.command_error?
|
73
|
-
return rs
|
74
|
-
end
|
75
|
-
|
76
|
-
# database del: Removes database key/value
|
77
|
-
#def database_del
|
78
|
-
# I am not quite sure how it works and what it do
|
79
|
-
# I will update code once I about this
|
80
|
-
#end
|
81
|
-
|
82
|
-
# database deltree: Removes database keytree/value
|
83
|
-
#def database_deltree
|
84
|
-
# I am not quite sure how it works and what it do
|
85
|
-
# I will update code once I about this
|
86
|
-
#end
|
87
|
-
|
88
|
-
# database get: Gets database value
|
89
|
-
#def database_get
|
90
|
-
# I am not quite sure how it works and what it do
|
91
|
-
# I will update code once I about this
|
92
|
-
#end
|
93
|
-
|
94
|
-
# database put: Adds/updates database value
|
95
|
-
#def database_put
|
96
|
-
# I am not quite sure how it works and what it do
|
97
|
-
# I will update code once I about this
|
98
|
-
#end
|
99
|
-
|
100
|
-
# exec: Executes a given Application. (Applications are the functions you use to create a dial plan in extensions.conf ).
|
101
|
-
# returns the message like 200, result=0
|
102
|
-
# to get result use rs.result
|
103
|
-
# exec return whatever the asterisk command returns
|
104
|
-
public
|
105
|
-
def exec(asterisk_application, options=nil)
|
106
|
-
cmd = 'EXEC ' + asterisk_application.to_s
|
107
|
-
cmd = cmd + ' ' + options.to_s if not options.nil?
|
108
|
-
rs = exec_command(cmd)
|
109
|
-
raise CommandError, rs.to_s if rs.command_error?
|
110
|
-
return rs
|
111
|
-
end
|
112
|
-
|
113
|
-
# get data: Gets data on a channel
|
114
|
-
public
|
115
|
-
def get_data(filename, timeout=nil, max_digit=nil)
|
116
|
-
cmd = 'GET DATA' + ' ' + filename.to_s
|
117
|
-
timeout = timeout.to_i * 1000 if not timeout.nil?
|
118
|
-
if (timeout.nil? and (not max_digit.nil?))
|
119
|
-
cmd = cmd + ' -1 ' + max_digit.to_s
|
120
|
-
elsif max_digit.nil?
|
121
|
-
cmd = cmd + ' ' + timeout.to_s
|
122
|
-
else
|
123
|
-
cmd = cmd + ' ' + timeout.to_s + ' ' + max_digit.to_s
|
124
|
-
end
|
125
|
-
|
126
|
-
rs = exec_command(cmd)
|
127
|
-
raise CommandError, rs.to_s if rs.command_error?
|
128
|
-
return rs
|
129
|
-
end
|
130
|
-
|
131
|
-
# get variable: Gets a channel variable
|
132
|
-
public
|
133
|
-
def get_variable(name)
|
134
|
-
cmd = 'GET VARIABLE ' + name.to_s
|
135
|
-
rs = exec_command(cmd)
|
136
|
-
raise CommandError, rs.to_s if rs.command_error?
|
137
|
-
return rs
|
138
|
-
end
|
139
|
-
|
140
|
-
# hangup: Hangup the current channel
|
141
|
-
public
|
142
|
-
def hangup(channel=nil)
|
143
|
-
cmd = 'HANGUP'
|
144
|
-
cmd = cmd + ' ' + channel.to_s if channel.nil?
|
145
|
-
rs = exec_command(cmd)
|
146
|
-
raise CommandError, rs.to_s if rs.command_error?
|
147
|
-
return rs
|
148
|
-
end
|
149
|
-
|
150
|
-
# noop: Does nothing
|
151
|
-
public
|
152
|
-
def noop(msg)
|
153
|
-
cmd = 'NOOP ' + msg.to_s
|
154
|
-
rs = exec_command(cmd)
|
155
|
-
raise CommandError, rs.to_s if rs.command_error?
|
156
|
-
return rs
|
157
|
-
end
|
158
|
-
|
159
|
-
# receive char: Receives one character from channels supporting it
|
160
|
-
public
|
161
|
-
def receive_char(timeout=0)
|
162
|
-
cmd = 'RECEIVE CHAR'
|
163
|
-
if not timeout.zero?
|
164
|
-
timeout = timeout * 1000
|
165
|
-
cmd = cmd + ' ' + timeout.to_s
|
166
|
-
end
|
167
|
-
rs = exec_command(cmd)
|
168
|
-
raise CommandError, rs.to_s if rs.command_error?
|
169
|
-
return rs
|
170
|
-
end
|
171
|
-
|
172
|
-
# receive text: Receives text from channels supporting it
|
173
|
-
public
|
174
|
-
def receive_text(timeout=0)
|
175
|
-
cmd = 'RECEIVE TEXT'
|
176
|
-
if not timeout.zero?
|
177
|
-
timeout = timeout * 1000
|
178
|
-
cmd = cmd + ' ' + timeout.to_s
|
179
|
-
end
|
180
|
-
rs = exec_command(cmd)
|
181
|
-
raise CommandError, rs.to_s if rs.command_error?
|
182
|
-
return rs
|
183
|
-
end
|
184
|
-
|
185
|
-
# record file: Records to a given file
|
186
|
-
public
|
187
|
-
def record_file(filename, format='gsm', escape_digits=nil, timeout=nil, beep=true)
|
188
|
-
cmd = 'RECORD FILE ' + filename.to_s + ' ' + format.to_s + ' '
|
189
|
-
if escape_digits.nil?
|
190
|
-
cmd = cmd + ' ' + '#'
|
191
|
-
else
|
192
|
-
cmd = cmd + ' ' + escape_digits.to_s
|
193
|
-
end
|
194
|
-
|
195
|
-
if timeout.nil?
|
196
|
-
timeout = -1
|
197
|
-
else
|
198
|
-
timeout = timeout * 1000 if timeout.to_i > 0
|
199
|
-
end
|
200
|
-
|
201
|
-
cmd = cmd + ' ' + timeout.to_s
|
202
|
-
cmd = cmd + ' ' + 'beep' if beep
|
203
|
-
rs = exec_command(cmd)
|
204
|
-
raise CommandError, rs.to_s if rs.command_error?
|
205
|
-
return rs
|
206
|
-
end
|
207
|
-
|
208
|
-
# say digits: Says a given digit string
|
209
|
-
public
|
210
|
-
def say_digits(digit_string, escape_digits=nil)
|
211
|
-
cmd = 'SAY DIGITS ' + digit_string.to_s + ' '
|
212
|
-
if escape_digits.nil?
|
213
|
-
cmd = cmd + '""'
|
214
|
-
else
|
215
|
-
cmd = cmd + escape_digits.to_s
|
216
|
-
end
|
217
|
-
rs = exec_command(cmd)
|
218
|
-
raise CommandError, rs.to_s if rs.command_error?
|
219
|
-
return rs
|
220
|
-
end
|
221
|
-
|
222
|
-
# say number: Says a given number
|
223
|
-
public
|
224
|
-
def say_number(number, escape_digits=nil)
|
225
|
-
cmd = 'SAY NUMBER ' + number.to_s + ' '
|
226
|
-
if escape_digits.nil?
|
227
|
-
cmd = cmd + '""'
|
228
|
-
else
|
229
|
-
cmd = cmd + escape_digits.to_s
|
230
|
-
end
|
231
|
-
rs = exec_command(cmd)
|
232
|
-
raise CommandError, rs.to_s if rs.command_error?
|
233
|
-
return rs
|
234
|
-
end
|
235
|
-
|
236
|
-
# say phonetic: Say the given character string.
|
237
|
-
public
|
238
|
-
def say_phonetic(string, escape_digits=nil)
|
239
|
-
cmd = 'SAY PHONETIC ' + '"' + string.to_s + '"'
|
240
|
-
cmd = cmd + ' ' + escape_digits.to_s if not escape_digits.nil?
|
241
|
-
rs = exec_command(cmd)
|
242
|
-
raise CommandError, rs.to_s if rs.command_error?
|
243
|
-
return rs
|
244
|
-
end
|
245
|
-
|
246
|
-
# say time: Say a time
|
247
|
-
# SAY TIME <time> <escape digits>
|
248
|
-
# <time> is number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
|
249
|
-
public
|
250
|
-
def say_time(time=Time.now.to_i, escape_digits='#')
|
251
|
-
cmd = 'SAY TIME ' + time.to_s
|
252
|
-
cmd = cmd + ' ' + escape_digits.to_s
|
253
|
-
rs = exec_command(cmd)
|
254
|
-
raise CommandError, rs.to_s if rs.command_error?
|
255
|
-
return rs
|
256
|
-
end
|
257
|
-
|
258
|
-
# send image: Sends images to channels supporting it
|
259
|
-
public
|
260
|
-
def send_image(image)
|
261
|
-
cmd = 'SEND IMAGE ' + image.to_s
|
262
|
-
rs = exec_command(cmd)
|
263
|
-
raise CommandError, rs.to_s if rs.command_error?
|
264
|
-
return rs
|
265
|
-
end
|
266
|
-
|
267
|
-
# send text: Sends text to channels supporting it
|
268
|
-
public
|
269
|
-
def send_text(text)
|
270
|
-
cmd = 'SEND TEXT ' + text.to_s
|
271
|
-
rs = exec_command(cmd)
|
272
|
-
raise CommandError, rs.to_s if rs.command_error?
|
273
|
-
return rs
|
274
|
-
end
|
275
|
-
|
276
|
-
# set callerid: Sets callerid for the current channel
|
277
|
-
public
|
278
|
-
def set_caller_id(number)
|
279
|
-
cmd = 'SET CALLERID ' + number.to_s
|
280
|
-
rs = exec_command(cmd)
|
281
|
-
raise CommandError, rs.to_s if rs.command_error?
|
282
|
-
return rs
|
283
|
-
end
|
284
|
-
|
285
|
-
# set context: Sets channel context
|
286
|
-
public
|
287
|
-
def set_context(context)
|
288
|
-
cmd = 'SET CONTEXT ' + context.to_s
|
289
|
-
rs = exec_command(cmd)
|
290
|
-
raise CommandError, rs.to_s if rs.command_error?
|
291
|
-
return rs
|
292
|
-
end
|
293
|
-
|
294
|
-
# set extension: Changes channel extension
|
295
|
-
public
|
296
|
-
def set_extension(extension)
|
297
|
-
cmd = 'SET EXTENSION ' + extension.to_s
|
298
|
-
rs = exec_command(cmd)
|
299
|
-
raise CommandError, rs.to_s if rs.command_error?
|
300
|
-
return rs
|
301
|
-
end
|
302
|
-
|
303
|
-
# set music: Enable/Disable Music on hold generator, example "SET MUSIC ON default"
|
304
|
-
public
|
305
|
-
def set_music(mode=true, moh_class='default')
|
306
|
-
if mode
|
307
|
-
mode = 'ON'
|
308
|
-
else
|
309
|
-
mode = 'OFF'
|
310
|
-
end
|
311
|
-
cmd = 'SET MUSIC ' + mode.to_s + ' ' + moh_class.to_s
|
312
|
-
rs = exec_command(cmd)
|
313
|
-
raise CommandError, rs.to_s if rs.command_error?
|
314
|
-
return rs
|
315
|
-
end
|
316
|
-
|
317
|
-
# set priority: Prioritizes the channel
|
318
|
-
public
|
319
|
-
def set_priority(priority)
|
320
|
-
cmd = 'SET PRIORITY ' + priority.to_i.to_s
|
321
|
-
rs = exec_command(cmd)
|
322
|
-
raise CommandError, rs.to_s if rs.command_error?
|
323
|
-
return rs
|
324
|
-
end
|
325
|
-
|
326
|
-
# set variable: Sets a channel variable
|
327
|
-
public
|
328
|
-
def set_variable(name, value)
|
329
|
-
cmd = 'SET VARIABLE ' + name.to_s + ' ' + '"' + value.to_s + '"'
|
330
|
-
rs = exec_command(cmd)
|
331
|
-
raise CommandError, rs.to_s if rs.command_error?
|
332
|
-
return rs
|
333
|
-
end
|
334
|
-
|
335
|
-
# stream file: Sends audio file on channel
|
336
|
-
public
|
337
|
-
def stream_file(filename, escape_digits='#')
|
338
|
-
cmd = 'STREAM FILE ' + filename.to_s
|
339
|
-
cmd = cmd + ' ' + '"' + escape_digits.to_s + '"'
|
340
|
-
rs = exec_command(cmd)
|
341
|
-
raise CommandError, rs.to_s if rs.command_error?
|
342
|
-
return rs
|
343
|
-
end
|
344
|
-
|
345
|
-
# tdd mode: Activates TDD mode on channels supporting it, to enable communication with TDDs.
|
346
|
-
public
|
347
|
-
def tdd_mode(settings=true)
|
348
|
-
if settings
|
349
|
-
settings = 'ON'
|
350
|
-
else
|
351
|
-
settings = 'OFF'
|
352
|
-
end
|
353
|
-
cmd = 'TDD MODE ' + settings.to_s
|
354
|
-
rs = exec_command(cmd)
|
355
|
-
raise CommandError, rs.to_s if rs.command_error?
|
356
|
-
return rs
|
357
|
-
end
|
358
|
-
|
359
|
-
# verbose: Logs a message to the asterisk verbose log
|
360
|
-
public
|
361
|
-
def verbose(message, level=3)
|
362
|
-
cmd = 'VERBOSE ' + '"' + message.to_s + '"' + ' ' + level.to_s
|
363
|
-
rs = exec_command(cmd)
|
364
|
-
raise CommandError, rs.to_s if rs.command_error?
|
365
|
-
return rs
|
366
|
-
end
|
367
|
-
|
368
|
-
# wait for digit: Waits for a digit to be pressed
|
369
|
-
public
|
370
|
-
def wait_for_digit(timeout=nil)
|
371
|
-
cmd = 'WAIT FOR DIGIT'
|
372
|
-
|
373
|
-
if timeout.nil?
|
374
|
-
timeout = -1
|
375
|
-
else
|
376
|
-
timeout = timeout * 1000
|
377
|
-
end
|
378
|
-
cmd = cmd + ' ' + timeout.to_i.to_s
|
379
|
-
rs = exec_command(cmd)
|
380
|
-
raise CommandError, rs.to_s if rs.command_error?
|
381
|
-
return rs
|
382
|
-
end
|
383
|
-
|
384
|
-
####### More commands ######
|
385
|
-
|
386
|
-
# record is just an enhancement of record file
|
387
|
-
# after each record, caller will get an option
|
388
|
-
# to record or accept. also this method would be
|
389
|
-
# called by max_tries argument, which can not
|
390
|
-
# be more than 255
|
391
|
-
def record(filename, format='gsm', escape_digits=nil, timeout=nil, beep=true, max_tries=255)
|
392
|
-
try_count = 0
|
393
|
-
try_again = true
|
394
|
-
|
395
|
-
while (try_again and (try_count < max_tries))
|
396
|
-
self.record_file(filename, format, escape_digits, timeout, beep)
|
397
|
-
|
398
|
-
replay = true
|
399
|
-
|
400
|
-
while replay
|
401
|
-
status = stream_file("ruby-agi/record-menu", "123#*")
|
402
|
-
$stderr.puts status.to_s
|
403
|
-
if status.result.zero?
|
404
|
-
status = self.wait_for_digit(10)
|
405
|
-
$stderr.puts status.to_s
|
406
|
-
end
|
407
|
-
if status.digit == '1' # accept record
|
408
|
-
try_again = false
|
409
|
-
replay = false
|
410
|
-
elsif status.digit == '2' # listen record
|
411
|
-
self.stream_file(filename)
|
412
|
-
replay = true
|
413
|
-
elsif status.digit == '3' # re-record
|
414
|
-
# we need to make sure to delete previously recored file
|
415
|
-
max_tries = max_tries.next
|
416
|
-
replay = false
|
417
|
-
elsif status.digit == '*' # replay menu
|
418
|
-
replay = true
|
419
|
-
elsif status.digit == '#' # quit
|
420
|
-
replay = false
|
421
|
-
try_again = false
|
422
|
-
else
|
423
|
-
end
|
424
|
-
end
|
425
|
-
end
|
426
|
-
end
|
427
|
-
|
428
|
-
|
429
|
-
def dial(telephone_number=nil, protocol=nil, username=nil, context='default', extension=nil, timeout=nil, options=nil)
|
430
|
-
dial_string = nil
|
431
|
-
|
432
|
-
if protocol == 'LOCAL'
|
433
|
-
return nil if (extension.nil? or context.nil?)
|
434
|
-
dial_string = "LOCAL/#{extension}@#{context}"
|
435
|
-
elsif protocol == 'IAX2'
|
436
|
-
return nil if (telephone_number.nil? or username.nil? or context.nil?)
|
437
|
-
telephone_number.strip!
|
438
|
-
dial_string = "IAX2/#{username}@#{context}/#{telephone_number}"
|
439
|
-
elsif protocol == 'SIP'
|
440
|
-
else
|
441
|
-
return nil
|
442
|
-
end
|
443
|
-
|
444
|
-
timeout.nil? ? dial_string += "|" : dial_string += "|#{timeout}"
|
445
|
-
options.nil? ? dial_string += "|" : dial_string += "|#{options}"
|
446
|
-
rs = exec('DIAL', dial_string)
|
447
|
-
return rs
|
448
|
-
end
|
449
|
-
|
450
|
-
###### Process methods #######
|
451
|
-
|
452
|
-
public
|
453
|
-
def raw_command(cmd)
|
454
|
-
rs = exec_command(cmd)
|
455
|
-
raise CommandError, rs.to_s if rs.command_error?
|
456
|
-
return rs
|
457
|
-
end
|
458
|
-
|
459
|
-
private
|
460
|
-
def escape(str)
|
461
|
-
rxp = Regexp.new(/\"/)
|
462
|
-
return str.gsub(rxp, '\"')
|
463
|
-
end
|
464
|
-
|
465
|
-
private
|
466
|
-
def exec_command(cmd)
|
467
|
-
rs = nil
|
468
|
-
begin
|
469
|
-
semaphore do
|
470
|
-
$stdout.puts cmd
|
471
|
-
responses = $stdin.gets
|
472
|
-
rs = ReturnStatus.new(cmd, responses)
|
473
|
-
if debug?
|
474
|
-
$stderr.puts " -- ruby-agi << #{cmd}"
|
475
|
-
$stderr.puts " -- ruby-agi >> #{rs.message}"
|
476
|
-
end
|
477
|
-
end
|
478
|
-
|
479
|
-
rescue Errno::EPIPE
|
480
|
-
raise HangupError, "Line hung up during command execution!!"
|
481
|
-
end
|
482
|
-
|
483
|
-
return rs
|
484
|
-
end
|
485
|
-
|
486
|
-
end
|
data.tar.gz.sig
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
wv(�8z��?{�1�K �M��vx��Sˤf���-M?E�_��2pt31��)1���w�ɝ�� }�ع���}��?*�@!�] i�6f��i�&�cc��@��Kl�k��+��O{�h�os�I��2����,3���d˜uM�����Y���5�(O��Rbos�ޤ����/�<����t=�x�_c�l1<��rn �B߰ӳ�]��#6��UZm]':���G���a���̂��6����$�1���6
|
metadata.gz.sig
DELETED
Binary file
|
File without changes
|
File without changes
|