#!/usr/local/bin/perl
#inizializza hash
#pop
%tipi_servizi_pop=(
	"login:"=>1,
	"select"=>2,
	"close"=>3
	);
#smtp
%tipi_servizi_smtp=(
		    "SMTP-Accept"=>0,
		    );
#imap
%tipi_servizi_imap=(
		    "login:"=>1,
		    "select"=>2,
		    "close"=>3
		    );
@matrice=0;
$ctrl=0;
print "insert filename and proper path\n";
$filedilettura=<STDIN>;
open(FILE,$filedilettura) or die "non posso aprire input file ";
while ($line=<FILE>){
    chomp($line);
    if($line=~/smtpd/){
#############
#           #
#  SMTP     #
#           #
#############
	@tmp_array=split(/ /,$line);
	$prot="SMTP";
	if($#tmp_array>6){next;}
	if($tmp_array[6]!~/SMTP-Accept/){next;}
	@time=data($tmp_array[0],$tmp_array[1]);
	if(($tmp_array[6]=~/ |queue/)&&($tmp_array!~/:/)){next;}
	($module,$env_id,$msg_id,$hostname,$ip_address,$env_senser,$msg_size,$num_rcpt,$rcp_list)=split(/:/,$tmp_array[6]);

#definisco type dal nome del modulo e inseriscilo in %tipi_servizi_smtp

	if(exists($tipi_servizi_smtp{$module})){

		if(defined($tipi_servizi_smtp{$module})){
			$type=$tipi_servizi_smtp{$module};}
		else{
			$tipo=0;
			while(($key,$value)=each(%tipi_servizi_smtp)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$type=$tipo+1;
			$tipi_servizi_smtp{$module}=$type;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%tipi_servizi_smtp)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$type=$tipo+1;
		$tipi_servizi_smtp{$module}=$type;
	}
#controlla se ci sono liste di recipienti
	if($rcp_list=~/,/){
	    @rcp_list=split /,/;
	}
#codice del cliente: questo e' solo l'indirizzo della macchina in cui e' collegato
	    
	if(exists($clients_smtp{$ip_address})){
		if(defined($clients_smtp{$ip_address})){
			$client_macc_id=$clients_smtp{$ip_address};}
		else{
			$tipo=0;
			while(($key,$value)=each(%clients_smtp)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$client_macc_id=$tipo+1;
			$clients_smtp{$ip_address}=$client_macc_id;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%clients_smtp)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$client_macc_id=$tipo+1;
		$clients_smtp{$ip_address}=$client_macc_id;
	}
	@tmp_arr_riv=($client_macc_id,@time,$prot,$type,$msg_size,$num_rcpt);
	push @matrice,[@tmp_arr_riv];
	$ctrl=1;
    }
    if($line=~/popd/){
    
#############
#           #
#   POP     #
#           #
#############  
    if($line!~/login:|select|close/){next;}
    else{
    	
#sono nel caso stringhe di interesse
    	@tmp_array=split(/ /,$line);
	$prot="POP";
	@time=data($tmp_array[0],$tmp_array[1]);
	$oper=$tmp_array[6];
# calcolo il codice operazione
	if(exists($tipi_servizi_pop{$oper})){
		if(defined($tipi_servizi_pop{$oper})){
			$type_ser=$tipi_servizi_pop{$oper};}
		else{
			$tipo=0;
			while(($key,$value)=each(%tipi_servizi_pop)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$type_ser=$tipo+1;
			$tipi_servizi_pop{$oper}=$type_ser;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%tipi_servizi_pop)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$type_ser=$tipo+1;
		$tipi_servizi_pop{$oper}=$type_ser;
	}

# codice client %clients_pop
	    
	$ip_address=substr($tmp_array[7],1,15);
	if(exists($clients_pop{$ip_address})){
		if(defined($clients_pop{$ip_address})){
			$ind_prov=$clients_pop{$ip_address};}
		else{
			$tipo=0;
			while(($key,$value)=each(%clients_pop)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$ind_prov=$tipo+1;
			$clients_pop{$ip_address}=$ind_prov;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%clients_pop)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$ind_prov=$tipo+1;
		$clients_pop{$ip_address}=$ind_prov;
	}
# calcolo codice per clienti pop %nome_clienti_pop
	$nome=$tmp_array[8];
	if(exists($nome_clienti_pop{$nome})){
		if(defined($nome_clienti_pop{$nome})){
			$client_id_pop=$nome_clienti_pop{$nome};}
		else{
			$tipo=0;
			while(($key,$value)=each(%nome_clienti_pop)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$client_id_pop=$tipo+1;
			$nome_clienti_pop{$nome}=$client_id_pop;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%nome_clienti_pop)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$client_id_pop=$tipo+1;
		$nome_clienti_pop{$nome}=$client_id_pop;
	}
	if($tmp_array[5]=~/Notice/){

#contiene tutte le informazioni
#tempo in cui l'utente si e' connesso
	    ($year,$month,$day)=split(?/?,$tmp_array[9],3);
	    $month--;
	    $tempo=$tmp_array[10];
	    @time_connection=($day,$month,$year,$tempo);
	    $durata=tempo_connessione($tmp_array[11]);
	    $bytes_letti=$tmp_array[12];
	    $bytes_scritti=$tmp_array[13];
	    $num_mailboxes=$tmp_array[14];
	}
	else{next;}
	@tmp_arr_riv=($client_id_pop,$ind_prov,@time_connection,@time,$durata,$prot,$type_ser,$bytes_letti,$bytes_scritti,$num_mailboxes);
	push @matrice,[@tmp_arr_riv];
	$ctrl=2;#serve per la stampa dei risultati
	    $matr_ref=\@matrice;
    }
}
    if($line=~/imapd/){
    
#############
#           #
#   IMAP    #
#           #
#############  
    if($line!~/login:|select|close/){next;}
    else{
    	
#sono nel caso stringhe di interesse
    	@tmp_array=split(/ /,$line);
	$prot="IMAP";
	@time=data($tmp_array[0],$tmp_array[1]);
	$oper=$tmp_array[6];
# calcolo il codice operazione
	if(exists($tipi_servizi_imap{$oper})){
		if(defined($tipi_servizi_imap{$oper})){
			$type_ser=$tipi_servizi_imap{$oper};}
		else{
			$tipo=0;
			while(($key,$value)=each(%tipi_servizi_imap)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$type_ser=$tipo+1;
			$tipi_servizi_imap{$oper}=$type_ser;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%tipi_servizi_imap)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$type_ser=$tipo+1;
		$tipi_servizi_imap{$oper}=$type_ser;
	}

# codice client %clients_imap
	    
	$ip_address=substr($tmp_array[7],1,15);
	if(exists($clients_imap{$ip_address})){
		if(defined($clients_imap{$ip_address})){
			$ind_prov=$clients_imap{$ip_address};}
		else{
			$tipo=0;
			while(($key,$value)=each(%clients_imap)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$ind_prov=$tipo+1;
			$clients_imap{$ip_address}=$ind_prov;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%clients_imap)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$ind_prov=$tipo+1;
		$clients_imap{$ip_address}=$ind_prov;
	}
# calcolo codice per clienti imap %nome_clienti_imap
	$nome=$tmp_array[8];
	if(exists($nome_clienti_imap{$nome})){
		if(defined($nome_clienti_imap{$nome})){
			$client_id_imap=$nome_clienti_imap{$nome};}
		else{
			$tipo=0;
			while(($key,$value)=each(%nome_clienti_imap)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
			$client_id_imap=$tipo+1;
			$nome_clienti_imap{$nome}=$client_id_imap;
		}
	}
	else{
		$tipo=0;
		while(($key,$value)=each(%nome_clienti_imap)){
				$tipo=($value>$tipo)?$value:$tipo;
			}
		$client_id_imap=$tipo+1;
		$nome_clienti_imap{$nome}=$client_id_imap;
	}
	if($tmp_array[5]=~/Notice/){

#contiene tutte le informazioni
#tempo in cui l'utente si e' connesso
	    ($year,$month,$day)=split(?/?,$tmp_array[9],3);
	    $month--;
	    $tempo=$tmp_array[10];
	    @time_connection=($day,$month,$year,$tempo);
	    $durata=tempo_connessione($tmp_array[11]);
	    $bytes_letti=$tmp_array[12];
	    $bytes_scritti=$tmp_array[13];
	    $num_mailboxes=$tmp_array[14];
	}
	else{next;}
	@tmp_arr_riv=($client_id_imap,$ind_prov,@time_connection,@time,$durata,$prot,$type_ser,$bytes_letti,$bytes_scritti,$num_mailboxes);
	push @matrice,[@tmp_arr_riv];
	$ctrl=3;#serve per la stampa dei risultati
	    $matr_ref=\@matrice;
    }
}
}
close(FILE);
open(FILE,$filedilettura) or die "non so qual'e' il file: ";
while($line=<FILE>){
	chomp($line);
	if($line=~/popd/){
#sono nel caso pop
	if($line!~/login:|select/){next;}
	@tmp=split(/ /,$line);
	$value=$nome_clienti_pop{$tmp[8]};
	for ($i=0;$i<$#matrice;$i++){
		if ($value==$matrice[$i][0]){
		    @time=data($tmp[0],$tmp[1]);
		    if($time[3]==$matrice[$i][5]){
			$matrice[$i][12].=$tipi_servizi_pop{$tmp[6]};
			last;
		    }
		    else{next;}
		}
		else{next;}
	    }
    }
	if($line=~/imapd/){
#sono nel caso imap
	if($line!~/login:|select/){next;}
	@tmp=split(/ /,$line);
	$value=$nome_clienti_imap{$tmp[8]};
	for ($i=0;$i<$#matrice;$i++){
		if ($value==$matrice[$i][0]){
		    @time=data($tmp[0],$tmp[1]);
		    if($time[3]==$matrice[$i][5]){
			$matrice[$i][12].=$tipi_servizi_imap{$tmp[6]};
			last;
		    }
		    else{next;}
		}
		else{next;}
	    }
    }
    }
close(FILE);
	
SWI:{
    if ($ctrl==1){open(OUTPUTFILE,">>dati_smtp_normalizzati");
		  open(TIPI,">>servizi_smtp");
		  while(($key,$value)=each(%tipi_servizi_smtp)){
		      print TIPI "$key $value\n";}
		  close(TIPI);
		  $campi=8;
		  last SWI;}
    if ($ctrl==2){open(OUTPUTFILE,">>dati_pop_normalizzati");
		  open(TIPI,">>servizi_pop");
		  while(($key,$value)=each(%tipi_servizi_pop)){
		      print TIPI "$key $value\n";}
		  close(TIPI);		  
		  $campi=16;
		  last SWI;}
    if ($ctrl==3){open(OUTPUTFILE,">>dati_imap_normalizzati");
		  open(TIPI,">>servizi_imap");
		  while(($key,$value)=each(%tipi_servizi_imap)){
		      print TIPI "$key $value\n";}
		  close(TIPI);
		  $campi=16;
		  last SWI;}
    die "c'e' qualche problema :";
}
for($i=0;$i<=$#matrice;$i++){
	for $y (0..$campi){
	print OUTPUTFILE "$matrice[$i][$y] ";
    }
	print OUTPUTFILE "\n";
}



##########################################################################
#subroutine per calcolare la data e sistemarla: argomenti sono
#    - primo campo del vettore $tmp_array
#    - secondo campo dello stesso
##########################################################################
sub data(\$\$){
    my ($elemento,$fascia)=($_[0],$_[1]);
	    (my $a1,my $a2,my $a3)=split(?/?,$elemento,3);
	    my $day=substr($a1,1,2);
	    my $month=substr($a2,0,3);
	    my $year=substr($a3,0,4);
	    my $hour=substr($a3,5,2);
	    my $min=substr($a3,8,2);
	    my $sec=substr($a3,11,2);

#sistemo le fasce orarie

	    if($fascia=~/-0800/){tr/-0800/-8.00/;}
	    else{
		my $rif=8;
		my $spiaz=substr($fascia,0,1).substr($fascia,2,1);
		my $valore=$spiaz+$rif;
		$hour+=$valore;
	    }

#sistemo i mesi

	  SWITCH:{
	      if($month eq "Jan"){$month=0;last SWITCH;}
	      if($month eq "Feb"){$month=1;last SWITCH;}
	      if($month eq "Mar"){$month=2;last SWITCH;}
	      if($month eq "Apr"){$month=3;last SWITCH;}
	      if($month eq "May"){$month=4;last SWITCH;}
	      if($month eq "Jun"){$month=5;last SWITCH;}
	      if($month eq "Jul"){$month=6;last SWITCH;}
	      if($month eq "Aug"){$month=7;last SWITCH;}
	      if($month eq "Sep"){$month=8;last SWITCH;}
	      if($month eq "Oct"){$month=9;last SWITCH;}
	      if($month eq "Nov"){$month=10;last SWITCH;}
	      if($month eq "Dec"){$month=11;last SWITCH;}
	  }

#definisco la variabile per i tempi

	    my $tempo=join(":",$hour,$min,$sec);
	    my @time=($day,$month,$year,$tempo);
    return @time;
}	    

#####################################################################
#subroutine calcola il tempo di connessione in secondi: parametro
#      -elemento $tmp_array[11]
#####################################################################
sub tempo_connessione(\$){
    my $tempo_conn=$_[0];
    my ($hour,$min,$sec)=split(/:/,$tempo_conn,3);
    my $calcola=($hour*3600)+($min*60)+$sec;
    return $calcola;
}
