On 2001.08.06, in <[EMAIL PROTECTED]>,
        "Mike Erickson" <[EMAIL PROTECTED]> wrote:
> 
> All that would be necessary is a maillist_file setting in my .muttrc
> just like alias_file, and add a command to add addresses to it. Although
> it'd be a nice bonus, it's not even necessary to autoparse the addresses
> out of an email. Just being able to maintain that list from within mutt
> would be good enough.

You can have the first part of this, of course, by adding
        source ~/.mutt/lists
or somesuch to your .muttrc file.  I have .muttrc functionalities split
out into several such files, all sourced from .muttrc.

The rest can be tricky; there's no sure-shot way to determine which
address from the header (if any) is a mailing list address. For every
rule you can make up, some list (or list manager) does it differently.

Here's a possibility, though. I'm in a perl-thwacking mood today, I
guess. Save this attachment in your path as "addlist", and try executing
it via macro something like this:

macro index \Co "<pipe-message>addlist ~/.mutt/lists<enter>" "Scan a message for 
mailing lists to add"
macro pager \Co "<pipe-message>addlist ~/.mutt/lists<enter>" "Scan a message for 
mailing lists to add"

The argument to addlist should be the file you store mutt's "lists" and
"subscribe" lines in.

This program will use formail to look for likely list names in the
message you're viewing or that's highlighted in the index. It will check
for what list patterns already are defined in your lists file, and if
any addresses it discovers in the message are not matched by a lists or
subscribe command, it will ask you whether it should add such a line to
the lists file, and then do it.

No particular reason for ^O except that it was available in my
configuration.  No warranty, use at your own risk, &c.

-- 
 -D.    [EMAIL PROTECTED]        NSIT    University of Chicago
#!/usr/bin/perl

require "ctime.pl";

if ($#ARGV != 0) {
        print STDERR "usage: $0 /path/to/lists/file\n";
        exit (1);
}

# Remember how to clean up the terminal
$sane = `stty -g </dev/tty`;
chomp $sane;


# Clean up on exit.
sub sanitize () {
        `stty $_[0] </dev/tty`;
}
for $sig (INT, QUIT, HUP, TERM) {
        $SIG{$sig} = \&sanitize($sane);
}

# Find usable addresses from the mail headers. Formail receives our stdin.
open (FORMAIL, "formail -xTo: -xCc: -xList-ID: -xSender: |") || exit (1);
while (<FORMAIL>) {
        chomp;
        s/^ *//;
        s/ *$//;
        s/.*<([^>]*)>.*/$1/;
        s/-(admin|owner|request)\@/\@/;
        s/^owner-//;
        s/\./\@/ unless (/\@/);
        $uniq{$_} = $_;
}
close (FORMAIL);

# Check extant lists file, and discover addresses already in it.
open (LISTS, $ARGV[0]);
while (<LISTS>) {
        chomp;
        s/^\s+//;
        next unless /^(lists|subscribe)/;
        s/#.*//;
        s/\s+$//;
        s/.*(lists|subscribe)\s*//;
        map { $already{$_} = $_ } split(' ', $_);
}
close (LISTS);

# Exclude matching addresses from new list.
for $addr (keys %uniq) {
        for $exist (keys %already) {
                delete $uniq{$addr} if ($addr =~ /$exist/);
        }
}

# Go into raw mode for the prompts.
`stty raw </dev/tty`;

# Prompt whether to add each address discovered.
$| = 1;
open (TTY, "/dev/tty");
for $addr (sort keys %uniq) {
        while (1) {
                print "Add $addr as a list [y/n]? ";
                sysread (TTY, $ch, 1);
                print "\r\n";
                if (lc($ch) eq "n") {
                        delete $uniq{$addr};
                        last;
                }
                last if (lc($ch) eq "y");
        }
}

# Clean up terminal.
&sanitize($sane);

# Append to lists file, if necessary.
@_ = keys %uniq;
$n = $#_ + 1;
if ($n > 0) {
        open (LISTS, ">> $ARGV[0]");
        $date = &ctime(time);
        chomp $date;
        $s = "s" if ($n > 1);
        print LISTS "# $n list$s added by $0 at $date\n";
        map {
                print LISTS "subscribe $_\n";
        } sort keys %uniq;
        print LISTS "\n";
        close (LISTS);
}

print "\n";
print "$n lists added to $ARGV[0].\n";
print "\n";
exit (0);

Reply via email to