#! /usr/bin/perl -w
use strict;
use warnings;
 
use Digest::MD5;
use Crypt::Rijndael;

my $http_user="username"; 
my $http_passwd="password";

sub md5sum{
  my $file = shift;
  my $digest = "";
  eval{
    open(FILE, $file) or die "[ERROR] md5sum: Can't find file $file\n";
    my $ctx = Digest::MD5->new;
    $ctx->addfile(*FILE);
    $digest = $ctx->hexdigest;
    close(FILE);
  };
  if($@){
    print $@;
    return "";
  }
  return $digest;
}

sub AlertMail{
 my $text = shift; 
 my $syscom = '/bin/echo '.$text.' | /usr/bin/mailx -s \'Atrerisk-Perl: Update List Error \' alerts@unappel.ch';
 system $syscom;
 return $?;
}

sub GetKey{
my $cle="2345678";
open(FileKey,"/root/100105-asterisk-list/KeyAstr") || AlertMail ("Error opening file KeyAstr") ;
$cle =$_  while(<FileKey>);
$cle =~ s/^\s+//;
$cle =~ s/\s+$//;
close (FileKey);
return $cle;
}

####################################   Liste Accounts   ####################################

my $fileN = "/root/100105-asterisk-list/Asterisk-Accounts.txt";
my $fileC = "/root/100105-asterisk-list/Asterisk-Accounts.crypt";
my $fileO = "/etc/asterisk/db-friends.conf";
my $comsys="/usr/bin/wget -q -N  --output-document='$fileN'  --http-user=$http_user --http-passwd=$http_passwd http://switzernet.com/folders/100104-php-asterisk/Asterisk-Accounts.php";
my $fg=0;
my $key = GetKey() ;
system $comsys;
if($?)
{
   AlertMail "warning in the command '$comsys'\nError number:$?\n";
   $comsys="/usr/bin/wget -q -N --output-document='$fileN' --http-user=$http_user --http-passwd=$http_passwd http://unappel.ch/folders/100104-php-asterisk/Asterisk-Accounts.php";
   system $comsys;
   if($?)
       {
           AlertMail "Error in the command '$comsys'\nError number:$?\n";
	   exit 0;
       }
}

open(FileCrypt,$fileN) || AlertMail ("Error opening file $fileN") ;
my $Lign;
my $OutPut;
$Lign.=$_ while (<FileCrypt>) ;
my @res_split_Acc=split('</TR>', $Lign);
my $cipher = Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_ECB() );
$cipher->set_iv($key);
#print("\nTotal ligne: ".scalar(@res_split_Acc));
for (my $i=0;$i<scalar(@res_split_Acc);$i++)
{
my @Td=split('</TD>', $res_split_Acc[$i]);
if (!(length($Td[0])%16)&&!(length($Td[1])%16))
{
$Td[0]=substr($cipher->decrypt($Td[0]), 0, 11);
$Td[1]=substr($cipher->decrypt($Td[1]), 0, 32);
$OutPut .= "[".$Td[0]."](phone)\nmd5secret=".$Td[1]."\n\n";
}
else
{
print("\nERREUR Crypatge length%16 in ligne $i: ".length($Td[0])." : ".length($Td[1]));
$Td[0]=substr($cipher->decrypt($Td[0]." "), 0, 11);
$Td[1]=substr($cipher->decrypt($Td[1]), 0, 32);
$OutPut .= "[".$Td[0]."](phone);BUG\nmd5secret=".$Td[1]."\n\n";
}
}
open (FH, ">$fileC") || AlertMail ("Error opening file $fileC") ;
print FH $OutPut;
close (FH);
close (FileCrypt);

my $md5C =  md5sum($fileC);
my $md5O =  md5sum($fileO);

AlertMail "Error calculates MD5 file ".$fileC."\n" unless($md5C ne "");
AlertMail "Error calculates MD5 file ".$fileO."\n" unless($md5O ne "");

unless ($md5O eq $md5C){
system "/bin/cp $fileC $fileO";
AlertMail "Error in the command 'cp $fileC $fileO'\nError number:$?\n" if($?);

system "/bin/chown --change asterisk:asterisk $fileO";
AlertMail "Error in the command 'chown --change asterisk:asterisk $fileO'\nError number:$?\n" if($?);

$fg=1;
}


