ISPConfig 2.x: How To Add A "Move to SPAM"-Option To Your ISPConfig Installation

Want to support HowtoForge? Become a subscriber!
 
Submitted by Grey (Contact Author) (Forums) on Mon, 2008-09-22 18:18. :: Anti-Spam/Virus | ISPConfig | Linux | Control Panels | Email

ISPConfig 2.x: How To Add a "Move to SPAM"-Option To Your ISPConfig Installation

Version 1.0b
Author: Jens Groh <jens [dot] groh [at] novatech [dot] org>
Last edited 09/23/2008

Overview:

This document describes the steps needed to add a third spam filter strategy to your ISPConfig 2.x installation. It will allow you and your users to select, wether they want to drop spam, allow spam or move it to the corresponding IMAP junk mail folder.

This howto was inspired by the forum thread from 'grant' (http://www.howtoforge.com/forums/showthread.php?t=15704) who described exchanged the drop strategy with moving. By following the steps below you won't replace the drop method but gain a third one.

Prerequisites:

In this howto I will assume you are using the following or a similar configuration:

  • ISPConfig (v2.2.23 or later)
  • At least one domain with a mailbox set up and running
  • IMAP is using Maildir format (this will not work with mbox!)

Step 1 - Changing Forms

The first step is to log into your ISPConfig Panel with the Admin account. Then enter the Administration tab and open the Form Designer with Edit Form. Search for isp - ISP User and click edit.
You should now see the complete form description of that particular form. Look out for spam_strategy and hit edit again.
Now change the Value list of that form element by inserting a line for our future move option so it shows:

accept,accept;
move,move;
discard,discard;
and save that form. Your mail user options in your domain should now show that new option in the spam strategy dropdown box - even though it has no function yet. Now for the console part: Let's edit some ISPConfig files, to breathe life into the new option.

Step 2 - Editing ISPConfig Source Files

Now we have to edit a bunch of files. I'll just write them down and explain later:

vi /root/ispconfig/scripts/lib/classes/ispconfig_procmail.lib.php

(around line 109):
         if(!is_dir($web_path."/user/".$user_username."/Maildir"))
$mod->log->caselog("maildirmake
".$web_path."/user/".$user_username."/Maildir &> /dev/null", "maildirmake
".$web_path."/user/".$user_username."/Maildir &> /dev/null", __LINE__);
        // ADD: create IMAP subfolders
        if(!is_dir($web_path."/user/".$user_username."/Maildir/.Drafts"))
$mod->log->caselog("maildirmake
".$web_path."/user/".$user_username."/Maildir/.Drafts &> /dev/null",
"maildirmake ".$web_path."/user/".$user_username."/Maildir/.Drafts &>
/dev/null", __LINE__);
        if(!is_dir($web_path."/user/".$user_username."/Maildir/.Junk"))
$mod->log->caselog("maildirmake
".$web_path."/user/".$user_username."/Maildir/.Junk &> /dev/null",
"maildirmake ".$web_path."/user/".$user_username."/Maildir/.Junk &>
/dev/null", __LINE__);
        if(!is_dir($web_path."/user/".$user_username."/Maildir/.Sent"))
$mod->log->caselog("maildirmake
".$web_path."/user/".$user_username."/Maildir/.Sent &> /dev/null",
"maildirmake ".$web_path."/user/".$user_username."/Maildir/.Sent &>
/dev/null", __LINE__);
        if(!is_dir($web_path."/user/".$user_username."/Maildir/.Trash"))
$mod->log->caselog("maildirmake
".$web_path."/user/".$user_username."/Maildir/.Trash &> /dev/null",
"maildirmake ".$web_path."/user/".$user_username."/Maildir/.Trash &>
/dev/null", __LINE__);
 
     if(!is_dir($web_path."/user/".$user_username."/Maildir"))
$mod->log->phpcaselog(mkdir($web_path."/user/".$user_username."/Maildir",
0700), "create ".$web_path."/user/".$user_username."/Maildir", $this->FILE,
__LINE__);
 
        // ADD: create IMAP subfolders
        if(!is_dir($web_path."/user/".$user_username."/Maildir/.Drafts"))
$mod->log->phpcaselog(mkdir($web_path."/user/".$user_username."/Maildir/.Drafts",
0700), "create ".$web_path."/user/".$user_username."/Maildir/.Drafts",
$this->FILE, __LINE__);
        if(!is_dir($web_path."/user/".$user_username."/Maildir/.Junk"))
$mod->log->phpcaselog(mkdir($web_path."/user/".$user_username."/Maildir/.Junk",
0700), "create ".$web_path."/user/".$user_username."/Maildir/.Junk",
$this->FILE, __LINE__);
 	if(!is_dir($web_path."/user/".$user_username."/Maildir/.Sent"))
$mod->log->phpcaselog(mkdir($web_path."/user/".$user_username."/Maildir/.Sent",
0700), "create ".$web_path."/user/".$user_username."/Maildir/.Sent",
$this->FILE, __LINE__);
 	if(!is_dir($web_path."/user/".$user_username."/Maildir/.Trash"))
$mod->log->phpcaselog(mkdir($web_path."/user/".$user_username."/Maildir/.Trash",
0700), "create ".$web_path."/user/".$user_username."/Maildir/.Trash",
$this->FILE, __LINE__);
 
     exec("chown -R ".$user_username.":web".$web_doc_id."
".$web_path."/user/".$user_username."/Maildir");
     exec("chmod 700 ".$web_path."/user/".$user_username."/Maildir");
 
 	// ADD: create IMAP subfolders
 	exec("chown -R ".$user_username.":web".$web_doc_id."
".$web_path."/user/".$user_username."/Maildir/.Drafts");
 	exec("chmod 700 ".$web_path."/user/".$user_username."/Maildir/.Drafts");
 	exec("chown -R ".$user_username.":web".$web_doc_id."
".$web_path."/user/".$user_username."/Maildir/.Junk");
 	exec("chmod 700 ".$web_path."/user/".$user_username."/Maildir/.Junk");
 	exec("chown -R ".$user_username.":web".$web_doc_id."
".$web_path."/user/".$user_username."/Maildir/.Sent");
 	exec("chmod 700 ".$web_path."/user/".$user_username."/Maildir/.Sent");
 	exec("chown -R ".$user_username.":web".$web_doc_id."
".$web_path."/user/".$user_username."/Maildir/.Trash");
 	exec("chmod 700 ".$web_path."/user/".$user_username."/Maildir/.Trash");
 
     if($user["user_admin"]){
       exec("rm -f $web_path/Maildir &> /dev/null");
      
$mod->log->phpcaselog(@symlink($web_path."/user/".$user_username."/Maildir",
$web_path."/Maildir"), "symlink ".$web_path."/Maildir", $this->FILE,
__LINE__);

(around line 300)
   $mod->tpl->define( array(table    => "spamassassin.rc.master"));
 
   if(!isset($user["spam_strategy"])) $user["spam_strategy"] = "accept";

   if($user["spam_strategy"] == "accept"){
     $spam_comment = "#";
     $spam_command = ".Junk/";
   } else {
     $spam_comment = "";
     if($user["spam_strategy"] == "discard"){
       $spam_command = "/dev/null";
     } else {
       $spam_command = ".Junk/";
     }
   }

(around line 330)
  
if(is_file('/home/admispconfig/ispconfig/tools/spamassassin/usr/local/bin/spamassassin')){
@@ -302,6 +333,7 @@
   // Variablen zuweisen
   $mod->tpl->assign( array(PREFS_FILE =>
$web_path."/user/".$user_username."/.user_prefs",
                            SPAM_COMMENT => $spam_comment,
                            SPAM_COMMAND => $spam_command,
                            SPAMASSASSIN_PATH => $spamassassin_path,
                                                    USERNAME =>
$user_username));

This adds core functionality. The change adds users standard Maildir mailboxes like Trash, Sent, Junk and Drafts every time a mailuser is created, so we can move spam into the correpsonding junk folder.

Find and add the move string throughout the next files like shown:

vi /home/admispconfig/ispconfig/lib/classes/ispconfig_isp_user.lib.php

(around line 102)
                         $doc->deck[1]->elements[11]->visible = 0;
                  }
                  $doc->deck[2]->elements[2]->values["accept"] =
$go_api->lng("txt_accept");
                  $doc->deck[2]->elements[2]->values["move"] =
$go_api->lng("txt_move");
                  $doc->deck[2]->elements[2]->values["discard"] =
$go_api->lng("txt_discard");
         }

vi /home/admispconfig/ispconfig/web/mailuser/mail/table/user.table.php

(around line 135)
                                                                   'regex' 
                      => "",
                                                                   'errmsg'
               => "",
                                                                   'value' 
               => array(        "accept" => $app->lng("txt_accept"),
                                                                           
                                             "move"    =>
$app->lng("txt_move"),
                                                                           
                                             "discard" =>
$app->lng("txt_discard")));
 
 $table['spam_hits'] = array('datatype'                 => "VARCHAR",

vi /home/admispconfig/ispconfig/web/mailuser/lib/lang/.lng

(example for en.lng, around line 33)
 $wb["txt_accept"] = "accept";
 $wb["txt_move"] = "move";
 $wb["txt_discard"] = "discard";

vi /home/admispconfig/ispconfig/web/isp_manager/lib/lang/.lng

(example for en.lng, around line 268 and line 452)
 $wb["Spamfilter"] = 'Spamfilter';
 $wb["Antivirus"] = 'Antivirus';
 $wb["accept"] = 'accept';
 $wb["move"] = 'move';
 $wb["discard"] = 'discard';
 $wb["Spam Strategie"] = 'Spam Strategy';
 $wb["Spam Hits"] = 'Spam Hits';
(...)
 $wb["Keine Vorlage verwenden"] = "Individual Settings";
 $wb["Keine Vorlage vorhanden"] = "No Hostingplan Available";
 $wb["txt_accept"] = "accept";
 $wb["txt_move"] = "move";
 $wb["txt_discard"] = "discard";
 $wb["Eigenschaften"] = "Properties";
 $wb["ISP Datenbank"] = "ISP Database";

vi /home/admispconfig/ispconfig/web/isp_kunde/lib/lang/.lng

(example for en.lng, around line 268 and line 449)
 $wb["Spamfilter"] = 'Spamfilter';
 $wb["Antivirus"] = 'Antivirus';
 $wb["accept"] = 'accept';
 $wb["move"] = 'move';
 $wb["discard"] = 'discard';
 $wb["Spam Strategie"] = 'Spam Strategy';
 $wb["Spam Hits"] = 'Spam Hits';
(...)
 $wb["Keine Vorlage verwenden"] = "Individual Settings";
 $wb["Keine Vorlage vorhanden"] = "No Hostingplan Available";
 $wb["txt_accept"] = "accept";
 $wb["txt_move"] = "move";
 $wb["txt_discard"] = "discard";
 $wb["Eigenschaften"] = "Properties";
 $wb["ISP Datenbank"] = "ISP Database";

The last three changes you have to do per language file or your interface will just show move instead of the corresponding term in your language. The three example snippets are from the en.lng files in those directories, edit other languages the same way (insert the move entries between the others).

Step 3 - Create Customized Templates

Now change to the /root/ispconfig/isp/conf directory and create a custom copy of spamassassin.rc.master:

cd /root/ispconfig/isp/conf
cp -pr spamassassin.rc.master ./customized_templates/ cd customized_templates

Now edit the custom template to reflect the needed changes:

# All mail tagged as spam (eg. with a score higher than the set threshold)
# is moved to "/dev/null".
{SPAM_COMMENT}:0:
{SPAM_COMMENT}* ^X-Spam-Status: Yes
{SPAM_COMMENT}{SPAM_COMMAND}

Step 4 (optional) - Implement Cleaning of your trash and spam folders

This has already been adressed by MuisjeNL in the following HowTo: Automatic Cleaning Of Trash And/Or Junk Folders With ISPConfig (With Roundcube + Tmpreaper / Tmpwatch)

Step 5 (optional) - Implement some userfriendlyness and create status mails

This is under heavy developement as we're using some pretty hacked perl code at the moment. I'll try and clean that up or transfer it to a pretty little script.

Conclusion

So now you can add a mailuser (or change an existing) to use the new move method. I tested it with a user getting around 100-200 spam mails a day and changed his strategy to move. With a webmailer like roundcube or by connecting to the mailbox via IMAP I almost had immediate results as the first mails dropped into the junk folder rather than his inbox.

Make sure that you check this before actually running it on production servers.
This tutorials comes with no garantuee whatsoever. Be wise, test it!

As this is the first HowTo on HowtoForge for me (and I'm no native speaker), I hope you'll have no problems in adapting it to your needs. Have fun with a great Software: ISPConfig!

Jens Groh for SANlutions Ltd.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Fri, 2009-05-15 23:15.

I've installed this workround for move spam into junk folder, it's work correctly but there's only a problem, the spam mail can't delete, and an ordinary email if delete one of it i get an error..there's something of wrong?

 ** Normal email can't delete (move on a specific folder)

 ** Spam can't delete (move on a specific folder)

Submitted by Anonymous (not registered) on Mon, 2009-12-07 05:29.
Are you using roundcube mail when you are deleting email. I couldnt delete an email because roundcube didn't know about a 'Trash' folder to delete to.
Submitted by Anonymous (not registered) on Fri, 2009-07-31 17:35.
i've the same problem..do you have found a solution?
Submitted by mumbly (not registered) on Tue, 2008-09-23 07:41.

No files with .patch extansion in my /root/ispconfig/scripts/lib/classes/ folder of my 2.2.25 ispconfig brand new install.

But i have .php files. Use these ones ???

Submitted by Grey (registered user) on Tue, 2008-09-23 13:20.

...I used to make patch files with diff before writing that howto to better outline the changes to those files.

That should indeed be .php instead of .patch. I'll correct that.

Thankies