procmail
[Top] [All Lists]

Re: How to include an on the fly autogenerated file in procmailrc

2007-11-11 03:13:18
On date Saturday 2007-11-10 18:18:20 -0800, Bart Schaefer wrote:
On Nov 10, 2007 3:36 PM, Stefano Sabatini 
<stefano(_dot_)sabatini-lala(_at_)poste(_dot_)it> wrote:

Here it is my problem: I see that there is the possibility to include
a procmailrc inside another. Would be possible to include a file
autogenerated on the fly, for example using some script, something
like the:
source "script args|"

feature of mutt?

Of course that's just a shorthand for redirecting the output of the
script into a file and then reading the file.  So it's certainly
possible to do that from procmail.

Wonderful.

There are any number of ways to do
so; one of them would be:

AUTO_RC = "$HOME/.procmailrc.$$"
:0
* $ ? script args > $AUTO_RC
{ INCLUDERC=$AUTO_RC }

This uses the "* ?" recipe mechanism to execute the script and test
whether it exited successfully before attempting to read the resulting
file.

Cleaning up the file afterward has been left to the imagination of the reader.

Thank you so much, I'm including a description of my environment in
the case it could result useful to others.

I ended up with something like this:

# MAILING LIST MANAGING
# all messages directed to some mailing list are immediately put 
# in a corresponding folder
AUTO_RC = mailing-lists-rc
:0
* $ ? print-mailing-lists-rc.pl ~/etc/mailing-lists > $AUTO_RC
{ INCLUDERC = $AUTO_RC }

The print-mailing-lists-rc.pl script looks like this:
------------------------------------------------------
#! /usr/bin/perl

use warnings;
use strict;

sub print_mailing_list_rc {
    my $address = shift @_;
    my $address_matching_re = quotemeta($address);
    my $nickname;

    # nickname is the part before any "@": e.g.
    # alice(_dot_)and(_dot_)friends(_at_)wonderland(_dot_)com -> 
alice.and.friends
    $address =~ m/^[^(_at_)]+/; 
    # set nickname to the matched value
    $nickname = $&;

    print << "EOT";
# $nickname
:0 H 
* ^(To|C[c]): .*${address_matching_re}.*
inbox/$nickname/

EOT
}

# read from stdin of from a file in input
while (<>) {
    chomp;
    print_mailing_list_rc ($_);
}
------------------------------------------------------

The ~/etc/mailing-lists file contains a textual list (one per line) of
the mailing-list I'm subscribed to (and may be processed as well by
other applications, for example by an user agent).

For example the resulting procmailrc rule for this mailing list will
appear as:

# procmail
:0 H 
* ^(To|C[c]): (_dot_)*procmail\(_at_)lists\(_dot_)RWTH\-Aachen\(_dot_)DE(_dot_)*
inbox/procmail/

One potential problem is that the quotemeta escaping mechanism used by
Perl might result inadequate to issue a corresponding egrep string.

For what regards the $AUTO_RC file deletion, I think it's a nice
debugging feature to keep it. I keep all the procmail configuration
files in ~/.procmail/ so it won't clutter the ~ environment.

Best regards.
-- 
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail