home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2754 < prev    next >
Encoding:
Text File  |  1991-02-14  |  7.6 KB  |  288 lines

  1. Newsgroups: alt.sources,comp.lang.perl
  2. From: dglo@ADS.COM (Dave Glowacki)
  3. Subject: A PERL script to check "junk" for newsgroups
  4. Message-ID: <~Y+&#J#@ads.com>
  5. Date: Wed, 13 Feb 91 23:25:39 GMT
  6.  
  7. Since, as a rule, EVERY C or shell program posted must be followed up
  8. by a PERL script, here's my version of NEWJUNK.
  9.  
  10. Mine is called 'check-junk'.  It grabs the Newsgroups: lines from all
  11. junked articles, processes them according to a couple of configuration
  12. files and either mails a report to any addresses specified on the command
  13. line or prints the report to stdout (if there weren't any arguments.)
  14.  
  15. The two configuration files are lists of patterns.  The first list
  16. (junk-trash-list) throws away the entire Newsgroups: line for an article
  17. if a pattern from it matches any of the newsgroups in the line.  The
  18. second list (junk-ignore-list) only ignores the newsgroup matched by
  19. a particular pattern.
  20.  
  21. I use 'junk-trash-list' to throw away references to regional newsgroups
  22. like 'sub', 'dnet', and 'ne' where things tend to be crossposted to
  23. both a regional group and a local group.  'Junk-ignore-list' is more of
  24. a specific newsgroup/hierarchy eliminator for things like 'alt.sex.*' or
  25. 'alt.desert.storm.its.not.scud.its.al-hussein.dammit'.
  26.  
  27. To install this, stick everything in /usr/lib/news (or wherever you
  28. put these things) and make sure NEWSCTL and NEWSARTS are set correctly.
  29. I run it with '/usr/lib/news/check-junk news@ads.com' every night
  30. before I expire.
  31.  
  32. #! /bin/sh
  33. # This is a shell archive.  Remove anything before this line, then feed it
  34. # into a shell via "sh file" or similar.  To overwrite existing files,
  35. # type "sh file -c".
  36. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  37. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  38. # If this archive is complete, you will see the following message at the end:
  39. #        "End of shell archive."
  40. # Contents:  check-junk junk-ignore-list junk-trash-list
  41. # Wrapped by dglo@saturn on Wed Feb 13 14:36:09 1991
  42. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  43. if test -f 'check-junk' -a "${1}" != "-c" ; then 
  44.   echo shar: Will not clobber existing file \"'check-junk'\"
  45. else
  46. echo shar: Extracting \"'check-junk'\" \(4127 characters\)
  47. sed "s/^X//" >'check-junk' <<'END_OF_FILE'
  48. X#!/usr/local/bin/perl
  49. X#
  50. X# Build a list of all newsgroups sent to 'junk' newsgroup
  51. X#    and either mail the report to the addresses listed on the command line
  52. X#        or print it to STDOUT
  53. X#
  54. X# The report is a series of lines of the form 'nnn articles for newsgroup'
  55. X#
  56. X# $Header: /var/news/src/ADS-scripts/RCS/check-junk,v 1.7 1991/02/13 22:31:08 dglo Exp $
  57. X
  58. X# subroutine to read in the C news environment
  59. X#
  60. X$NEWSCONFIG='/usr/lib/news/bin/config';
  61. X%NEWSENV = ();
  62. Xsub newsconfig {
  63. X  if (open(NEWSENV, "sh -x $NEWSCONFIG 2>&1  |")) {
  64. X    while (<NEWSENV>) {
  65. X      $NEWSENV{$1} = $2 if (/(.*)=(.*)\n/);
  66. X    }
  67. X    close(NEWSENV);
  68. X    1;
  69. X  } else {
  70. X    0;
  71. X  }
  72. X}
  73. X
  74. X# News locations (handle both C news and B news)
  75. X#
  76. Xif (&newsconfig()) {
  77. X  $NEWSCTL = $NEWSENV{'NEWSCTL'};
  78. X  $NEWSARTS = $NEWSENV{'NEWSARTS'};
  79. X} else {
  80. X  $NEWSCTL="/usr/lib/news";
  81. X  $NEWSARTS="/usr/spool/news";
  82. X}
  83. X
  84. X# see if the result is supposed to be mailed somewhere
  85. X#
  86. X$maillist = '';
  87. Xwhile (@ARGV > 0) {
  88. X  $_ = pop(ARGV);
  89. X  $maillist .= ' ' . $_;
  90. X}
  91. X
  92. X# either write to a temp file (to be possibly mailed) or to STDOUT
  93. X#
  94. Xif ($maillist) {
  95. X  $tmpfile = "/tmp/junkmail.$$";
  96. X  open(TMPFILE, ">$tmpfile") || die "Can't open a temporary file!\n";
  97. X} else {
  98. X  open(TMPFILE, ">-") || die "Couldn't send output to STDOUT!\n";
  99. X  select(TMPFILE); $| = 1; select(STDOUT);
  100. X}
  101. X
  102. X# read in list of patterns for which entire Newsgroups line is trashed
  103. X#
  104. X@trashlist = ();
  105. Xif ( -e "$NEWSCTL/junk-trash-list" ) {
  106. X  if (open(LIST, "$NEWSCTL/junk-trash-list")) {
  107. X    while (<LIST>) {
  108. X      chop;
  109. X      push(trashlist, $_);
  110. X    }
  111. X    close(LIST);
  112. X  } else {
  113. X    print TMPFILE "Couldn't open '$NEWSCTL/junk-trash-list'!\n";
  114. X  }
  115. X} else {
  116. X  print TMPFILE "Couldn't find '$NEWSCTL/junk-trash-list'!\n";
  117. X}
  118. X
  119. X# read in list of patterns to ignore
  120. X#
  121. X@ignorelist = ();
  122. Xif ( -e "$NEWSCTL/junk-ignore-list" ) {
  123. X  if (open(LIST, "$NEWSCTL/junk-ignore-list")) {
  124. X    while (<LIST>) {
  125. X      chop;
  126. X      push(ignorelist, $_);
  127. X    }
  128. X    close(LIST);
  129. X  } else {
  130. X    print TMPFILE "Couldn't open '$NEWSCTL/junk-ignore-list'!\n";
  131. X  }
  132. X} else {
  133. X  print TMPFILE "Couldn't find '$NEWSCTL/junk-ignore-list'!\n";
  134. X}
  135. X
  136. X# read in list of good newsgroups
  137. X#
  138. X%newsgroup = ();
  139. Xif ( -e "$NEWSCTL/active") {
  140. X  if (open(ACTIVE, "$NEWSCTL/active")) {
  141. X    while (<ACTIVE>) {
  142. X      s/ .*\n//;
  143. X      $newsgroup{$_} = 1;
  144. X    }
  145. X    close(ACTIVE);
  146. X  } else {
  147. X    print TMPFILE "Couldn't open '$NEWSCTL/active'!\n";
  148. X  }
  149. X} else {
  150. X  print TMPFILE "Couldn't find '$NEWSCTL/active'!\n";
  151. X}
  152. X
  153. Xopen(JUNKNG, "grep '^Newsgroups:' $NEWSARTS/junk/* |") ||
  154. X    die "Couldn't search for Newsgroups in articles in 'junk'!\n";
  155. Xwhile (<JUNKNG>) {
  156. X  chop;
  157. X
  158. X  # get the list of newsgroups
  159. X  #
  160. X  s/^.*:Newsgroups: //;
  161. X  s/\s*//g;
  162. X  $list = $_;
  163. X
  164. X  # see if we should trash this line
  165. X  #
  166. X  foreach $_ (split(/,/, $list)) {
  167. X    foreach $i (@trashlist) {
  168. X      if (/$i/) {
  169. X    $list = '';
  170. X    last;
  171. X      }
  172. X    }
  173. X  }
  174. X
  175. X  # Check each newsgroup on the line
  176. X  #
  177. X  foreach $_ (split(/,/, $list)) {
  178. X
  179. X    # if it doesn't already exist...
  180. X    #
  181. X    if ($newsgroup{$_}) {
  182. X      $unignored = 0;
  183. X    } else {
  184. X
  185. X      # see if it's one we WANT to junk
  186. X      #
  187. X      $unignored = 1;
  188. X      foreach $i (@ignorelist) {
  189. X    if (/$i/) {
  190. X      $unignored = 0;
  191. X      last;
  192. X    }
  193. X      }
  194. X    }
  195. X
  196. X    # found one we may want to keep
  197. X    #
  198. X    if ($unignored) {
  199. X      $allng{$_}++;
  200. X      if ($list ne $_) {
  201. X    if (defined($thislist{$_})) {
  202. X      $thislist{$_} .= ':' . $list;
  203. X    } else {
  204. X      $thislist{$_} = $list;
  205. X    }
  206. X      }
  207. X    }
  208. X  }
  209. X}
  210. X
  211. X# routine to sort the list of junked newsgroups
  212. X#
  213. Xsub nogood {
  214. X  local($result);
  215. X
  216. X  $result = $allng{$b} - $allng{$a};
  217. X  return $result if ($result);
  218. X
  219. X  if ($a lt $b) {
  220. X    return -1;
  221. X  } elsif ($a gt $b) {
  222. X    return 1;
  223. X  }
  224. X  return 0;
  225. X}
  226. X
  227. X# print the report
  228. X#
  229. Xforeach $i (sort nogood keys(allng)) {
  230. X  $plural = ($allng{$i} == 1 ? " " : "s");
  231. X  print TMPFILE $allng{$i}," article",$plural," for ",$i;
  232. X  print TMPFILE " (",$thislist{$i},")" if (defined($thislist{$i}));
  233. X  print TMPFILE "\n";
  234. X}
  235. Xclose(TMPFILE);
  236. X
  237. X# mail the report (if there's something to mail)
  238. X#
  239. Xif ($maillist) {
  240. X  system "Mail -s 'Junked newsgroups' $maillist < $tmpfile" if ( -s $tmpfile );
  241. X  unlink $tmpfile;
  242. X}
  243. END_OF_FILE
  244. if test 4127 -ne `wc -c <'check-junk'`; then
  245.     echo shar: \"'check-junk'\" unpacked with wrong size!
  246. fi
  247. chmod +x 'check-junk'
  248. # end of 'check-junk'
  249. fi
  250. if test -f 'junk-ignore-list' -a "${1}" != "-c" ; then 
  251.   echo shar: Will not clobber existing file \"'junk-ignore-list'\"
  252. else
  253. echo shar: Extracting \"'junk-ignore-list'\" \(99 characters\)
  254. sed "s/^X//" >'junk-ignore-list' <<'END_OF_FILE'
  255. X^alt\.desert\.storm\.its\.*
  256. X^alt\.drugs
  257. X^alt\.sex.*
  258. X^erg\..*
  259. X^eunet\..*
  260. X^eucon\..*
  261. X^la\..*
  262. X^to\..*
  263. END_OF_FILE
  264. if test 99 -ne `wc -c <'junk-ignore-list'`; then
  265.     echo shar: \"'junk-ignore-list'\" unpacked with wrong size!
  266. fi
  267. # end of 'junk-ignore-list'
  268. fi
  269. if test -f 'junk-trash-list' -a "${1}" != "-c" ; then 
  270.   echo shar: Will not clobber existing file \"'junk-trash-list'\"
  271. else
  272. echo shar: Extracting \"'junk-trash-list'\" \(37 characters\)
  273. sed "s/^X//" >'junk-trash-list' <<'END_OF_FILE'
  274. X^dnet\..*
  275. X^ne\..*
  276. X^sub\..*
  277. X^znet\..*
  278. END_OF_FILE
  279. if test 37 -ne `wc -c <'junk-trash-list'`; then
  280.     echo shar: \"'junk-trash-list'\" unpacked with wrong size!
  281. fi
  282. # end of 'junk-trash-list'
  283. fi
  284. echo shar: End of shell archive.
  285. exit 0
  286. --
  287. Dave Glowacki          dglo@ads.com          Advanced Decision Systems
  288.