[comp.mail.sendmail] filtering incoming mail? :-|

shawn@litsun8.litsun.epfl.edu (Shawn Koppenhoefer) (12/17/90)

 
Is it possible to somehow filter mail
so that any mail with a keyword in
the subject line is junked right away?
(sort of a junk mail filter i guess)
I want mail to work just like normal except
not have some come to me at all.

I've aked a number of people and they
all tell me it's impossible.
--
_________________________________________________________________
| shawn@litsun.epfl.ch     -.-  KLEIN BOTTLE FOR SALE ...\#######
| Shawn Edwin Koppenhoefer \_/         ... enquire within \######
| LIT-EPFL (Ecole Polytechnique Federal De Lausanne)_______\#####

kaul@icarus.eng.ohio-state.edu (Rich Kaul) (12/18/90)

In article <SHAWN.90Dec17133759@litsun8.litsun.epfl.edu> shawn@litsun8.litsun.epfl.edu (Shawn Koppenhoefer) writes:
   Is it possible to somehow filter mail
   so that any mail with a keyword in
   the subject line is junked right away?
   [...]
   I've aked a number of people and they
   all tell me it's impossible.

It's possible.  You might look at elm's filter function, but I'll show
you how to do it yourself on BSD systems.  For SysV sites you can do a
similar procedure, but it has to act directly on your mail file.

Under BSD you can create a ~/.forward file which can run a filter on
your mail.  For example, my ~/.forward file looks like:
"|/usr/1/kaul/bin/ppmd /usr/1/kaul kaul >>/usr/1/kaul/.maillog 2>&1"
I'll append the version of the perl script I use to filter my mail at
the end of this.  It looks at the 'From ' and 'CC: ' lines to figure
out who's sending me the mail to catagorize it.  You can easily change
it to work on the Subject: line if you like.  I have a shell version
but that uses about 4 times the computer resources.  I'll send it to
you if you like.

A brief overview: I get too much mail, so I filter it.  A small list
of people can send me stuff that actually gets into my mail box for my
immediate attention.  Nearly everything else is placed into a
structure where I can run my news reader (GNUS, an emacs based news
reader) on the individual messages in exactly the same manner as
reading newsgroups.  These are the lower priority, read once and throw
away messages, or stuff from mailing lists that I don't want to
constantly have interrupting me.  I only see those when I find time to
read news.  There are a few other support things that are needed to
turn this into a complete news reading system, but those are on
tut.cis.ohio-state.edu in ~ftp/pub/gnu/gnus if you want to see them.

#!/usr/bin/perl

# A crude attempt at a perl mail delivery agent.  The basic premise is
# that mail from certain people demands attention immediately, so it
# gets put in the normal system mailbox.  Mail from mailing lists is
# to be placed in various subdirectories in a format that GNUS can
# handle for reading.  Mail from the random users gets placed in a
# third directory ($root).  This is, of course, easily customizable.
# Delivering into a directory requires a .last file to keep track of
# article numbers for GNUS.
#
# Pardon the perl style; I'm just learning but this works!  Comments
# welcome.
#
# Author:	Rich Kaul (kaul@icarus.eng.ohio-state.edu)
# Date:		11/13/90
# The credit where credit is due department:  the core of the message
# parsing is based on Larry Wall's mailagent script, although the rest
# of this ugly mess (which nobody else would want to claim) is mine.

($HOME, $USER) = @ARGV;

$root = "$HOME/Memos/personal";		# root of personal news tree.
$dest = $root;
$box = "/usr/spool/mail/$USER";		# default mail box.
$GIVE_UP_BOX = "$HOME/mail_dump";	# emergency dumping box.

$LOCK_SH = 1;				# Values for flock() calls.
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;

umask(077);				# Get a little privacy.

# Now run and get the headers.  We have to parse the headers before we
# can figure out delivery.
while (<stdin>) {
    # Do this on the From_ line only.
    if (1 .. 1) {
	if (/^From\s+(\S+)\s+[^\n]*(\w{3}\s+\d+\s+\d+:\d+)/) { $from = $1; }
	$from =~ s/@.*//;		# remove trailing @machine
	$from =~ s/%.*//;		# remove trailing %machine
	$from = $1 if $from =~ /.*!([^\n]+)/; # remove leading ! paths
    }

    # This section operates on the header of the message.  We create
    # an array of header keys to work on later.
    if (1 .. /^\s*$/) {
	s/^From: ([^<]*)\s+<(.*)>$/From: $2 ($1)/;	# rewrite ugly header
	$header .= $_;
	chop;
	if (/^\s*$/) {
	    foreach $key (keys(header)) {
		eval "\$H$key = \$header{'$key'}";
	    }
	}
	else {
	    if (s/^([-\w]+):\s*//) {
		($headline = $1) =~ y/A-Z-/a-z_/;
		$header{$headline} .= "\n" if $header{$headline} ne '';
	    }
	    else {
		s/^\s+/ /;
	    }
	    $header{$headline} .= $_;
	}
    }
    # And here we make the body of the message.
    else {
	$body .= $_;
	}
}

