home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2827 < prev    next >
Encoding:
Internet Message Format  |  1991-02-22  |  3.9 KB

  1. From: Sepp@ppcger.ppc.sub.org (Josef Wolf)
  2. Newsgroups: alt.sources,comp.lang.perl
  3. Subject: Re: An AWK script to check "junk" for newsgroups
  4. Message-ID: <u4+nMD@ppcger.ppc.sub.org>
  5. Date: 19 Feb 91 19:41:56 GMT
  6.  
  7. dglo@ADS.COM (Dave Glowacki) writes:
  8.  
  9. ] Since, as a rule, EVERY C or shell program posted must be followed up
  10. ] by a PERL script, here's my version of NEWJUNK.
  11.  
  12. Well. Fine fine. But what about using standard-tools? Which *IX is
  13. _delivered_ with Perl?
  14.  
  15. Now here is my version of NEWJUNK. It could have been better, but older
  16. versions of gawk have these ugly memory-leak, so you have to sort out
  17. the 'Newsgroups:' lines and pipe it into gawk :-(
  18.  
  19. The awk-version will most likely be slower than the C-version and the
  20. Perl-Version, but it schould run on most *IX with little modifications.
  21.  
  22. This version uses 3 config files:
  23. /usr/lib/news/newjunk.active    this are the newsgroups, I am interested
  24. /usr/lib/news/newjunk.trash     to throw away the entire Newsgroups:-line
  25. /usr/lib/news/newjunk.junk      Newsgroups, I don't want
  26.  
  27. In the config-files you can use regular expressions. Here is my
  28. newjunk.active, for example:
  29.  
  30. ---- snipp ----
  31. # newjunk.active
  32. #
  33. # these are the newsgroups I want to have complete, if they will be
  34. # found in junk
  35. ^comp\.sys\..*
  36. ^comp\.os\..*
  37. ^comp\.mail.*
  38. ^dnet\..*
  39. ^eunet\..*
  40. ^mnet\..*
  41. ^ppc\..*
  42. ^sub\..*
  43. #               I want all.sources.all
  44. .*\.sources.*
  45. #               and all.os9.all
  46. .*\.os9\..*
  47. ---- snipp ----
  48.  
  49. Here goes newjunk.awk. Just pipe all '^Newsgroups:' into 'awk -f newjunk.awk'
  50.  
  51. ---- snipp---
  52. BEGIN {
  53.  
  54. # read in active
  55.   FS = ":";
  56. #      ^^^  my news-system needs this one
  57.   while (getline <"/usr/lib/news/active" > 0)
  58.     if (length ($1))
  59.       active [activecount++] = $1;
  60.  
  61. # read in config files
  62.   while (getline tmp <"/usr/lib/news/newjunk.active" > 0)
  63.     if (length (tmp) && !match (tmp, "^#"))
  64.       nactive [nactivecount++] = tmp;
  65.  
  66.   while (getline tmp <"/usr/lib/news/newjunk.trash" > 0)
  67.     if (length (tmp) && !match (tmp, "^#"))
  68.       trash [trashcount++] = tmp;
  69.  
  70.   while (getline tmp <"/usr/lib/news/newjunk.junk" > 0)
  71.     if (length (tmp) && !match (tmp, "^#"))
  72.       junk [junkcount++] = tmp;
  73.  
  74.   FS = ",";
  75. # newsgroups are separated with kommas
  76. }
  77.  
  78. function insert_newsgroup(ng) {
  79.  
  80. # if newsgroup is alraedy inserted, we can save some time
  81.   for (k = 0; k < newcount; k++)
  82.     if (ng == newgroups [k])
  83.       return;
  84.  
  85. # skip newsgroup if it is already active
  86.   for (k = 0; k < activecount; k++)
  87.     if (ng == active [k])
  88.       return;
  89.  
  90. # insert newsgroup
  91.   newgroups [newcount++] = ng;
  92. }
  93.  
  94.  
  95. // {
  96.  
  97. # check every newsgroup given in input line
  98.   for (j = 1; j <= NF; j++) {
  99.  
  100. # do we want this newsgroup?
  101.     for (i = 0; i < nactivecount; i++) {
  102.       if (match ($j, nactive [i])) {
  103.         insert_newsgroup($j);
  104. #        break;
  105. # don't know why I get some bus-error at this break -- sigh!
  106. # but the script runs without this too (grinn :-)
  107.       }
  108.     }
  109.  
  110. # is there any trash-newsgroup?
  111.     for (i = 0; i < trashcount; i++)
  112.       if (match ($j, trash [i]))
  113.         next;
  114.  
  115. # no trash-groups -> sort out the junk-newsgroups
  116.     to_insert_count = 0;
  117.     for (i = 0; i < junkcount; i++)
  118.       if (!match ($j, junk [i]))
  119.         to_insert [to_insert_count++] = $j;
  120.   }
  121.  
  122. # insert them now
  123.   for (i = 0; i < to_insert_count; i++)
  124.     insert_newsgroup(to_insert [i]);
  125. }
  126.  
  127. END {
  128.   for (i = 0; i < newcount; i++) {
  129. # insert the command for YOUR inews here
  130.     cmd = "inews -ad=local '-c=newgroup:" newgroups[i] "' </nil";
  131.     system (cmd);
  132.     print newgroups [i];
  133.   }
  134. }
  135. ---- snipp ----
  136.  
  137. Greetings
  138.         Sepp
  139.  
  140. Disclaimer: I had no time to make much tests of this version of newjunk.awk 
  141.             If there are bugs, please let me know :-)
  142.  
  143. | Josef Wolf, Germersheim, Germany | +49 7274 8047  -24 Hours- (call me :-)
  144. | ...!ira.uka.de!smurf!ppcger!sepp | +49 7274 8048  -24 Hours-
  145. |     sepp@ppcger.ppc.sub.org      | +49 7274 8967  18:00-8:00, Sa + Su 24h
  146. |  "is there anybody out there?"   | all lines 300/1200/2400 bps 8n1
  147.