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

  1. #! /usr/bin/perl
  2. #
  3. # senddigest - send a mailing list digest file
  4. #
  5. #
  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 $REPLYFILE");
  134.   if ($#ERRORS > -1) {
  135.     open(T, ">$TMPFILE");
  136.     print T "From: $LISTOWNER\n";
  137.     print T "Subject: Errors from $LISTREQUEST\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. ## If no digest, then no send
  167. &finish() if (! -e $DIGESTFILE);
  168.  
  169. $FQDN = $GLOBALS{'FQDN'};
  170.  
  171. $DATE = `date`; chop($DATE);
  172.  
  173. @TEXT = ();
  174.  
  175. $LISTADDR = "$LISTNAME@$FQDN (The $LISTNAME digest)";
  176. $DIGESTADDR = "$LISTNAME-digest-mail";
  177.  
  178. $MAILCMD = "$GLOBALS{'SENDMAIL'} $DIGESTADDR";
  179. $MAXSIZE = 30000;
  180.  
  181. system("mv $DIGESTFILE $TMPFILE");
  182. open(D,"<$TMPFILE") || &DIE("$TMPFILE: $!\n");
  183. open(T,">$REPLYFILE") || &DIE("$REPLYFILE: $!\n");
  184.  
  185. sub Headers
  186. {
  187.   local($p) = @_;
  188.   print T "To: $DIGESTADDR@$FQDN\n";
  189.   print T "From: $LISTADDR\n";
  190.   print T "Reply-To: $LISTADDR\n";
  191.   print T "Errors-To: $LISTOWNER@$FQDN\n";
  192. #  print T "Sender: $LISTOWNER@$FQDN\n";
  193.   print T "Subject: Digest: $LISTNAME for $DATE ($p)\n";
  194.   print T "\nThis is the automated digest of the $LISTNAME mailing list.\n";
  195.   print T "You may send replies to $LISTADDR.\n";
  196.   print T "Your replies will be automatically included in the next digest.\n";
  197.   print T "If you experience problems, please contact $LISTOWNER@$FQDN.\n";
  198. }
  199.  
  200. $parts = 1;
  201. &Headers($parts);
  202. print T "\nTABLE OF CONTENTS:\n";
  203.  
  204. $subjects = 0;
  205.  
  206. while (<D>)
  207. {
  208.   if (/^Subject:\s*/)
  209.   {
  210.     local($s) = $_;
  211.     chop($s);
  212.     $s =~ s/^Subject:\s*//;
  213.     $subjects += 1;
  214.     if (length($s) > 50)
  215.     {
  216.       $s = substr($s,0,50) . "...";
  217.     }
  218.     $s = sprintf("%3d. %s", $subjects, $s);
  219.     print T $s,"\n";
  220.     $subjects = sprintf("%3d", $subjects);
  221. #    $TEXT[$#TEXT+1] = "\n------- TOPIC: $subjects -------\n";
  222.      $TEXT[$#TEXT+1] = "\n";
  223.   }
  224. #print "1 " . $_;
  225.   $TEXT[$#TEXT+1] = $_;
  226. }
  227. close(D);
  228. #print "#######\n";
  229. system("rm -f $TMPFILE");
  230.  
  231. $size = 0;
  232.  
  233. if ($subjects)
  234. {
  235.   local($i);
  236.   print T "\n";
  237.   for($i=0;$i<=$#TEXT;$i++) {
  238.     $size += length($TEXT[$i]);
  239. #print "2 " . $TEXT[$i];
  240.     if ($size >= $MAXSIZE)
  241.     {
  242.       # Look for the next topic line
  243.       $_ = $TEXT[$i];
  244.       if (/^From:/)
  245.       {
  246.     # Close down, and send...
  247.         close(T);
  248.     system("cat $REPLYFILE | $MAILCMD");
  249.     open(T,">$REPLYFILE") || &DIE("$REPLYFILE: $!\n");
  250.     $parts += 1;
  251.     $size = 0;
  252.     &Headers($parts);
  253.     print T "\n";
  254.       }
  255.     }
  256.     print T $TEXT[$i]; 
  257.   }
  258.   close(T);
  259.  
  260.   # The digested file 
  261. #print "-----------------\n";
  262. #open(T,"<$REPLYFILE");
  263. #while (<T>) { print "3 " . $_; }
  264. #close(T);
  265.   system("cat $REPLYFILE | $MAILCMD");
  266. }
  267. else
  268. {
  269.   close(T);
  270. }
  271. &finish();
  272.