[alt.sources] generic mail-to-news interface

tchrist@convex.com (Tom Christiansen) (10/16/89)

Here is a perl script that you can use to send mail messages
to newsgroups.  It expects to be passed the newsgroup as an
argument, or else included in the headers.  You may have to
alter the pathname for inews.

It doesn't really *require* perl version 3, although you'd
have to tweak STDIN to stdin and the

    print INEWS while <STDIN>;
to 
    while (<stdin>) {
	print INEWS;
    }

As well as the one "warn" to "print stderr".

--tom

ps: kudos to Dave Cohrs <dave@cs.wisc.edu> for the original version
    of this script written in csh+grep+sed+ed.

=================  CUT HERE ===============================================
#!/usr/local/bin/perl

($program = $0) =~ s%.*/%%;

( $version  ) = $] =~ /(\d+\.\d+).*\nPatch level/;
die "$program: requires at least version 3 of perl\n" 
	if $version < 3;

$inews = "/usr/spool/news/lib/inews"; 
$inews = "/usr/local/bin/inews" unless -x $inews;
die "$program: can't find inews\n"  unless -x $inews;
$iopts = "-h";


if ($#ARGV < 0) {  
    # $newsgroup = "test";
    # we'll expect the newsgroup line in the body
} elsif ($#ARGV == 0) {  
    $newsgroup = $ARGV[0];
} else {
    die "usage: $program [newsgroup]\n";
} 

# in case inews dumps core or something crazy
$SIG{'PIPE'} = "plumber";
sub plumber { die "$program: \"$inews\" died prematurely!\n"; }

open (INEWS, "| $inews $iopts") ||
    die "$program: can't run $inews\n";

# header munging loop
while (<STDIN>) {
   last if /^$/;  

   # transform real from: line back to icky style
   s/^From:\s+(.*) <(.*)>/From: $2 ($1)/;

   # transform from_ line to path header; also works locally
   s/^From\s+(\S+)@(\S+).*/Path: $2!$1/
     || s/^From\s+(\S+)[^@]*$/Path: $1\n/;

   print INEWS 
       if /^(From|Subject|Date|Path|Newsgroups|Organization|Message-ID):/i;
   $saw_subject |= ( $+ eq 'Subject' );
   $saw_newsgroup |= ( $+ eq 'Newsgroups' );
} 

warn "$program: didn't expect newsgroup in both headers and ARGV\n"
    if $newsgroup && $saw_newsgroup;

die "$program: didn't get newsgroup from either headers or ARGV\n"
    unless $newsgroup || $saw_newsgroup;

printf INEWS "Newsgroups: %s\n", $newsgroup if $newsgroup;
print  INEWS "Subject: Untitled\n" unless $saw_subject;
print  INEWS "\n";

print INEWS while <STDIN>;   # gobble rest of message

close INEWS;
exit $?;
=================  END OF CUT HERE ========================================

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"