Subversion Repositories sysadmin_scripts

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 rodolico 1
#! /usr/bin/perl -w
2
 
3
# getMailFoldersToProcess
4
# Author: R. W. Rodolico
5
# 20131031
6
#
7
#    This program is free software: you can redistribute it and/or modify
8
#    it under the terms of the GNU General Public License as published by
9
#    the Free Software Foundation, either version 3 of the License, or
10
#    (at your option) any later version.
11
#
12
#    This program is distributed in the hope that it will be useful,
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
#    GNU General Public License for more details.
16
#
17
#    You should have received a copy of the GNU General Public License
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
# 
20
# script will update two files, CLEANUP_FILE and $SA_LEARN_FILE
21
# $CLEANUP_FILE contains directories to be processed by clean_old_files script
22
#    you should have a script that will go through these directories and removes
23
#    old files. One possibility is the cleanup script available with this
24
#    program.
25
# $SA_LEARN_FILE contains directories to be processed by sa_learn script
26
#    At a minimum, you should run the following command to process these files
27
#    sa-learn --spam --progress -f $SA_LEARN_FILE
28
#    where $SA_LEARN_FILE is the same file as defined here and
29
#    --progress is used only for interactive processing (shows progress)
30
 
31
print "Getting folders to process\n";
32
 
33
my $ROOT_DIR = '/var/vmail';
34
my @SPAM_FOLDER_NAME = ( '.thisisspam' );
35
my @HAM_FOLDER_NAME = ( '.thisisnotspam' );
36
my @JUNK_FOLDER_NAME = ( '.junk', '.trash', '.deleted items' );
37
my $CONTROL_FOLDER = '/opt/mailmanage/data';
38
my $CLEANUP_FILE = 'mail_cleanup.list';
39
my $SA_LEARN_FILE = 'sa_learn.list';
40
my @SUBDIRS_TO_PROCESS = ( 'new', 'cur' );
41
my $CONFIG_FILE_NAME = 'mailmanage.cfg';
42
 
43
my @IGNORE_ROOT = ( 'somedomain.com' );
44
 
45
sub getScriptLocation {
46
   use strict;
47
   use File::Spec::Functions qw(rel2abs);
48
   use File::Basename;
49
   return dirname(rel2abs($0));
50
}
51
 
52
sub readConfig {
53
   my $scriptLocation = &getScriptLocation();
54
   my $return = '';
55
   if ( -e "$scriptLocation/$CONFIG_FILE_NAME" ) {
56
      open CFG,"<$scriptLocation/$CONFIG_FILE_NAME" or die "Could not read $scriptLocation/$CONFIG_FILE_NAME: $!\n";
57
      $return = join ( '', <CFG> );
58
      close CFG;
59
   }
60
   return $return;
61
}
62
 
63
$config = &readConfig();
64
eval( $config );
65
 
66
# now we have all the variables set, calculate full path to files
67
$CLEANUP_FILE = "$CONTROL_FOLDER/$CLEANUP_FILE";
68
$SA_LEARN_FILE = "$CONTROL_FOLDER/$SA_LEARN_FILE";
69
 
70
# build our ignore regex. this should build a string similar to
71
# ((client1)|(client2)|(client3))
72
my $ignore_regex = '((' . join( ')|(', @IGNORE_ROOT ) . '))';
73
 
74
# make sure the control folder exists
75
`mkdir -p $CONTROL_FOLDER` unless -e $CONTROL_FOLDER;
76
 
77
my @spam_dirs = ();
78
my @ham_dirs = ();
79
my @junk_dirs = ();
80
# find all the directories we want
81
foreach my $folder ( @SPAM_FOLDER_NAME ) {
82
   my @temp = `find $ROOT_DIR -type d -iname "$folder"`;
83
   chomp @temp;
84
   @temp = grep  !/$ignore_regex/,  @temp;
85
   @spam_dirs = ( @spam_dirs, @temp );
86
}
87
foreach my $folder ( @HAM_FOLDER_NAME ) {
88
   my @temp = `find $ROOT_DIR -type d -iname "$folder"`;
89
   chomp @temp;
90
   @temp = grep  !/$ignore_regex/,  @temp;
91
   @ham_dirs = ( @ham_dirs, @temp );
92
}
93
foreach my $folder ( @JUNK_FOLDER_NAME ) {
94
   my @temp = `find $ROOT_DIR -type d -iname "$folder"`;
95
   chomp @temp;
96
   @temp = grep  !/$ignore_regex/,  @temp;
97
   @junk_dirs = ( @junk_dirs, @temp );
98
}
99
 
100
# now, find all the ones that need to be processed by sa-learn as spam
101
my @output = ();
102
foreach my $dir ( @spam_dirs ) { # do all the spam folders
103
   foreach my $subdir ( @SUBDIRS_TO_PROCESS ) { # create a separate entry for each subfolder
104
      push @output, "spam:dir:$dir/$subdir";
105
   }
106
}
107
 
108
# find all the ham folders for sa-learn
109
foreach my $dir ( @ham_dirs ) {
110
   foreach my $subdir ( @SUBDIRS_TO_PROCESS ) { # create a separate entry for the subfolders
111
      push @output, "ham:dir:$dir/$subdir";
112
   }
113
}
114
# dump the sa-learn stuff to the correct file
115
open OUT, ">$SA_LEARN_FILE" or die "Could not write to $SA_LEARN_FILE: $!\n";
116
#print OUT "# List of directories to be processed by sa-learn\n";
117
print OUT join( "\n", @output ) . "\n";
118
close OUT;
119
 
120
# every so often, we will clean up some directories. In this case, we are 
121
# cleaning up the junk folder, the found spam folder and the found ham folder
122
@output = ();
123
foreach my $dir ( @spam_dirs, @ham_dirs, @junk_dirs ) { # join all the directories we found
124
   foreach my $subdir ( @SUBDIRS_TO_PROCESS ) { # add the subfolders to it
125
      push @output, "$dir/$subdir";
126
   }
127
}
128
 
129
# dump the output to the cleanup file
130
open OUT, ">$CLEANUP_FILE" or die "Could not write to $CLEANUP_FILE: $!\n";
131
print OUT "# List of directories to be auto-cleaned\n";
132
print OUT join( "\n", @output ) . "\n";
133
close OUT;
134
 
135
print "# List of directories to be auto-cleaned\n";
136
print join( "\n", @output ) . "\n";
137
 
138
1;