#!/usr/bin/perl # # call_inbound.agi # # runs at the beginning and ending of every inbound call received # # You need to put lines similar to those below in your extensions.conf file: # # parameters for call_inbound.agi (7 fields separated by five dashes "-----"): # 1. the extension of the phone to ring as defined in the asterisk.phones table # 2. the phone number that was called, for the live_inbound/_log entry # 3. a text description of the number that was called in # 4-7. optional fields, they are also passed as fields in the GUI to web browser # # ;inbound calls: #exten => _*NXXNXXXXXX*3429,3,AGI(call_inbound.agi,spa2000-----8005553429-----Inbound 800-----x-----y-----z-----w) # # # # Copyright (C) 2006 Matt Florell LICENSE: GPLv2 # $V = 1; # set to 1 for verbose mode $M = 1; # set to 1 for 2 line messages mode ### Make sure this file is in a path or put the absolute path to it require("/home/cron/AST_SERVER_conf.pl"); # local configuration file $US='_'; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year = ($year + 1900); $Y = substr($year, 0, 1); $mon++; if ($mon < 10) {$mon = "0$mon";} if ($mday < 10) {$mday = "0$mday";} if ($hour < 10) {$Fhour = "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"; $filedate = "$US$Y$mon$mday$hour$min$sec$US"; ### begin parsing run-time options ### if (length($ARGV[0])>1) { if ($V) {print STDERR "Perl Environment Dump:\n";} $i=0; while ($#ARGV >= $i) { $args = "$args $ARGV[$i]"; if ($V) {print STDERR "$i|$ARGV[$i]|\n";} $i++; } ### list of command-line array arguments: @ARGV_vars = split(/-----/, $ARGV[0]); $phone_ext = $ARGV_vars[0]; $inbound_number = $ARGV_vars[1]; $comment_a = $ARGV_vars[2]; $comment_b = $ARGV_vars[3]; $comment_c = $ARGV_vars[4]; $comment_d = $ARGV_vars[5]; $comment_e = $ARGV_vars[6]; if ($args =~ /--help/i) { print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n"; } else { if ($args =~ /-V/i) { $V=1; } if ($args =~ /-debug/i) { $DG=1; } if ($args =~ /-dbAVS/i) { $DGA=1; } if ($args =~ /-q/i) { $q=1; $Q=1; } if ($args =~ /-t/i) { $TEST=1; $T=1; } } } else { if ($V) {print "no command line options set\n";} } $|=1; while() { chomp; last unless length($_); if ($V) { if (/^agi_(\w+)\:\s+(.*)$/) { $AGI{$1} = $2; } } if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $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;} if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;} if ( $calleridname =~ /\"/) {$calleridname =~ s/\"//gi;} if ( (length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) ) ) {$callerid = $calleridname;} ### allow for ANI being sent with the DNIS "*3125551212*9999*" if ($extension =~ /^\*\d\d\d\d\d\d\d\d\d\d\*/) { $callerid = $extension; $callerid =~ s/\*\d\d\d\d\*$//gi; $callerid =~ s/\*\d\d\d\d$//gi; $callerid =~ s/^\*//gi; $extension =~ s/^\*\d\d\d\d\d\d\d\d\d\d\*//gi; $extension =~ s/\*$//gi; ### set the callerid if sent with DNIS print "SET CALLERID $callerid\n"; checkresult($result); } } if ($V) { if ($V) {print STDERR "AGI Environment Dump:\n";} foreach $i (sort keys %AGI) { if ($V) {print STDERR " -- $i = $AGI{$i}\n";} } } $callerid = "\"$calleridname\" <$callerid>"; if ($V) {print STDERR "AGI Environment Dump: |$unique_id|$channel|$extension|$type|$callerid|\n";} if ($extension =~ /h/i) {$stage = 'END';} else {$stage = 'START';} ### call start stage if ($stage =~ /START/) { if ($V) {print STDERR "\nCALL INBOUND STARTED\n";} if ($M) {print STDERR "+++++ CALL INBOUND START : |$unique_id|$channel|$extension|$type|$callerid|$now_date\n";} # if ($channel =~ /^SIP/) {$channel =~ s/-.*//gi;} # if ($channel =~ /^Zap\//) {$channel =~ s/-\d$//gi;} use Net::MySQL; if (!$DB_port) {$DB_port='3306';} my $mysql = Net::MySQL->new(hostname=>"$DB_server", database=>"$DB_database", user=>"$DB_user", password=>"$DB_pass", port => "$DB_port") or die "Couldn't connect to database: $DB_server - $DB_database\n"; $stmtA = "INSERT INTO live_inbound (uniqueid,channel,server_ip,caller_id,extension,phone_ext,start_time,inbound_number,comment_a,comment_b,comment_c,comment_d,comment_e) values('$unique_id','$channel','$server_ip','$callerid','$extension','$phone_ext','$now_date','$inbound_number','$comment_a','$comment_b','$comment_c','$comment_d','$comment_e')"; if ($V) {print STDERR "\n|$stmtA|\n";} $mysql->query($stmtA) or die "Couldn't execute query:\n"; $stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,phone_ext,start_time,inbound_number,comment_a,comment_b,comment_c,comment_d,comment_e) values('$unique_id','$channel','$server_ip','$callerid','$extension','$phone_ext','$now_date','$inbound_number','$comment_a','$comment_b','$comment_c','$comment_d','$comment_e')"; if ($V) {print STDERR "\n|$stmtA|\n";} $mysql->query($stmtA) or die "Couldn't execute query:\n"; $mysql->close; } ### call end stage else { if ($V) {print STDERR "\nCALL INBOUND HUNG UP\n";} use Net::MySQL; if (!$DB_port) {$DB_port='3306';} my $dbh = Net::MySQL->new(hostname=>"$DB_server", database=>"$DB_database", user=>"$DB_user", password=>"$DB_pass", port => "$DB_port") or die "Couldn't connect to database: $DB_server - $DB_database\n"; if ($V) {print STDERR "\nCONNECTED: deleting live_inbound record for $unique_id on $server_ip\n";} $stmtA = "DELETE from live_inbound where uniqueid='$unique_id' and server_ip='$server_ip'"; $dbh->query($stmtA) or die "Couldn't execute query:\n"; $dbh->close; if ($M) {print STDERR "+++++ CALL INBOUND HUNGUP: |$unique_id|\n";} } if ($V) {print STDERR "\nAll Done Call Inbound Exiting:\n";} 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++; } }