home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3461 < prev    next >
Encoding:
Internet Message Format  |  1991-06-08  |  3.5 KB

  1. From: dwallach@soda.berkeley.edu (Dan Wallach)
  2. Newsgroups: comp.lang.perl,alt.sources
  3. Subject: Yet another newsrc fixer
  4. Message-ID: <1991Jun7.195223.22125@agate.berkeley.edu>
  5. Date: 7 Jun 91 19:52:23 GMT
  6.  
  7. Well, this is my first perl program, so you'll have to excuse the verbosity.
  8. I'm sure Larry, Randall, or Tom could probably write this in about three lines
  9. of modem noise, but it does what I want. :-)
  10.  
  11. Basically, you keep a file in your home directory called .news.favorite
  12. which lists, in order, what groups you prefer reading.  When 100 new newsgroups
  13. suddenly get added one day, run the program, and your favorites stay on
  14. top of the newsgroup, and the remainder are sorted first by : or !, then
  15. alphabetically.
  16.  
  17. JAPH,
  18.  
  19. Dan Wallach
  20. dwallach@soda.berkeley.edu
  21.  
  22. P.S.  Kudos to Larry and Randall for the Perl book.  I just read it, sat
  23.       down, and cranked this program straight out.
  24.  
  25.  
  26. (I call this fixnewsrc -- you can call it whatever you want :-)
  27. #!/usr/bin/perl
  28.  
  29. # FixNewsrc V1.0 by Dan Wallach
  30. # dwallach@soda.berkeley.edu
  31.  
  32. sub counter {
  33.     $counter++;
  34.     print STDERR "$counter..." if ($counter % 100) == 0;
  35. }
  36.  
  37. sub tally_counter {
  38.     print STDERR "$counter\n";
  39. }
  40.  
  41. sub clear_counter {
  42.     $counter = 0;
  43. }
  44.  
  45. sub print_favorites {
  46.     print STDERR "Parsing favorites: ";
  47.     foreach(<FAVORITE>) {
  48.     chop;
  49.     &counter;
  50.     push (@output, "$newsrc{$_}\n");
  51.     undef $newsrc{$_};
  52.     }
  53.     &tally_counter;
  54. }
  55.  
  56. if(@ARGV) {
  57.     print <<NO_MORE_HELP;
  58. fixnewsrc, V1.0 by Dan Wallach <dwallach@soda.berkeley.edu>
  59.  
  60. Usage: $0       [no arguments]
  61.  
  62. This program sorts your .newsrc, putting groups you read on top.  In addition,
  63. if you have a file in your home directory called .news.favorite, then the
  64. list of newsgroups in this file appear at the top of your .newsrc, so you
  65. can still read groups in your favorite order.
  66.  
  67. Example:
  68.  
  69. rec.humor.funny
  70. alt.fan.warlord
  71. comp.windows.x.announce
  72. ucb.computing.announce
  73. comp.lang.perl
  74.  
  75. Here, you will read rec.humor.funny first, and so on through comp.lang.perl,
  76. then you will continue reading active groups in alphabetical order.
  77. NO_MORE_HELP
  78.     exit 0;
  79. }
  80.  
  81. die "No .newsrc file!" unless -e "$ENV{HOME}/.newsrc";
  82. open(NEWSRC, "$ENV{HOME}/.newsrc") || die "Can't open .newsrc";
  83.  
  84. # we want to keep this associative array around for printing favorites
  85. # so if we've already printed something, we just delete it from the
  86. # associative array, and go on.
  87.  
  88. print STDERR "Reading groups: ";
  89. &clear_counter;
  90. foreach(<NEWSRC>) {
  91.     chop;
  92.     &counter;
  93.     local($group) = split(/[:!]/);  # not necessary, but fun
  94.     $newsrc{$group} = $_;
  95. }
  96. &tally_counter;
  97.  
  98. # output time... clear the counter and let's deal with the favorites file
  99. &clear_counter;
  100.  
  101. if (open(FAVORITE, "$ENV{HOME}/.news.favorite")) {
  102.     &print_favorites;
  103. } else {
  104.     print "No .news.favorite file found.  Just sorting .newsrc\n";
  105. }
  106.  
  107. print STDERR "Sorting...";
  108. @newsrc = sort %newsrc;
  109. print STDERR "Generating output: ";
  110.  
  111. # normally, when we go from the associative array to the scalar array,
  112. # we get a ton of junk -- the associative array's keys and also, for some
  113. # strange reason, blank lines where I undefined stuff earlier.  The if/elsif
  114. # here weeds all that crap out.
  115. foreach(@newsrc) {
  116.     if(/:/) {
  117.     &counter;
  118.     push (@output, "$_\n");
  119.     } elsif (/!/) {
  120.     &counter;
  121.     push (@output2, "$_\n");
  122.     }
  123. }
  124. &tally_counter;
  125.  
  126. close(NEWSRC);
  127. rename("$ENV{HOME}/.newsrc", "$ENV{HOME}/.newsrc.bak") ||
  128.     die "Can't rename .newsrc";
  129.  
  130. open(NEWSRC, "> $ENV{HOME}/.newsrc") || die "Can't open .newsrc for writing";
  131. print NEWSRC @output, @output2;
  132.