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