Results 1 to 10 of 25

Thread: Any way to add message filters from command line?

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    58
    Rep Power
    8

    Default Any way to add message filters from command line?

    Guys,

    I have a rather large procmail config and I'd like to convert it to Zimbra filters. Is there any way to add a filter (message filter) to an account from the command line?

    Thanks!
    Jbwiv

  2. #2
    phoenix is offline Zimbra Consultant & Moderator
    Join Date
    Sep 2005
    Location
    Vannes, France
    Posts
    23,587
    Rep Power
    58

    Default

    Yes, search the forums for 'sieve'.
    Regards


    Bill


    Acompli: A new adventure for Co-Founder KevinH.

  3. #3
    Join Date
    Feb 2007
    Posts
    58
    Rep Power
    8

    Default

    Ok, got it working, thanks. For anyone else trying to do this, here's a quick and dirty perl script that'll convert your procmail ^TO rules to sieve rules. Mind you, I hacked this quickly and it assumes some things about your .procmailrc AND the paths to your folders (for example, it assumes you have .folder style courier folders), so PLEASE check your results before running. I make not guarantees it'll work at all for you.

    You essentially run it passing the path of your procmailrc as the first arg, zimbra account name as second arg, and then direct it's output to a file. Then, run this file as a shell script...

    Hope it helps:

    Code:
    #!/usr/bin/perl
    
    $file = $ARGV[0];
    $acct = $ARGV[1];
    
    sub output {
    $list = shift;
    $folder = shift;
    print <<EOL
    # $list
    if anyof (header :contains "to" "$list" ,
     header :contains "cc" "$list" )
    {
        fileinto "/$folder";
        stop;
    }
    EOL
    }
    
    print "#!/bin/bash\nzmprov ma $acct zimbraMailSieveScript '\n";
    print 'require ["fileinto", "reject", "tag", "flag"];' . "\n";
    open(FH, $file) or die "Can't open $file\n";
    $to = "";
    $fo = "";
    while (<FH>) {
            $line = $_;
            if ($line=~/\^TO_(.*)/) {
                    $to = $1;
                    $line = <FH>;
                    $fo = $1 if ($line=~/\.(.*)\/new/);
            }
    
            if ($fo && $to) {
                    output($to, $fo);
                    $to = "";
                    $fo = "";
            }
    }
    print "'\n";

  4. #4
    Join Date
    Aug 2005
    Posts
    1,433
    Rep Power
    12

    Default Wiki?

    Pretty cool ! Any chance you could post this to the Migration wiki for posterity at some point?
    Bugzilla - Wiki - Downloads - Before posting... Search!

  5. #5
    Join Date
    Feb 2007
    Posts
    58
    Rep Power
    8

    Default

    Sure...when things slow down this week I'll do just that...

  6. #6
    phoenix is offline Zimbra Consultant & Moderator
    Join Date
    Sep 2005
    Location
    Vannes, France
    Posts
    23,587
    Rep Power
    58

    Default

    Yes, that's a neat solution.
    Regards


    Bill


    Acompli: A new adventure for Co-Founder KevinH.

  7. #7
    Join Date
    Sep 2006
    Location
    Davis, CA
    Posts
    64
    Rep Power
    9

    Post using zmprov to set mail filter sieve scripts

    My rules didn't comply for that conversion script, but once you have your rules converted, below is a way to stick them into Zimbra.

    Create a file like the following and pipe it to zmprov. Note that the newlines inside the value for zimbraMailSieveScript must be escaped:
    Code:
    ma jdoe zimbraMailSieveScript '\
    require ["fileinto", "reject", "tag", "flag"];\
    # Helpdesk Tickets\
    if anyof (header :contains "subject" "[Lib Help #" )\
    {\
        tag "Tickets";\
        flag "flagged";\
        stop;\
    }\
    '
    Maybe you have a filter you want to pre-populate for a set of workstation support people. Create a file with your filters in it, like this call it filters.tmpl:
    Code:
    require ["fileinto", "reject", "tag", "flag"];\
    # Helpdesk Tickets\
    if anyof (header :contains "subject" "[Lib Help #" )\
    {\
        tag "Tickets";\
        flag "flagged";\
        stop;\
    }\
    Then you can pre-populate users' filters like this:
    Code:
     cat users.txt | while read user; do
         echo "ma $user zimbraMailSieveScript '\\" >> filters.zmp
         cat filters.tmpl >> filters.zmp
         echo "'" >> filters.zmp
     done
    Now pipe filters.zmp to zmprov.

    If you have tags in your filters like my example, you'll need to pre-create those. Put them in a file called tags.tmpl (one per line) and do this.
    Code:
     cat users.txt | while read user; do
        echo "sm $user" >> tags.zmp
        cat tags.tmpl | while read tag; do
             echo "createTag '$tag'" >> tags.zmp
         done
         echo "exit" >> tags.zmp 
    done
    Now pipe tags.zmp to zmprov. Note, you should create the tags before the filters.
    Last edited by dlbewley; 02-28-2007 at 05:41 PM. Reason: fix logic error in tags script

  8. #8
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    6

    Default

    I'd like to give my contribution to this discussion.

    We've recently migrated from Postfix+Dovecot to Zimbra and we had the same issue with filter rules.

    We had this kind of input:

    Code:
    #### machinename ##### 
    if address :is "From" "root@machinename.domain.tld" { 
      # CRON 
      if header :contains ["Subject"] ["Cron <root@machinename.domain.tld>"] { 
        fileinto "INBOX.machinename.cron"; 
        stop; 
      } 
      # LOGWATCH 
      if header :contains ["Subject"] ["Logwatch for"] { 
        fileinto "INBOX.machinename.logwatch"; 
        stop; 
      } 
      # CATCHALL 
      fileinto "INBOX.machinename"; 
      stop; 
    } 
    ##################
    So we solved the situation with this perl script:
    Code:
    #!/usr/bin/perl -w
    
    use warnings;
    use strict;
    
    open(FILE,"dovecot.sieve") || die ('unable to read rules');
    my @line=<FILE>;
    close(FILE);
    
    my @rule;
    my $index=0;
    
    foreach my $val (@line) {
            next if $val =~ m/^$/;
            if ($val =~ m/#### (.*) #####/) {
                    $rule[$index]{machine} = $1;  
            }
            if ($val =~ m/if header :contains \["Subject"\] \["Cron <(.*@.*)>"\]/) {
                    $rule[$index]{cron} = $1; 
            }
            if ($val =~ m/##################/) {
                    $index++;
            }
    }
    
    for my $i ( 0 .. $#rule ) { 
    
                    my $machine=$rule[$i]{machine};
                    my $mailcron=$rule[$i]{cron};
    
                    print "zmmailbox -a admin -p accountpass -m accountname afrl $machine.cron header \"Subject\" contains \"Cron <root\@$machine>\" fileinto /Inbox/$machine.cron\n";
                    print "zmmailbox -a admin -p accountpass -m accountname afrl $machine.logwatch header  \"Subject\" contains \"Logwatch for\" fileinto /Inbox/$machine.logwatch\n";
                    print "zmmailbox -a admin -p accountpass -m accountname afrl $machine.catchall header \"From\" contains \"root\@$machine.hermess.it\" fileinto /Inbox/$machine\n";
    }
    A little bit...raw, but it works!

Similar Threads

  1. Add Sound to Filters
    By switchisp in forum Users
    Replies: 4
    Last Post: 09-12-2013, 01:07 PM
  2. need advice on configuring zimbra to work with fax server
    By pheonix1t in forum Administrators
    Replies: 0
    Last Post: 07-11-2007, 07:46 PM
  3. Message filters - bulk tagging / filing
    By murky in forum Users
    Replies: 1
    Last Post: 02-08-2007, 11:57 PM
  4. Replies: 42
    Last Post: 08-11-2006, 09:50 AM
  5. Replies: 4
    Last Post: 08-10-2006, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •