PERL-SIPCALLOUT-AGI

#!/usr/bin/perl
// SipDialOUT.agi by imran 
use Asterisk::AGI;
use lib "/var/lib/asterisk/agi-bin";
use Db;

$AGI = new Asterisk::AGI;
$|=1;

my %input = $AGI->ReadParse();
$Digit	=$ARGV[0];
$CLI	=$ARGV[1];
#$Zap="Zap/18-12312";

my %CDR_HASH;
my $gsm_file="";
my $wav_file="";
my $d_path  ="/recordings";

$CDR_HASH{CALLERID}	 =$CLI;
$CDR_HASH{CALLRECORDINGFILE} ='';      

MyVerbose("Call is being originated from CLI: $CLI");

if($CLI eq "")
{
	MyVerbose("User not recognized on channel bank CLI : $CLI");	
	exitWithCongestion();
}


if($Digit==9)
{

		$DialedNo= $AGI->get_data('dialtone',6000,1);
		MyVerbose("\$DialedNo : $DialedNo");
		
		$DialedNo1= $AGI->get_data('silence',5000,3);
		MyVerbose("\$DialedNo1 : $DialedNo1");
		
		
		$TimeOut =5000;
		$Digit  	=3;
		if($DialedNo1 =~/^3/gsi)
		{
			$TimeOut=10000;	
			$Digit  	=7;
		}
		elsif($DialedNo ==1)
		{
			$TimeOut=8000;	
			$Digit  	=5;
		}
		elsif($DialedNo ==0)
		{
			$TimeOut=11000;	
			$Digit  	=10;
		}
		
		$DialedNo2= $AGI->get_data('silence',$TimeOut,$Digit);
		MyVerbose("\$DialedNo2 : $DialedNo2");
		
		$DialedNo1.=$DialedNo2;
		
		$DialedNoToValid=$DialedNo . $DialedNo1;
		$DialedNo ="" . $DialedNo . $DialedNo1;
		MyVerbose("\$DialedNo : $DialedNo");
		
		my $dialstr;
		$dialstr = "Zap/g1/" . $DialedNo . "|1000|tTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
		MyVerbose("\$dialstr : $dialstr");
		
		if(!IsInteger($DialedNo))
		{
			exitWithCongestion();
		}
		
		%Rights	=		GetLineRights($CLI);
		%Type=GetDialedNumberType("$DialedNoToValid"); 
		
		$Authorized=1;
		if($Type{localNumber} && !$Rights{IsLocalAllowed})
		{
			MyVerbose("CLI : $CLI is not authorized to call to Local # $DialedNoToValid");
			$Authorized=0;
		}
		
		if($Type{Mobile} && !$Rights{IsMobileAllowed})
		{ 
			MyVerbose("CLI : $CLI is not authorized to call to Mobile # $DialedNoToValid");                                                
			$Authorized=0;                                  
		}                                                 
		
		if($Type{NationWide} && !$Rights{IsNationWideAllowed})
		{
			MyVerbose("CLI : $CLI is not authorized to call to NWD # $DialedNoToValid");                                                
			$Authorized=0;
		}
		if($Type{International} && !$Rights{IsInternationalAllowed})
		{
			MyVerbose("CLI : $CLI is not authorized to call to International # $DialedNoToValid");                                                
			$Authorized=0;
		}
		if(!$Authorized)
		{
			$AGI->answer();
			$AGI->stream_file('auth-incorrect');
			sleep(4);
			exitWithCongestion();
		}
		if($Type{Mobilink})
		{
			$dialstr = "Zap/g4/" . $DialedNoToValid . "|1000|tTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
			$CDR_HASH{CALLEDID}	 =$DialedNoToValid ;
			$CDR_HASH{TRUNK}="Zap/g4/";
		}
		else
		{
			$CDR_HASH{CALLEDID}	 =$DialedNo;
			$CDR_HASH{TRUNK}="Zap/g1/";
		}
		$AGI->answer();
							
		$res = $AGI->exec("SetCIDNum $Rights{DirectPhone}");
		$res = $AGI->exec("SetCIDName $Rights{Name}");
		
		if ($Rights{CALLRECORDING})
		{
			callrecording($CDR_HASH{CALLEDID});
		}
		
		$res = $AGI->exec("DIAL $dialstr");
		
		$answeredtime = $AGI->get_variable("ANSWEREDTIME");
		$dialstatus = $AGI->get_variable("DIALSTATUS");
		$CDR_HASH{DIALSTATUS}=$dialstatus;
		$CDR_HASH{ANSWERTIME}=$answeredtime;
		MyVerbose("\$answeredtime: $answeredtime");
		MyVerbose("\$dialstatus: $dialstatus");
		
		
} ################ end digit 9 if case
elsif($Digit==1)
{
		$DialedNo=$AGI->get_data('silence',20000,3);
		$DialedNo ="1" . $DialedNo;
		$dialstr = "SIP/" . $DialedNo . "\@carrier|1000|trTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
		
		if(!IsInteger($DialedNo))
		{
			exitWithCongestion();
		}
		
		
		$CDR_HASH{TRUNK}="Sip";
		$CDR_HASH{CALLEDID}	 =$DialedNo;
		
		if ($Rights{CALLRECORDING})
		{
			callrecording($CDR_HASH{CALLEDID});
		}
		
		$res = $AGI->exec("DIAL $dialstr");	
		
		$answeredtime = $AGI->get_variable("ANSWEREDTIME");
		$dialstatus = $AGI->get_variable("DIALSTATUS");
		$CDR_HASH{DIALSTATUS}=$dialstatus;
		$CDR_HASH{ANSWERTIME}=$answeredtime;
		
		MyVerbose1("\$dialstatus: $dialstatus");
		
}################ end digit 1 if else case
elsif($Digit==2)
{
		$DialedNo=$AGI->get_data('silence',20000,2);
		$DialedNo ="2" . $DialedNo;
		$dialstr = "SIP/" . $DialedNo . "\@carrier|1000|trTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
		if(!IsInteger($DialedNo))
		{
			exitWithCongestion();
		}
		
		$CDR_HASH{TRUNK}="Sip";
		$CDR_HASH{CALLEDID}	 =$DialedNo;
		
		if ($Rights{CALLRECORDING})
		{
			callrecording($CDR_HASH{CALLEDID});
		}
		
		$res = $AGI->exec("DIAL $dialstr");	
		
		$answeredtime = $AGI->get_variable("ANSWEREDTIME");
		$dialstatus = $AGI->get_variable("DIALSTATUS");
		$CDR_HASH{DIALSTATUS}=$dialstatus;
		$CDR_HASH{ANSWERTIME}=$answeredtime;
		 
		MyVerbose("\$dialstatus: $dialstatus");
				
} ################ end digit 2 if else case


