From: colin@Cayman.COM (Colin "Atilla" Steele)
Newsgroups: alt.zines
Subject: Re: crossover, samizdat generally
Date: 15 Sep 92 14:42:28 GMT
Organization: Cayman Systems Inc., Cambridge, MA
In-reply-to: jaffray@sunkist.berkeley.edu's message of 13 Sep 92 01:56:48 GMT


Here's a small piece of what you're looking for.  It's a perl-based
mail filter that can be used to file mail in appropriate "piles".  If
you need additional explaination on how to use it, how to set up
filters, just drop me a line.

cut here ----------------------------------------------------------------------
#!/bin/perl
# Replace the previous line with the path to perl
# on your machine.
# 		
###############################################################################
# FILEMAIL.PL
# Author:  Colin A. Steele (colin@Cayman.com)
# Tue Sep 15 09:56:08 EDT 1992
# Redistribute freely.  Go nuts ;-)
# Report bugs to colin@Cayman.com.  I may not fix it, but I'll be sympathetic.
#     
# Usage:  filemail.pl [-d] [-p] [-l]
# use -d for debugging messages.
# use -p to print input to stdout.
# use -l to make filemail.pl log what it does.
###############################################################################
require "getopts.pl";

#
# Check command line options
# -d for debug, -p for print the input to stdout, -l for logging
#
&Getopts('dlp');
$DEBUG = $opt_d;
$PRINT = $opt_p;
$LOG = $opt_l;

#
# Set up default variables.
#
($DEBUG) && (print "Setting up default variables.\n\n");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
$mon++;
$today = "$mon_$mday_$year";
$inheader = 0;
$header = "";
($to,$cc,$bcc,$from,$subject) = ();
$outFile = "/home/caicos/colin/Mail/unread-mail";
$logFile = "/home/caicos/colin/Mail/filemail.log";
$opened = 0;
$logopened = 0;

while(<>){
  # Unix mail format
  if (/^From \S*\s*... ... .. ..:..:.. ....$/) {
    ($DEBUG) && (print "Unix mail format\n");
    ($DEBUG) && (print "Collecting header...");
    $inheader = 1;
  }
  # Babyl format
  if (/^\037\014$/) {
    ($DEBUG) && (print "Babyl mail format\n");
    ($DEBUG) && (print "Collecting header...");
    $inheader = 1;
  }
  
  if (/^$/) {
    $inheader = 0;
    ($DEBUG) && (print "\n");
  }
  
  #
  # While in the header, collect it up.
  #
  if($inheader){
    $header .= $_;
    ($DEBUG) && (print "...");
  }

  #
  # Then, if not in the header, and a header has been collected,
  # set the to, cc, bcc, etc. variables.
  #
  elsif($header){
    ($hdr = $header) =~ s/,(\n|\s)+/,/g;
    
    $to = $1 if $hdr =~ /\nTo: (.*)\n/; 
    $cc = $1 if $hdr =~ /\nCc: (.*)\n/; 
    $bcc = $1 if $hdr =~ /\nBcc: (.*)\n/; 
    $from = $1 if $hdr =~ /\nFrom: (.*)\n/; 
    $subject = $1 if $hdr =~ /\nSubject: (.*)\n/;

    if ($DEBUG) {
      print "To: $to\n";
      print "CC: $cc\n";
      print "BCC: $bcc\n";
      print "From: $from\n";
      print "Sub: $subject\n";
    }

    ############
    # Here's the actual filtering part.  Set the outfile based on
    # what we've gleaned in the header of the message.
    ############
    # Filter one:  if it's to support... (no commas in to field)
    if ($to !~ /,/ && $to =~ /(^|\W+)support@Cayman.COM(\W+|$)/) {
      $outFile = "/home/caicos/colin/Mail/unread-support-mail";
    }
    # Filter 1a:  if it's to support... (no commas in to field)
    if ($to !~ /,/ && $to =~ /(^|\W+)support@cayman.Cayman.COM(\W+|$)/) {
      $outFile = "/home/caicos/colin/Mail/unread-support-mail";
    }
    # Filter 2:  if it's from bart...
    if ($from !~ /,/ && $from =~ /(^|\W+)bart(\W+|$)/) {
      $outFile = "/home/caicos/colin/Mail/unread-bart-mail";
    }

    #
    # Logging
    #
    if ($LOG) {
      (&DEBUG) && (print "Logging message\n");
      open(LOG, ">>$logFile") && ($logopened = 1);
      ($logopened) && (print LOG "***\n$today\t$from\t$Sub\n$outFile\n***\n");
    }
    
    #
    # Open up the output file(s).
    #
		(print "File:  $outFile\n") if $DEBUG;
		(print "File2: $outFile2\n") if $DEBUG;
    $opened = 0;
    $opened2 = 0;
    $opened = 1 if open(FP, ">>$outFile");
		if ($DEBUG && !$opened) {
				print "Couldn't open $outFile\n";
		}
    if($outFile2) {
      $opened2 = 1 if open(FP2, ">>$outFile2");
			if ($DEBUG && !$opened2) {
					print "Couldn't open $outFile2\n";
			}
    }
		

    #
    # Print the header to stdout, and if an output file has been
    # opened, also print the header to that file.
    #
    ($PRINT) && (print "$header\n");
    (print FP "$header\n") if $opened;
    (print FP2 "$header\n") if $opened2;
    
    #
    # Since we've collected and printed out the header:
    # Reset the header variable and the to, cc, etc. variables.
    #
    $header = "";
    ($to,$cc,$bcc,$from,$subject) = ();
  }

  #
  # Once the header has been taken care of, print following lines to stdout,
  # and if an output file has been opened, also print the line to that file.
  #
  else {
    ($PRINT) && (print);
    (print FP) if $opened;
    (print FP2) if $opened2;
  }
}

#
# Clean up.
#
close(FP) if $opened;
close(FP2) if $opened2;

--


------------------------------------------------------------------------------
 Colin Steele     | Cayman Systems, 26 Landsdowne St., Cambridge, MA 02139
 colin@cayman.com | (617) 494-1916 x209 | applelink D0523 | Fax (617) 494-9270
Newsgroups: alt.cesium