home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff319.lzh / CNewsSrc / cnews.orig.lzh / relay / sh / inews < prev    next >
Text File  |  1989-06-27  |  11KB  |  369 lines

  1. #! /bin/sh
  2. # inews [-p] [-debug k] [-x site] [-hMD] [-t subj] [-n ng] [-e exp] [-F ref] \
  3. #  [-d dist] [-a mod] [-f from] [-o org] [-C ng] [file...] - inject news:
  4. #    censor locally-posted article and field the "inews -C" kludge;
  5. #    munge the articles, enforce feeble attempts at Usenet security,
  6. #    generate lots of silly headers.
  7. #
  8. # Yes, it's big, slow and awkward.  The alternative is casting a lot of
  9. # local policy in C.
  10.  
  11. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  12. . ${NEWSCONFIG-/usr/lib/news/bin/config}
  13. export NEWSCTL NEWSBIN NEWSARTS NEWSPATH NEWSUMASK NEWSMASTER NEWSCONFIG
  14. PATH=$NEWSCTL/bin:$NEWSBIN/inject:$NEWSBIN/relay:$NEWSBIN:$NEWSPATH; export PAT
  15.  
  16. PASSEDFROM='';    export PASSEDFROM    # passed to anne.jones in environ.
  17.  
  18. debug=''            # flags
  19. exclusion=''
  20. hdrspresent=no
  21. autopost=no
  22. waitcmd=''
  23. relayopts=-i            # redirect stdout to log
  24.  
  25. whoami=/tmp/in$$who        # just created to determine effective uid
  26. input=/tmp/in$$in        # uncensored input
  27. inhdrs=/tmp/in$$hdr        # generated by tear: headers
  28. inbody=/tmp/in$$body        # generated by tear: body
  29. censart=/tmp/in$$cens        # censored input
  30. nglist=/tmp/in$$ngs        # newsgroups: list
  31. modroute=/tmp/in$$route        # route to moderator's forwarder
  32. exitflag=/tmp/in$$exit        # exit status, if present
  33. outfile=/tmp/in$$out        # relaynews stdout
  34. grpok=/tmp/in$$grp        # flag file: groups okay if present
  35. rmlist="$inhdrs $inbody $input $censart $nglist $modroute $exitflag $outfile $g
  36. pok"
  37.  
  38. umask $NEWSUMASK
  39.  
  40. # "inews -p": invoke rnews
  41. case "$1" in
  42. -p)
  43.     shift
  44.     exec rnews $*        # rnews, bailing out at or near line 1
  45.     ;;
  46. esac
  47.  
  48. # process arguments: for options, cat headers onto $input; cat files onto $inpu
  49.  
  50. >$input
  51. cleanup="test ! -f $HOME/dead.article -o -w $HOME/dead.article &&
  52.   cat $input >>$HOME/dead.article &&
  53.   { echo $0: article in $HOME/dead.article >&2; rm -f $rmlist; }; exit 1"
  54. trap "$cleanup" 0 1 2 3 15
  55. while :
  56. do
  57.     case $# in
  58.     0)    break ;;        # arguments exhausted
  59.     esac
  60.  
  61.     case "$1" in
  62.     # peculiar to C news
  63.     -debug)    shift; debug="$1" ;;
  64.     -A)    autopost=yes ;;        # wait for free space
  65.     -V)    relayopts= ;;        # verbose: don't redirect stdout (or stderr)
  66.     -W)    waitcmd=wait ;;        # wait for completion
  67.     # useful standard options
  68.     -h)    hdrspresent=yes ;;
  69.     -x)    shift; exclusion="-x $1" ;;    # you're welcome, erik (2.11)
  70.     # silly options supplied by newsreaders
  71.     -a)    shift; echo "Approved: $1" >>$input ;;
  72.     -c)    shift; echo "Control: $1" >>$input ;;
  73.     -d)    shift; echo "Distribution: $1" >>$input ;;
  74.     -e)    shift; echo "Expires: $1" >>$input ;;
  75.     -f)    shift; echo "From: $1" >>$input ;;
  76.     -n)    shift; echo "Newsgroups: $1" >>$input ;;
  77.     -t)    shift; echo "Subject: $1" >>$input ;;    # aka Title:
  78.     -D)    # obsolete, undocumented: meant "don't check for recordings".
  79.         # last present in B 2.10.1, invoked by readnews for followups.
  80.         ;;
  81.     -F)    # undocumented in B 2.10.1, documented in B 2.11.
  82.         shift; echo "References: $1" >>$input ;;
  83.     -M)    # this apparently just sets From: to the author of the article
  84.         # instead of the poster (moderator), by leaving the From: line
  85.         # alone (under -h); easy to implement.
  86.         ;;
  87.  
  88.     # pass next options as environment variables to anne.jones
  89.     -o)    shift; ORGANIZATION="$1"; export ORGANIZATION ;;
  90.  
  91.     -C)    # megakludge-o-rama
  92.         # first, permit only to super-users
  93.         >$whoami
  94.         case "`ls -l $whoami | awk '{print $3}'`" in
  95.         root)    : a winner ;;
  96.         *)
  97.             echo "$0: only super-users may create news groups" >&2
  98.             exit 1
  99.             ;;
  100.         esac
  101.         rm -f $whoami
  102.  
  103.         inewsopt="$1"        # for use in message body
  104.         shift            # skip -C to get ng as $1
  105.  
  106.         cat <<! >>$input    # generate a control message
  107. Newsgroups: $1
  108. Control: newgroup $1
  109. Subject: newgroup $1
  110. Approved: above-user@above-host
  111.  
  112. This article generated by inews $inewsopt $1.
  113. !
  114.         ;;
  115.     -*)
  116.         echo "$0: bad option $1" >&2
  117.         exit 1
  118.         ;;
  119.     *)                    # is a filename; append file
  120.         # B 2.11 kludge: assume -h if input starts with headers.
  121.         # apparently the B 2.11 newsreaders assume this.
  122.         tear /tmp/in$$ <$1
  123.         if test -s $inhdrs; then
  124.             hdrspresent=yes
  125.         fi
  126.  
  127.         case "$hdrspresent" in
  128.         no)    echo "" >>$input; hdrspresent=yes ;;
  129.         esac
  130.         # capture incoming news in case relaynews fails
  131.         if cat $inhdrs $inbody >>$input; then
  132.             : far out
  133.         else
  134.             echo "$0: lost news; cat status $?" >&2
  135.             exit 1
  136.         fi
  137.         fileseen=yes
  138.         ;;
  139.     esac
  140.     shift        # pass option or filename (any value was done above)
  141. done
  142.  
  143. # if no files named, read stdin
  144. case "$fileseen" in
  145. yes)    ;;
  146. *)
  147.     # B 2.11 kludge: assume -h if input starts with headers
  148.     # apparently the B 2.11 newsreaders assume this.
  149.     tear /tmp/in$$
  150.     if test -s $inhdrs; then
  151.         hdrspresent=yes
  152.     fi
  153.  
  154.     case "$hdrspresent" in
  155.     no)    echo "" >>$input; hdrspresent=yes ;;
  156.     esac
  157.     # capture incoming news in case relaynews fails
  158.     if cat $inhdrs $inbody >>$input; then
  159.         : far out
  160.     else
  161.         echo "$0: lost news; cat status $?" >&2
  162.         exit 1
  163.     fi
  164.     ;;
  165. esac
  166. trap '' 1 2 15            # ignore signals to avoid losing articles
  167.  
  168. # run the remainder in the background for the benefit of impatient people
  169. # who lack a window system
  170. (
  171. trap "$cleanup" 0
  172. tear /tmp/in$$ <$input        # output in $inhdrs and $inbody
  173. # pad zero-line articles, since old B [ir]news are confused by them
  174. # and the news readers generate zero-line control messages, alas.
  175. if test ! -s $inbody; then
  176.     (echo '';
  177.      echo This article was probably generated by a buggy news reader.) \
  178.      >$inbody
  179. fi
  180.  
  181. # deduce which tr we have: v6 or v7
  182. case "`echo B | tr A-Z a-z `" in
  183. b)    trversion=v7 ;;
  184. B)    trversion=v6 ;;            # or System V
  185. esac
  186. export trversion
  187.  
  188. # post with new headers and .signature
  189. (anne.jones <$inhdrs        # bash headers
  190.  # echo "Lines: `        # sop to msb, just uncomment to use
  191.  # if test -r $HOME/.signature; then
  192.  #    (cat $inbody; echo '-- '; sed 4q $HOME/.signature) | wc -l
  193.  # else
  194.  #    wc -l <$inbody
  195.  # fi
  196.  # `"
  197.  
  198.  # strip invisible chars from body, a la B news
  199.  case "$trversion" in
  200.  v7)    tr -d '\1-\7\13\14\16-\37' ;;
  201.  v6)    tr -d '[\1-\7]\13\14[\16-\37]' ;;
  202.  esac <$inbody
  203.  
  204.  if test -r $HOME/.signature; then
  205.     echo "-- "; sed 4q $HOME/.signature    # glue on first bit of signature
  206.  fi) >$censart
  207.  
  208. # to post or to mail? that is the question; whether 'tis nobler in the mind
  209. # to suffer the slings and arrows of outrageous mailers - Bill Shakespeare
  210. if grep -s '^Control:' $inhdrs; then
  211.     echo "control"            # a dreadful hack around all.all.ctl
  212. else
  213.     sed -n '
  214. /^Newsgroups:[     ]/{
  215. s/^Newsgroups:[     ]*\(.*\)$/\1/p
  216. q
  217. }
  218. ' <$inhdrs
  219. fi >$nglist
  220.  
  221. if test ! -s $nglist; then        # no Newsgroups:
  222.     exit 1                # anne.jones will have already complained
  223. fi
  224.  
  225. # look up groups in active, to determine disposition of this message.
  226. # n, x and (unapproved) m flags are dealt with on the spot; if none are
  227. # seen, the article is posted normally.
  228. # escape egrep metacharacters.  In theory one could add " ' ` \
  229. egreppat="^(` sed -e 's/[.+*()|[]/\\\\&/g' -e 's/,/|/g' <$nglist `) "
  230. egrep "$egreppat" $NEWSCTL/active >/dev/null || {
  231.     echo "$0: `cat $nglist` matches no groups in $NEWSCTL/active" >&2
  232.     exit 1
  233. }
  234. rm -f $grpok
  235. egrep "$egreppat" $NEWSCTL/active |
  236.     (while read ng high low flag junk    # look at next group's active entry
  237.     do
  238.         >>$grpok
  239.         case "$flag" in
  240.         [nx])
  241.             echo "$0: bugger off, $ng may not be posted to." >&2
  242.             echo 1 >$exitflag
  243.             trap 0        # this is a child process - no cleanup
  244.             exit 1        # dregs in /tmp/in$$*
  245.             ;;
  246.         m)
  247.             if grep -s '^Approved:[     ]' $inhdrs; then
  248.                 rm $modroute        # just post normally
  249.             else
  250.                 # un-Approved: mail it to the moderator(s).
  251.                 echo "%s" >$modroute    # in case no route
  252.                 # look for route for this group
  253.                 cat $NEWSCTL/mailpaths |
  254.                     while read ngpat route junk
  255.                     do
  256.                         # a dreadful B 2.11 hack:
  257.                         # backbone == all
  258.                         case "$ngpat" in
  259.                         backbone) ngpat="all" ;;
  260.                         esac
  261.                         if gngp -a "$ngpat" $nglist >/dev/null; then
  262.                             echo "$route" >$modroute
  263.                             break    # take only 1st match
  264.                         fi
  265.                     done
  266.             fi
  267.             # ngpat and route are not set here, damn it!
  268.             if test -s $modroute; then
  269.                 # an unapproved article in a mod group:
  270.                 # mail the article to this moderator.
  271.                 moderator=`
  272.                  sed "s/%s/\` echo $ng | tr . - \`/" $modroute
  273.                 `
  274.                 echo "$0: mailing your article to $moderator" >&2
  275.                 mail $moderator <$censart
  276.                 rm -f $rmlist
  277.                 echo 0 >$exitflag
  278.                 trap 0    # this is a child process - did cleanup
  279.                 exit 0
  280.             fi
  281.             ;;
  282.  
  283.             # "" matches short active entries,
  284.             #    to be backward compatible.
  285.             # * matches garbage flags, to be cautious.
  286.         y|""|*)
  287.             # okay so far, but wait until we see all Newsgroups:.
  288.             ;;
  289.         esac
  290.     done
  291.     trap 0                    # paranoia - no clean up
  292.     )
  293. if test ! -r $grpok; then
  294.     echo "$0: no active groups in `cat $nglist`" >&2
  295.     exit 1            # abnormal exit - cleans up, makes dead.article
  296. fi
  297. if test -f $exitflag; then
  298.     exitstatus="`cat $exitflag`"
  299.     case "$exitstatus" in
  300.     0)    trap 0 ;;    # normal exit - cleanup done, no dead.article
  301.     esac
  302.     exit $exitstatus    # trap 0 will cleanup, make dead.article
  303. fi
  304.  
  305. # deal with inadequate free space
  306. case "$autopost" in
  307. no)
  308.     if test "`spacefor 1 articles`" -le 0; then
  309.         echo "$0: too little space free on $NEWSARTS" >&2
  310.         exit 1            # dregs in /tmp/in$$* for trap 0
  311.     fi
  312.     ;;
  313. *)
  314.     iter=0
  315.     while test "`spacefor 1 articles`" -le 0 -o "`spacefor 1 control`" -le 0
  316.     do
  317.         sleep 30
  318.         iter=`expr $iter + 1`
  319.         case "$iter" in
  320.         3)
  321.             mail "$NEWSMASTER" <<!
  322. Subject: free space too low on $NEWSARTS
  323.  
  324. There is too little free space on $NEWSARTS for inews to run comfortably.
  325. !
  326.             ;;
  327.         esac
  328.     done
  329.     ;;
  330. esac
  331.  
  332. # to get here, we must have seen no n, x, nor (unapproved) m flags.
  333. # <$censart is used rather than a pipe to work around a bug in the 4.2 sh
  334. # which made it sometimes return the wrong exit status (that of anne.jones).
  335. # execute relaynews commands on the server, for the sake of locking.
  336. # may not use "exec" or sh will leave /tmp/sh* files from here docs in /tmp.
  337. me="`hostname`"
  338. server=`cat $NEWSCTL/server 2>/dev/null`
  339. case "$server" in
  340. "")    server="$me" ;;            # if no server file, assume this is it
  341. esac
  342. case "$me" in
  343. $server)
  344.     relaynews $relayopts -s $exclusion -d "$debug" <$censart
  345.     status=$?
  346. #    echo "status $? from relaynews" >>/tmp/inewsdebug # DEBUG
  347.     ;;
  348. *)
  349.     status=`rsh $server \
  350. "PATH=$PATH relaynews $relayopts -s $exclusion -d \"$debug\"; echo status $?" \
  351.         <$censart >$outfile; sed -n '/^status /s///p' $outfile `
  352.     sed '/^status /d' $outfile    # print relaynews's stdout
  353.     ;;
  354. esac
  355. case "$status" in
  356. 0)
  357.     rm -f $rmlist            # far out, it worked: clean up
  358.     if test ! -f $NEWSCTL/sys; then
  359.         echo "$0: $NEWSCTL/sys missing; your news can't leave this machine" >&2
  360.     fi
  361.     trap 0                # normal exit: cleanup done
  362.     ;;
  363. esac
  364. exit $status                # trap 0 may cleanup, make dead.article
  365. ) &
  366. $waitcmd                # wait if -W given
  367. trap 0                    # let the background run on unmolested
  368. exit
  369.