home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2371 / gatenews < prev    next >
Encoding:
Text File  |  1990-12-28  |  3.2 KB  |  127 lines

  1. :
  2. # gatenews - a script to gate news from news to mail
  3. # Clay Luther, 1990, Version 1
  4. # cluther@supernet.haus.com
  5. # Usage: gatenews dest spoolfile from
  6.  
  7. # Defines
  8. SPOOL='/usr/spool/news/gatenews'
  9. NEWSDIR='/usr/lib/news'
  10.  
  11. if [ $# -ne 3 ]
  12. then
  13.   echo "Usage: gatenews dest spoolfile from"
  14.   echo "  where dest is the address of the destination, and"
  15.   echo "  spoolfile is the name of a file in $SPOOL"
  16.   echo "  from is the local address to set the from line to"
  17.   exit 1
  18. fi
  19.  
  20. DEST=$1
  21. HIST="$NEWSDIR/gatenews.log"
  22. SFILE="$SPOOL/$2"
  23. NEWS='news'
  24. MAIL='/usr/local/bin/fastmail'
  25. TMP="/tmp/gate$$"
  26. MAILNAME="$NEWSDIR/mailname"
  27. HOST=`cat $MAILNAME`
  28. ORG="Gated to News by $HOST" 
  29. FNAME="$3@$HOST"
  30.  
  31.  
  32. if [ ! -f "$SFILE" ]
  33. then
  34.   exit 0
  35. fi
  36.  
  37. if [ ! -f "$HIST" ]
  38. then
  39.   echo "" | cat > $HIST
  40.   chmod +rw $HIST
  41.   chown $NEWS $HIST
  42. fi
  43.  
  44. S=`cat $SFILE`
  45. rm -f $SFILE
  46.  
  47. for F in $S
  48. do
  49.   if [ -f "$F" ] 
  50.   then
  51. #    echo $F
  52.     MID=`grep "^Message-ID:" $F`   # Obtain Message ID
  53.     if [ "$MID" ]
  54.     then
  55.       INSTR="$MID -> $DEST"
  56.       PREV=`grep "$MID" $HIST`          # Check for it in gatehist file
  57.       if [ -z "$PREV" ]
  58.       then  # he hasn't been mailed before
  59.  
  60. # Get the Organization Line - IMPORTANT
  61.         FORG=`grep "^Organization:.*$ORG" $F`   
  62.         if [ ! "$FORG" ]
  63.         then   # it's ok, we didn't make this!
  64. #more $F
  65.         # Save the From line
  66.         FROM=`grep "^From:" $F | awk '{ \
  67.                                     size=split($0,s)
  68.                                     for (i=2;i<=size;i++) {
  69.                                       printf("%s ",s[i])
  70.                                     }
  71.                                     printf("%s","\n")
  72.                                     }'`
  73.         # Save the Subject line
  74.         SUBJ=`grep "^Subject:" $F | awk '{ \
  75.                                     size=split($0,s)
  76.                                     for (i=2;i<=size;i++) {
  77.                                       printf("%s ",s[i])
  78.                                     }
  79.                                     printf("%s","\n")
  80.                                     }'`
  81.  
  82.         # Make an entry in $TMP
  83.         (
  84.         echo "X-Gateway: $HOST"
  85.         ) | cat > $TMP
  86.  
  87.         # The above is necessary to catch mail echoed back to us!
  88.  
  89.         # Strip away the news headers and store output in $TMP
  90.         awk '\
  91.                BEGIN {
  92.                  x=0
  93.                }
  94.                {
  95.                  if ( x == 0 ) {
  96.                    if ( NF == 0 ) {
  97.                      x=1
  98.                    }
  99.                  } else {
  100.                    print $0
  101.                  }
  102.                }' $F >> $TMP 
  103.  
  104.          # OK, we have a From line, a Subject Line, and a source file
  105.          # Time to mail it and log it
  106.          
  107.          # This line may need tweaking, depending on your mailer
  108.          $MAIL -r "$FROM" -s "$SUBJ" -f "$FNAME" $TMP $DEST
  109.          # Log the message id
  110.          echo $INSTR | cat >> $HIST
  111.        fi  # we didn't make it
  112.        fi  # wasn't in hist file
  113.      fi    # if grep $MID
  114.    fi    # $F existed
  115. done     # with loop
  116.  
  117. # Clean up
  118. rm -f $TMP
  119.  
  120. # Check the $HIST log for being way too long
  121. LEN=`wc -l $HIST | awk '{print $1}'`
  122. if [ $LEN -gt 2000 ]
  123. then
  124.   tail -2000 $HIST > $TMP
  125.   mv $TMP $HIST
  126. fi
  127.