if ( $dialstatus eq 'ANSWER')
{
	#saveCdrData(%CDR_HASH);
}                            


if ($Rights{CALLRECORDING})
{
	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");	
}

saveCdrData(%CDR_HASH);

$AGI->hangup();
exit;


sub MyVerbose
{
   my ($Msg) = @_;
	#return;
	$Msg =~  s/\n/ /gi ;
	$AGI->verbose("DialoutCLI.pl_Debug: $Msg", 3) ;	
}

sub IsInteger 
{
    	my $InputString = shift;
    	
    	if ($InputString !~ /^[0-9]+$/) 
    	{
    		return 0;
    	}
    	else 
    	{
    		return 1;	
    	}	
}

sub exitWithCongestion
{
    MyVerbose("Executing hangup due to invalid dialed number");
    $res = $AGI->exec("Congestion");
    sleep(3);
    $AGI->hangup();
    exit;
}
    
sub GetLineRights
{
	  	my $Line = shift;
	  	
	  	@Result=Db->ExecuteQuery("
	  	select
			IsLocalAllowed          ,
			IsMobileAllowed         ,
			IsNationWideAllowed     ,
			IsInternationalAllowed  ,
			
			Name,
			DirectPhone,
			CallRecording,
			VoiceMail
			
			from siprelation
			where
			username=\"$Line\"
	  	");
	  	my %Hash;
	  	$Hash{IsLocalAllowed}		=				$Result[1][0];
	  	$Hash{IsMobileAllowed}		=				$Result[1][1];
	  	$Hash{IsNationWideAllowed}	=				$Result[1][2];
	  	$Hash{IsInternationalAllowed}	=				$Result[1][3];
	  	
# by imran					
	  	$Hash{Name}				=				$Result[1][4];
	  	$Hash{DirectPhone}			=				$Result[1][5];
	  	$Hash{CALLRECORDING}		=				$Result[1][6];
	  	$Hash{VOICEMAIL}			=				$Result[1][7];
# end	  	
	  	
	  	return %Hash;
	
}

sub GetDialedNumberType
{
	  	my $DialNumber = shift;
	  	
	  	my %Hash;
	  	$Hash{localNumber}		=0;
	  	$Hash{Mobile}			=0;
	  	$Hash{Mobilink}		=0;
	  	$Hash{NationWide}		=0;
	  	$Hash{International}	=0;
	  	
	  	if(length($DialNumber)==7)
	  	{
	  		$Hash{localNumber}				=1;
	  		
	  	}
	  	
	  	if( (substr($DialNumber,0,2) eq "03") && (length($DialNumber)==11) )
		{                           
			
			$Hash{Mobile}					=1;      
		}                                
	  	
	 	if( ( ($DialNumber =~ /^0300/gsi) || ($DialNumber =~ /^0301/gsi)) && (length($DialNumber)==11))
	  	{
	  		$Hash{Mobilink}				=1;
	  	}
	  		
	  	
	  	if($DialNumber =~ /^00/gsi)
	  	{
	  		$Hash{International}			=1;
	  	}
	  	if(($DialNumber =~ /^0/gsi) && !$Hash{International} && !$Hash{Mobile}) 
	  	{
	  		$Hash{NationWide}				=1;
	  	}
	  		
	return %Hash;
}

# table mycdr by imran
# IN Hash
# OUT nothing
sub saveCdrData
{ 
	my (%HASH)=@_;

	$HASH{CONTEXT}  ="";
	
	$HASH{BILLSEC}  = "";


	Db->ExecuteQuery("insert into mycdr(src,dst,dcontext,channel,duration,billsec,disposition,calldate,recordedfile)
	values('$HASH{CALLERID}','$HASH{CALLEDID}','$HASH{CONTEXT}','$HASH{TRUNK}','$HASH{ANSWERTIME}','$HASH{BILLSEC}','$HASH{DIALSTATUS}',sysdate(),'$HASH{CALLRECORDINGFILE}')");
	
}

sub callrecording
{
	my ($phoneno) =@_;
	my $CalledNumber = $phoneno;
	my $CurDate;
	my $calleridnum = $CLI;
	
	$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");
	
	$CDR_HASH{CALLRECORDINGFILE} = $gsm_file;
} 

#!/usr/bin/perl
// SipDialOUT.agi by imran 
use Asterisk::AGI;
use lib "/var/lib/asterisk/agi-bin";
use Db;

$AGI = new Asterisk::AGI;
$|=1;

my %input = $AGI->ReadParse();
$Digit	=$ARGV[0];
$CLI	=$ARGV[1];
#$Zap="Zap/18-12312";

my %CDR_HASH;
my $gsm_file="";
my $wav_file="";
my $d_path  ="/recordings";

$CDR_HASH{CALLERID}	 =$CLI;
$CDR_HASH{CALLRECORDINGFILE} ='';      

MyVerbose("Call is being originated from CLI: $CLI");

if($CLI eq "")
{
	MyVerbose("User not recognized on channel bank CLI : $CLI");	
	exitWithCongestion();
}


if($Digit==9)
{

		$DialedNo= $AGI->get_data('dialtone',6000,1);
		MyVerbose("\$DialedNo : $DialedNo");
		
		$DialedNo1= $AGI->get_data('silence',5000,3);
		MyVerbose("\$DialedNo1 : $DialedNo1");
		
		
		$TimeOut =5000;
		$Digit  	=3;
		if($DialedNo1 =~/^3/gsi)
		{
			$TimeOut=10000;	
			$Digit  	=7;
		}
		elsif($DialedNo ==1)
		{
			$TimeOut=8000;	
			$Digit  	=5;
		}
		elsif($DialedNo ==0)
		{
			$TimeOut=11000;	
			$Digit  	=10;
		}
		
		$DialedNo2= $AGI->get_data('silence',$TimeOut,$Digit);
		MyVerbose("\$DialedNo2 : $DialedNo2");
		
		$DialedNo1.=$DialedNo2;
		
		$DialedNoToValid=$DialedNo . $DialedNo1;
		$DialedNo ="" . $DialedNo . $DialedNo1;
		MyVerbose("\$DialedNo : $DialedNo");
		
		my $dialstr;
		$dialstr = "Zap/g1/" . $DialedNo . "|1000|tTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
		MyVerbose("\$dialstr : $dialstr");
		
		if(!IsInteger($DialedNo))
		{
			exitWithCongestion();
		}
		
		%Rights	=		GetLineRights($CLI);
		%Type=GetDialedNumberType("$DialedNoToValid"); 
		
		$Authorized=1;
		if($Type{localNumber} && !$Rights{IsLocalAllowed})
		{
			MyVerbose("CLI : $CLI is not authorized to call to Local # $DialedNoToValid");
			$Authorized=0;
		}
		
		if($Type{Mobile} && !$Rights{IsMobileAllowed})
		{ 
			MyVerbose("CLI : $CLI is not authorized to call to Mobile # $DialedNoToValid");                                                
			$Authorized=0;                                  
		}                                                 
		
		if($Type{NationWide} && !$Rights{IsNationWideAllowed})
		{
			MyVerbose("CLI : $CLI is not authorized to call to NWD # $DialedNoToValid");                                                
			$Authorized=0;
		}
		if($Type{International} && !$Rights{IsInternationalAllowed})
		{
			MyVerbose("CLI : $CLI is not authorized to call to International # $DialedNoToValid");                                                
			$Authorized=0;
		}
		if(!$Authorized)
		{
			$AGI->answer();
			$AGI->stream_file('auth-incorrect');
			sleep(4);
			exitWithCongestion();
		}
		if($Type{Mobilink})
		{
			$dialstr = "Zap/g4/" . $DialedNoToValid . "|1000|tTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
			$CDR_HASH{CALLEDID}	 =$DialedNoToValid ;
			$CDR_HASH{TRUNK}="Zap/g4/";
		}
		else
		{
			$CDR_HASH{CALLEDID}	 =$DialedNo;
			$CDR_HASH{TRUNK}="Zap/g1/";
		}
		$AGI->answer();
							
		$res = $AGI->exec("SetCIDNum $Rights{DirectPhone}");
		$res = $AGI->exec("SetCIDName $Rights{Name}");
		
		if ($Rights{CALLRECORDING})
		{
			callrecording($CDR_HASH{CALLEDID});
		}
		
		$res = $AGI->exec("DIAL $dialstr");
		
		$answeredtime = $AGI->get_variable("ANSWEREDTIME");
		$dialstatus = $AGI->get_variable("DIALSTATUS");
		$CDR_HASH{DIALSTATUS}=$dialstatus;
		$CDR_HASH{ANSWERTIME}=$answeredtime;
		MyVerbose("\$answeredtime: $answeredtime");
		MyVerbose("\$dialstatus: $dialstatus");
		
		
} ################ end digit 9 if case
elsif($Digit==1)
{
		$DialedNo=$AGI->get_data('silence',20000,3);
		$DialedNo ="1" . $DialedNo;
		$dialstr = "SIP/" . $DialedNo . "\@carrier|1000|trTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
		
		if(!IsInteger($DialedNo))
		{
			exitWithCongestion();
		}
		
		
		$CDR_HASH{TRUNK}="Sip";
		$CDR_HASH{CALLEDID}	 =$DialedNo;
		
		if ($Rights{CALLRECORDING})
		{
			callrecording($CDR_HASH{CALLEDID});
		}
		
		$res = $AGI->exec("DIAL $dialstr");	
		
		$answeredtime = $AGI->get_variable("ANSWEREDTIME");
		$dialstatus = $AGI->get_variable("DIALSTATUS");
		$CDR_HASH{DIALSTATUS}=$dialstatus;
		$CDR_HASH{ANSWERTIME}=$answeredtime;
		
		MyVerbose1("\$dialstatus: $dialstatus");
		
}################ end digit 1 if else case
elsif($Digit==2)
{
		$DialedNo=$AGI->get_data('silence',20000,2);
		$DialedNo ="2" . $DialedNo;
		$dialstr = "SIP/" . $DialedNo . "\@carrier|1000|trTL(" . ((30*60 + 0)*1000) .":7000:5000)"; 
		if(!IsInteger($DialedNo))
		{
			exitWithCongestion();
		}
		
		$CDR_HASH{TRUNK}="Sip";
		$CDR_HASH{CALLEDID}	 =$DialedNo;
		
		if ($Rights{CALLRECORDING})
		{
			callrecording($CDR_HASH{CALLEDID});
		}
		
		$res = $AGI->exec("DIAL $dialstr");	
		
		$answeredtime = $AGI->get_variable("ANSWEREDTIME");
		$dialstatus = $AGI->get_variable("DIALSTATUS");
		$CDR_HASH{DIALSTATUS}=$dialstatus;
		$CDR_HASH{ANSWERTIME}=$answeredtime;
		 
		MyVerbose("\$dialstatus: $dialstatus");
				
} ################ end digit 2 if else case


if ( $dialstatus eq 'ANSWER')
{
	#saveCdrData(%CDR_HASH);
}                            


if ($Rights{CALLRECORDING})
{
	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");	
}

saveCdrData(%CDR_HASH);

$AGI->hangup();
exit;


sub MyVerbose
{
   my ($Msg) = @_;
	#return;
	$Msg =~  s/\n/ /gi ;
	$AGI->verbose("DialoutCLI.pl_Debug: $Msg", 3) ;	
}

sub IsInteger 
{
    	my $InputString = shift;
    	
    	if ($InputString !~ /^[0-9]+$/) 
    	{
    		return 0;
    	}
    	else 
    	{
    		return 1;	
    	}	
}

sub exitWithCongestion
{
    MyVerbose("Executing hangup due to invalid dialed number");
    $res = $AGI->exec("Congestion");
    sleep(3);
    $AGI->hangup();
    exit;
}
    
sub GetLineRights
{
	  	my $Line = shift;
	  	
	  	@Result=Db->ExecuteQuery("
	  	select
			IsLocalAllowed          ,
			IsMobileAllowed         ,
			IsNationWideAllowed     ,
			IsInternationalAllowed  ,
			
			Name,
			DirectPhone,
			CallRecording,
			VoiceMail
			
			from siprelation
			where
			username=\"$Line\"
	  	");
	  	my %Hash;
	  	$Hash{IsLocalAllowed}		=				$Result[1][0];
	  	$Hash{IsMobileAllowed}		=				$Result[1][1];
	  	$Hash{IsNationWideAllowed}	=				$Result[1][2];
	  	$Hash{IsInternationalAllowed}	=				$Result[1][3];
	  	
# by imran					
	  	$Hash{Name}				=				$Result[1][4];
	  	$Hash{DirectPhone}			=				$Result[1][5];
	  	$Hash{CALLRECORDING}		=				$Result[1][6];
	  	$Hash{VOICEMAIL}			=				$Result[1][7];
# end	  	
	  	
	  	return %Hash;
	
}

sub GetDialedNumberType
{
	  	my $DialNumber = shift;
	  	
	  	my %Hash;
	  	$Hash{localNumber}		=0;
	  	$Hash{Mobile}			=0;
	  	$Hash{Mobilink}		=0;
	  	$Hash{NationWide}		=0;
	  	$Hash{International}	=0;
	  	
	  	if(length($DialNumber)==7)
	  	{
	  		$Hash{localNumber}				=1;
	  		
	  	}
	  	
	  	if( (substr($DialNumber,0,2) eq "03") && (length($DialNumber)==11) )
		{                           
			
			$Hash{Mobile}					=1;      
		}                                
	  	
	 	if( ( ($DialNumber =~ /^0300/gsi) || ($DialNumber =~ /^0301/gsi)) && (length($DialNumber)==11))
	  	{
	  		$Hash{Mobilink}				=1;
	  	}
	  		
	  	
	  	if($DialNumber =~ /^00/gsi)
	  	{
	  		$Hash{International}			=1;
	  	}
	  	if(($DialNumber =~ /^0/gsi) && !$Hash{International} && !$Hash{Mobile}) 
	  	{
	  		$Hash{NationWide}				=1;
	  	}
	  		
	return %Hash;
}

# table mycdr by imran
# IN Hash
# OUT nothing
sub saveCdrData
{ 
	my (%HASH)=@_;

	$HASH{CONTEXT}  ="";
	
	$HASH{BILLSEC}  = "";


	Db->ExecuteQuery("insert into mycdr(src,dst,dcontext,channel,duration,billsec,disposition,calldate,recordedfile)
	values('$HASH{CALLERID}','$HASH{CALLEDID}','$HASH{CONTEXT}','$HASH{TRUNK}','$HASH{ANSWERTIME}','$HASH{BILLSEC}','$HASH{DIALSTATUS}',sysdate(),'$HASH{CALLRECORDINGFILE}')");
	
}

sub callrecording
{
	my ($phoneno) =@_;
	my $CalledNumber = $phoneno;
	my $CurDate;
	my $calleridnum = $CLI;
	
	$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");
	
	$CDR_HASH{CALLRECORDINGFILE} = $gsm_file;
} 

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+