This patch allows you to enable spam reporting in imp/conf.php
set a spam@address.com , and optionally spam delete enable.

When a user clicks "Report as Spam" in message.php, the message is
redirected to the spam@address.com without modifying/adding any headers
(such as Reset headers when using Redirect function in IMP), except for a
extra Received line which doesn't hurt much..

This patch was very useful for me to allow to report spam directly to
sa-learn (from SpamAssassin) to teach the bayesian classifier server-wide

If using maildrop as the delivery agent (recommended):
Simply, in the spam@address.com maildir put a .mailfilter with 3 lines like:

exception {
  cc "| /usr/local/bin/sa-learn --spam --local --single --no-rebuild"
}

And run a cron regularly to rebuild the bayes database:
0       */2     *       *       *       vpopmail        /usr/local/bin/sa-learn --rebuild


Tim Hasson
tim@aidasystems.com
Jan 28, 2004


--- imp-3.2.1/message.php	Thu Jan 16 14:29:02 2003
+++ /www/htdocs/webmail/web/mail/message.php	Thu Jan 29 02:21:18 2004
@@ -402,37 +402,107 @@
      if (!$conf['spam']['reporting']) {
          break;
      }
+     /* Pipe message through spam reporting program (SpamAssassin) */
+     if($conf['spam']['program']) {
+
+         $raw_msg = imap_fetchheader($imp['stream'], $index, FT_UID) . "\n" . imap_body($imp['stream'], $index, FT_UID);
+
+         $pipe = popen($conf['spam']['program'], 'w');
+         fwrite($pipe, $raw_msg);
+         pclose($pipe);
+
+  	 Horde::raiseMessage(_("This message has been reported as spam."), HORDE_MESSAGE);
+         break;
+
+     }
+
 
-     /* Fetch the raw message contents (headers and complete body). */
-     $raw_msg = imap_fetchheader($imp['stream'], $index, FT_UID) . "\n" .
-         imap_body($imp['stream'], $index, FT_UID);
-
-     /* Build the MIME structure. */
-     include_once HORDE_BASE . '/lib/MIME/Message.php';
-     include_once HORDE_BASE . '/lib/MIME/Part.php';
-
-     $mime = new MIME_Message($raw_msg);
-     $body = new MIME_Part('text/plain', $raw_msg);
-     $mime->addPart($body);
-     $msg = $mime->toString();
-
-     $headers['Message-ID'] = '<' . uniqid(time() . '.') . '@' . $_SERVER['SERVER_NAME'] . '>';
-     $headers['Date'] = date('r');
-     $headers['To'] = $conf['spam']['email'];
-     $headers['From'] = $user_identity->getFromLine();
      $recipients = $conf['spam']['email'];
-     $headers['Subject'] = _("Spam Report from") . ' ' . $imp['user'];
-     $headers = $mime->header($headers);
+     $orig_headers = @imap_fetchheader($imp['stream'], $index, FT_UID);
+     $orig_headers = str_replace("\r", '', $orig_headers);
+     $header_arr = explode("\n", $orig_headers);
+
+     foreach ($header_arr as $header) {
+         if (!empty($header) && $header != "\n") {
+             if (preg_match('|^([-\w]*): (.*)|', $header, $pieces)) {
+                 if (isset($headers[$pieces[1]])) {
+                     $headers[$pieces[1]] .= "\n" . $header;
+                 } else {
+                     $headers[$pieces[1]] = $pieces[2];
+                 }
+                 $last = $pieces[1];
+              } else {
+                 $headers[$last] .= "\n" . $header;
+             }
+         }
+     }
+
+
      IMP::addSiteHeaders($headers);
+     $msg = @imap_body($imp['stream'], $index, FT_UID);
 
      include_once 'Mail.php';
      if (!empty($imp['smtphost'])) {
          $conf['mailer']['params']['host'] = $imp['smtphost'];
      }
-     $mailer = &Mail::factory($conf['mailer']['type'], $conf['mailer']['params']);
-     $mailer->send($recipients, $headers, $msg);
+
+       $params = IMP::prepareMailerParams();
+       $mailer = &Mail::factory($conf['mailer']['type'], $params);
+       $status = $mailer->send(MIME::encodeAddress($recipients), $headers, $msg);
+
 
      Horde::raiseMessage(_("This message has been reported as spam to your system administrator."), HORDE_MESSAGE);
+     /* Delete Spam after report */
+     if ($conf['spam']['reporting_delete']) {
+
+
+       if (!IMP_Message::delete($index)) {
+           $array_index++;
+           $index = $sorted[$array_index];
+       } else {
+          if ($prefs->getValue('use_trash')) {
+               /* Nuke the message we've just deleted from the
+                  message array. */
+               $sorted = array_merge(array_slice($sorted, 0, $array_index),
+                                     array_slice($sorted, $array_index + 1));
+               $imp['msgl'] = implode(':', $sorted);
+               $imp['msgcount']--;
+               $index = $sorted[$array_index];
+           } elseif (strstr($imp['protocol'], 'pop3')) {
+               /* IMP_Message::delete() takes care of expunging the
+                  mailbox to make the delete stick; now we just need
+                  to recalculate the message list. */
+               if ($prefs->getValue('sortby') != SORTTHREAD) {
+                   $sorted = imap_sort($imp['stream'], $prefs->getValue('sortby'), $prefs->getValue('sortdir'), SE_UID);
+               } else {
+                   IMP_Message::threadSort();
+               }
+               $sorted = IMP_Message::range($sorted, $array_index + $imp['offset'] + 1,
+                                            $imp['offset'], $array_index, $imp['msgcount']);
+               $imp['msgl'] = implode(':', $sorted);
+               $index = $sorted[$array_index];
+           } elseif ($prefs->getValue('delhide')) {
+               if ($prefs->getValue('sortby') != SORTTHREAD) {
+                   $sorted = imap_sort($imp['stream'], $prefs->getValue('sortby'),
+                                       $prefs->getValue('sortdir'), SE_UID, 'UNDELETED');
+               } else {
+                   IMP_Message::threadSort();
+               }
+               $sorted = IMP_Message::range($sorted, $array_index + $imp['offset'] + 1,
+                                            $imp['offset'], $array_index, $imp['msgcount']);
+               $imp['msgl'] = implode(':', $sorted);
+               $index = $sorted[$array_index];
+           } else {
+               $array_index++;
+               $index = $sorted[$array_index];
+           }
+       }
+       if ($prefs->getValue('mailbox_return')) {
+           header('Location: ' . Horde::applicationUrl('mailbox.php?start=' . ($array_index + $imp['offset'] + 1), true));
+           exit;
+       }
+
+     }
      break;
 
  case ADD_ADDRESS:
