Mail Filter

Discussion in 'Installation/Configuration' started by Rescue9, Jul 6, 2009.

  1. Rescue9

    Rescue9 Member

    I can't understand how to get the mail filter to work with folders that contain spaces. I'm coming from an ISPConfig2 setup and the procmail.rc worked wonderfully with this, but .mailfilter doesn't want to play nice.

    I have multiple folders titled .Yahoo Groups.MyYahooGroup. In the procmailrc it was listed as \.Yahoo\ Groups\.MyYahooGroup. I've tried every configuration of that I could, and even searched the web. The only fix I could find was actually nesting the entire directory inside quotes, but this would require the user manually editing the .mailfilter as opposed to using the web frontend.

    Any help is appreciated!
     
  2. Rescue9

    Rescue9 Member

    Is there no way to use a folder name that has a space in it now?

    If this is not possible with the current setup, is it possible to rewrite the code that generates the .mailfilter file from the web front end. My understanding is that if the folder name is in quotes, then it should work correctly. If the script that generates the .mailfilter file rewrote the filter as a variable, rather than inputting the text directly inline, then it might be possible to use various folder names.

    MyFilterName="$DEFAULT/.Yahoo Groups.MyFolderName"

    I've gotten my information from various places, but here is the one that most accurately describes what I'm trying to say: http://www.nabble.com/Maildrop-TO-expression-td17189650.html
     
  3. till

    till Super Moderator Staff Member ISPConfig Developer

  4. Rescue9

    Rescue9 Member

    Bug Report submitted: #806
     
  5. Rescue9

    Rescue9 Member

    Forgive me if I start thinking aloud here, but I'm hoping that someone might be able to interpret my ramblings. Keep in mind that at this time THIS CODE DOES NOT WORK. DO NOT TRY TO USE IT. I'm going to try debugging this later myself and also manually edit the .mailfilter code to see if using a variable overcomes the space. If it does, then editing the code to use a variable should kill the bug.

    Looking at mail_user_filter_edit.php, it seems like if we take this:
    Code:
    		if($this->dataRecord["action"] == 'move') {
    		
    			$content .= "
    `test -e ".'$DEFAULT/.'.$this->dataRecord["target"]."`
    if ( ".'$RETURNCODE'." != 0 )
    {
      `maildirmake -f ".$this->dataRecord["target"].' $DEFAULT'."`
      `chmod -R 0700 ".'$DEFAULT/'.$this->dataRecord["target"]."`
      `echo INBOX.".$this->dataRecord["target"]." >> ".'$DEFAULT'."/courierimapsubscribed`
    }
    ";		
    		}
    and change it to something like this :
    Code:
                   $WhatIsMyTarget = $this->dataRecord["target"]
    		if($this->dataRecord["action"] == 'move') {
    		
    			$content .= "
    `test -e ".'$DEFAULT/.'.$WhatIsMyTarget."`
    if ( ".'$RETURNCODE'." != 0 )
    {
      `maildirmake -f ".$WhatIsMyTarget.' $DEFAULT'."`
      `chmod -R 0700 ".'$DEFAULT/'.$WhatIsMyTarget."`
      `echo INBOX.".$WhatIsMyTarget." >> ".'$DEFAULT'."/courierimapsubscribed`
    }
    ";		
    		}
    Then the spacing in the folder names should work. That all depends on whether the information in the nabble.com link works, and using a variable overcomes the spacing.
     
  6. till

    till Super Moderator Staff Member ISPConfig Developer

    This will not work as $this->dataRecord["target"]
    is also a variable like $WhatIsMyTarget, so your code does the same then the original code. I guess you mixed up that $this->dataRecord["target"] and $WhatIsMyTarget are variables interpreted and replaced by PHP when the code of the form is executed and they are not variables inerpreted by maildrop like $DEFAULT.
     
  7. Rescue9

    Rescue9 Member

    Till.... makes sense. What happens if we move the " $WhatIsMyTarget = $this->dataRecord["target"] " line into the $content section as such:

    Code:
                  
    		if($this->dataRecord["action"] == 'move') {
    		
    			$content .= "
     $WhatIsMyTarget = $this->dataRecord["target"]
    `test -e ".'$DEFAULT/.'.$WhatIsMyTarget."`
    if ( ".'$RETURNCODE'." != 0 )
    {
      `maildirmake -f ".$WhatIsMyTarget.' $DEFAULT'."`
      `chmod -R 0700 ".'$DEFAULT/'.$WhatIsMyTarget."`
      `echo INBOX.".$WhatIsMyTarget." >> ".'$DEFAULT'."/courierimapsubscribed`
    }
    ";		
    		}
    This way the $WhatIsMyTarget variable will be interpreted by maildrop instead of being interpreted by php, right? Also, I'm sure that my ' & " are so screwed up. If you could write the $WhatIsMyTarget variable as the $DEFAULT variable is written then it should be interpreted by maildrop. If not, how do we get the variable interpreted by maildrop instead of being interpreted by php?

    I'm going to try loading up the ISP3 server today and changing the .mailfilter file manually to see if this even works.
     
    Last edited: Jul 8, 2009
  8. Rescue9

    Rescue9 Member

    Ok, after much trial and error from a complete php noob, I've figured a few things out.

    First, I have verified that using a $variable inside the .mailfilter file works. I stripped out quite a bit of the .mailfilter file as follows:
    Code:
    ### BEGIN FILTER_ID:2
    
    if (/^Subject:.*\[BGTEditors\]/:h)
    {
    myfolder = "$DEFAULT/.Yahoo Groups.BGTEditors/"
    exception {
    to $myfolder
    }
    }
    ### END FILTER_ID:2
    
    Assigning myfolder must happen without $. Trying to assign it as $myfolder breaks it. Also, the $DEFAULT/.Yahoo Groups.BGTEditors/ MUST be wrapped in quotes. Not having these also breaks it.

    Now, not knowing how ISPConfig writes these rules, I can only assume that having multiple rules with only 1 $myfolder would break things. I plan on changing this variable to [filter_id]EndFolder as such; 2EndFolder. As each FilterID is unique, this should help avoid variables being named the same.

    Right now, I'm working on modifying the mail_user_filter_edit.php to write the .mailfilter as above. I'll work on changing the variable later.
     
  9. Rescue9

    Rescue9 Member

    Ok... here's a dirty little fix for the folder name problem.
    Code:
                    if($this->dataRecord["action"] == 'move') {
                            $content .= 'EndFolder = "$DEFAULT/.' . $this->dataRecord['target'] . '/"' . "\n";
                            $content .= "to " . '$EndFolder' . "\n";
                    } else {
                            $content .= "to /dev/null\n";
                    }
               
    
    I've tested it various times, and it seems to work properly if the folder is already created. I'm pretty sure it's not going to work if the folder doesn't exist as creating a folder with spaces in the name seems like it could be a problem in itself.

    I'm whipped... mind is frazzled. I'll post to bugtracker also. I know it's going to take a bit more work to make it stable. Still hoping someone else will take my ideas and run with them.
     
  10. Rescue9

    Rescue9 Member

    Changed it around a bit more so that the variables are unique. Something didn't work right when I tried it last night though. All the emails went to the last box in the .mailfilter file. I think I may have screwed things up by manually deleting the .mailfilter file and messing up sections of the db.

    Anyway, here's the final code for mailbox's that have CREATED folders with spaces. Once again, this is likely not to work with folders that need creating by the script. I'll work on that at a later date, or the devs can modify this to suit their needs. I'm going to completely reinstall and verify operation.


    Code:
                    if($this->dataRecord["action"] == 'move') {
                            $content .= 'ID' . "$this->id" . 'EndFolder = "$DEFAULT/.' . $this->dataRecord['target'] . '/"' . "\n";
                            $content .= "to ". '$ID' . "$this->id" . 'EndFolder' . "\n";
                    } else {
                            $content .= "to /dev/null\n";
                    }
    
                    $content .= "}\n";
                    $content .= "}\n";
    
    
     
  11. Rescue9

    Rescue9 Member

    Another Edit

    I've messed with the code a bit more to make it more friendly for the maildirmake area. I'm actually not sure it works yet as my ISPC3 server is currently on the inside network. I'll hook it up to the outside network tomorrow and test out the code during the day.

    Here are the changes I've made to the code:
    Code:
            function getRule() {
    
                    $content = '';
                    $content .= '### BEGIN FILTER_ID:'.$this->id."\n";
    
                    $TargetNoQuotes = $this->dataRecord["target"];
                    $TargetQuotes = "\"$TargetNoQuotes\"";
    
                    $TestChDirNoQuotes = '$DEFAULT/.'.$TargetNoQuotes;
                    $TestChDirQuotes = "\"$TestChDirNoQuotes\"";
    
                    $MailDirMakeNoQuotes = $TargetQuotes.' $DEFAULT';
    
                    $EchoTargetFinal = $TargetNoQuotes;
    
    
                    if($this->dataRecord["action"] == 'move') {
    
                    $content .= "
    `test -e ".$TestChDirQuotes."`
    if ( ".'$RETURNCODE'." != 0 )
    {
      `maildirmake -f $MailDirMakeNoQuotes`
      `chmod -R 0700 ".$TestChDirQuotes."`
      `echo \"INBOX.$EchoTargetFinal\" >> ".'$DEFAULT'."/courierimapsubscribed`
    }
    ";
                    }
    
                    $content .= "if (/^".$this->dataRecord["source"].":";
    
    

    Here is the output of the code box:
    Code:
    ### BEGIN FILTER_ID:8
    
    `test -e "$DEFAULT/.Yahoo Groups.BGT"`
    if ( $RETURNCODE != 0 )
    {
      `maildirmake -f "Yahoo Groups.BGT" $DEFAULT`
      `chmod -R 0700 "$DEFAULT/.Yahoo Groups.BGT"`
      `echo "INBOX.Yahoo Groups.BGT" >> $DEFAULT/courierimapsubscribed`
    }
    if (/^Subject:.*\[backpackgeartest\]/:h)
    {
    exception {
    ID8EndFolder = "$DEFAULT/.Yahoo Groups.BGT/"
    to $ID8EndFolder
    }
    }
    ### END FILTER_ID:8
    
    Wrapping the code in quotes effectively handles mail folders with spaces. I've also verified that all commands work as written and that the quotes don't break the command.

    I'm not quite clear on how maildrop handles the .mailfilter file enough yet, but one of the things that I'm worried about is the 'test -e' line. My understanding of the test cmd is that it doesn't return anything. If this is the case, then the $RETURNCODE variable should always be null. how can you evaluate a null variable.

    Maybe the line should read:
    Code:
    `test -e ".$TestChDirQuotes." && exit 1 || exit 0`
    if ( ".'$RETURNCODE'." != 1 )
    
    Till or Falko care to comment on this test statement? Am I thinking correctly, or does maildrop interpret this correctly as is?
     
    Last edited: Jul 12, 2009
  12. Rescue9

    Rescue9 Member

    I'm up and running on the ISPC3 server. I've tested the following code and verify that this works. If the directory doesn't exist, maildrop runs the commands and makes the directory.
    Code:
    `test -e ".$TestChDirQuotes." && exit 1 || exit 0`
    if ( ".'$RETURNCODE'." != 1 )
    My only problem now is the fact that ALL mail is sent to the first folder in the .mailfilter list. There must be a problem with this code.
    Code:
    if (/^Subject:.*\[backpackgeartest\]/:h)
    EDIT: The problem isn't necessarily with the above code. The problem seems to be that when ISPC3 rewrites the rules it doesn't always \ out the brackets. I tried creating rules without brackets and it worked perfectly. However, when I put brackets in there, the first rule in the .mailfilter file didn't have brackets, but the second did. This explains why all the mail is going to the first filter in the list even if it's not supposed to be matching.
     
    Last edited: Jul 13, 2009

Share This Page