Converting .maildelivery rules to .procmailrc

  Subject: Re: Converting .maildelivery rules to procmail
  From: Diego Zamboni
  Date: Fri, 20 Oct 2000 10:34:46 -0500 (08:34 PDT)

I used Anderson's program as a base, and modified it to result in the one I'm attaching. It splits fields according to the proper format of the .maildelivery file (using the Text::ParseWords module), and handles both ^ and > actions. Of course, it doesn't understand all the possible formats of the .maildelivery file, but it converted mine quite well. I removed the part that prints a header because I already had a .procmailrc with some definitions, but you can uncomment that if you want.

Enjoy,

--Diego

Note - I added support for the + action and generalized the qpipe case. One thing I had to fix up by hand was that my qpipe commands called programs in my bin directory as "bin/mumble" but I needed to specify "$HOME/bin/mumble" in my .procmailrc file

--Brent Welch

  #!perl -w
  # maild2procm.pl - (convert of .maildelivery file to .procmailrc format
  # Reads from argument or stdin, outputs to stdout.
  # Based on a program by Anderson McCammont.
  # Modified by Diego Zamboni, Oct 2000.
  # Modified by Brent Welch, Dec 2003
  use Text::ParseWords;
  use strict;
  # put in some standard header lines
  #&printhead();
  while (<>) {
      # procmail TO rules cover cc as well
      next if (/^cc/i);
      # pass comments through
      if (/^\#/) { print ; next; };
      chomp;
      my @w=parse_line('\s+', undef, $_);
      if (@w != 5) {
        warn "Weird line in .maildelivery: $_";
        next;
      }
      my ($type, $pat, $action, $result, $string)=@w;
  #    ($type,$pat,$junk,$junk,$junk,$junk,$fold) = split();
  #    $fold =~ tr/+\"\n//d;
      # we'll deal with default 'the procmail way'
      next if ($type =~ /default/);
      #print "type:$type\tpat:$pat\tfold:$fold\n";
      $pat=~s/\[/\\\[/;
      $pat=~s/\]/\\\]/;
      my $extra_flags="";
      $extra_flags.="c" if $result eq "R";
      # This doesn't cover all the cases yet, but at least some common ones.
      if ($action eq "^" || $action eq "qpipe") {
        my @cmd=split(' ',$string);
        if ($cmd[0] =~ /rcvstore$/) {
          my $folder=$cmd[1];
          $folder=~tr/\+//d;
          print ":0 w$extra_flags :$folder/\$LOCKEXT\n";
          if ($type =~ /to/i) {
            # ^TO is special and matches cc etc
            print "* ^TO$pat\n";
          } else {
            print "* ^",ucfirst($type),".*$pat\n";
          }
          print "| rcvstore +$folder\n\n";
        } else {
          print ":0 w$extra_flags :inbox/\$LOCKEXT\n";
          if ($type =~ /to/i) {
            # ^TO is special and matches cc etc
            print "* ^TO$pat\n";
           } else {
            print "* ^",ucfirst($type),".*$pat\n";
          }
          print "| $string\n\n";
        }
      }
      elsif ($action eq "+") {
        my $folder=$string;
        $folder=~tr/\+//d;
        print ":0 w$extra_flags :$folder/\$LOCKEXT\n";
        if ($type =~ /to/i) {
          # ^TO is special and matches cc etc
          print "* ^TO$pat\n";
        } else {
          print "* ^",ucfirst($type),".*$pat\n";
        }
        print "| rcvstore +$folder\n\n";
      }
      elsif ($action eq ">") {
        print ":0 $extra_flags\n";
        if ($type =~ /to/i) {
        # ^TO is special and matches cc etc
        print "* ^TO$pat\n";
        } else {
        print "* ^",ucfirst($type),".*$pat\n";
        }
        print "$string\n\n";
      }
      else {
        warn "Don't know how to handle this line: $_";
        next;
      }
  }
  # and a standard catch-all last rule
  &printtrail();
  sub printhead {
      print <<'EndOfHead';
  # man procmailrc for format procmailex for examples
  MAILDIR=$HOME/Mail
  VERBOSE=off
  LOGFILE=$MAILDIR/procmail.log
  # get the mh utils on your path
  PATH=$PATH:/ms/dist/fsf/PROJ/mh/6.8.3/etc
  # no biff notification
  COMSAT=no
  # paranoia (or testing) - mkdir $MAILDIR/back and uncommment this
  #:0 c
  #backup
  #:0 ic
  #| cd backup && rm -f dummy `ls -t msg.* | sed -e 1,32d`
  # remove dups
  :0 Wh: msgid.lock
  | formail -D 8192 msgid.cache
  # * ^TOmatches cc etc (man procmailrc)
  EndOfHead
  }
  sub printtrail {
      print <<'EndOfTrail';
  ####################################
  # DEFAULT ACTION - DON'T REMOVE THIS
  ####################################
  :0 w: inbox/$LOCKEXT
  | rcvstore +inbox
  EndOfTrail
  }


Updated on 11 Dec 2003, 04:52 GMT
Search - Recent Changes - 2 References - Index - Go to Beedub's Wiki - Help