I couldn't find anyone who has done this already, so I did it myself -
anyone who needs this is welcome to use my solution/code.  My solution
requires an IMAP server and bayes to be in mysql.  It also requires
SquirrelMail.  It also requires a /tmp directory.  Since squirrelmail
requires a unix-like system (I belive) and IMAP, you should be all set.

I'm not using Exim - I run Postfix, but that shouldn't make a difference.
I use amavis-new/sa with squirrelmail on top.  I downloaded the
squirrelmail amavisnewsql 0.7.2-1.4 plugin and modified it from there.

The plugin includes a "whitelist user" and other sa controls for the
specific user, but totally misses the bayes aspect of spamassassin.  The
plugin takes some time to configure, but is simple if you just follow the
directions.

Once you get that working, replace the setup.php and create a new
bayes.php in the plugins/amavisdnewsql directory.  I've attached the
bayes.php and setup.php files as .txt files, so remove the .txt extension.

Once you do that, on the top of every message, there will be a "This is
spam" and "This is NOT spam" link.  It will issue a
"/usr/local/bin/sa-learn -D --[sp|h]am", so make sure sa-learn is in this
directory.

***IMPORTANT:  One last thing - make sure you turn on the
"bayes_sql_override_username <user that runs spamassassin or spamd>" in
local.cf or your bayes database will only work for the user that the
webserver runs as.  It took me a while to figure this one out...

If you have any questions or problems with this, please email me.

Keith Hackworth
[EMAIL PROTECTED]

