-
Notifications
You must be signed in to change notification settings - Fork 9
/
agi-set_variables.agi
executable file
·396 lines (355 loc) · 11.9 KB
/
agi-set_variables.agi
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
#!/usr/bin/perl
#
# agi-set_variables.agi version 2.4
#
# runs after a call_log before call is placed out the trunk and sets all
# vicidial_list fields as variables for the call
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ; VICIDIAL_auto_dialer transfer script AMD with Load Balanced:
# exten => _91NXXNXXXXXX,1,AGI(agi://127.0.0.1:4577/call_log)
# exten => _91NXXNXXXXXX,n,AGI(agi-set_variables.agi,)
# exten => _91NXXNXXXXXX,n,Dial(${TESTSIPTRUNK}/${EXTEN:2},,tTor)
# exten => _91NXXNXXXXXX,n,Hangup
#
# Copyright (C) 2011 Matt Florell <[email protected]> LICENSE: AGPLv2
#
# changes:
# 101118-1409 - First build
# 101125-2115 - Added parsing for 3-way calls
# 110404-1604 - Added dial_timeout and cpd_amd_action variables
#
$script = 'agi-set_variables.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG)
{$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$CLI_exten = $ARGV_vars[0];
$force_playback = $ARGV_vars[1];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1; $calleridnum = $callerid;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$callerid =~ s/^\"//gi;
$callerid =~ s/\".*$//gi;
# ### set the callerid to the ACQS value(calleridname)
# print "SET CALLERID $callerid\n";
# checkresult($result);
# print STDERR "callerID changed: $callerid\n";
}
if ( (
(length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) )
) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\dW\d\d\d\d\d\d\d\d\d\dW$/) ) )
{
$callerid = $calleridname;
# ### set the callerid to the ACQS value(calleridname)
# print "SET CALLERID $callerid\n";
# checkresult($result);
# print STDERR "callerID changed: $callerid\n";
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$VDADcampaign='';
$VDADphone='';
$VDADphone_code='';
$auto_dial_timeout='60';
$cpd_amd_action='DISABLED';
$callerid =~ s/\"//gi;
$callerid =~ s/ .*//gi;
$CIDlead_id = $callerid;
if ($CIDlead_id =~ /W\d\d\d\d\d\d\d\d\d\dW$/)
{
@CIDlead_id_array = split(/W/, $CIDlead_id);
$CIDlead_id = $CIDlead_id_array[1];
}
else
{
$CIDlead_id = substr($CIDlead_id, 10, 10);
}
$CIDlead_id = ($CIDlead_id + 0);
if ( ($CLIlead_id > 0) && ($CIDlead_id < 1) ) {$CIDlead_id = $CLIlead_id;}
$VD_lead_id = $CIDlead_id;
if ($AGILOG) {$agi_string = "+++++ VARIABLE SET START : |$CIDlead_id|$now_date|$AST_ver|$priority|$calleridname|"; &agi_output;}
########## FIND vicidial_auto_calls record ##########
$stmtA = "SELECT campaign_id,phone_number FROM vicidial_auto_calls where callerid='$callerid' order by auto_call_id desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($AGILOG) {$agi_string = "$sthArows|$stmtA|"; &agi_output;}
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$campaign_id = $aryA[0];
$dialed_number = $aryA[1];
$sthA->finish();
}
$stmtA = "SELECT dial_timeout,cpd_amd_action FROM vicidial_campaigns where campaign_id='$campaign_id' limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($AGILOG) {$agi_string = "$sthArows|$stmtA|"; &agi_output;}
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$auto_dial_timeout = $aryA[0];
# only set if auto-dial call
if ($callerid =~ /^V/)
{
$cpd_amd_action = $aryA[1];
}
$sthA->finish();
}
$stmtA = "SELECT lead_id,entry_date,modify_date,status,user,vendor_lead_code,source_id,list_id,phone_number,title,first_name,middle_initial,last_name,address1,address2,address3,city,state,province,postal_code,country_code,gender,date_of_birth,alt_phone,email,security_phrase,comments,called_count,last_local_call_time,rank,owner FROM vicidial_list where lead_id='$VD_lead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$lead_id = $aryA[0];
$entry_date = $aryA[1];
$modify_date = $aryA[2];
$status = $aryA[3];
$user = $aryA[4];
$vendor_lead_code = $aryA[5];
$source_id = $aryA[6];
$list_id = $aryA[7];
$phone_number = $aryA[8];
$title = $aryA[9];
$first_name = $aryA[10];
$middle_initial = $aryA[11];
$last_name = $aryA[12];
$address1 = $aryA[13];
$address2 = $aryA[14];
$address3 = $aryA[15];
$city = $aryA[16];
$state = $aryA[17];
$province = $aryA[18];
$postal_code = $aryA[19];
$country_code = $aryA[20];
$gender = $aryA[21];
$date_of_birth = $aryA[22];
$alt_phone = $aryA[23];
$email = $aryA[24];
$security_phrase = $aryA[25];
$comments = $aryA[26];
$called_count = $aryA[27];
$last_local_call_time = $aryA[28];
$rank = $aryA[29];
$owner = $aryA[30];
}
$sthA->finish();
$lead_id =~ s/ /+/gi;
$entry_date =~ s/ /+/gi;
$modify_date =~ s/ /+/gi;
$status =~ s/ /+/gi;
$user =~ s/ /+/gi;
$vendor_lead_code =~ s/ /+/gi;
$source_id =~ s/ /+/gi;
$list_id =~ s/ /+/gi;
$phone_number =~ s/ /+/gi;
$title =~ s/ /+/gi;
$first_name =~ s/ /+/gi;
$middle_initial =~ s/ /+/gi;
$last_name =~ s/ /+/gi;
$address1 =~ s/ /+/gi;
$address2 =~ s/ /+/gi;
$address3 =~ s/ /+/gi;
$city =~ s/ /+/gi;
$state =~ s/ /+/gi;
$province =~ s/ /+/gi;
$postal_code =~ s/ /+/gi;
$country_code =~ s/ /+/gi;
$gender =~ s/ /+/gi;
$date_of_birth =~ s/ /+/gi;
$alt_phone =~ s/ /+/gi;
$email =~ s/ /+/gi;
$security_phrase =~ s/ /+/gi;
$comments =~ s/ /+/gi;
$called_count =~ s/ /+/gi;
$last_local_call_time =~ s/ /+/gi;
$rank =~ s/ /+/gi;
$owner =~ s/ /+/gi;
$campaign_id =~ s/ /+/gi;
$dialed_number =~ s/ /+/gi;
$AGI->exec("EXEC Set(_lead_id=$lead_id)");
$AGI->exec("EXEC Set(_entry_date=$entry_date)");
$AGI->exec("EXEC Set(_modify_date=$modify_date)");
$AGI->exec("EXEC Set(_status=$status)");
$AGI->exec("EXEC Set(_user=$user)");
$AGI->exec("EXEC Set(_vendor_lead_code=$vendor_lead_code)");
$AGI->exec("EXEC Set(_source_id=$source_id)");
$AGI->exec("EXEC Set(_list_id=$list_id)");
$AGI->exec("EXEC Set(_phone_number=$phone_number)");
$AGI->exec("EXEC Set(_title=$title)");
$AGI->exec("EXEC Set(_first_name=$first_name)");
$AGI->exec("EXEC Set(_middle_initial=$middle_initial)");
$AGI->exec("EXEC Set(_last_name=$last_name)");
$AGI->exec("EXEC Set(_address1=$address1)");
$AGI->exec("EXEC Set(_address2=$address2)");
$AGI->exec("EXEC Set(_address3=$address3)");
$AGI->exec("EXEC Set(_city=$city)");
$AGI->exec("EXEC Set(_state=$state)");
$AGI->exec("EXEC Set(_province=$province)");
$AGI->exec("EXEC Set(_postal_code=$postal_code)");
$AGI->exec("EXEC Set(_country_code=$country_code)");
$AGI->exec("EXEC Set(_gender=$gender)");
$AGI->exec("EXEC Set(_date_of_birth=$date_of_birth)");
$AGI->exec("EXEC Set(_alt_phone=$alt_phone)");
$AGI->exec("EXEC Set(_email=$email)");
$AGI->exec("EXEC Set(_security_phrase=$security_phrase)");
$AGI->exec("EXEC Set(_comments=$comments)");
$AGI->exec("EXEC Set(_called_count=$called_count)");
$AGI->exec("EXEC Set(_last_local_call_time=$last_local_call_time)");
$AGI->exec("EXEC Set(_rank=$rank)");
$AGI->exec("EXEC Set(_owner=$owner)");
$AGI->exec("EXEC Set(_campaign_id=$campaign_id)");
$AGI->exec("EXEC Set(_dialed_number=$dialed_number)");
$AGI->exec("EXEC Set(_auto_dial_timeout=$auto_dial_timeout)");
$AGI->exec("EXEC Set(_cpd_amd_action=$cpd_amd_action)");
$dbhA->disconnect();
if ($AGILOG) {$agi_string = "+++++ VARIABLE SET DONE EXITING : |$CIDlead_id|$now_date|$calleridname|$phone_number|"; &agi_output;}
exit;
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}