Author |
|
sirarthur Newbie
Joined: 06 March 2010
Online Status: Offline Posts: 3
|
Posted: 07 March 2010 at 10:19pm | IP Logged
|
|
|
This is a hack to make Spamassassin to work along with Afterlogic, made and tested on Debian 5 lenny.
You'll need Spamassassin, spamc and php5-cgi
In lenny just type:
Code:
apt-get install spamassassin spamc php5-cgi |
|
|
Next make sure you activate spam filter on Afterlogic adminpanel (you need this for it to create the .Spam folders) and make sure Afterlogic XMail is running on the same user:group of apache - or you'll need to manually chmod the .Spam folders. You'll also need to have the exec command available on PHP.
Also edit the spamd initscript (/etc/init.d/spamassassin) and add --allow-tell switch to DOPTIONS
Now login to webmail and check if you already have the Spam (Junk) mail box.
Next you'll need to hack Afterlogic Webmail, open the file %webmail%/common/wmserver/class_wmserver.php
Locate the function:
Code:
function SpamMessage($user, $domain, $fullMsgPath, $isSpamAccount)
{ |
|
|
and right after the open bracellet add:
Code:
/* EXEC SPAMC */
exec("spamc -L spam < $fullMsgPath"); |
|
|
Next the function:
Code:
function NotSpamMessage($user, $domain, $fullMsgPath, $isSpamAccount)
{ |
|
|
and add:
Code:
/* EXEC SPAMC */
exec("spamc -L ham < $fullMsgPath");
|
|
|
to the top of it.
This will allow your Spamassassin to learn from user input (when you press that mark as junk button).
Now go to XMail MailRoot and create a new file, on it type:
Code:
#!/bin/bash
touch /tmp/spamwait
/var/MailRoot/filters/spamassassin.filter.php $1 $2 &
while [ -f /tmp/spamwait ]
do
echo "waiting" >> /dev/null
done
exit 7 |
|
|
save as spamassassin.sh and chmod a+x on it.
This file will call and daemonize our php script.
Make a backup of the original antispam.tab file.
Next create another file and write in it:
Code:
"executor" "/bin/bash" &nb sp; "/var/MailRoot/filters/spamassassin.sh" @@FILE @@RRCPT
|
|
|
(those are Tabs not Spaces between each argument and is just one line)
save it as antispam.tab
Now the PHP script you need to create, executor would break the spamassassin direct input so we need to do it on another way.
Code:
#!/usr/bin/php-cgi
<?php
ini_set("max_exec_time",600);
$File = $argv[1];
$To = $argv[2];
$MaxFS = 5 * 1048576;
$size = filesize($File);
if($size > $MaxFS){
exit(3);
$logline = "\r\n[ ".date("d-m-Y H:i:s")." ] - Mail to $To - Too big. Not checked!";
file_put_contents("/var/MailRoot/log s/spamassassin-" . date("dmY") . ".log",$logline,FILE_APPEND);
if(file_exists("/tmp/spamwait")) unlink("/tmp/spamwait");
}
$f = file($File);
$pos = 1;
$restoreFromSA = "";
$originalHeader = "";
$body = "";
$beforeHeader = true;
$inHeader = true;
$inRestore = true;
$restoreArray = array();
$MainInput = "";
foreach($f as $l){
if($beforeHeader == true){
$MainInput .= $l;
if(strstr($l,"<<MAIL-DATA>>")) $beforeHeader = false;
continue;
}
if(strstr($l,"Received:") && $inRestore == true) $inRestore = false;
if($inRestore == false) $pos++;
if($pos <= 5){
if(trim($l)) $restoreFromSA .= $l;
if(trim($l)) $restoreArray[] = rtrim($l);
}
if($inHeader == true){
if($l == "\r\n"){
$inHeader = false;
}else{
$originalHeader .= $l;
}
}else{
$body .= $l;
}
}
$tmpFile = md5(rand() . time());
while(file_exists("/tmp/$tmpFile")){
$tmpFile = md5(rand() . time());
}
file_put_contents("/tmp/$tmpFile",$originalHeader . "\r\n" . $body);
exec("/usr/bin/spamc -u www-data < /tmp/$tmpFile",$res);
unlink("/tmp/$tmpFile");
$result = $restoreFromSA;
$hpos = 1;
$isSpam = false;
$inHead = true;
foreach($res as $r){
if(strstr($r,"X-Spam-Status: Yes")) {
$isSpam = true;
}
if($hpos >= 3) {
if($inHead == true && !in_array($r,$restoreArray) && trim($r)) {
$result .= "$r";
if(!strstr($r,"\r\n")) $result .= "\r\n";
}elseif($inHead == false){
$result .= $r;
if(!strstr($r,"\r\n")) $result .= "\r\n";
}
}
if(strstr($r,"X-Spam-Prev-Subject:") ) {
$inHead = false;
$result .= "\r\n";
}
$hpos++;
}
$markas = $isSpam == true ? "SPAM" : "NOT SPAM";
$logline = "\r\n[ ".date("d-m-Y H:i:s")." ] - Mail to $To - Marked as $markas";
file_put_contents("/var/MailRoot/logs/spamassassin-" . date("dmY") . ".log",$logline,FILE_APPEND);
if(file_exists("/tmp/spamwait")) unlink("/tmp/spamwait");
$try = 0;
$test = false;
if($isSpam == true){
while($test == false && $try < 20){
$test = replaceSpam();
$try++;
}
}
function replaceSpam(){
global $File,$To,$result;
//Let XMail put the mail on the inbox
sleep(3); //wait a few seconds
$fname = end(explode("/",$File));
$domU = explode("@",$To);
$dir = "/var/MailRoot/domains/";
if(is_dir($dir . strtolower($domU{1}))){
$dir .= strtolower($domU[1]);
}else{
return false;
}
if(is_dir($dir . "/" . strtolower($domU[0]))){
$dir .= "/" . strtolower($domU[0]) . "/Maildir/new/";
}else{
return false;
}
if(is_dir($dir)){
$dh = opendir($dir);
$filefound = false;
$fnames = explode(".",$fname);
$fname_in = $fnames[0] . ".";
$fname_out = "." . $fnames[2] . "." . $fnames[3];
while (($file = readdir($dh)) !== false && $filefound == false) {
if(is_dir($dir . $file)) continue;
if(strstr($file,$fname_in) && strstr($file,$fname_out)){
$NewFile = $dir . $file;
$FileName = $file;
$filefound = true;
}
}
if($filefound == false) return false;
}else{
return false;
}
file_put_contents($dir . "../.Spam/new/$FileName",$result);
unlink($NewFile);
return true;
}
?>
|
|
|
Note in this line:
exec("/usr/bin/spamc -u www-data < /tmp/$tmpFile",$res);
you may need to replace www-data with the user under apache is running. This is to link the learning with the tests.
Save the php file as spamassassin.filter.php on your filters folder.
Now check if all users have a .Spam folder along with its' folder structure, specially .Spam/new and that XMail user can write to it.
When you're ready to go, uncomment the antispam.tab line on your filters.in.php and that's it.
|
Back to Top |
|
|
sirarthur Newbie
Joined: 06 March 2010
Online Status: Offline Posts: 3
|
Posted: 08 March 2010 at 2:19am | IP Logged
|
|
|
Sorry, correct this on the php script:
Code:
if($size > $MaxFS){
exit(3);
$logline = "\r\n[ ".date("d-m-Y H:i:s")." ] - Mail to $To - Too big. Not checked!";
file_put_contents("/var/MailRoot/log s/spamassassin-" . date("dmY") . ".log",$logline,FILE_APPEND);
if(file_exists("/tmp/spamwait")) unlink("/tmp/spamwait");
}
|
|
|
To:
Code:
if($size > $MaxFS){
$logline = "\r\n[ ".date("d-m-Y H:i:s")." ] - Mail to $To - Too big. Not checked!";
file_put_contents("/var/MailRoot/log s/spamassassin-" . date("dmY") . ".log",$logline,FILE_APPEND);
if(file_exists("/tmp/spamwait")) unlink("/tmp/spamwait");
exit(3);
}
|
|
|
Otherwise the shell script would never exit if you receive an email over 5Mb.
Really sorry, happens when you do things over night.
|
Back to Top |
|
|