diff --git a/agi/cm_lookup.agi b/agi/cm_lookup.agi new file mode 100644 index 00000000..f22f2141 --- /dev/null +++ b/agi/cm_lookup.agi @@ -0,0 +1,581 @@ +#!/usr/bin/perl +# +# cm_lookup.agi version 2.4 +# +# for use with a Call Menu in the custom dialplan section. +# +# ; settings for this script: +# ; 1. The vicidial_list field to do a lookup on ('vendor_lead_code' is default) +# ; 2. Type of lookup to do: (default is SYSTEM) +# ; SYSTEM will search the entire system +# ; CAMPAIGNLISTS will search inside all of the active lists within the campaign +# ; CAMPLISTSALL will search inside all of the active and inactive lists within the campaign +# ; LIST will search only within the specified List ID +# ; 3. If CAMPAIGN or LIST is used above, this is the campaign_id or list_id to be used +# ; 4. TTS confirmation of name (1 for yes, 2 for no) put in TTS prompt ID +# +# Found entries will send the call to the 'A' option in the source call menu +# Not-Found entries will send the call to the 'B' option in the source call menu +# +#; example with 8-digit vendor lead codes +# exten => _XXXXXXXX,1,AGI(cm_lookup.agi,vendor_lead_code---CAMPLISTSALL---TESTCAMP---lastname_oneyestwono) +# +# Copyright (C) 2011 Matt Florell LICENSE: AGPLv2 +# +# changes: +# 111004-1450 - First Build +# + +$script = 'cm_lookup.agi'; +$S='*'; +$US='_'; +$vicidial_list_fields = '|lead_id|vendor_lead_code|source_id|list_id|gmt_offset_now|called_since_last_reset|phone_code|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|status|entry_date|entry_list_id|modify_date|user|'; + + +($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"; +$CLInow_date = "$year-$mon-$mday\\ $hour:$min:$sec"; +$start_time=$now_date; +$CIDdate = "$mon$mday$hour$min$sec"; +$tsSQLdate = "$year$mon$mday$hour$min$sec"; +$SQLdate = "$year-$mon-$mday $hour:$min:$sec"; +$SQLdateBEGIN = $SQLdate; + while (length($CIDdate) > 9) {$CIDdate =~ s/^.//gi;} # 0902235959 changed to 902235959 + +# default path to astguiclient configuration file: +$PATHconf = '/etc/astguiclient.conf'; + +open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n"; +@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";} +if (!$CEPLOGfile) {$CEPLOGfile = "$PATHlogs/Cepstral.$year-$mon-$mday";} + +use DBI; +use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second +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,local_gmt 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]; + $local_gmt = $aryA[1]; + 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]); + + $field = $ARGV_vars[0]; + $search_method = $ARGV_vars[1]; + $search_id = $ARGV_vars[2]; + $tts_prompt = $ARGV_vars[3]; + } +if ( (length($field) < 2) || ($vicidial_list_fields !~ /\|$field\|/) ) + {$field = 'vendor_lead_code';} +if (length($search_method) < 1) + {$search_method = 'SYSTEM';} +if (length($search_id) < 1) + {$search_method = 'SYSTEM';} +### END parsing run-time options ### + + +$|=1; +while() + { + chomp; + last unless length($_); + if ($AGILOG) + { + if (/^agi_(\w+)\:\s+(.*)$/) + { + $AGI{$1} = $2; + } + } + if (/^agi_context\:\s+(.*)$/) {$context = $1;} + if (/^agi_priority\:\s+(.*)$/) {$priority = $1;} + if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;} + 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 ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) ) + { + $calleridname = $callerid; + $calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi; + $calleridname =~ s/\"|\" //gi; + } + +$callerid =~ s/\D|\'//gi; +$calleridname =~ s/unknown|\'//gi; +if ( (!$callerid) or ($callerid =~ /unknown/) ) + {$callerid = $calleridname;} + + +$listSQL = ''; +if ($search_method =~ /LIST$/) + {$listSQL = "and list_id='$search_id'";} +if ($search_method =~ /CAMPAIGNLISTS$|CAMPLISTSALL$/) + { + $activeSQL=''; + if ($search_method =~ /CAMPAIGNLISTS$/) + {$activeSQL = "where active='Y'";} + ### Grab all active lists associated with campaign_id + $cbc=0; + $SlistsSQL=''; + $stmtA = "SELECT list_id FROM vicidial_lists where campaign_id='$search_id' $activeSQL limit 1000;"; + $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; + $sthArows=$sthA->rows; + while ($sthArows > $cbc) + { + @aryA = $sthA->fetchrow_array; + $SlistsSQL .= "'$aryA[0]',"; + $cbc++; + } + $sthA->finish(); + chop($SlistsSQL); + + if (length($SlistsSQL)>3) + {$listSQL = "and list_id IN($SlistsSQL)";} + } + +if ($AGILOG) {$agi_string = "searching for account by $field|$search_method|$search_id|$extension"; &agi_output;} + +$found_extension='B'; +$found_priority='1'; + +$stmtA= "SELECT lead_id,phone_number,first_name,last_name from vicidial_list where $field='$extension' $listSQL order by last_local_call_time desc limit 1;"; +$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]; + $phone_number = $aryA[1]; + $first_name = $aryA[2]; + $last_name = $aryA[3]; + $sthA->finish(); + + $found_extension='A'; + $found_priority='1'; + + + ### BEGIN if TTS prompt is defined, generate it and wait for "1 for yes or 2 for no" response + ### if no response or no match then continue on as if nothing played + if (length($tts_prompt)>1) + { + if ($AGILOG) {$agi_string = "-- TTS : |$tts_prompt|"; &agi_output;} + + $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='$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; + $TTS_lead_id = $aryA[0]; + $TTS_entry_date = $aryA[1]; + $TTS_modify_date = $aryA[2]; + $TTS_status = $aryA[3]; + $TTS_user = $aryA[4]; + $TTS_vendor_lead_code = $aryA[5]; + $TTS_source_id = $aryA[6]; + $TTS_list_id = $aryA[7]; + $TTS_phone_number = $aryA[8]; + $TTS_title = $aryA[9]; + $TTS_first_name = $aryA[10]; + $TTS_middle_initial = $aryA[11]; + $TTS_last_name = $aryA[12]; + $TTS_address1 = $aryA[13]; + $TTS_address2 = $aryA[14]; + $TTS_address3 = $aryA[15]; + $TTS_city = $aryA[16]; + $TTS_state = $aryA[17]; + $TTS_province = $aryA[18]; + $TTS_postal_code = $aryA[19]; + $TTS_country_code = $aryA[20]; + $TTS_gender = $aryA[21]; + $TTS_date_of_birth = $aryA[22]; + $TTS_alt_phone = $aryA[23]; + $TTS_email = $aryA[24]; + $TTS_security_phrase = $aryA[25]; + $TTS_comments = $aryA[26]; + $TTS_called_count = $aryA[27]; + $TTS_last_local_call_time = $aryA[28]; + $TTS_rank = $aryA[29]; + $TTS_owner = $aryA[30]; + } + $sthA->finish(); + + $stmtA = "SELECT tts_text,tts_voice FROM vicidial_tts_prompts where tts_id='$tts_prompt';"; + $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; + $TTS_voice = $aryA[1]; + $TTS_text = $aryA[0]; + ### BEGIN replace variables with record values ### + $TTS_text =~ s/--A--lead_id--B--/$TTS_lead_id/gi; + $TTS_text =~ s/--A--entry_date--B--/$TTS_entry_date/gi; + $TTS_text =~ s/--A--modify_date--B--/$TTS_modify_date/gi; + $TTS_text =~ s/--A--status--B--/$TTS_status/gi; + $TTS_text =~ s/--A--user--B--/$TTS_user/gi; + $TTS_text =~ s/--A--vendor_lead_code--B--/$TTS_vendor_lead_code/gi; + $TTS_text =~ s/--A--source_id--B--/$TTS_source_id/gi; + $TTS_text =~ s/--A--list_id--B--/$TTS_list_id/gi; + $TTS_text =~ s/--A--phone_number--B--/$TTS_phone_number/gi; + $TTS_text =~ s/--A--title--B--/$TTS_title/gi; + $TTS_text =~ s/--A--first_name--B--/$TTS_first_name/gi; + $TTS_text =~ s/--A--middle_initial--B--/$TTS_middle_initial/gi; + $TTS_text =~ s/--A--last_name--B--/$TTS_last_name/gi; + $TTS_text =~ s/--A--address1--B--/$TTS_address1/gi; + $TTS_text =~ s/--A--address2--B--/$TTS_address2/gi; + $TTS_text =~ s/--A--address3--B--/$TTS_address3/gi; + $TTS_text =~ s/--A--city--B--/$TTS_city/gi; + $TTS_text =~ s/--A--state--B--/$TTS_state/gi; + $TTS_text =~ s/--A--province--B--/$TTS_province/gi; + $TTS_text =~ s/--A--postal_code--B--/$TTS_postal_code/gi; + $TTS_text =~ s/--A--country_code--B--/$TTS_country_code/gi; + $TTS_text =~ s/--A--gender--B--/$TTS_gender/gi; + $TTS_text =~ s/--A--date_of_birth--B--/$TTS_date_of_birth/gi; + $TTS_text =~ s/--A--alt_phone--B--/$TTS_alt_phone/gi; + $TTS_text =~ s/--A--email--B--/$TTS_email/gi; + $TTS_text =~ s/--A--security_phrase--B--/$TTS_security_phrase/gi; + $TTS_text =~ s/--A--comments--B--/$TTS_comments/gi; + $TTS_text =~ s/--A--called_count--B--/$TTS_called_count/gi; + $TTS_text =~ s/--A--last_local_call_time--B--/$TTS_last_local_call_time/gi; + $TTS_text =~ s/--A--rank--B--/$TTS_rank/gi; + $TTS_text =~ s/--A--owner--B--/$TTS_owner/gi; + ### END replace variables with record values ### + + ### BEGIN check for --C-- and --D-- tags for static file replacement + if ( ($TTS_text =~ /--C--/) || ($TTS_text =~ /--D--/) ) + { + $Cdash='--C--'; + $Ddash='--D--'; + @static_check = split(/--C--/,$TTS_text); + $sc=0; + $sr=0; + foreach(@static_check) + { + if ($sc > 0) + { + $static_check[$sc] =~ s/--D-.*//gi; + $static_check_lc = lc($static_check[$sc]); + print STDERR " Checking For Static Replacement: $static_check[$sc]|$static_check_lc$wav\n"; + $static_found=0; + if (-e "/var/lib/asterisk/sounds/tts_static/$static_check_lc$wav") + { + $CDstatic = "