home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / inject / injnews < prev    next >
Text File  |  1994-09-08  |  5KB  |  160 lines

  1. #! /bin/sh
  2. # injnews [-debug k] [-x site] [-ANV] - `sensible' news injection interface:
  3. #    censor locally-posted article on stdin; munge the article, enforce
  4. #    feeble attempts at Usenet security, generate lots of silly headers.
  5. #
  6. # It could be faster; the alternative is casting a lot of local policy in C.
  7.  
  8. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  9. . ${NEWSCONFIG-/etc/news/bin/config}
  10.  
  11. PATH=$NEWSCTL/bin:$NEWSBIN/inject:$NEWSBIN:$NEWSPATH; export PATH
  12.  
  13. debug=''            # flags
  14. exclusion=''
  15. autopost=no; export autopost
  16. relayopts=-i; export relayopts    # redirect stdout to log
  17. badsites=; export badsites    # export to pnews
  18.  
  19. input=/tmp/in$$in        # uncensored input; save on error
  20. inhdrs=/tmp/in$$hdr        # generated by tear: headers
  21. inbody=/tmp/in$$body        # generated by tear: body
  22. censart=/tmp/in$$cens        # censored input
  23. outfile=/tmp/in$$out        # relaynews stdout
  24. rmlist="$input $inhdrs $inbody $censart $outfile"
  25.  
  26. umask $NEWSUMASK
  27.  
  28. # process arguments: for options, cat headers onto $input; cat files onto $input
  29.  
  30. # save input in dead.article, then clean up
  31. savecleanup="test ! -f $HOME/dead.article -o -w $HOME/dead.article &&
  32.   cat $input >>$HOME/dead.article &&
  33.   { echo $0: article in $HOME/dead.article >&2; rm -f $rmlist; }; exit 1"
  34. justcleanup="rm -f $rmlist; exit 0"    # normal exit - no dead.article
  35. trap "$savecleanup" 0 1 2 3 15
  36.  
  37. ### option processing ###
  38.  
  39. while :
  40. do
  41.     case $# in
  42.     0)    break ;;        # arguments exhausted
  43.     esac
  44.  
  45.     case "$1" in
  46.     # peculiar to C news
  47.     -debug)    shift; debug="$1" ;;
  48.     -A)    autopost=yes ;;            # wait for free space
  49.     -N)    justfilter=yes; trap "$justcleanup" 0 1 2 3 15 ;;
  50.     -V)    relayopts= ;;    # verbose: don't redirect stdout (or stderr)
  51.     # useful standard option
  52.     -x)    shift; badsites="$badsites$1!" ;; # you're welcome, erik (B 2.11)
  53.     *)                    # bad option or a file name
  54.         echo "usage: $0 [-debug k] [-x site] [-ANV]" >&2
  55.         exit 1
  56.         ;;
  57.     esac
  58.     shift                    # pass option
  59. done
  60.  
  61. ### beat up article on stdin ###
  62. cat >$input                    # **
  63.  
  64. if
  65.     case "$justfilter" in    # ** pnews takes 3.2 seconds
  66.     yes) pnews /tmp/in$$ <$input ;;    # output in $inhdrs & $inbody
  67.     *)   pnews /tmp/in$$ <$input >$censart ;;    # output in $inhdrs & $inbody
  68.     esac
  69. then    :
  70. else    exit $?            # clean up; pnews will have complained
  71. fi
  72.  
  73. # to post or to mail? that is the question; whether 'tis nobler in the mind
  74. # to suffer the slings and arrows of outrageous mailers - Bill Shakespeare
  75.  
  76. ngs=` awk '
  77.     # a dreadful hack around all.all.ctl
  78.     /^Control:/    { print "control"; ngs=""; exit }
  79.     /^Newsgroups:/    { ngs=$2 }        # save for END
  80.     END        { if (ngs != "") print ngs }
  81.     ' $inhdrs `
  82. case "$ngs" in            # any Newsgroups:?
  83. '')    exit 1 ;;        # nope: pnews will have already complained
  84. ,*|*,|*,,*)
  85.     echo "$0: illegal null newsgroup(s) in \`$ngs'" >&2
  86.     exit 1 ;;
  87. *[\    \ ]*)
  88.     echo "$0: illegal whitespace in \`$ngs'" >&2
  89.     exit 1 ;;
  90. esac
  91.  
  92. # look up groups in active, to determine disposition of this message.
  93. # n, x and (unapproved) m flags are dealt with on the spot; if none are
  94. # seen, the article is posted normally.
  95. # POLICY: what happens to = groups; how are m articles mailed?
  96. actflag="` actflag $ngs `"        # ** takes 0.4 seconds
  97. case "$actflag" in
  98. '')
  99.     echo "$0: $ngs: no groups in active file" >&2
  100.     exit 1
  101.     ;;
  102. [nx]*)
  103.     # TODO: deal better when only some groups are n or x.
  104.     set $actflag
  105.     ng="$2"
  106.     echo "$0: sorry, $ng may not be posted to locally." >&2
  107.     exit 1
  108.     ;;
  109. m*)
  110.     if grep -s '^Approved:[     ]' $inhdrs >/dev/null; then
  111.         :        # just post normally
  112.     else
  113.         case "$justfilter" in
  114.         yes)    ;;
  115.         *)
  116.             set $actflag
  117.             ng="$2"
  118.             # unApproved article: mail to a moderator.
  119.             moderator="` modroute $ng `"
  120.             echo "mailing your article to $moderator"
  121.             mail "$moderator" <$censart
  122.             ;;
  123.         esac
  124.         trap "$justcleanup" 0    # normal exit - no dead.article
  125.         exit 0
  126.     fi
  127.     ;;
  128.  
  129. y|*)                # * matches garbage flags, to be cautious.
  130.     # okay
  131.     ;;
  132. esac
  133.  
  134. ### if the article was mailed, that happened above; posting will follow ###
  135.  
  136. # to get here, we must have seen no n, x, nor (unapproved) m flags.
  137. # <$censart is used rather than a pipe to work around a bug in the 4.2 sh
  138. # which made it sometimes return the wrong exit status (that of anne.jones).
  139. # may not use "exec" or sh will leave /tmp/sh* files from here docs in /tmp.
  140.  
  141. if
  142.     case "$justfilter" in
  143.     yes)    : ;;
  144.     *)
  145.         # relayopts="$relayopts -s -d ''$debug" \
  146.         PATH=$NEWSCTL/bin:$NEWSBIN/input:$NEWSBIN:$NEWSPATH
  147.         newsspool -g 0 <$censart        # TODO: pass relayopts
  148.         ;;
  149.     esac
  150. then
  151.     # POLICY: warn the user about missing sys file?
  152.     if test ! -f $NEWSCTL/sys; then
  153.         echo "$0: $NEWSCTL/sys missing; your news can't leave this machine" >&2
  154.     fi
  155.     trap "$justcleanup" 0        # normal exit - no dead.article
  156.     exit 0
  157. else
  158.     exit $?                # trap 0 may cleanup, make dead.article
  159. fi
  160.