home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / refetch.shar < prev    next >
Encoding:
Internet Message Format  |  1990-02-28  |  7.7 KB

  1. Path: tut.cis.ohio-state.edu!snorkelwacker!usc!elroy.jpl.nasa.gov!jato!lwall
  2. From: lwall@jato.Jpl.Nasa.Gov (Larry Wall)
  3. Newsgroups: news.admin,comp.lang.perl,alt.sources
  4. Subject: Re: Question on non-dbm history files
  5. Message-ID: <2943@jato.Jpl.Nasa.Gov>
  6. Date: 28 Feb 90 19:18:36 GMT
  7. References: <253@uucs1.UUCP> <1990Feb28.172640.25896@utzoo.uucp> <EMV.90Feb28134417@duby.math.lsa.umich.edu>
  8. Reply-To: lwall@jato.Jpl.Nasa.Gov (Larry Wall)
  9. Followup-To: comp.lang.perl
  10. Organization: Jet Propulsion Laboratory, Pasadena, CA
  11. Lines: 281
  12. Xref: tut.cis.ohio-state.edu news.admin:8075 comp.lang.perl:480 alt.sources:1494
  13.  
  14. In article <EMV.90Feb28134417@duby.math.lsa.umich.edu> emv@math.lsa.umich.edu (Edward Vielmetti) writes:
  15. :    The one other purpose that dbm (and the assorted substitutes for it)
  16. :    is intended to fulfill is random article lookup by message-ID.  Most of
  17. :    the news readers will try to do this in some circumstances, but the
  18. :    circumstances in question are usually use of some obscure command that
  19. :    nobody ever invokes in practice.
  20. : Here's an extremely rough cut at "article", a program to fetch
  21. : usenet articles by Message-ID over NNTP, intended to be somewhat
  22. : less obscure than most news readers. Invoke it like so:
  23. :     article "<253@uucs1.UUCP>"
  24. : once you've configured it appropriately.
  25. : I would like to teach it to cope with history file formats & be
  26. : generally more nice, but for me it works just dandy for now.
  27.  
  28. Here's a vaguely related script that does dbm history file lookups and nntp
  29. to refetch articles from an nntp server that were dropped in the bitbucket
  30. for some reason (usually by running out of disk space, or some such). 
  31. It should probably extract the default list of newsgroups from the sys file,
  32. but I was lazy.
  33.  
  34. Larry Wall
  35. lwall@jpl-devvax.jpl.nasa.gov
  36.  
  37. #!/bin/sh
  38. : make a subdirectory, cd to it, and run this through sh.
  39. echo 'If this kit is complete, "End of kit" will echo at the end'
  40. echo Extracting refetch
  41. sed >refetch <<'!STUFFY!FUNK!' -e 's/X//'
  42. X#!/usr/bin/perl
  43. X
  44. X$restart = shift(@ARGV);
  45. X
  46. X$server = 'jato';
  47. X$newsgroups =
  48. X'ca.*,comp.*,gnu.*,jpl.*,la.*,misc.*,nasa.*,news.*,rec.*,sci.*,soc.*,talk.*';
  49. X
  50. Xprint "Server? [$server] ";
  51. Xchop($ans = <STDIN>);
  52. X$server = $ans if $ans;
  53. X
  54. X$pat = 'S n C4 x8';
  55. X
  56. X$af_unix = 1;
  57. X$af_inet = 2;
  58. X
  59. X$stream = 1;
  60. X$datagram = 2;
  61. X
  62. X($name,$aliases,$proto) = getprotobyname('tcp');
  63. X$tcp = $proto;
  64. X
  65. X($name,$aliase,$port,$proto) = getservbyname('nntp','tcp');
  66. X$nntp = $port;
  67. X
  68. Xif ($server =~ /^\d+\./) {
  69. X    @bytes = split(/\./,$server);
  70. X}
  71. Xelse {
  72. X    ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($server);
  73. X    die "Can't lookup $server\n" unless $name;
  74. X    @bytes = unpack("C4",$addrs[0]);
  75. X}
  76. X
  77. X$this = pack($pat,$af_inet,1492,      0,0,0,0);
  78. X$that = pack($pat,$af_inet,$nntp,@bytes);
  79. X
  80. Xsocket(NNTP,$af_inet,$stream,$tcp) || die "socket: $!\n";
  81. Xbind(NNTP,$this) || die "bind: $!\n";
  82. Xconnect(NNTP,$that) || die "connect: $!\n";
  83. X
  84. Xselect(NNTP); $| = 1; select(STDOUT); $| = 1;
  85. X
  86. Xprint "\nConnected to NNTP server at $server (",join('.',@bytes),").\n\n";
  87. X
  88. Xif (!$restart) {
  89. X    print "Newsgroups? [$newsgroups] ";
  90. X    chop($ans = <STDIN>);
  91. X    $newsgroups = $ans if $ans;
  92. X
  93. X    $oneday = 60 * 60 * 24;
  94. X    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  95. X    localtime(time-$oneday);
  96. X    $yesterday = sprintf("%02d%02d%02d",$year,$mon+1,$mday);
  97. X
  98. X    while (length($date) != 6) {
  99. X    print "\nSince date? [$yesterday] ";
  100. X    chop($date = <STDIN>);
  101. X    if ($date < 0) {
  102. X        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  103. X           localtime(time - $oneday * $date);
  104. X        $date = sprintf("%02d%02d%02d",$year,$mon+1,$mday);
  105. X    }
  106. X    else {
  107. X        $date = $yesterday unless $date;
  108. X    }
  109. X    }
  110. X
  111. X    $now = sprintf("%02d%02d%02d",$hour,$min,$sec);
  112. X
  113. X    while (length($time) != 6) {
  114. X    print "\nSince time? [$now] ";
  115. X    chop($time = <STDIN>);
  116. X    $time = $now unless $time;
  117. X    }
  118. X}
  119. X
  120. Xfork && exit;
  121. X
  122. Xopen(STDOUT,">refetch.log");
  123. Xopen(STDERR,">&STDOUT");
  124. X
  125. Xselect(STDERR); $| = 1;
  126. Xselect(STDOUT); $| = 1;
  127. X
  128. Xgoto label if $restart;
  129. X
  130. Xdbmopen(dhist,"history",0666) || die "Can't open history dbm file: $!\n";
  131. X
  132. Xprint STDERR "Loading history...";
  133. Xopen(hist,'/usr/lib/news/history') || die "Can't open history file";
  134. X($st_dev,$st_ino,$st_mode,$st_nlink,$st_uid,$st_gid,$st_rdev,$st_size,
  135. X    $st_atime,$st_mtime,$st_ctime,$st_blksize,$st_blocks) = stat(hist);
  136. Xfor ($offset = $st_size - 100000; $offset > 0; $offset -= 100000) {
  137. X    if (seek(hist,$offset,0)) {
  138. X    $_ = <hist>;        # probably starts in middle of a line
  139. X    $_ = <hist>;
  140. X    m|    (\d+)/(\d+)/(\d+)| || next;
  141. X    last if $3 * 10000 + $1 * 100 + $2 < $date;
  142. X    }
  143. X    else {
  144. X    $offset = -1;
  145. X    }
  146. X}
  147. Xseek(hist,0,0) if $offset < 0;
  148. Xwhile (<hist>) {
  149. X    m|    (\d+)/(\d+)/(\d+)| || next;
  150. X    last if $3 * 10000 + $1 * 100 + $2 >= $date;
  151. X}
  152. X$pct = int(tell(hist) * 100 / $st_size);
  153. Xprint STDERR "starting at $pct%...";
  154. X$pos = tell(hist);
  155. Xwhile (<hist>) {
  156. X    /^(<[^>]*>)/ && ($history{$1} = $pos + 0);
  157. X    $pos = tell(hist);
  158. X}
  159. Xprint STDERR "done\n";
  160. X
  161. Xprint NNTP "newnews $newsgroups $date $time\n";
  162. X
  163. Xopen(TMP,">/tmp/refetch$$") || die "Can't open tmp file";
  164. X
  165. Xwhile (<NNTP>) {
  166. X    last if /^230/;
  167. X}
  168. X
  169. Xchdir "/usr/spool/news" || die "Can't cd to /usr/spool/news: $!\n";
  170. X
  171. Xwhile (<NNTP>) {
  172. X    chop;
  173. X    chop;
  174. X    $Messid = $_;
  175. X    y/A-Z/a-z/;
  176. X    last if $_ eq '.';
  177. X    if ($history{$Messid}) {
  178. X    $loc = $dhist{$_ . "\000"};
  179. X    $loc = $dhist{$Messid . "\000"} if $loc eq '';
  180. X    if ($loc eq '') {
  181. X        $loc = $history{$Messid};
  182. X        print STDERR "???d\t",$Messid,"\n";
  183. X    }
  184. X    else {
  185. X        ($loc) = unpack("l",$loc);
  186. X        if ($loc != $history{$Messid}) {
  187. X        print STDERR "???\t$loc != $history{$Messid}\n";
  188. X        $loc = $history{$Messid};
  189. X        }
  190. X    }
  191. X    seek(hist,$loc,0);
  192. X    $histline = <hist>;
  193. X    ($messid,$date,$artlist) = split(/\t/,$histline);
  194. X    if ($messid =~ /^</) {
  195. X        if ($messid ne $Messid) {
  196. X        delete $dhist{$_ . "\000"};
  197. X        print STDERR ">>>m$messid\t",$Messid,"\n";
  198. X        print TMP $Messid,"\n";
  199. X        next;
  200. X        }
  201. X        @artlist = split(' ',$artlist);
  202. X        $exists = 0;
  203. X        for (@artlist) {
  204. X        y|.|/|;
  205. X        if (-e $_) {
  206. X            if (-z _) {
  207. X            --$exists;
  208. X            unlink $_;
  209. X            print STDERR "\t\t$Messid $_ zero size\n";
  210. X            }
  211. X            else {
  212. X            print STDERR "\t\t$Messid $_ exists\n";
  213. X            ++$exists;
  214. X            last;
  215. X            }
  216. X        }
  217. X        else {
  218. X            print STDERR "\t\t$Messid $_ doesn't exist\n";
  219. X        }
  220. X        }
  221. X        if ($exists < 1) {
  222. X        delete $dhist{$_ . "\000"};
  223. X        if ($exists < 0) {
  224. X            print STDERR ">>>z\t",$Messid,"\n";
  225. X        }
  226. X        else {
  227. X            print STDERR ">>>e\t",$Messid,"\n";
  228. X        }
  229. X        print TMP $Messid,"\n";
  230. X        next;
  231. X        }
  232. X    }
  233. X    else {
  234. X        delete $dhist{$_ . "\000"};
  235. X        print STDERR ">>>s\t$Messid\t",$_,"\n";
  236. X        print TMP $Messid,"\n";
  237. X        next;
  238. X    }
  239. X    print STDERR "\t",$Messid,"\n";
  240. X    }
  241. X    else {
  242. X    delete $dhist{$_ . "\000"};
  243. X    print STDERR ">>>h\t",$Messid,"\n";
  244. X    print TMP $Messid,"\n";
  245. X    }
  246. X}
  247. Xclose TMP;
  248. Xdbmclose(dhist);
  249. X
  250. Xlabel:
  251. Xif ($restart) {
  252. X    open(TMP,"/tmp/refetch$restart") || die "Can't reopen /tmp/refetch$restart: $!";
  253. X}
  254. Xelse {
  255. X    open(TMP,"/tmp/refetch$$") || die "Can't reopen /tmp/refetch$$: $!";
  256. X}
  257. X
  258. Xwhile (<TMP>) {
  259. X    chop;
  260. X    $article = $_;
  261. X    print NNTP "article $_\n";
  262. X    ($_ = <NNTP>) =~ /^220/ || (warn("Not 220 on $article: $_"),next);
  263. X    open(RNEWS,"|/usr/local/bin/rnews");
  264. X    while (<NNTP>) {
  265. X    s/\r\n$/\n/;
  266. X    last if $_ eq ".\n";
  267. X    s/^\.\././;
  268. X    print RNEWS;
  269. X    }
  270. X    close RNEWS;
  271. X    if ($?) {
  272. X    printf STDERR "Exit %d sig %d from rnews on %s\n",
  273. X        $? >> 8; $? & 255, $article;
  274. X    }
  275. X    else {
  276. X    print STDERR "OK    $article\n";
  277. X    }
  278. X}
  279. X
  280. Xprint NNTP "quit\n";
  281. Xwhile (<NNTP>) {
  282. X    ;
  283. X}
  284. X
  285. X# unlink "/tmp/refetch$$";
  286. X
  287. Xprint STDERR "done\n";
  288. !STUFFY!FUNK!
  289. echo ""
  290. echo "End of kit"
  291. : I do not append .signature, but someone might mail this.
  292. exit
  293.  
  294.