1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
| #!/usr/bin/perl
###
### "PhoneBlast" calldb.pl
### Philip Rosenberg-Watt, 2011
###
use strict;
use warnings;
use Asterisk::AGI;
use File::Copy;
### Configure initial call delay and additional calls afterwards:
my $delay = 20; # How long to wait before placing first outbound call
my $callspacing = 1; # Seconds betwen calls. (Now handled by $movecallfilescmd, but kept to keep order of files)
my $callbackdelay = 60; # How long to wait vefore calling back user to verify PIN w/ double opt-in method
my $agi = new Asterisk::AGI;
my %input = $agi->ReadParse();
my $spool_dir = "/var/spool/asterisk/outgoing";
my $wav_dir = "/var/lib/asterisk/sounds/calldb/cache";
my $movecallfilescmd = "/var/lib/asterisk/agi-bin/calldb_movecallfiles.pl";
my $swiftcmd = "/usr/local/bin/swift"; # Make sure this exists; script does not check for this
my $tmp_dir = "/tmp/callfiles"; # Temporary directory to store call files before they're placed
my $maxretries = 3; # Maximum times to retry an outbound call
my $retrytime = 60; # How long to wait between retries
my $waittime = 45; # How long to wait for someone to answer phone before hanging up
my $chan = "Local";
my $dbname = $agi->get_variable("DBNAME");
my $dbdid = $agi->get_variable("DBDID");
my $dbdesc = $agi->get_variable("DBDESC");
my $cid = $input{callerid};
my $pin = $agi->get_variable("PIN");
if ($pin) { # CID will be empty if it is a PIN verification callback, therefore set $cid to retrieved variable
$cid = $agi->get_variable("CALLEDCID");
}
my $callerid = ""PhoneBlast" ";
my $dbfile = "/var/lib/asterisk/agi-bin/phonenum-db-".$dbname.".txt";
my $outfile = $wav_dir."/".$dbname."_".$cid."_outgoing";
my $tmp_outfile = "/tmp/".$dbname."_".$cid."_outgoing";
my $namefile = $wav_dir."/".$dbname."_".$cid."_name";
my $tmp_namefile = "/tmp/".$dbname."_".$cid."_name";
my $dbdescfile = $wav_dir."/".$dbname."_name";
my @db;
my %dbh;
my $count;
my $input = 0;
my $breakloop = 1;
### Create temporary dir if it doesn't exist
unless (-d $tmp_dir) {
system("mkdir $tmp_dir");
$agi->verbose("$tmp_dir does not exist, creating directory.");
}
### Create calldb cache dir if it doesn't exist
unless (-d $wav_dir) {
system("mkdir $wav_dir");
$agi->verbose("$wav_dir does not exist, creating directory.");
}
### Create group name description file if it doesn't already exist
unless (-f $dbdescfile.".wav") {
system("$swiftcmd "$dbdesc" -o $dbdescfile.wav");
$agi->verbose("$dbdescfile does not exist, creating file.");
}
&cidcheck();
### If PIN is defined we know this is a PIN verification callback
if ($pin) {
$agi->answer();
$agi->stream_file("silence/2");
$agi->stream_file("hello");
ENTERPASSWORD:
$agi->exec("Read","READPIN,calldb/please-enter-your-temporary-verification-password-now,4");
my $readpin = $agi->get_variable("READPIN");
### Add CID to DB if PIN is correct.
if ($readpin == $pin) {
&dbread();
push(@db, $cid);
&dbsave();
$agi->stream_file('silence/1');
$agi->stream_file('auth-thankyou');
$agi->stream_file('silence/1');
$agi->say_digits("$cid");
$agi->stream_file('num-was-successfully');
$agi->stream_file('added');
$agi->stream_file('silence/1');
&recname();
} else {
$agi->stream_file('silence/1');
$agi->stream_file('vm-incorrect');
&loopcheck();
$agi->stream_file('silence/1');
$agi->stream_file('wrong-try-again-smarty');
goto ENTERPASSWORD;
}
}
&dbread();
### If caller is not on list, ask if caller wants to join.
if (not exists $dbh{$cid}) {
while (!$input) {
&saywelcome();
if (!$input) { $agi->stream_file('calldb/not-on-list-would-you-like-to-join'); }
if (!$input) { $input = $agi->stream_file('1-yes-2-no', '12'); }
if (!$input) { $input = $agi->stream_file('silence/6', '12'); }
if (!$input) { &loopcheck(); }
}
$input = $input - 48;
### Give callback instructions and speak the PIN twice, then generate callback and hangup
if ($input == 1 ) {
$pin = &genpin(4);
$agi->stream_file("silence/1");
$agi->stream_file("privacy-your-callerid-is");
$agi->say_digits("$cid");
$agi->stream_file("calldb/pin-callback-instructions");
$agi->say_digits("$pin");
&gencallback();
$agi->stream_file("silence/4");
$agi->say_digits("$pin");
$agi->stream_file("silence/4");
$agi->say_digits("$pin");
&saygoodbye();
} elsif ($input == 2) {
&saygoodbye();
}
}
&saywelcome();
### The main menu of the system
MAINMENU:
### If for some reason the caller is missing a recorded name (e.g. CID was added to DB file manually), prompt caller to record name
unless (-f $namefile.".wav") {
&recname();
}
$agi->stream_file('main-menu');
$agi->stream_file('silence/1');
$input = 0;
$breakloop = 1;
### Loop main menu options with a loop check.
while (!$input) {
if(!$input) { $input = $agi->stream_file('to-compose-a-message', '12345'); }
if(!$input) { $input = $agi->stream_file('press-1', '12345'); }
if(!$input) { $input = $agi->stream_file('calldb/to-remove-yr-tn-from-list', '12345'); }
if(!$input) { $input = $agi->stream_file('press-2', '12345'); }
if(!$input) { $input = $agi->stream_file('calldb/to-rerecord-yr-name', '12345'); }
if(!$input) { $input = $agi->stream_file('press-3', '12345'); }
if(!$input) { $input = $agi->stream_file('to-hang-up', '12345'); }
if(!$input) { $input = $agi->stream_file('press-4', '12345'); }
if(!$input) { $input = $agi->stream_file('silence/6', '12345'); }
if(!$input) { &loopcheck(); }
}
$input = $input - 48;
### Caller wants to record and broadcast an outbound message
if ($input == 1) {
&dbread();
### Warn if phone list is empty
if ($count == 0) {
$agi->stream_file("silence/1");
$agi->stream_file("beeperr");
$agi->stream_file("beeperr");
$agi->stream_file("warning");
$agi->stream_file("calldb/nobody-else-on-call-list");
$agi->stream_file("nobody-but-chickens");
$agi->stream_file("please-try-again-later");
### Record the outgoing message
} else {
RECORD:
$agi->stream_file('silence/1');
$agi->stream_file('vm-rec-temp');
$agi->stream_file('beep');
$agi->record_file("$tmp_outfile","wav","#","-1",,"1","s=10");
$agi->stream_file('silence/1');
PLAYBACK:
$agi->stream_file("$tmp_outfile");
$input = 0;
$breakloop = 1;
### Press 1 to accept, 2 to listen, 3 to re-record
while (!$input) {
if (!$input) { $input = $agi->stream_file('vm-review', '123'); }
if (!$input) { $input = $agi->stream_file('silence/6', '123'); }
if (!$input) { &loopcheck(); }
}
$input = $input - 48;
### Count number of outbound calls, generate calls, and hangup
if ($input == 1) {
move("$tmp_outfile".".wav","$outfile".".wav");
&gencalls();
$agi->stream_file('silence/1');
$agi->stream_file("vm-msgsaved");
$agi->stream_file("the-num-i-have-is");
$agi->say_number("$count");
$agi->stream_file("outbound");
if ($count == 1) {
$agi->stream_file("call");
} else {
$agi->stream_file("calls");
}
$agi->stream_file("calldb/sending-now");
$agi->stream_file("auth-thankyou");
} elsif ($input == 2) {
goto PLAYBACK;
} elsif ($input == 3) {
goto RECORD;
}
}
### Remove caller from list
} elsif ($input == 2) {
&dbread();
@db = grep { $_ != $cid } @db;
&dbsave();
$agi->stream_file('silence/1');
$agi->say_digits("$cid");
$agi->stream_file('num-was-successfully');
$agi->stream_file('removed');
$agi->stream_file('silence/1');
### Re-record caller name
} elsif ($input == 3) {
&recname();
goto MAINMENU;
### Graceful hangup
} elsif ($input == 4) {
&saygoodbye();
} elsif ($input == 5) {
&dbread();
$count += 1;
$agi->stream_file("silence/1");
$agi->stream_file("the-num-i-have-is");
$agi->say_number("$count");
if ($count == 1) {
$agi->stream_file("user");
} else {
$agi->stream_file("users");
}
$agi->stream_file("vm-star-cancel");
$input = 0;
my $spoken = 0;
foreach my $phonenum (@db) {
my $namefile = $wav_dir."/".$dbname."_".$phonenum."_name";
if (-f $namefile.".wav") {
if (!$input) { $input = $agi->stream_file("$namefile", '*'); }
$spoken++;
}
}
if ($spoken != $count && !$input) {
$agi->stream_file("and");
my $unspoken = $count - $spoken;
$agi->say_number("$unspoken");
if ($unspoken == 1) {
$agi->stream_file("user");
} else {
$agi->stream_file("users");
}
$agi->stream_file("calldb/with-no-recorded-name");
}
$agi->stream_file("silence/1");
goto MAINMENU;
}
&saygoodbye();
###### Subroutines
### Read DB file into memory
sub dbread {
unless (-f $dbfile) {
system("touch $dbfile");
$agi->verbose("$dbfile does not exist, creating file.");
}
open(DB, $dbfile) || $agi->verbose('ERROR: Could not open file!');
@db=;
close(DB);
chomp(@db);
@dbh{@db}=();
$count = @db - 1;
}
### Save DB to file
sub dbsave {
use Fcntl qw(:flock :seek);
open(DB,">$dbfile") || die("Could not open file!");
flock(DB, LOCK_EX);
seek(DB, 0, SEEK_SET);
foreach my $phonenum (@db) {
print DB "$phonenumn";
}
close(DB);
}
### Greet caller with his recorded name (if available) and list name
sub saywelcome {
$agi->stream_file('silence/1');
$agi->stream_file('welcome');
if (-f $namefile.".wav") {
$agi->stream_file("$namefile");
}
$agi->stream_file("calldb/this-is-the-phoneblast-list");
$agi->stream_file("$dbdescfile");
$agi->stream_file('calldb/welcome');
$agi->stream_file('silence/1');
}
### Say goodbye and hangup
sub saygoodbye {
$agi->stream_file('silence/1');
$agi->stream_file('goodbye');
$agi->stream_file('silence/1');
$agi->hangup();
exit(0);
}
### Disconnect after a loop of 3
sub loopcheck {
$breakloop++;
if ($breakloop > 3) {
$agi->stream_file("beeperr");
$agi->stream_file("connection-timed-out");
&saygoodbye();
}
}
### Check if CallerID is valid
sub cidcheck ($) {
if ($cid =~ m/[2-9]{1}[0-9]{2}[2-9]{1}[0-9]{6}/) {
return;
} else {
$agi->stream_file('silence/1');
$agi->stream_file('beeperr');
$agi->stream_file('beeperr');
$agi->stream_file('beeperr');
$agi->stream_file('warning');
$agi->stream_file('calldb/not-10-digits-or-callerid-blocked');
$agi->stream_file("please-try-again-later");
&saygoodbye();
}
}
### Record caller's name
sub recname {
my $newrecording = 0;
if (-f $namefile.".wav") {
$agi->stream_file("silence/1");
$agi->stream_file("calldb/your-recorded-name-is");
$agi->stream_file("$namefile");
} else {
RECORD:
$agi->stream_file('silence/1');
$agi->stream_file('vm-rec-name');
$agi->stream_file('beep');
$agi->record_file("$tmp_namefile","wav","#","-1",,"1","s=3");
$newrecording = 1;
$agi->stream_file('silence/1');
$agi->stream_file("$tmp_namefile");
}
SUBMENU:
$input = 0;
$breakloop = 1;
while (!$input) {
if (!$input) { $input = $agi->stream_file('to-accept-recording', '123'); }
if (!$input) { $input = $agi->stream_file('press-1', '123'); }
if (!$input) { $input = $agi->stream_file('to-listen-to-it', '123'); }
if (!$input) { $input = $agi->stream_file('press-2', '123'); }
if (!$input) { $input = $agi->stream_file('to-rerecord-it', '123'); }
if (!$input) { $input = $agi->stream_file('press-3', '123'); }
if (!$input) { $input = $agi->stream_file('silence/6', '123'); }
if (!$input) { &loopcheck(); }
}
$input = $input - 48;
if ($input == 1) {
if ($newrecording) {
system("mv $tmp_namefile.wav $namefile.wav");
}
$agi->stream_file('silence/1');
$agi->stream_file('auth-thankyou');
$agi->stream_file('silence/1');
return;
} elsif ($input == 2) {
$agi->stream_file('silence/1');
if ($newrecording) {
$agi->stream_file("$tmp_namefile");
} else {
$agi->stream_file("$namefile");
}
goto SUBMENU;
} elsif ($input == 3) {
goto RECORD;
}
return;
}
### Generate the outbound calls
sub gencalls {
&dbread();
my $initialdelay = $delay;
@db = grep { $_ != $cid } @db;
foreach my $phonenum (@db) {
my $randomnumber = &genpin(32);
my $tmp_filename = sprintf("%s/%s-%s-%s-%s.call", $tmp_dir, $dbname, $phonenum, $cid, $randomnumber);
open(CALLFILE, ">$tmp_filename");
printf CALLFILE q{#
Channel: %s/%s@from-internal
MaxRetries: %s
RetryTime: %s
WaitTime: %s
CallerID: %s
SetVar: PLAYFILE=%s
SetVar: NAMEFILE=%s
SetVar: DBNAME=%s
SetVar: DBFILE=%s
SetVar: DBDESCFILE=%s
SetVar: KEEPCID=TRUE
Application: AGI
Data: calldb_announce.pl
}, $chan, $phonenum, $maxretries, $retrytime, $waittime, $callerid, $outfile, $namefile, $dbname, $dbfile, $dbdescfile;
close(CALLFILE);
system ("touch -d "$delay seconds" $tmp_filename");
$delay = $delay + $callspacing;
}
&bgmovefiles();
return;
}
### Generate a random PIN
sub genpin ($) {
$pin = "";
my @pinchars=('1'..'9');
foreach (1..$_[0]) {
$pin.=$pinchars[rand @pinchars];
}
return $pin;
}
### Generate PIN verification callback
sub gencallback {
$callerid = ""PhoneBlast" ";
my $tmp_filename = sprintf("%s/%s-%s-callback.call", $tmp_dir, $dbname, $cid);
open(CALLFILE, ">$tmp_filename");
printf CALLFILE q{#
Channel: %s/%s@from-internal
MaxRetries: 0
CallerID: %s
SetVar: PIN=%s
SetVar: DBNAME=%s
SetVar: DBDID=%s
SetVar: KEEPCID=TRUE
SetVar: CALLEDCID=%s
Application: AGI
Data: calldb.pl
}, $chan, $cid, $callerid, $pin, $dbname, $dbdid, $cid;
close(CALLFILE);
system ("touch -d "$callbackdelay seconds" $tmp_filename");
&bgmovefiles();
return;
}
## Background process to move files
sub bgmovefiles() {
my $pid = fork;
die "Fork failed: $!" unless defined $pid;
unless ($pid) {
use Proc::Daemon;
Proc::Daemon::Init();
system("$movecallfilescmd");
die "Exec failed: $!n";
exit(0);
}
}
exit(0);
|