#! /usr/bin/perl -w
# getMailFoldersToProcess
# Author: R. W. Rodolico
# 20131031
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# script will update two files, CLEANUP_FILE and $SA_LEARN_FILE
# $CLEANUP_FILE contains directories to be processed by clean_old_files script
# you should have a script that will go through these directories and removes
# old files. One possibility is the cleanup script available with this
# program.
# $SA_LEARN_FILE contains directories to be processed by sa_learn script
# At a minimum, you should run the following command to process these files
# sa-learn --spam --progress -f $SA_LEARN_FILE
# where $SA_LEARN_FILE is the same file as defined here and
# --progress is used only for interactive processing (shows progress)
print "Getting folders to process\n";
my $ROOT_DIR = '/var/vmail';
my @SPAM_FOLDER_NAME = ( '.thisisspam' );
my @HAM_FOLDER_NAME = ( '.thisisnotspam' );
my @JUNK_FOLDER_NAME = ( '.junk', '.trash', '.deleted items' );
my $CONTROL_FOLDER = '/opt/mailmanage/data';
my $CLEANUP_FILE = 'mail_cleanup.list';
my $SA_LEARN_FILE = 'sa_learn.list';
my @SUBDIRS_TO_PROCESS = ( 'new', 'cur' );
my $CONFIG_FILE_NAME = 'mailmanage.cfg';
my @IGNORE_ROOT = ( 'somedomain.com' );
sub getScriptLocation {
use strict;
use File::Spec::Functions qw(rel2abs);
use File::Basename;
return dirname(rel2abs($0));
}
sub readConfig {
my $scriptLocation = &getScriptLocation();
my $return = '';
if ( -e "$scriptLocation/$CONFIG_FILE_NAME" ) {
open CFG,"<$scriptLocation/$CONFIG_FILE_NAME" or die "Could not read $scriptLocation/$CONFIG_FILE_NAME: $!\n";
$return = join ( '', );
close CFG;
}
return $return;
}
$config = &readConfig();
eval( $config );
# now we have all the variables set, calculate full path to files
$CLEANUP_FILE = "$CONTROL_FOLDER/$CLEANUP_FILE";
$SA_LEARN_FILE = "$CONTROL_FOLDER/$SA_LEARN_FILE";
# build our ignore regex. this should build a string similar to
# ((client1)|(client2)|(client3))
my $ignore_regex = '((' . join( ')|(', @IGNORE_ROOT ) . '))';
# make sure the control folder exists
`mkdir -p $CONTROL_FOLDER` unless -e $CONTROL_FOLDER;
my @spam_dirs = ();
my @ham_dirs = ();
my @junk_dirs = ();
# find all the directories we want
foreach my $folder ( @SPAM_FOLDER_NAME ) {
my @temp = `find $ROOT_DIR -type d -iname "$folder"`;
chomp @temp;
@temp = grep !/$ignore_regex/, @temp;
@spam_dirs = ( @spam_dirs, @temp );
}
foreach my $folder ( @HAM_FOLDER_NAME ) {
my @temp = `find $ROOT_DIR -type d -iname "$folder"`;
chomp @temp;
@temp = grep !/$ignore_regex/, @temp;
@ham_dirs = ( @ham_dirs, @temp );
}
foreach my $folder ( @JUNK_FOLDER_NAME ) {
my @temp = `find $ROOT_DIR -type d -iname "$folder"`;
chomp @temp;
@temp = grep !/$ignore_regex/, @temp;
@junk_dirs = ( @junk_dirs, @temp );
}
# now, find all the ones that need to be processed by sa-learn as spam
my @output = ();
foreach my $dir ( @spam_dirs ) { # do all the spam folders
foreach my $subdir ( @SUBDIRS_TO_PROCESS ) { # create a separate entry for each subfolder
push @output, "spam:dir:$dir/$subdir";
}
}
# find all the ham folders for sa-learn
foreach my $dir ( @ham_dirs ) {
foreach my $subdir ( @SUBDIRS_TO_PROCESS ) { # create a separate entry for the subfolders
push @output, "ham:dir:$dir/$subdir";
}
}
# dump the sa-learn stuff to the correct file
open OUT, ">$SA_LEARN_FILE" or die "Could not write to $SA_LEARN_FILE: $!\n";
#print OUT "# List of directories to be processed by sa-learn\n";
print OUT join( "\n", @output ) . "\n";
close OUT;
# every so often, we will clean up some directories. In this case, we are
# cleaning up the junk folder, the found spam folder and the found ham folder
@output = ();
foreach my $dir ( @spam_dirs, @ham_dirs, @junk_dirs ) { # join all the directories we found
foreach my $subdir ( @SUBDIRS_TO_PROCESS ) { # add the subfolders to it
push @output, "$dir/$subdir";
}
}
# dump the output to the cleanup file
open OUT, ">$CLEANUP_FILE" or die "Could not write to $CLEANUP_FILE: $!\n";
print OUT "# List of directories to be auto-cleaned\n";
print OUT join( "\n", @output ) . "\n";
close OUT;
print "# List of directories to be auto-cleaned\n";
print join( "\n", @output ) . "\n";
1;