diff -up smeserver-learn-1.0/root/etc/e-smith/db/configuration/defaults/Learn/Verbose.VERBOSE smeserver-learn-1.0/root/etc/e-smith/db/configuration/defaults/Learn/Verbose --- smeserver-learn-1.0/root/etc/e-smith/db/configuration/defaults/Learn/Verbose.VERBOSE 2016-03-17 01:21:14.985000000 -0400 +++ smeserver-learn-1.0/root/etc/e-smith/db/configuration/defaults/Learn/Verbose 2016-03-17 01:21:36.424000000 -0400 @@ -0,0 +1 @@ +enabled diff -up smeserver-learn-1.0/root/usr/bin/Learn.pl.VERBOSE smeserver-learn-1.0/root/usr/bin/Learn.pl --- smeserver-learn-1.0/root/usr/bin/Learn.pl.VERBOSE 2016-03-17 01:20:18.570000000 -0400 +++ smeserver-learn-1.0/root/usr/bin/Learn.pl 2016-03-17 01:19:16.000000000 -0400 @@ -57,6 +57,15 @@ binmode(SADB); my $md5 = Digest::MD5->new->addfile(SADB)->b64digest; close(SADB); +#Verbose mode +my $verbose= $sadb->get_prop("Learn", "Verbose") || "enabled"; +my $outputfile=($verbose eq "disabled")? "/dev/null": '/tmp/LearnLog.txt'; +my $LOG; +open ( $LOG, '+>', $outputfile) unless ($verbose eq "enabled"); +# select new filehandle +select $LOG unless ($verbose eq "enabled"); +my $currentuser; + # Running for every user foreach my $user (@users) { my $firstname = $user->prop('FirstName'); @@ -68,9 +77,10 @@ foreach my $user (@users) { next if ( defined @exclude and @found = grep { $_ eq $key } @exclude ); next unless ( ! defined @include or @found = grep { $_ eq $key } @include ); - printf("Checking for user (%s): %s %s\n", $key,$firstname, $lastname); + $currentuser = sprintf("Checking for user (%s): %s %s\n", $key,$firstname, $lastname); + print $currentuser unless ($verbose eq "active"); - my $MailDir = ($key eq "admin")? "/home/e-smith" . "/Maildir" : "/home/e-smith/files/users/" . $key . "/Maildir"; + my $MailDir = ($key eq "admin")? "/home/e-smith" . "/Maildir" : "/home/e-smith/files/users/" . $key . "/Maildir"; my @modes = ("LearnAsSpam","LearnAsHam","LearnInWL"); foreach my $mode (@modes) { @@ -80,7 +90,7 @@ foreach my $user (@users) { # getting dir name to search according to actual mode my $dirname = $sadb->get_prop($mode, "dir"); if ( !(defined($dirname)) ) { - print "Errors in DB, dir subkey not present for key $mode\n"; + Vact();print "Errors in DB, dir subkey not present for key $mode\n"; next; } # adding heading periode if missing as this is an IMAP folder @@ -97,7 +107,7 @@ foreach my $user (@users) { if (! @logdirs and ($sadb->get_prop($mode, "Uniq") eq "enabled")) { ($login,$pass,$uid,$gid) = getpwnam($key) or die "$key not in passwd file"; - print "+->mkdir :". $MailDir . "/" . $dirname . "\n"; + Vact();print "+->mkdir :". $MailDir . "/" . $dirname . "\n"; foreach $a ('','/cur','/tmp','/new'){ mkdir $MailDir . "/" . $dirname . "$a"; chown $uid,$gid,$MailDir . "/" . $dirname . "$a"; @@ -112,7 +122,7 @@ foreach my $user (@users) { $a=(substr($a, 0, 1) eq '.')? $a: ".$a"; my $Link= $MailDir . "/" . $a; if (! -e $Link) { - print "+->create link $Link on $junkdir\n"; + Vact(); print "+->create link $Link on $junkdir\n"; symlink($junkdir, $Link) or die "Can't create symlink from $Link to $junkdir: $!"; }; } @@ -123,7 +133,7 @@ foreach my $user (@users) { if ($mode eq "LearnAsSpam" and $sadb->get_prop($mode, "DeleteAfterLearn") eq "enabled" and $DelayToMove >0) { my $SpamDir = $MailDir . "/" . $logdir . "/cur/"; return unless (-e $SpamDir and -d $SpamDir); - @files=(); + @files=(); find(\&wanted, $junkdir ."/cur" ); find(\&wanted, $junkdir ."/new" ) if ($sadb->get_prop($mode, "LearnNew") eq "enabled" or $sadb->get_prop($mode, "LearnNew") eq "junkmail"); sub wanted { @@ -137,8 +147,8 @@ foreach my $user (@users) { my %filehash=@files; #puts the array into a hash for easy printing. Key=file, Value=date undef @files; foreach $filename (sort keys %filehash){ - print "--> moving $filename $filehash{$filename}\n"; - move $filename,"$MailDir/$dirname/cur" ; + Vact(); print "--> moving $filename $filehash{$filename}\n"; + move $filename,"$MailDir/$dirname/cur" ; } } @@ -152,11 +162,11 @@ foreach my $user (@users) { find( { wanted => sub { -f && $_ =~ m/($logdir\/cur|$logdir\/new|$logdir\/tmp)/ && $counter++;}, no_chdir => 1, follow_fast => 1 }, $SpamDir ); if ($mode eq "LearnAsSpam" and $counter>0) { my $result = `/usr/bin/sa-learn --spam $SpamDir`; - chomp($result); printf("+Learning Spam from %s: %s\n",$logdir,$result); + chomp($result); Vact(); printf("+Learning Spam from %s: %s\n",$logdir,$result); } elsif ($mode eq "LearnAsHam" and $counter>0) { my $result = `/usr/bin/sa-learn --ham $SpamDir`; - chomp($result);printf("+Learning Ham from %s: %s\n",$logdir,$result); + chomp($result); Vact(); printf("+Learning Ham from %s: %s\n",$logdir,$result); } @list= ($sadb->get_prop($mode, "LearnNew") eq "enabled") ? ('/cur/','/new/'):('/cur/'); @@ -176,7 +186,7 @@ foreach my $user (@users) { #taking action according to actual mode if ($mode eq "LearnInWL") { - printf("+Learning in WhiteList: %s\n",$filetolearnbash); + Vact(); printf("+Learning in WhiteList: %s\n",$filetolearnbash); `/usr/bin/LearnInWL.pl $filetolearnbash`; } # if we are in LearnAsSpam mode and DeleteAfterLearn is enabled delete message, else tagging a move message @@ -234,3 +244,21 @@ if ($newmd5 ne $md5) { print "\n"; } +# restore STDOUT +select STDOUT; +# read log and print on STDOUT if mode active +if ($verbose eq "active"){ + seek $LOG, 0, 0; + while (<$LOG>) { + print $_; + } +} +close($LOG) unless ($verbose eq "enabled"); +unlink $outputfile; + +# sub to print the user line if something happen on active verbose mode +sub Vact() { +return unless ($verbose eq "active"); +print $currentuser; +$currentuser=""; +}