#!/usr/bin/perl # Copyright (c) 2003 by Matthias Schmidt # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # - Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # - Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided # with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # $Id: spam.pl,v 1.3 2003/10/09 09:12:05 ms Exp $ ############################################################################### # Add the following line to your crontab. This will send a spam report 5 min # after 6am every day # 5 6 * * * /app/home/user/spam.pl # Path to procmail.log (LOGFILE in .procmailrc) $procmail_log = "$ENV{'HOME'}/mail/log/procmail.log"; # Basename of your spam folder (eg. mine is /home/user/mail/spam) $spam_folder = "spam"; # Send spam report to local SMTP $rcpt = "$ENV{'USER'}"; # OR send it via sendmail to an external address #$rcpt = 'me@isp.com'; # Path to error log file $error_log = "$ENV{'HOME'}/spam.pl.error"; # mktemp(1) is not available on all systems, so we have to use this crappy # alternative $temp_mail = "$ENV{'HOME'}/.temp-spam"; ############################################################################### $datum = `date +%d.%m.%Y`; chomp($datum); $zeit = `date +%H:%M:%S`; chomp($zeit); $normal_counter = 0; $spam_mails = 0; sub parse_log; sub write_error; sub check_status; sub generate_report; ## # Parse procmail.log for new mails ## sub parse_log { check_status($procmail_log); open (fd, "<$procmail_log") or die write_error("Cannot open $procmail_log...\n"); while () { if (/From/) { $pos = index($_, ' ', 6); $temp = substr($_, 5, $pos - 5); $from[$spam_mails] = $temp; $normal_counter++; } if (/Subject:/) { $temp = substr($_, index($_, ":")+2); $subject[$spam_mails] = $temp; } if ((/Folder: $spam_folder/) || (/Folder: \/dev\/null/)) { chomp($from[$spam_mails]); chomp($subject[$spam_mails]); $from2[$spam_mails] = "From\t: $from[$spam_mails]\nSubject\t: $subject[$spam_mails]"; $spam_mails++; } } close (fd); } ## # Generate a little spam report ## sub generate_report { open (fd, ">$temp_mail") or die write_error("Cannot open $temp_mail...\n"); print fd "Report generated at $datum : $zeit\n\n"; print fd "-----------------------------------------------------------------\n"; print fd "Total number of received mails\t: $normal_counter\n"; print fd "Total number of spam mail\t: $spam_mails\n"; print fd "-----------------------------------------------------------------\n\n"; print fd "Information about the new mails:\n\n"; for ($h = 0; $h < $spam_mails; $h++) { print fd "$from2[$h]\n\n"; } print fd "-----------------------------------------------------------------\n\n"; close(fd); } ## # Log all errors ## sub write_error { my $msg = $_[0]; open (fd, ">>$error_log") or die "Cannot open $error_log...\n"; print fd "$datum - $zeit : $msg"; close (fd); } ## # Check stat(2) for a file ## sub check_status { my $file = $_[0]; if (!(-e $file)) { write_error("$file does not exists.\n"); exit 1; } if (!(-r $file)) { write_error("Cannot read $file.\n"); exit 1; } if (!(-o $file)) { write_error("I am not the owner of $file.\n"); exit 1; } if (-l $file) { write_error("$file is a symbolic link.\n"); exit 1 } if (-z $file) { write_error("$file is empty.\n"); exit 1; } } # Is temp_mail available on startup? if (-e $temp_mail) { write_error("Removing $temp_mail ...\n"); unlink $temp_mail or write_error("Cannot unlink $temp_mail...\n"); } parse_log(); # No new mails if ($spam_mails == 0) { write_error("No new spam mail -> quit.\n"); exit 1; } generate_report(); system("mail -s \"-- $spam_mails new spam emails\" $rcpt < $temp_mail"); unlink $temp_mail or write_error("Cannot delete $temp_mail...\n"); # Update via mailstat system("mailstat $procmail_log > /dev/null");