#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
use IO::File;
use Mail::Message;
use Email::Valid;
use Mail::Message::Attachment::Stripper;
my $mail = Mail::Message->read(\*STDIN);
my $stripper = Mail::Message::Attachment::Stripper->new($mail);
my $now_string = strftime "%F_%H:%M:%S", localtime;
my Mail::Message $msg = $stripper->message;
my @attachments = $stripper->attachments;
#my $msg_file = new IO::File "/home/pr/tmp/att/" . $now_string . "--message", "w" or die "Can not create msg file!";
#print $msg_file $msg;
# prolezeme prilohy zajima nas text/plain s nazvem Kontakty
foreach my $a( @attachments) {
next if ($a->{content_type} !~ /text\/plain/i && $a->{filename} !~ /kontakty/i) ;
# uloznime attachmenty do history storage
my $f = new IO::File "/home/pr/tmp/att/" . $now_string . "--" . $a->{filename}, "w" or die "Can not create history file!";
print $f $a->{payload};
# prechroupeme tak aby tam zustal jen seznam email adres
$_ = $a->{payload};
tr/A-Z/a-z/;
# splitnem stream kvuli Email::Valid a overime
@_ = split();
foreach ($_) {
$_ = Email::Valid->address($_);
};
# ulozime do konecneho souboru
my $senders_file = new IO::File "/home/pr/tmp/att/.rzp_senders.txt", "w" or die "Can not create rzp_senders file!";
# zpatlame mailovou notifikaci (oznameni o provedenem update)
print "Pokus o aktualizaci @ $now_string temito udaji:\n\n---\n";
foreach (@_) {
print $senders_file "$_\n";
print "$_\n";
};
print "---\n";
};