#! /usr/bin/perl

use Net::SMTP::SSL;

open FILE, ">>/tmp/dispatchlog" or die $!;

my $nummer = ${ARGV[0]};
my $meldung = ${ARGV[1]};
my $timeunix = ${ARGV[2]};
my $subric = ${ARGV[3]};


my $time = localtime($timeunix);

chomp($nummer);
chomp($meldung);
chomp($time);
chomp($subric);

open RICLIST, "</etc/pocserv/dispatch/profile" or die $!;
my @riclist = <RICLIST>;
close RICLIST;

my $dispatch = 0;

foreach (@riclist) {

	if ($_ =~ /$nummer/) {
		$dispatch = 1;
	print "RIC in profile...\n";
	}

}




# Dispatch Log schreiben ausser bei keep-alive

if ($dispatch == 1) {

	print FILE "$time\n";
	print FILE "$nummer\n";
	print FILE "$meldung\n";
	print FILE "$subric\n";
	close FILE;



my $stichwort = "ALARMIERUNG";;

if ($subric == 1) {
   $stichwort = "EINSATZALARM";
}

if ($subric == 3) {
   $stichwort = "LEITSTELLE ANRUFEN";
}

if ($subric == 2) {
   $stichwort = "EINSATZ OHNE EILE";
}

if ($subric == 0) {
   $stichwort = "PROBEALARM";
}




# Einlesen der passenden Profile

my $profileref = getprofiles($nummer);
my @profile = @{$profileref};



# Prozessierung: Passende eMails sichen und abschicken

foreach (@profile) {
	chomp($_);
	my $mailref = getmail($_);
	my @mail = @{$mailref};

	send_mail(\@mail, $stichwort, $meldung, $timeunix, $_);

}


# Prozessierung: PROWL abschicken

foreach (@profile) {
        chomp($_);
        my $prowlref = getprowl($_);
        my @prowl = @{$prowlref};

	my $prof = $_;
	foreach (@prowl) {
		
		system("prowl.pl -apikey=${_} -application=POCSERV -event=\"$stichwort - $date\"  -notification=\"$meldung\"");
		print "Delivered message $meldung to prowl receiver ${_}\n";
	}


}



}



# SUBS ###########################################################






sub send_mail {


my $password = "ballaballa";
my $account = "BLUPPBLUPP";
my $email = "testmail\@googlemail.com";
my $server = "smtp.googlemail.com";

my $toref = shift;
my @to = @{$toref};
my $stichwort = shift;
my $text = shift;
my $timeunix = shift;
my $profil = shift;


my $date=localtime($timeunix);




#foreach (@to) {


	$smtp = Net::SMTP::SSL->new($server, Port=>465);

	# Autentifizierung am Postausgangsserver
	$smtp->auth($account, $password) || die "Authentication failed!\n";



	$smtp->mail($email);

	foreach (@to) {
		$smtp->bcc($_);
	}

	#$smtp->to(\@to);
	#$smtp->bcc("pocserv\@ff-stettfeld.de");

	$smtp->data;

	$smtp->datasend("From: $email\n");
	$smtp->datasend("To: \n");	

	#foreach (@to) {
	#	$smtp->datasend("To: $_\n");
	#}
	$smtp->datasend("Subject: $stichwort\n");
	$smtp->datasend("\n");

	$smtp->datasend("========================================================================\n");
	$smtp->datasend("   !!! $stichwort !!!\n");
	$smtp->datasend("========================================================================\n");
	$smtp->datasend("\n");
	$smtp->datasend("    empfangen:.$date\n");
	$smtp->datasend("    typ:.......$stichwort (subric: $subric)\n");
	$smtp->datasend("    text:......$text\n");
	$smtp->datasend("\n");
	$smtp->datasend("========================================================================\n");
	$smtp->datasend("\n\n\n");
	
	$smtp->datasend("Achtung! Testbetrieb, diese Lösung befindet sich noch in Entwicklung!\n\nKontakt: thomas\@herzogonline.net\n\n");

	$smtp->datasend("Bitte beachten: Dies ist kein offizieller Alarmierungsdienst, vor der Inanspruchnahme "); 
	$smtp->datasend("von Sonderrechten nach StVO ist das tatsächliche Vorliegen einer Alarmierung nachzuprüfen!\n");
	$smtp->datasend("Für etwaige Schäden und Kosten durch eine Fehlalarmierung infolge einer fehlerhaften");
	$smtp->datasend(" Meldungsverarbeitung jedweder Art übernimmt der Betreiber keine Verantwortung.");
	$smtp->datasend("\n\nBei Fragen zu diesem Dienst bitte einfach auf diese Mail antworten.\n");
	$smtp->datasend("\n\n\n\nProfil des Empfaengers: $profil\n");
	$smtp->dataend;
	$smtp->quit;

	foreach (@to) {

		print "Mail gesendet an <$_> mit Betreff <$subject> und Text <$text>\n";

	}
}

#}


sub getmail {

	my $TYP = shift();
	my @RETURN;

	open (EMAILS, "</etc/pocserv/dispatch/MAIL");

	my @LINES = <EMAILS>;

	foreach (@LINES) {


		chomp($_);
		if ($_ =~ /$TYP/) {

			my @ELEMS = split(/ /, $_);
			push (@RETURN, $ELEMS[0]);
		}

	}



	close EMAILS;

	print "eMail-Adressen eingelesen:\n";

	foreach (@RETURN) {

		print "  $TYP: $_\n";

	}

	return \@RETURN;

}


sub getprowl {

        my $TYP = shift();
        my @RETURN;

        open (PROWL, "</etc/pocserv/dispatch/PROWL");

        my @LINES = <PROWL>;

        foreach (@LINES) {


                chomp($_);
                if ($_ =~ /$TYP/) {

                        my @ELEMS = split(/ /, $_);
                        push (@RETURN, $ELEMS[0]);
                }

        }



        close PROWL;

        print "PROWL API Keys eingelesen:\n";


       foreach (@RETURN) {

                print "  $TYP: $_\n";

        }

        return \@RETURN;


}



# einlesen aller Profile, gibt Array zutreffender Profile zurück
sub getprofiles {

	my $NUMBER = shift();
	my @RETURN;

	if ($NUMBER !~ /\d+/) {
		return \@RETURN;
	}

	open (PROFILE, "</etc/pocserv/dispatch/profile");
	my @LINES = <PROFILE>;

	print "Eingelesene Profile:\n";

       foreach (@LINES) {
	
		chomp($_);
                my @ELEMS = split(/\s*:\s*/, $_);

        
                if ($_ =~ /$NUMBER/) {
                        push (@RETURN, $ELEMS[0]);
                }

	        print "  $ELEMS[0]\n";

        }

	


       	print "RIC / Schleifennummer passt auf folgende Profile:\n";
        
        foreach (@RETURN) {
        
                print "  $NUMBER: $_\n";

        }





	close (PROFILE);


       return \@RETURN;


}
