PERL-ZAPOUTGOING-AGI

#!/usr/bin/perl
// zap call outging by imran ( Pakistan )

use Asterisk::AGI;
use lib "/var/www/cgi-bin/modules";
use Db;

$AGI = new Asterisk::AGI;
$|=1;
my %input = $AGI->ReadParse();
($calleridnum, $phoneno, $quiet) = @ARGV;

$phoneno =~ s/^9//gsi;

my @Result=Db->ExecuteQuery("select co_no,outgoing_prefix  from codetail");
my %CoPrHash;
my $ZapStr;
my $g_Cents;
my $d_path	 = "/var/www/html/recordings";
my $gsm_file;
my $wav_file;
my $CALLSTATUS;
my $callstart;
my $answeredtime;
my %g_DATA;
my $AvailChan;
my $Co;
my $prefix;

 // NOTE DIAL STRING SHOULD BE AS >>>>>/zap/1&Zap/2&Zap
	
	for($i=1;$i<=$#Result;$i++)
	{	
		$Co						=$Result[$i][0];
		$prefix				=$Result[$i][1];
		
		$CoPrHash{$Co} = $prefix;		
		
		$ZapStr .=	"Zap/$Co";
		if($i != $#Result)
		{
			$ZapStr .= "&"	;
		}
		
	}
	
$res = $AGI->exec("ChanIsAvail $ZapStr");

$AvailChan 		= $AGI->get_variable("AVAILCHAN");


if($AvailChan !~ /Zap/gsi)
{
		$AGI->stream_file('all-circuits-busy-now');	
		exit;
}

my ($OrderId,$callrate) = setup_billing($calleridnum);
if ( $OrderId eq '-1')
{
		#No Talk time balance in ur account
		$AGI->stream_file('not-enough-credit'); #disconnected
		$AGI->hangup();
		exit;
}


$AvailChan =~s/-(.*)$//gsi;

$dialstr = "$AvailChan/" . $CoPrHash{$LineNo} . $phoneno ;


#.... gloable variable
my $strSQL	= "
				select 
				call_recording,
				local_calling,
				international_calling,
				international_calling_ww35,
				international_calling_all,
				call_forwarding,
				
				extension_busy,
				extension_answered,
				extension_logged
				 
				from buddies_relation
				where name=\"$calleridnum\"
				";
my @Result	= Db->ExecuteQuery($strSQL);

$g_DATA{CALLRECORDING} = $Result[1][0];
$g_DATA{LOCALCALLING}  = $Result[1][1];
$g_DATA{INTCALL}			 = $Result[1][2];
$g_DATA{INTCALLWW35} 	 = $Result[1][3];
$g_DATA{INTCALLALL}    = $Result[1][4];
$g_DATA{CALLFORWARDING}= $Result[1][5];
$g_DATA{EXTBUAY}		   = $Result[1][6];
$g_DATA{EXTANSWER}	   = $Result[1][7];
$g_DATA{EXTLOG}				 = $Result[1][8];

my @r	= Db->ExecuteQuery("select min(name), max(name) from sip_buddies");
$g_DATA{MINEXT}				 = $r[1][0];
$g_DATA{MAXEXT}				 = $r[1][1];

				
			if( ( $g_DATA{LOCALCALLING} eq 'Y' )  &&  not( $g_DATA{MINEXT} <= $phoneno && $g_DATA{MAXEXT} >= $phoneno ) )#local calling
			{
						if ( $g_DATA{CALLRECORDING} eq 'Y' )
						{
								callrecording($phoneno);		
						}

						# execute local call
						$res = $AGI->exec("DIAL $dialstr");
						
						$CALLSTATUS = $AGI->get_variable(DIALSTATUS);
						
						if ( not( $CALLSTATUS eq 'ANSWER' || $CALLSTATUS eq 'ANSWERED' ) )
						{
							# the number u have dialed is not responding please call later
							$AGI->stream_file('pls-try-call-later');	
							
						}	
						else
						{
								$answeredtime = $AGI->get_variable(ANSWEREDTIME);
								#savecdr($calleridnum, $phoneno, "", $CALLSTATUS, $answeredtime, "",$callstart,"");
	 							#CutBalance($OrderId,$callrate,$answeredtime);
						}

						if ( $g_DATA{CALLRECORDING} eq 'Y' )
						{
							my $C 	= $AGI->get_variable(CHANNEL);
							my $t = $AGI->exec("StopMonitor $C");
							$AGI->hangup();
							sleep(2);
							system("sox $d_path/$gsm_file $d_path/$wav_file");
						}		
			}
			else
			{
					# sorry you r not authoriezed to have a local call
					$AGI->stream_file('not-auth-pstn');	
					$AGI->hangup();
			}

exit;
sub callrecording
{
		my ($phoneno) =@_;
		my $CalledNumber = $phoneno;
		my $CurDate;
		
			$callstart= currentDbDate();	
			$CurDate 	= $AGI->get_variable("DATETIME");
			$CurDate 	=~ s/://gsi;
			$CurDate	=substr($CurDate,4,4) . substr($CurDate,2,2) .substr($CurDate,0,2) .substr($CurDate,8,7);
			$gsm_file = "Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate.gsm";
			$wav_file = "Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate.WAV";
			$res = $AGI->exec("Monitor gsm|$d_path/Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate|m");
			#system("sox $d_path/$wav_file -r 8000 $d_path/$gsm_file resample -ql");
			#$AGI->verbose("imran sox  = ( sox $d_path/$wav_file -r 8000 $d_path/$gsm_file )\n",3);
} 

sub currentDbDate
{
	
		my $strSQL	= "
				select curdate()

					";
					
	my @Result	= Db->ExecuteQuery($strSQL);
	return $Result[1][0];
}

sub myVerbose
{
	my ($pStr)=@_;
	
	return;
	$pStr =~ s/\n/ /ige;
	
	$AGI->verbose("$pStr\n", 3);

}

sub savecdr() 
{
	my ($callerid, $callednum, $trunk, $disposition, $billseconds, $billcost,$callstart,$resellerrate) = @_;
	my $strSQL	= "	
	INSERT INTO cdrs (callerid,callednum,trunk,disposition,billseconds,billcost,callstart,resellerrate) VALUES 
	(
	\"$callerid\",
	\"$callednum\",
	\"$trunk\",
	\"$disposition\",
	\"$billseconds\",
	\"$billcost\",
	\"$callstart\",
	\"$resellerrate\"
	)
	";	
	myVerbose($strSQL);
	
	my @Result	= Db->ExecuteQuery($strSQL);
	
}

sub setup_billing
{
		my ( $Exten ) =@_;

		my @Result	= Db->ExecuteQuery("select id,talktime from buddies_relation where name='$Exten'");
		my $Cents		= $Result[1][1];
		$g_Cents    = $Result[1][1];
		if($Cents<=0)
		{
			$Cents=0;
			return ('-1','-1');
		}

		my $bal=$Cents/100;
		my $DollarBal			=sprintf("%d",$bal);
		my $CentsBal			=sprintf("%d",(sprintf("%d",$bal*100)%100));
		my $Totalcents 		=$DollarBal*100 +  $CentsBal;
		
		my %Hash;
		
		$Hash{Rates} =0.002;
		
		my $totalCents	=$Totalcents ;
		my $callrate		=sprintf("%d",$Hash{Rates}*100);
		my $totalMinutes=sprintf("%d",$totalCents/$callrate);                   
		my $totalSeconds=sprintf("%d",((($totalCents%$callrate)/$callrate)*60));
		
		
		return ($Result[1][0],$callrate);
}

sub CutBalance
{
	
		my ($id,$Rate,$CallSec) = @_;
		my $min=sprintf("%d",$CallSec/60);
		my $Sec=sprintf("%d",$CallSec%60);		
		if($Sec>0)
		{
		$min++;
		}
		
		my $Cut=	$min*$Rate;
			
		my $amount = $g_Cents - $Cut;
		
		my $strSQL	= "Update buddies_relation set talktime =\"$amount\" where id=\"$id\" ";
			
		Db->ExecuteQuery($strSQL);
}

#!/usr/bin/perl
// zap call outging by imran ( Pakistan )

use Asterisk::AGI;
use lib "/var/www/cgi-bin/modules";
use Db;

$AGI = new Asterisk::AGI;
$|=1;
my %input = $AGI->ReadParse();
($calleridnum, $phoneno, $quiet) = @ARGV;

$phoneno =~ s/^9//gsi;

my @Result=Db->ExecuteQuery("select co_no,outgoing_prefix  from codetail");
my %CoPrHash;
my $ZapStr;
my $g_Cents;
my $d_path	 = "/var/www/html/recordings";
my $gsm_file;
my $wav_file;
my $CALLSTATUS;
my $callstart;
my $answeredtime;
my %g_DATA;
my $AvailChan;
my $Co;
my $prefix;

 // NOTE DIAL STRING SHOULD BE AS >>>>>/zap/1&Zap/2&Zap
	
	for($i=1;$i<=$#Result;$i++)
	{	
		$Co						=$Result[$i][0];
		$prefix				=$Result[$i][1];
		
		$CoPrHash{$Co} = $prefix;		
		
		$ZapStr .=	"Zap/$Co";
		if($i != $#Result)
		{
			$ZapStr .= "&"	;
		}
		
	}
	
$res = $AGI->exec("ChanIsAvail $ZapStr");

$AvailChan 		= $AGI->get_variable("AVAILCHAN");


if($AvailChan !~ /Zap/gsi)
{
		$AGI->stream_file('all-circuits-busy-now');	
		exit;
}

my ($OrderId,$callrate) = setup_billing($calleridnum);
if ( $OrderId eq '-1')
{
		#No Talk time balance in ur account
		$AGI->stream_file('not-enough-credit'); #disconnected
		$AGI->hangup();
		exit;
}


$AvailChan =~s/-(.*)$//gsi;

$dialstr = "$AvailChan/" . $CoPrHash{$LineNo} . $phoneno ;


#.... gloable variable
my $strSQL	= "
				select 
				call_recording,
				local_calling,
				international_calling,
				international_calling_ww35,
				international_calling_all,
				call_forwarding,
				
				extension_busy,
				extension_answered,
				extension_logged
				 
				from buddies_relation
				where name=\"$calleridnum\"
				";
my @Result	= Db->ExecuteQuery($strSQL);

$g_DATA{CALLRECORDING} = $Result[1][0];
$g_DATA{LOCALCALLING}  = $Result[1][1];
$g_DATA{INTCALL}			 = $Result[1][2];
$g_DATA{INTCALLWW35} 	 = $Result[1][3];
$g_DATA{INTCALLALL}    = $Result[1][4];
$g_DATA{CALLFORWARDING}= $Result[1][5];
$g_DATA{EXTBUAY}		   = $Result[1][6];
$g_DATA{EXTANSWER}	   = $Result[1][7];
$g_DATA{EXTLOG}				 = $Result[1][8];

my @r	= Db->ExecuteQuery("select min(name), max(name) from sip_buddies");
$g_DATA{MINEXT}				 = $r[1][0];
$g_DATA{MAXEXT}				 = $r[1][1];

				
			if( ( $g_DATA{LOCALCALLING} eq 'Y' )  &&  not( $g_DATA{MINEXT} <= $phoneno && $g_DATA{MAXEXT} >= $phoneno ) )#local calling
			{
						if ( $g_DATA{CALLRECORDING} eq 'Y' )
						{
								callrecording($phoneno);		
						}

						# execute local call
						$res = $AGI->exec("DIAL $dialstr");
						
						$CALLSTATUS = $AGI->get_variable(DIALSTATUS);
						
						if ( not( $CALLSTATUS eq 'ANSWER' || $CALLSTATUS eq 'ANSWERED' ) )
						{
							# the number u have dialed is not responding please call later
							$AGI->stream_file('pls-try-call-later');	
							
						}	
						else
						{
								$answeredtime = $AGI->get_variable(ANSWEREDTIME);
								#savecdr($calleridnum, $phoneno, "", $CALLSTATUS, $answeredtime, "",$callstart,"");
	 							#CutBalance($OrderId,$callrate,$answeredtime);
						}

						if ( $g_DATA{CALLRECORDING} eq 'Y' )
						{
							my $C 	= $AGI->get_variable(CHANNEL);
							my $t = $AGI->exec("StopMonitor $C");
							$AGI->hangup();
							sleep(2);
							system("sox $d_path/$gsm_file $d_path/$wav_file");
						}		
			}
			else
			{
					# sorry you r not authoriezed to have a local call
					$AGI->stream_file('not-auth-pstn');	
					$AGI->hangup();
			}

exit;
sub callrecording
{
		my ($phoneno) =@_;
		my $CalledNumber = $phoneno;
		my $CurDate;
		
			$callstart= currentDbDate();	
			$CurDate 	= $AGI->get_variable("DATETIME");
			$CurDate 	=~ s/://gsi;
			$CurDate	=substr($CurDate,4,4) . substr($CurDate,2,2) .substr($CurDate,0,2) .substr($CurDate,8,7);
			$gsm_file = "Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate.gsm";
			$wav_file = "Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate.WAV";
			$res = $AGI->exec("Monitor gsm|$d_path/Callfrom-$calleridnum-CallTo-$CalledNumber-at-$CurDate|m");
			#system("sox $d_path/$wav_file -r 8000 $d_path/$gsm_file resample -ql");
			#$AGI->verbose("imran sox  = ( sox $d_path/$wav_file -r 8000 $d_path/$gsm_file )\n",3);
} 

sub currentDbDate
{
	
		my $strSQL	= "
				select curdate()

					";
					
	my @Result	= Db->ExecuteQuery($strSQL);
	return $Result[1][0];
}

sub myVerbose
{
	my ($pStr)=@_;
	
	return;
	$pStr =~ s/\n/ /ige;
	
	$AGI->verbose("$pStr\n", 3);

}

sub savecdr() 
{
	my ($callerid, $callednum, $trunk, $disposition, $billseconds, $billcost,$callstart,$resellerrate) = @_;
	my $strSQL	= "	
	INSERT INTO cdrs (callerid,callednum,trunk,disposition,billseconds,billcost,callstart,resellerrate) VALUES 
	(
	\"$callerid\",
	\"$callednum\",
	\"$trunk\",
	\"$disposition\",
	\"$billseconds\",
	\"$billcost\",
	\"$callstart\",
	\"$resellerrate\"
	)
	";	
	myVerbose($strSQL);
	
	my @Result	= Db->ExecuteQuery($strSQL);
	
}

sub setup_billing
{
		my ( $Exten ) =@_;

		my @Result	= Db->ExecuteQuery("select id,talktime from buddies_relation where name='$Exten'");
		my $Cents		= $Result[1][1];
		$g_Cents    = $Result[1][1];
		if($Cents<=0)
		{
			$Cents=0;
			return ('-1','-1');
		}

		my $bal=$Cents/100;
		my $DollarBal			=sprintf("%d",$bal);
		my $CentsBal			=sprintf("%d",(sprintf("%d",$bal*100)%100));
		my $Totalcents 		=$DollarBal*100 +  $CentsBal;
		
		my %Hash;
		
		$Hash{Rates} =0.002;
		
		my $totalCents	=$Totalcents ;
		my $callrate		=sprintf("%d",$Hash{Rates}*100);
		my $totalMinutes=sprintf("%d",$totalCents/$callrate);                   
		my $totalSeconds=sprintf("%d",((($totalCents%$callrate)/$callrate)*60));
		
		
		return ($Result[1][0],$callrate);
}

sub CutBalance
{
	
		my ($id,$Rate,$CallSec) = @_;
		my $min=sprintf("%d",$CallSec/60);
		my $Sec=sprintf("%d",$CallSec%60);		
		if($Sec>0)
		{
		$min++;
		}
		
		my $Cut=	$min*$Rate;
			
		my $amount = $g_Cents - $Cut;
		
		my $strSQL	= "Update buddies_relation set talktime =\"$amount\" where id=\"$id\" ";
			
		Db->ExecuteQuery($strSQL);
}

Created by: ddx, Last modification: Mon 23 of Apr, 2012 (23:39 UTC) by admin
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+