home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / bmw-5 / part01 / digestify < prev    next >
Text File  |  1994-07-09  |  5KB  |  217 lines

  1. #! /usr/bin/perl
  2.  
  3. # Digestify -- accept a mail message on standard input.  Strip the headers
  4. # and cat into the digest file.  Build the digest table of contents file
  5. # as well.
  6.  
  7. $USAGE = "$0 <listname>\n";
  8.  
  9. if ($#ARGV != 0) { die ("$USAGE"); }
  10.  
  11. $LISTNAME = $ARGV[0];
  12. $LISTNAME =~ tr/A-Z/a-z/;  # Convert everything to lower case for now.
  13.  
  14. %GLOBALS = ();
  15.  
  16. ## We need to set the hostname.  You this is done by querying hostname and
  17. ## domainname.  You can override this here or set your FQDN global explicitly
  18. ## in the bmw.cf file.
  19.  
  20. $HOSTNAME = `hostname`; chop($HOSTNAME);
  21. $DOMAINNAME = `domainname`; chop($DOMAINNAME);
  22.  
  23. ## Path to bmw.cf - CHANGE THIS FOR YOUR SYSTEM
  24. #$BMWCF = "/etc/bmw.cf";
  25. $BMWCF = "/home/bmw/bmw.cf";
  26.  
  27. ## Process the config file and learn about all the globals.
  28. ## FORMAT OF CONFIG FILE:
  29. ##   Each line of the config file should have the format
  30. ##     <globalname> = <value>
  31. ##   for example,
  32. ##     BASEDIR = /usr/local/lib/bmw
  33. ##
  34. ## I now set the defaults.  You should override these in your bmw.cf file.
  35. ##
  36. $GLOBALS{'SENDMAIL'} = "/usr/lib/sendmail";
  37. $GLOBALS{'SENDMAILOPTS'} = "";
  38. $GLOBALS{'ENCODE'} = "/usr/bin/uuencode";
  39. $GLOBALS{'COMPRESS'} = "/bin/gzip";
  40. $GLOBALS{'COMPSUFFIX'} = ".gz";
  41. $GLOBALS{'ARCDIR'} = "/home/ftp/pub/lists";
  42. $GLOBALS{'ARCOWNER'} = "ftp";
  43. $GLOBALS{'OWNER'} = "postmaster";
  44. $GLOBALS{'DEBUG'} = 0;
  45. $GLOBALS{'LOG'} = 0;
  46. $GLOBALS{'MAXGETS'} = 5;
  47. $GLOBALS{'PREFERFTP'} = 0;
  48. $GLOBALS{'TRACE'} = 0;
  49. $GLOBALS{'BASEDIR'} = "/usr/local/lib/bmw";
  50. $GLOBALS{'FQDN'} = $HOSTNAME . "." . $DOMAINNAME;
  51. $GLOBALS{'DIGEST'} = 0;
  52. $GLOBALS{'TMPDIR'} = "/var/tmp";
  53. $GLOBALS{'USER'} = "bmw";
  54. $GLOBALS{'GROUP'} = "bin";
  55.  
  56.  
  57. @VALIDGLOBALS = ("SENDMAIL", "SENDMAILOPTS", "TMPDIR", "ENCODE",
  58.                  "COMPRESS", "COMPSUFFIX", "ARCDIR", "ARCOWNER",
  59.                  "OWNER", "DEBUG", "LOG", "MAXGETS", "PREFERFTP",
  60.                  "TRACE", "BASEDIR", "FQDN", "DIGEST", "USER", "GROUP");
  61.  
  62.  
  63.  
  64. sub validGlobal {
  65. ## Determine if the string is a valid global reference.
  66.   local($s) = @_;
  67.   local($i);
  68.   for ($i=0; $i<=$#VALIDGLOBALS; $i++) {
  69.     if ($s eq $VALIDGLOBALS[$i]) { return 1; }
  70.   }
  71.   return 0;
  72. }
  73.  
  74.  
  75.  
  76. ## Load the globals from the cf file.
  77. if (-e $BMWCF) {
  78.   local($lno) = 0;
  79.   local(@LINE);
  80.   open(CF, "<$BMWCF") || die "Cannot open $BMWCF: $!\n";
  81.   while (<CF>) {
  82.     $lno++;
  83.     chop;
  84.     tr/\t //d;
  85.     @LINE = split("=");
  86.     if (!$LINE[0]) { die "ERROR in $BMWCF line $lno; $_\n"; }
  87.     $LINE[0] =~ tr/a-z/A-Z/;
  88.     if (!&validGlobal($LINE[0])) {
  89.       die "ERROR in $BMWCF line $lno; Unknown global \"$LINE[0]\"\n";
  90.     }
  91. ## Good.  We have a valid global
  92.     $GLOBALS{$LINE[0]} = $LINE[1];
  93.   }
  94. }
  95. else {
  96.   die "$BMWCF does not exist!\n";
  97. }
  98.  
  99. if ($GLOBALS{'DEBUG'}) {
  100.   local($key);
  101.   foreach $key (keys %GLOBALS) {
  102.     print "$key = $GLOBALS{$key}",$LF;
  103.   }
  104. }
  105.  
  106.  
  107. ## Internal global values.  Do not fiddle with these without good reason.
  108. $VERSION = "5.0";
  109. $LISTDIR = $GLOBALS{'BASEDIR'} . "/$LISTNAME";
  110. $DIGESTFILE = "$LISTDIR/digest";
  111. $TOCFILE = "$LISTDIR/digesttoc";
  112. $LOGFILE = "$LISTDIR/log";
  113. $MESSAGE = "$LISTDIR/message";
  114. $HELP = "$LISTDIR/help";
  115. $WDIR = "";                   # working directory suffix for gets
  116.  
  117. $TMPFILE = $GLOBALS{'TMPDIR'} . "/bmw$$";
  118. $REPLYFILE = "$TMPFILE.reply";
  119. $TMPLOGFILE = "$TMPFILE.log";
  120.  
  121. $LISTOWNER = "$LISTNAME-owner";
  122. $LISTREQUEST = "$LISTNAME-request";
  123.  
  124. @ERRORS = ();
  125. %LOCKS = ();
  126.  
  127. ##
  128. ## Exit routines.  These make sure everything is cleaned up.
  129. ##
  130. sub finish {
  131.   local($rc) = @_;
  132.   local($i);
  133.   system("rm -f $TMPFILE");
  134.   if ($#ERRORS > -1) {
  135.     open(T, ">$TMPFILE");
  136.     print T "Subject: Errors from $LISTREQUEST\n";
  137.     print T "From: $LISTOWNER\n";
  138.     print T "To: $LISTOWNER\n\n";
  139.     for ($i = 0; $i <= $#ERRORS; $i++) {
  140.       print T $ERRORS[$i],$LF;
  141.     }
  142.     close(T);
  143.     system("cat $TMPFILE | $GLOBALS{'SENDMAIL'} \'$LISTOWNER\'");
  144.     system("rm -f $TMPFILE")
  145.   }
  146.   exit($rc);
  147. }
  148.  
  149.  
  150. sub DIE {
  151.   local($msg) = @_;
  152.   print STDERR "$msg",$LF;
  153.   $ERRORS[$#ERRORS+1] = $msg;
  154.   local($key);
  155.   foreach $key (keys %LOCKS) {
  156.     system("rm -f $key");
  157.   }
  158.   &finish(-1);
  159. }
  160.  
  161.  
  162. # Test for things that must exist.
  163.  
  164. &DIE("$LISTDIR does not exist!\n") if (! -d $LISTDIR);
  165.  
  166.  
  167. $FROM = "";
  168. $SUBJECT = "";
  169. $MID = "";
  170. $DATE = `date`; chop($DATE);
  171.  
  172. if (! -e $DIGESTFILE)
  173. {
  174.   system("touch $DIGESTFILE");
  175.   chown $GLOBALS{'USER'}, $GLOBALS{'GROUP'}, $DIGESTFILE;
  176.   chmod 0664 , $DIGESTFILE;
  177. }
  178.  
  179. shift;
  180.  
  181. open(TF,">>$DIGESTFILE") || &DIE("Could not open $DIGESTFILE: $!\n");
  182.  
  183. $HEADERS = 1;
  184. $INFROM = 0;
  185. while (<>)
  186. {
  187.  if ($HEADERS) {
  188.     if ($INFROM && /^\s/) {
  189.       local($morefrom);
  190.       $morefrom = $1 if m/^\s*(.*)\n/;
  191.       $FROM = $FROM . $morefrom . "\n";
  192.     }
  193.     else {
  194.       $INFROM = 0;
  195.       if (/^Subject:/) { $SUBJECT = $_; }
  196.       if (/^From:/) { $FROM = $_; }
  197.       if (/^Message-Id:/) { $MID = $_; }
  198.       if ($_ eq "\n") {
  199. ## Blank line ends headers.
  200.         $HEADERS = 0;
  201.         print TF "\n" . $SUBJECT;
  202.         print TF $FROM;
  203.         print TF "Date: " . $DATE . "\n";
  204.         print TF $MID;
  205.         print TF "Reply-To: $LISTNAME@$GLOBALS{'FQDN'}\n\n";
  206.       }
  207.     }
  208.   }
  209.   else {
  210.     print TF $_;
  211.   }
  212. }
  213.  
  214. close(TF);
  215. &finish();
  216.  
  217.