Spamreporter 0.1b for courier imap/pop3 maildir\'s

<?php
/* 
    Spamreporter 0.1b by Robert[@]Klikics[.de] 
    License: GPL
    Changed: 2006-26-03

    Usage: Just edit the CONFIG and add a cronjob
           which runs the script i.e. daily.

    Info:  Actually only tested with Courier-IMAP maildir's 
           ("cur" and "new" - folders will be checked) and Postfix
           with virtual-users (needs Delivered-To in mailheader).
           
    Please mail any additions or bugs to me ...
*/


/* CONFIG */

// Spamfolders to check
$folders[1] = "/home/rob/Maildir/.Spam/";
$folders[2] = "/home/mailuser/robert/Maildir/.Spam/";
// $folders[3] = "";


// sender for reports
$sender = "postmaster@supermario.klikics.de";


// strip ***SPAM*** or anything else from subject in reports?
$strip = true;     // true|false


// pattern to strip in report-subjects (i.e. ***SPAM***)
// only needed if $strip = true
$strip_pattern = "***SPAM***";


// path to statusfile (must be writeable!)
$statfile = "/tmp/spamreport.status";


/* END OF CONFIG */


/* Do not change anything below */

foreach($folders as $folder) {

    $ms = "";
    unset($mails, $cnt, $firstcnt, $fcnt, $dummy);

    // "cur"-folder
    $fp = opendir($folder."cur");

    while($mail = readdir($fp)) {

        // ignore . and ..
        if($mail != "." && $mail != "..")   {
            
            $mails[] = "cur/".$mail;
            $firstcnt++;
        }
    }
    
    closedir($fp);

    // "new"-folder
    $fp = opendir($folder."new");

    while($mail = readdir($fp)) {

        // ignore . and ..
        if($mail != "." && $mail != "..")   {

            $mails[] = "new/".$mail;
            $firstcnt++;
        }
    }

    closedir($fp);

    // break if no mails found
    if(!$firstcnt)  continue;

    // reverse array to list newer mails on top
    $mails = array_reverse($mails);

    // get infos from mailfiles
    foreach($mails as $file) {
        
        $handle = fopen($folder.$file, "r");

        while($line = fgets($handle)) {

               // extract interesting lines
               if( preg_match("/^To:/i", $line) )           $raw_to = $line;
               if( preg_match("/^From:/i", $line) )         $from   = $line;
               if( preg_match("/^Subject:/i", $line) )      $subj   = $line;
               if( preg_match("/^Date:/i", $line) )         $date   = $line;
               if( preg_match("/^X-Spam-Score:/i", $line) ) $score  = $line;
               if( preg_match("/^Delivered-To:/i", $line) ) $raworig= $line; 
          }
     

     // extract recipient from "Delivered-To:" for sending spamreport
     $dummy = explode(":", $raworig);
     $to    = trim($dummy[1]);

     // get time of last info from statfile
     foreach( file($statfile) as $sline ) {

           if( preg_match("/".$to."/i", $sline) ) {

                $dummy   = explode(",", $sline);
                $lastnfo = $dummy[1]; 
                break;
           }
      }

     $ftime = filectime($folder.$file);

     if($ftime > $lastnfo) {
    
        // strip subject
        if($strip) {

            $subj = str_replace($strip_pattern, "", $subj);
            $subj = str_replace("  ", " ", $subj);  // also strip not needed whitespaces
         }

        // build infostring
        $str = "-----------------------\n";
        $str.= $from.$raw_to.$subj.$date.$score;
        $str.= "-----------------------\n\n";
        $ms .= $str;

        fclose($handle);
        $cnt++; 
       
      }
      
      }     // end of file-loop



      if($cnt) {

        // write statusfile
        $newfile = "";
        
        foreach( file($statfile) as $statline ) {

            if( preg_match("/^".$to."/i", $statline) )   {  $newfile.= $to.",".time()."\n"; $fcnt++; }
            else                                         {  $newfile.= $statline; }

         }

        if(!$fcnt) $newfile.= $to.",".time()."\n";   // new entry for this email in statfile

        $fp = fopen($statfile, "w+");
        fwrite($fp, $newfile, strlen($newfile));
        fclose($fp);

        // build & send mail
        $head  = "From: $sender";
        $date  = date("Y-m-d H:i", $lastnfo);
        $msg   = "Since the last spamreport at $date there are $cnt new spams in your spamfolder:\n\n" . $ms;
        mail($to, "[Spamreport] New spams trapped at $to!", $msg, $head);       
    }
    
}

clearstatcache();

?>

Powered by klikics.de