home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / mail2news < prev    next >
Encoding:
Internet Message Format  |  1989-10-26  |  2.9 KB

  1. Path: tut.cis.ohio-state.edu!cs.utexas.edu!uunet!convex!convex.com!tchrist
  2. From: tchrist@convex.com (Tom Christiansen)
  3. Newsgroups: alt.sources
  4. Subject: generic mail-to-news interface
  5. Message-ID: <2100@convex.UUCP>
  6. Date: 16 Oct 89 13:24:34 GMT
  7. Sender: news@convex.UUCP
  8. Reply-To: tchrist@convex.com (Tom Christiansen)
  9. Organization: Convex Computer Corporation, Richardson, Tx.
  10. Lines: 89
  11.  
  12.  
  13. Here is a perl script that you can use to send mail messages
  14. to newsgroups.  It expects to be passed the newsgroup as an
  15. argument, or else included in the headers.  You may have to
  16. alter the pathname for inews.
  17.  
  18. It doesn't really *require* perl version 3, although you'd
  19. have to tweak STDIN to stdin and the
  20.  
  21.     print INEWS while <STDIN>;
  22. to 
  23.     while (<stdin>) {
  24.     print INEWS;
  25.     }
  26.  
  27. As well as the one "warn" to "print stderr".
  28.  
  29. --tom
  30.  
  31. ps: kudos to Dave Cohrs <dave@cs.wisc.edu> for the original version
  32.     of this script written in csh+grep+sed+ed.
  33.  
  34. =================  CUT HERE ===============================================
  35. #!/usr/local/bin/perl
  36.  
  37. ($program = $0) =~ s%.*/%%;
  38.  
  39. ( $version  ) = $] =~ /(\d+\.\d+).*\nPatch level/;
  40. die "$program: requires at least version 3 of perl\n" 
  41.     if $version < 3;
  42.  
  43. $inews = "/usr/spool/news/lib/inews"; 
  44. $inews = "/usr/local/bin/inews" unless -x $inews;
  45. die "$program: can't find inews\n"  unless -x $inews;
  46. $iopts = "-h";
  47.  
  48.  
  49. if ($#ARGV < 0) {  
  50.     # $newsgroup = "test";
  51.     # we'll expect the newsgroup line in the body
  52. } elsif ($#ARGV == 0) {  
  53.     $newsgroup = $ARGV[0];
  54. } else {
  55.     die "usage: $program [newsgroup]\n";
  56.  
  57. # in case inews dumps core or something crazy
  58. $SIG{'PIPE'} = "plumber";
  59. sub plumber { die "$program: \"$inews\" died prematurely!\n"; }
  60.  
  61. open (INEWS, "| $inews $iopts") ||
  62.     die "$program: can't run $inews\n";
  63.  
  64. # header munging loop
  65. while (<STDIN>) {
  66.    last if /^$/;  
  67.  
  68.    # transform real from: line back to icky style
  69.    s/^From:\s+(.*) <(.*)>/From: $2 ($1)/;
  70.  
  71.    # transform from_ line to path header; also works locally
  72.    s/^From\s+(\S+)@(\S+).*/Path: $2!$1/
  73.      || s/^From\s+(\S+)[^@]*$/Path: $1\n/;
  74.  
  75.    print INEWS 
  76.        if /^(From|Subject|Date|Path|Newsgroups|Organization|Message-ID):/i;
  77.    $saw_subject |= ( $+ eq 'Subject' );
  78.    $saw_newsgroup |= ( $+ eq 'Newsgroups' );
  79.  
  80. warn "$program: didn't expect newsgroup in both headers and ARGV\n"
  81.     if $newsgroup && $saw_newsgroup;
  82.  
  83. die "$program: didn't get newsgroup from either headers or ARGV\n"
  84.     unless $newsgroup || $saw_newsgroup;
  85.  
  86. printf INEWS "Newsgroups: %s\n", $newsgroup if $newsgroup;
  87. print  INEWS "Subject: Untitled\n" unless $saw_subject;
  88. print  INEWS "\n";
  89.  
  90. print INEWS while <STDIN>;   # gobble rest of message
  91.  
  92. close INEWS;
  93. exit $?;
  94. =================  END OF CUT HERE ========================================
  95.  
  96.     Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
  97.     Convex Computer Corporation                            tchrist@convex.COM
  98.          "EMACS belongs in <sys/errno.h>: Editor too big!"
  99.  
  100.