2000-03-02-12:53:32 Phil Staub:
> 2000-03-01-20:34:48 Bennett Todd:
> >     f=`date +%s`.$$.`hostname`
> >     cd $maildir
> >     cat >tmp/$f
> >     mv tmp/$f new/
>
> Assuming you were going to use this written as is (i.e., in
> Bourne shell) how would this be incorporated into a procmail
> recipe? Would you literally put this in the .procmailrc file, or
> write a shell script and invoke it from .procmailrc?

Whew! What a question! Warning, this is going to drag on for a bit,
I feel a major torrent welling up. Also, you can probably get
better-informed answers if you took this question to the procmail
mailing list (I'm pretty sure you can get there from
www.procmail.org); I'm no procmail guru. First off, to answer the
question as posed, I _think_ you can maybe just go

    :0
    * whatever pattern
    |f=`date +%s`.$$.`hostname`;cd $HOME/foo;cat tmp/$f;mv tmp/$f new/

directly. And you can use backslashes to continue the line if you
don't want to stretch it out that way.

Much clearer, and probably exactly as efficient, would be to have
e.g. ~/bin/maildir-write:

        #!/bin/sh
        f=`date +%s`.$$.`hostname`
        die(){ echo "`basename $0`: $*">&2; exit 75; }
        test $# eq 1 || die "syntax: $0 maildir-name"
        cd "$1" || die "cannot cd "$1"
        cat >"tmp/$f" || die "write error saving message to tmp/$f"
        mv "tmp/$f" new/ || die "rename tmp/$f new/ failed"

But recent versions of procmail have maildir support built in, so
I'd definitely use one of them and simply code

        :0
        * whatever pattern
        foo/

and be done with it.

If for some reason I couldn't install a recent procmail, well, I
would probably rather use a tight, tiny maildir writer written in
C.  I'm pretty sure there's one linked off the www.qmail.org, my
link has gone stale, but if I couldn't find a nice one off the
shelf, it wouldn't take long to write one; that code above is little
better than one syscall per program of real work getting done. If I
couldn't for some reason install a teensy executable written in C,
I'd look for a more potent interpreter than /bin/sh; I suspect one
wee-teensy perl script would run quicker than invocations of date,
hostname, cat, and mv. Perl's way bigger, sure, but those forks and
execs have to add up sometime. Perhaps

        #!/usr/bin/perl -w
        use strict;
        use Sys::Hostname;
        use Fatal qw(chdir open close rename);
        $SIG{__DIE__} = sub { warn $_[0]; exit(75); };
        die "syntax: $0 maildir-name" unless @ARGV == 1;
        my $f = join('.', time, $$, hostname);
        chdir $ARGV[0];
        open(FO, ">tmp/$f");
        while (<>) { print FO $_; }
        close FO;
        rename "tmp/$f", "new/$f";

I'm sure a wizard with suitable expertise could do the same thing in
any of a number of other languages, including probably python, lisp,
scheme, tcl, lua, awk, bash, zsh, ....

But the builtin support in procmail is first preferred, and failing
that the tightly-coded maildir writer in compiled C, since this
gizmo gets called once for every message sent to your mailbox, and
mailboxes can catch hundreds of messages a minute, easy. Hundreds of
messages a second on fast boxes. If you can't get either of those,
it's probably worth writing implementations in as many of the
available languages as possible, benchmarking them all, and seeing
which is fastest on your system.

-Bennett

PGP signature

Reply via email to