# Ok, now figure out where to deliver the message.  The first case is
# mail from well behaved mailing lists (i.e. ones that advertise that
# they are mailing lists).
if($from =~ /firearms/) {$dest = "$root/firearms";}
elsif($from =~ /Info-VM/ || $from =~ /Bug-VM/) {$dest="$root/bug-vm";}
elsif($from =~ /freemacs/) {$dest = "$root/freemacs";}

# Sometimes we have dumb mailing lists that can't get their act
# together.  The interviews, oct and some freemacs stuff are prime examples.
if ($Hto =~ /gif/ || $Hcc =~ /gif/) {$dest = "$root/gif";}
elsif($Hto =~ /gnuplot/ || $Hcc =~ /gnuplot/) {$dest = "$root/gnuplot";}
elsif ($Hto =~ /freemacs/ || $Hcc =~ /freemacs/) {$dest = "$root/freemacs";}
elsif ($Hto =~ /interviews/i || $Hcc =~ /interviews/i ) { 
    $dest = "$root/interviews";}
elsif ($Hto =~ /vem/i || $Hto =~ /oct/i || $Hcc =~ /vem/i || $Hcc =~ /oct/i) {
    $dest = "$root/vem";}
elsif($Hto =~ /xviewbug-trackers/ || $Hcc =~ /xviewbug-trackers/) {
    $dest = "$root/xview";}

# Here we dump system messages.
if($from =~ /root/ || $from =~ /news/) { $dest = "$root/system";}

# Now we list the people who can crash into the mailbox.
if($from =~ /karl_kl/ || $from =~ /alden/ || $from =~ /monty/) {$dest = $box;}
elsif($from=~/pnelson/ || $from=~/wilson/i || $from =~ /gratz/i) {$dest=$box;}
elsif($from =~ /bibyk/ || $from =~ /adkins/ || $from =~ /zaka/) {$dest = $box;}
elsif($from =~ /kaul/ || $from =~ /aspark/) {$dest = $box;}

# Now we do the delivery.  There are two cases.  If we are delivering
# to a directory find and update the .last file there.
if ( -d $dest ) {
    # It's going to be a news article, so change the From_ line
    $header =~ s/^From /Unix-From: /;
    $all = $header . $body;

    $count_file = "$dest/\.last";
    open(COUNTER,"+<$count_file")|| do gag("Can't open $dest/.last ($< $>): $!");
    while (<COUNTER>) {
	chop;
	$count = $_;
	$count++;
    }
    do flocker(COUNTER,$LOCK_EX);	# Lock the file, just in case.
    seek(COUNTER,0,0);
    print COUNTER $count,"\n";
    do flocker(COUNTER,$LOCK_UN);

    # We now have the article number we need.
    open(ART,">>$dest/$count") || do gag("Can't open $dest/$count ($< $>): $!");
    print ART $all,"\n";
} else {
    # Here we're delivering to a file, which is easier.  Build and deliver.
    $all = $header . $body;
    open(BOX, ">>$dest") || do gag ("Can't open $dest ($< $>): $!");

    do flocker(BOX,$LOCK_EX);
    print BOX $all,"\n\n";
    do flocker(BOX,$LOCK_UN);
}

# File locking subroutine.
sub flocker {
    local ($file, $mode) = @_;
    eval 'flock($file,$mode);';
    seek($file, 0, 2);		# in case it was appended while we were waiting
}

# For some reason mail couldn't get delivered.  Dump the message in the
# emergency mailbox and exit.
sub gag {
    open(ERR_BOX, ">>$GIVE_UP_BOX") || die "I'm really hosed.  I can't even open the emergency box $GIVE_UP_BOX\n";

    print @_;
    do flocker(ERR_BOX,$LOCK_EX);
    print ERR_BOX $all,"\n\n";
    do flocker(ERR_BOX,$LOCK_UN);
    exit 1;
}
    

-- 
Rich Kaul                         | Every man is given the key to the door
kaul@icarus.eng.ohio-state.edu    | of heaven; unfortunately, the same key
or ...!osu-cis!kaul		  | opens the door to hell.

gdtltr@brahms.udel.edu (root@research.bdi.com (Systems Research Supervisor)) (12/18/90)

In article <SHAWN.90Dec17133759@litsun8.litsun.epfl.edu> shawn@litsun.epfl.ch writes:
=>
=> 
=>Is it possible to somehow filter mail
=>so that any mail with a keyword in
=>the subject line is junked right away?
=>(sort of a junk mail filter i guess)
=>I want mail to work just like normal except
=>not have some come to me at all.
=>
=>I've aked a number of people and they
=>all tell me it's impossible.

   I believe that the .maildelivery support in MMDF and MH will let you do