>
> I'm trying to start using Bayes and sa-learn for the first time, now
> that Bayes supports SQL.
>
> I run a smallish system (about 80 users spread over three domains).
> The basic setup is Exim -> SpamAssassin 3 -> Exim -> amavis -> Exim ->
> delivery. (That is -- SA and amavis are Exim router-transport pipes;
> neither knows of the other's existence.)
>
> Apart from me, none of my users have home directories; Exim uses SQL
> for all account information. Mail is stored in Maildir format in
> /mail/DOMAIN/USER.
>
> The majority of my users use Squirrelmail.
>
> I would like to enable some sort of false-negative/false-positive
> reporting for them, as I would imagine that the Bayes system is not
> very useful if it's getting uncorrected FN/FP data. However, every
> piece of documentation I've seen for sa-learn assumes (1) a unix
> account to correspond to the mailbox owner, and (2) that SQL is not
> being used for anything.
>
> Can someone point me in the right direction? I'd really like to take
> advantage of Bayes, but the documentation is so haphazard right now
> that I just don't know what to do.
>
>
>
>
> --
> Daniel Drucker / [EMAIL PROTECTED]
>
>
<?php

/*
 * AmavisNewSQL - AmavisNew+SQL+SpamAssassin+Quarantine+This is [not] spam 
plugin for SquirrelMail
 * By Jared Watkins and slightly modified by Keith Hackworth (sorry Jared)
 */

function amavisnewsql_version()
{
  return '0.7.2';
}

include(SM_PATH.'plugins/amavisnewsql/config.php');


function squirrelmail_plugin_init_amavisnewsql () {
  include(SM_PATH.'plugins/amavisnewsql/config.php');

  global $squirrelmail_plugin_hooks;

  $squirrelmail_plugin_hooks['optpage_register_block']['amavisnewsql'] = 
'amavisnewsql_optpage_register_block';

  $squirrelmail_plugin_hooks['read_body_header_right']['amavisnewsql'] = 
'amavisnewsql_address_add';



  if($CONFIG["use_quarantine"]) {
     $squirrelmail_plugin_hooks['menuline']['amavisnewsql'] = 
'amavisnewsql_spam_quarantine';
     #$squirrelmail_plugin_hooks['left_main_after']['amavisnewsql'] = 
'amavisnewsql_spam_quarantine';
  }

}


function amavisnewsql_address_add() {  // Borrowed from address_add plugin
    global $message;
    global $passed_id;
    global $mailbox;
    if (!$message || !isset($message)) return;

    $header = $message->rfc822_header;
    $decodedfrom = $header->getAddr_s('from');

    $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
    $Host_RegExp_Match = '(' . $IP_RegExp_Match . 
'|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
    $Email_RegExp_Match = '[0-9a-z]([-_.+|]?[_0-9a-z|])*(%' . 
$Host_RegExp_Match . ')?@' . $Host_RegExp_Match;
    $regs = array();
    while (eregi($Email_RegExp_Match, $decodedfrom, $regs)) {
       $decodedfrom = substr(strstr($decodedfrom, $regs[0]), strlen($regs[0]));
       $fromaddress = urlencode($regs[0]);
    }

    echo " | ";
    bindtextdomain ('amavisnewsql', SM_PATH . 'plugins/amavisnewsql/locale');
    textdomain ('amavisnewsql');

    displayInternalLink 
("plugins/amavisnewsql/amavisnewsql.php?action=add_edit_wb_address&WorB=W&priority=7&address=$fromaddress",
 _("Whitelist Sender"), 'right');
    echo " | ";
    bindtextdomain ('amavisnewsql', SM_PATH . 'plugins/amavisnewsql/locale');
    textdomain ('amavisnewsql');
    displayInternalLink 
("plugins/amavisnewsql/bayes.php?action=bayes_learn&type=s&passed_id=$passed_id&mailbox=$mailbox",
 _("This is Spam"), 'right');
    echo " | ";
    bindtextdomain ('amavisnewsql', SM_PATH . 'plugins/amavisnewsql/locale');
    textdomain ('amavisnewsql');
    displayInternalLink 
("plugins/amavisnewsql/bayes.php?action=bayes_learn&type=h&passed_id=$passed_id&mailbox=$mailbox",
 _("This is NOT Spam"), 'right');
    echo " ";

    bindtextdomain ('squirrelmail', SM_PATH . 'locale');
    textdomain ('squirrelmail');


}


function amavisnewsql_optpage_register_block () {
  global $optpage_blocks;

    bindtextdomain ('amavisnewsql', SM_PATH . 'plugins/amavisnewsql/locale');
    textdomain ('amavisnewsql');

    $optpage_blocks[] =
    array (
           'name' => _("SpamAssassin Configuration"),
           'url'  => '../plugins/amavisnewsql/amavisnewsql.php',
           'desc' => _("Here you may define your own white/black lists and 
customize your spam scoring rules."),
           'js'   => FALSE);
    bindtextdomain ('squirrelmail', SM_PATH . 'locale');
    textdomain ('squirrelmail');

}


function amavisnewsql_spam_quarantine () {
    bindtextdomain ('amavisnewsql', SM_PATH . 'plugins/amavisnewsql/locale');
    textdomain ('amavisnewsql');

    displayInternalLink ('plugins/amavisnewsql/quarantine.php', _("Spam"), '');

    bindtextdomain ('squirrelmail', SM_PATH . 'locale');
    textdomain ('squirrelmail');

    #echo "<p align=center><a target=\"right\" 
href=\"../plugins/amavisnewsql/quarantine.php\">Quarantine</a>";

}

?>
<?php

/**
 * bayes.php
 *
 * This is written by Keith Hackworth.  Feel free to use this code as you wish.
 * 
 * This is the code to train bayes from SquirrelMail.
 *
 * $Id: bayes.php,v 1.1 2004/09/23 15:57:39 $
 */

/* Path for SquirrelMail required files. */
define('SM_PATH','../../');

/* SquirrelMail required files. */
require_once(SM_PATH . 'include/validate.php');
require_once(SM_PATH . 'functions/global.php');
require_once(SM_PATH . 'functions/imap.php');
require_once(SM_PATH . 'functions/html.php');
require_once(SM_PATH . 'functions/url_parser.php');

function report_spam($imapConnection,$id, $passed_ent_id, $type) {
    global $uid_support;

    $read=sqimap_run_command ($imapConnection, "FETCH $id BODY[HEADER]", true, 
$a, $b, $uid_support);
        $procid=uniqid(rand());
        $file="/tmp/salearn.$procid";
        $handle=fopen($file,"w+");
    $cnum = 0;
    for ($i=1; $i < count($read); $i++) {
        $line=$read[$i];
        fputs($handle,"$line");
    }
    $read=sqimap_run_command ($imapConnection, "FETCH $id BODY[TEXT]", true, 
$a, $b, $uid_support);
    $cnum = 0;
    for ($i=1; $i < count($read); $i++) {
        $line= $read[$i];
        fputs($handle,"$line");
    }
    sqimap_logout($imapConnection);
        fclose($handle);
        $out=shell_exec("cat $file | /usr/local/bin/sa-learn -D --$type");
        unlink($file);
}

/* get global vars */
if ( sqgetGlobalVar('passed_id', $temp, SQ_GET) ) {
  $passed_id = (int) $temp;
}
sqgetGlobalVar('type', $type, SQ_GET);
if ($type=='s') {
        $type="spam";
}
else {
        $type="ham";
}
if ( sqgetGlobalVar('mailbox', $temp, SQ_GET) ) {
  $mailbox = $temp;
}
if ( !sqgetGlobalVar('passed_ent_id', $passed_ent_id, SQ_GET) ) {
  $passed_ent_id = '';
} 
sqgetGlobalVar('key',        $key,          SQ_COOKIE);
sqgetGlobalVar('username',   $username,     SQ_SESSION);
sqgetGlobalVar('onetimepad', $onetimepad,   SQ_SESSION);
sqgetGlobalVar('delimiter',  $delimiter,    SQ_SESSION);

$imapConnection = sqimap_login($username, $key, $imapServerAddress, 
                               $imapPort, 0);
$mbx_response = sqimap_mailbox_select($imapConnection, $mailbox, false, false, 
true);

$header = report_spam($imapConnection,$passed_id, $passed_ent_id, $type); 
if ($type=="spam") {
        
header("Location:../../src/delete_message.php?mailbox=$mailbox&message=$passed_id&startMessage=1");
}
else {
        
header("Location:../../src/read_body.php?mailbox=$mailbox&passed_id=$passed_id&startMessage=1");
}
?>

Reply via email to