####################################   Liste Nodes   ####################################

my $DN=0;
$fileN = "/root/100105-asterisk-list/Asterisk-Nodes.txt";
$fileC = "/etc/asterisk/db-peers.conf";
$comsys="/usr/bin/wget -q -N --output-document='$fileN' --http-user=$http_user --http-passwd=$http_passwd http://switzernet.com/folders/100104-php-asterisk/Asterisk-Nodes.php";
system $comsys;
if($?)
{
   AlertMail "warning in the command '$comsys'\nError number:$?\n";
   $comsys="/usr/bin/wget -q -N --output-document='$fileN' --http-user=switzernet --http-passwd=$http_passwd http://unappel.ch/folders/100104-php-asterisk/Asterisk-Nodes.php";
   system $comsys;
   if($?)
       {
           AlertMail "Error in the command '$comsys'\nError number:$?\n";
	   exit 0;
       }
}

my $md5N =  md5sum($fileN);
$md5C =  md5sum($fileC);

AlertMail "Error calculates MD5 file ".$fileN."\n" unless($md5N ne "");
AlertMail "Error calculates MD5 file ".$fileC."\n" unless($md5C ne "");

unless($md5N eq $md5C){
system "/bin/cp $fileN $fileC";
AlertMail "Error in the command 'cp $fileN $fileC'\nError number:$?\n" if($?);

system "/bin/chown --change asterisk:asterisk $fileC";
AlertMail "Error in the command 'chown --change asterisk:asterisk $fileC'\nError number:$?\n" if($?);
$DN=1;
$fg=1;
}

####################################   Liste Customer-Ip   ####################################

$fileN = "/root/100105-asterisk-list/Asterisk-customer-ip.txt";
$fileC = "/etc/asterisk/db-customer-ip.conf";
$comsys="/usr/bin/wget -q -N --output-document='$fileN' --http-user=$http_user --http-passwd=$http_passwd http://switzernet.com/folders/100104-php-asterisk/Asterisk-customer-ip.php";
system $comsys;
if($?)
{
   AlertMail "warning in the command '$comsys'\nError number:$?\n";
   $comsys="/usr/bin/wget -q -N --output-document='$fileN' --http-user=$http_user --http-passwd=$http_passwd http://unappel.ch/folders/100104-php-asterisk/Asterisk-customer-ip.php";
   system $comsys;
   if($?)
       {
           AlertMail "Error in the command '$comsys'\nError number:$?\n";
           exit 0;
       }
}

$md5N =  md5sum($fileN);
$md5C =  md5sum($fileC);

AlertMail "Error calculates MD5 file ".$fileN."\n" unless($md5N ne "");
AlertMail "Error calculates MD5 file ".$fileC."\n" unless($md5C ne "");

unless($md5N eq $md5C){
system "/bin/cp $fileN $fileC";
AlertMail "Error in the command 'cp $fileN $fileC'\nError number:$?\n" if($?);

system "/bin/chown --change asterisk:asterisk $fileC";
AlertMail "Error in the command 'chown --change asterisk:asterisk $fileC'\nError number:$?\n" if($?);

$fg=1;
}

####################################   Liste Deny-Node   ####################################
if ($DN)
{
$fileC = "/etc/asterisk/db-peers.conf";
$fileN = "/etc/asterisk/db-deny-node.conf";
my $host;
my $ligne;
my $OutDN='';
open FICHIER,"< $fileC" || AlertMail ("Error opening file $fileC") ;
while ($ligne = <FICHIER>){
if(($host)=$ligne=~ /^host=(.+)/)
	{
	chomp($host);
	$OutDN .="deny=$host\n";
	}
}
close FICHIER;
open (FHDN, ">$fileN") || AlertMail ("Error opening file $fileN") ;
print FHDN $OutDN;
close (FHDN);
system "/bin/chown --change asterisk:asterisk $fileN";
AlertMail "Error in the command 'chown --change asterisk:asterisk $fileN'\nError number:$?\n" if($?);
}

####################################   Reload Asterisk   ####################################

if ($fg)
{
system "/etc/init.d/asterisk reload";
AlertMail "Error in the command '/etc/init.d/asterisk reload'\nError number:$?\n" if($?);
}

