home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / usenet / procsjnk.sh < prev    next >
Encoding:
Text File  |  1989-03-21  |  8.6 KB  |  287 lines

  1. 23-Nov-85 06:10:04-MST,9005;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Sat 23 Nov 85 06:09:52-MST
  4. Received: from usenet by TGR.BRL.ARPA id a003838; 23 Nov 85 7:40 EST
  5. From: Charlie Perkins <charliep@polaris.uucp>
  6. Newsgroups: net.sources
  7. Subject: The (nearly) Compleat junk news processor
  8. Message-ID: <298@polaris.UUCP>
  9. Date: 22 Nov 85 01:24:20 GMT
  10. To:       unix-sources@BRL-TGR.ARPA
  11.  
  12. =========
  13. Are you tired of just tossing all the articles in your
  14. junk repository just because it's too much hassle to
  15. clean 'em up?  Do you really want to make sure that
  16. there is EVEN MORE news available to your users?
  17.  
  18. Well, then here is just the thing you need.  I have
  19. been working on this program very sporadically for
  20. a long time, and it seems to be pretty reliable.
  21. You will probably want to modify certain things
  22. about it, not least the shell variables at the tops
  23. of the programs like $LIB, etc.  You may want to
  24. modify it so that junk is kept around less often
  25. than I keep it.  I have some other (less useful)
  26. processing I do as a further stage.
  27.  
  28. The "main" program is "process_junk", which
  29. calls "modactive" and "getnewsgps".  The latter
  30. two programs are much less useful by themselves,
  31. and could reasonably become C programs if speed
  32. becomes an issue.  One general area that could
  33. be improved is to add locking so that these scripts
  34. could be run without disabling the news.
  35.  
  36. I don't have a shar program handy, so just separate
  37. the programs which all three are preceded by lines
  38. containing '='s.
  39.  
  40. I will be happy to distribute bug fixes and/or
  41. improvements if they are sent to me.
  42.  
  43. ===================================================
  44. process_junk
  45. ===================================================
  46. SPOOL=/usr/spool/news
  47. NEWSBIN=$SPOOL/bin
  48. LIB=/usr/local/lib/news
  49. ACTIVE=$LIB/active
  50. LOG=$LIB/log
  51. JUNK=$SPOOL/junk
  52. GRPCHARS=",-a-z0-9."
  53. LOCALGP=ibm
  54.  
  55. PATH=/usr/local:/bin:/usr/bin:$NEWSBIN
  56. count=
  57. export PATH count
  58. #
  59. #    NOTE: You should make sure that no new articles can be posted while
  60. #          this script is running, otherwise $ACTIVE might be screwed up
  61. #          a little bit.
  62. #    It assumes that:
  63. #          1. It is OK to create any necessary newsgroups,
  64. #          2. The active file is in news 2.10.2 format,
  65. #          3. Posting to fa.*, mod.* is disallowed,
  66. #          4. Posting to na.*, net.* $LOCALGP.* is allowed, and
  67. #          5. It is OK to leave unprocessed articles in $JUNK.
  68. #          6. There are commas between newsgroups when there an
  69. #         article is posted to multiple newsgroups.
  70. #          7. No "parent" newsgroup directories need to be created.
  71. #         (This assumption is often violated).
  72. #
  73. #    NOTE: A check is performed to make sure that all the current copies 
  74. #        of the junk article are links to the same file.    
  75. #
  76. #    NOTE: If a junk article does not need to be posted to ANY groups,
  77. #        this script doesn't recognize it and leaves the article in $JUNK
  78. #        I do have additional (less interesting) processing that I do in
  79. #        this case.
  80. #
  81. #    Because of #1, a "grep '^Newsgroups: ' $JUNK/*" might be advisable
  82. #    before running this script.
  83. #
  84. #    Last note: don't shoot me if some comment or another is outdated.
  85. #
  86. cd $JUNK
  87. junkcount=`sed -n "/^junk /s/junk \([0-9]*\).*/\1/p" $ACTIVE`
  88. case "$junkcount" in
  89. "")    exit
  90. esac
  91. newsgrps=`while test "$junkcount" -ge ${i=0}000
  92.     do
  93.         case "$i" in
  94.         0)    getnewsgps [1-9] [1-9]? [1-9]??;;
  95.         *)    getnewsgps ${i}???
  96.         esac
  97.         i=\`expr $i + 1\`
  98.     done | tr ' ' '\012' | sort -u`
  99. (cd $LIB; tar cf - active ) | (cd /usr/tmp; tar xpvf - )    # Save a $ACTIVE
  100. #
  101. #    Crunch, crunch -- do a newsgroup at a time...
  102. #
  103. for newsgrp in $newsgrps
  104. do
  105.     ngline='^Newsgroups:.*( |,)'$newsgrp'(,|$)'
  106.     articles=`2> /dev/null egrep -l "$ngline" [1-9] [1-9]? [1-9]??
  107.         i=1
  108.         while test ${i}000 -le $junkcount
  109.         do
  110.             2> /dev/null egrep -l "$ngline" ${i}???
  111.             i=\`expr $i + 1\`
  112.         done`
  113.     newsdir=$SPOOL/`echo $newsgrp | sed "s;\.;/;g"`
  114.     count=`sed -n "/^$newsgrp /s/$newsgrp \([0-9]*\).*/\1/p" $ACTIVE | sed 1q`
  115.     if test -z "$count"   -a   ! -d $newsdir
  116.     then
  117.     #
  118.     #    Directories will exist for non-existent
  119.     #    newsgroups, when the newsgroup has a sub-group
  120.     #    that was created first (e.g, "mod.std.c" came
  121.     #    before "mod.std").
  122.     #
  123.     #    NOTE that this does not handle the case when
  124.     #    parent directories also need to be created.
  125.     #
  126.         date_field=`date '+%h %d %H:%M'`
  127.         echo "$date_field    local.prjunk    Junk processing: creating newsgrp $newsdir."
  128.         mkdir $newsdir
  129.     fi | tee -a $LOG
  130.     count=`expr 0$count + 0`        # Toss leading zeroes.
  131.     if test ! -d $newsdir
  132.     then
  133.         date_field=`date '+%h %d %H:%M'`
  134.         echo "$date_field    local.prjunk    Junk processing: Bad directory $newsdir!" |
  135.             tee -a $LOG
  136.         continue
  137.     fi
  138.     prevcount=$count
  139.     chown news $newsdir
  140.     INDEX=$newsdir/IndeX
  141.     #
  142.     #    Create indexes for each newsgroup
  143.     #    -- save the time needed for continually
  144.     #       grepping each article.
  145.     #
  146.     ( cd $newsdir
  147.       2> /dev/null grep '^Message-ID: <' [1-9] [1-9]? [1-9]??
  148.         i=1
  149.       while test ${i}000 -le $count
  150.       do
  151.           2> /dev/null grep '^Message-ID: <' ${i}???
  152.           i=`expr $i + 1`
  153.       done ) | sed "s/\([0-9]*\):\(.*\)/\2 \1/" | uniq -2 > $INDEX
  154.     for article in $articles
  155.     do
  156.         articleID=`sed -n "/^Message-ID: </s///p
  157.                     10q" $article | sed 1q`
  158.         #
  159.         #    This new strategy will fail in the following case:
  160.         #        the article needs to be posted to some of the
  161.         #        newsgroups in the Newsgroup line, but not all
  162.         #        of them.  I have never seen this case.
  163.         #        I have an old version that handles it, though.
  164.         #    Also, it removes articles after they have been
  165.         #        processed.  That may be undesirable.
  166.         #        (our "rm" secretly saves them for a day...)
  167.         #
  168.         oldartnum=`sed -n "/^Message-ID: <$articleID/s/.* //p" $INDEX`
  169.         case "$oldartnum" in
  170.         "")    count=`expr 0"$count" + 1`
  171.             newsart=$newsdir/$count
  172.             date_field=`date '+%h %d %H:%M'`
  173.             if test ! -f $newsart    &&    ln $article $newsart
  174.             then
  175.                 echo "$date_field    local.prjunk    Linked $article to $newsart."
  176.                 echo "Message-ID: <$articleID $count" >> $INDEX
  177.                 oldartnum=$count
  178.                 linegrps=`getnewsgps $article |
  179.                         tr ' ' '\012' | sort -u`
  180.                 case "$linegrps" in
  181.                 *$newsgrp)    rm $article
  182.                 esac    # If it was $article's last newsgroup.
  183.             else
  184.                 echo "$date_field    local.prjunk    Link problem; $article $newsart"
  185.             fi | tee -a $LOG
  186.         esac
  187.         echo article="$article", oldartpath="$newsdir/$oldartnum"
  188.     done
  189.     #
  190.     #     All articles belonging to $newsgrp have been processed.
  191.     #    Adjust the count field in $ACTIVE.
  192.     #
  193.     case "$prevcount" in
  194.     "$count")    ;;
  195.     *)        modactive $newsgrp $count
  196.     esac
  197. done
  198. #
  199. #    Final cleanup; modify entry for junk newsgroup in $ACTIVE.
  200. #
  201. date_field=`date '+%h %d %H:%M'`
  202. oldarts=`ls [1-9]* | sort -n`    ||
  203.     { echo "$date_field    local.prjunk    ls problem during $JUNK cleanup." |
  204.                     tee -a $LOG
  205.       exit ; }
  206. count=0
  207. for article in $oldarts
  208. do
  209.     count=`expr $count + 1`
  210.     if test ! -f $count   &&   ln $article $count
  211.     then
  212.         rm $article
  213.     else
  214.         date_field=`date '+%h %d %H:%M'`
  215.         echo "$date_field    local.prjunk    mv $article $count fails in $JUNK cleanup." |
  216.             tee -a $LOG
  217.     fi
  218. done
  219. modactive junk $count
  220. ===================================================
  221. modactive
  222. ===================================================
  223. SPOOL=/usr/spool/news
  224. NEWSBIN=$SPOOL/bin
  225. LIB=/usr/local/lib/news
  226. ACTIVE=$LIB/active
  227. LOG=$LIB/log
  228. JUNK=$SPOOL/junk
  229. LOCALGP=ibm
  230. STATE=ny
  231. PATH=/bin
  232.  
  233. newsgrp=$1
  234. count=$2
  235.  
  236. case $count in
  237. [1-9])        count=0000$count ;;
  238. [1-9]?)        count=000$count ;;
  239. [1-9]??)    count=00$count ;;
  240. [1-9]???)    count=0$count ;;
  241. [1-9]????)    ;;
  242. *)        
  243.         date_field=`date '+%h %d %H:%M'`
  244.         echo "$date_field    local.modact    Problem in $newsgrp, count=$count." >> $LOG
  245.         continue
  246. esac
  247.  
  248. therenow=`grep "^$newsgrp " $ACTIVE`
  249. case "$therenow" in    # if empty, it's a new newsgroup.
  250. "")    parent=`expr "$newsgrp" : "\([^.]*\)."`
  251.     case "$parent" in
  252.     mod|fa|ba)        postable=n ;;
  253.     $LOCALGP|$STATE|net|na)    postable=y ;;
  254.     *)            echo "Problem with $newsgrp; parent=$parent..."
  255.                 continue
  256.     esac
  257.     echo "$newsgrp $count 00001 $postable" >> $ACTIVE ;;
  258. *)    sed "/^$newsgrp /s/ [0-9]*/ $count/" $ACTIVE > /tmp/junkactive$$
  259.     mv /tmp/junkactive$$ $ACTIVE
  260. esac
  261. ===================================================
  262. getnewsgps
  263. ===================================================
  264. #!/bin/sh
  265.  
  266. PATH=/bin:/usr/bin
  267. GRPCHARS=",-a-z0-9."
  268.  
  269. case $# in
  270. 0|1|2|3|4|5|6|7) # For only a few arguments, it's wasteful to have big pipelines.
  271.     for i
  272.     do
  273.         2> /dev/null sed -n "/^Newsgroups: [$GRPCHARS]*$/s/,/ /g
  274.                /^Newsgroups: \([ $GRPCHARS]*\)$/s//\1/p
  275.                10q" $i
  276.     done ;;
  277. *)    # For lots of arguments, the for loop is wasteful.
  278.     2> /dev/null grep '^Newsgroups: ' $* |
  279.         sed "s/\([0-9]*\):Newsgroups: \(.*\)/\2 \1/" |
  280.         uniq -1  | sed "s/ [0-9]*$//
  281.             s/,/ /g"
  282. esac
  283. -- 
  284.  
  285. Charlie Perkins, IBM T.J. Watson Research    philabs!polaris!charliep,
  286.         perk%YKTVMX.BITNET@berkeley,  perk.yktvmx.ibm@csnet-relay
  287.