this. MMDF supports it directly, while MH uses the ".forward to a pipe"
facility to call a program called slocal to process the .maildelivery .
You can specify exactly what to do with a message with a paritcular From:,
Subject:, etc. including piping it to another program, putting it in your
normal maildrop, and appending it to a file (e.g. /dev/null.)

                                        Gary Duzan
                                        Time  Lord
                                    Third Regeneration



-- 
                            gdtltr@brahms.udel.edu
   _o_                      ----------------------                        _o_
 [|o o|]   Two CPU's are better than one; N CPU's would be real nice.   [|o o|]
  |_o_|           Disclaimer: I AM Brain Dead Innovations, Inc.          |_o_|

barnett@grymoire.crd.ge.com (Bruce Barnett) (12/19/90)

In article <SHAWN.90Dec17133759@litsun8.litsun.epfl.edu> shawn@litsun8.litsun.epfl.edu (Shawn Koppenhoefer) writes:


   Is it possible to somehow filter mail
   so that any mail with a keyword in
   the subject line is junked right away?

Put the following in your .mailrc file:


delete /keyword

This should work for SunOS. I think it will work for other BSD
systems
--
Bruce G. Barnett	barnett@crd.ge.com	uunet!crdgw1!barnett

rlk@think.com (Robert Krawitz) (12/20/90)

In article <SHAWN.90Dec17133759@litsun8.litsun.epfl.edu>, shawn@litsun8 (Shawn Koppenhoefer) writes:
]
] 
]Is it possible to somehow filter mail
]so that any mail with a keyword in
]the subject line is junked right away?
](sort of a junk mail filter i guess)
]I want mail to work just like normal except
]not have some come to me at all.

If your home directory, or some other directory that you have access to,
is mounted on the machine that does mail delivery, it is indeed
possible.  There is a package called pmd (Personal Mail Daemon) which
was written by Jim Aspnes a number of years ago that does this
filtering.  It can deliver mail to various files depending upon
combinations of the sender, recipient, subject, etc.

It is available by anonymous ftp from think.com (131.239.2.1) as
/pmdc.tar.Z.  Be warned that there are some bugs, and that various
people have given me patches that have not made it back in.  However, it
does an effective job for me and a number of other users.
-- 
ames >>>>>>>>>  |	Robert Krawitz <rlk@think.com>	245 First St.
bloom-beacon >  |think!rlk	(postmaster)		Cambridge, MA  02142
harvard >>>>>>  .	Thinking Machines Corp.		(617)234-2116

acbhour@accucx.cc.ruu.nl (Rudi van Houten) (12/20/90)

In <1990Dec19.231137.7462@Think.COM> rlk@think.com (Robert Krawitz) writes:

>In article <SHAWN.90Dec17133759@litsun8.litsun.epfl.edu>, shawn@litsun8 (Shawn Koppenhoefer) writes:
>]
>] 
>]Is it possible to somehow filter mail
>]so that any mail with a keyword in
>]the subject line is junked right away?
>](sort of a junk mail filter i guess)
>]I want mail to work just like normal except
>]not have some come to me at all.

>If your home directory, or some other directory that you have access to,
>is mounted on the machine that does mail delivery, it is indeed
>possible.  There is a package called pmd (Personal Mail Daemon) which
>was written by Jim Aspnes a number of years ago that does this
>filtering.  It can deliver mail to various files depending upon
>combinations of the sender, recipient, subject, etc.

>It is available by anonymous ftp from think.com (131.239.2.1) as
>/pmdc.tar.Z.  Be warned that there are some bugs, and that various
>people have given me patches that have not made it back in.  However, it
>does an effective job for me and a number of other users.

Also if you are using sendmail as MTA the utility filter in the
package elm can do the job for you.

Quoted from the elm monthly posting:

!! The following sites have agreed to make Elm available via anonymous ftp.
!! 
!! 	Site			Contact
!! 	mthvax.cs.miami.edu	a.e.mossberg, aem@mthvax.cs.miami.edu
!! 	  (129.171.32.5)
!! 	wuarchive.wustl.edu	David J. Camp, david@wubios.WUstl.EDU
!!           (128.252.135.4)
!! 
!! 	In Europe:
!! 	sol.cs.ruu.nl		Edwin Kremer, edwin@cs.ruu.nl
!! 	(131.211.80.5)
!! 
!! 	In the UK:
!! 	uk.ac.soton.ecs		T.Chown@ecs.soton.ac.uk (bitnet)
!! 				T.Chown@uk.ac.soton.ecs (JANET)
!! 
-- 
Rudi van Houten	<acbhour@cc.ruu.nl>
Academisch Computer Centrum Utrecht / Besturings Systemen
Budapestlaan 8  -  3584 CD  -  Utrecht  -  Netherlands
Tel: +31 30 531731		Fax: +31 30 531633