home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume31 / pgnews / part01 < prev    next >
Encoding:
Text File  |  1992-07-15  |  6.8 KB  |  224 lines

  1. Newsgroups: comp.sources.misc
  2. From: mcgough@wrdis01.af.mil (Jeffrey B. McGough)
  3. Subject:  v31i039:  pgnews - Perl NNTP client to save articles into mailbox, Part01/01
  4. Message-ID: <1992Jul14.214733.1957@sparky.imd.sterling.com>
  5. X-Md4-Signature: c6345e9e9b47ead8f1fa0da4885f3a92
  6. Date: Tue, 14 Jul 1992 21:47:33 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: mcgough@wrdis01.af.mil (Jeffrey B. McGough)
  10. Posting-number: Volume 31, Issue 39
  11. Archive-name: pgnews/part01
  12. Environment: Perl, NNTP
  13.  
  14. This is pgnews, a PERL NNTP client that grabs news articles by newsgroup 
  15. from a specified NNTP server and and saves them to a specified file in 
  16. mailbox format.
  17.  
  18. Pgnews needs a file named .pgnews to read its newsgroup, last message, and 
  19. savefile from.  .pgnews format is:
  20.  
  21.     newsgroup number savefile
  22.  
  23. Example:
  24. comp.unix.wizards 7800 cuw
  25. comp.unix.shell 3203 cus
  26. comp.unix.questions 546 cuq
  27.  
  28. comp.unix.wizards will be saved to file cuw in mailbox format starting 
  29. at article 7800 etc.
  30.  
  31. My thanks to kenr@bridge.cc.rochester.edu and gort@bridge.cc.rochester.edu
  32. for help with the bug fixes...
  33.  
  34. ------ cut ----
  35. #! /bin/sh
  36. # This is a shell archive.  Remove anything before this line, then unpack
  37. # it by saving it into a file and typing "sh file".  To overwrite existing
  38. # files, type "sh file -c".  You can also feed this as standard input via
  39. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  40. # will see the following message at the end:
  41. #        "End of shell archive."
  42. # Contents:  pgnews
  43. # Wrapped by mcgough@wrdis01 on Fri Jul 10 13:33:52 1992
  44. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  45. if test -f 'pgnews' -a "${1}" != "-c" ; then 
  46.   echo shar: Will not clobber existing file \"'pgnews'\"
  47. else
  48. echo shar: Extracting \"'pgnews'\" \(4527 characters\)
  49. sed "s/^X//" >'pgnews' <<'END_OF_FILE'
  50. X#! /usr/bin/perl
  51. X#/****************************************************
  52. X#*****************************************************
  53. X#**
  54. X#** SOURCE NAME | pgnews, (Perl Get News)
  55. X#**             | 
  56. X#**    SYNOPSIS | pgnews
  57. X#**             | 
  58. X#** DESCRIPTION | pgnews goes to a specified NNTP server
  59. X#**             | and retrieves news articles by newsgroup
  60. X#**             | and saves them to a specified file in
  61. X#**             | mailbox format.
  62. X#**             | Please see the NOTES section.
  63. X#**             | 
  64. X#**     CHANGES | Programmer:         Date:     Reason/Comments
  65. X#**             | Jeffrey B. McGough  09-05-91  initial
  66. X#**             | Jeffrey B. McGough  09-06-91  Added select (see FIXES)
  67. X#**             | Jeffrey B. McGough  10-06-91  Fixed erronious end of article
  68. X#**             | Jeffrey B. McGough  07-09-92  Fixed dup article bug
  69. X#**             | 
  70. X#**       NOTES | Pgnews needs a file named .pgnews to read
  71. X#**             | its newsgroup, last message, and savefile from.
  72. X#**             | .pgnews format is:
  73. X#**             | newsgroup number savefile
  74. X#**             | Example:
  75. X#**             | comp.unix.wizards 7800 cuw
  76. X#**             | comp.unix.shell 3203 cus
  77. X#**             | comp.unix.questions 546 cuq
  78. X#**             | 
  79. X#**             | comp.unix.wizards will be saved to file cuw in
  80. X#**             | mailbox format starting at article 7800 etc.
  81. X#**             | 
  82. X#**       FIXES | 09-06-91: added select on S to keep the client
  83. X#**             | from getting out of sync.
  84. X#**             | Jeffrey B. McGough  mcgough@wrdis01.af.mil
  85. X#**             | 
  86. X#**             | 10-06-91: Fixed an overlooked END of ARTICLE
  87. X#**             | bug... Thanks to a member(s) of the issos
  88. X#**             | group at Ohio State.
  89. X#**             | Jeffrey B. McGough  mcgough@wrdis01.af.mil
  90. X#**             | 
  91. X#**             | 07-09-92: Fixed a duplicate article bug
  92. X#**             | pointed out to me by kenr@bridge.cc.rochester.edu
  93. X#**             | and gort@bridge.cc.rochester.edu. Thanks
  94. X#**             | for the help with the fix.
  95. X#**             | Jeffrey B. McGough  mcgough@wrdis01.af.mil
  96. X#**             | 
  97. X#****************************************************/
  98. X
  99. Xrequire 'sys/socket.ph'; # The way I coded the sockets is this necessary?
  100. X
  101. X$port = 119; # For NNTP
  102. X# HOSTNAME for the server...
  103. X#$host = 'localhost';
  104. X$host = 'emory.mathcs.emory.edu';
  105. X# Pack format...
  106. X$sockaddr = 'S n a4 x8';
  107. X
  108. X$DOMAIN = 2;
  109. X$STYLE = 1;
  110. X
  111. X$newsfile = '.pgnews';
  112. X$nnewsfile = '.pgnews.new';
  113. X
  114. X$rin = $rout = '';
  115. X
  116. X($name, $aliases, $proto) = getprotobyname('tcp');
  117. X($name, $aliases, $type, $len, $hostaddr) = gethostbyname($host);
  118. X
  119. X$sock = pack($sockaddr, $DOMAIN, $port, $hostaddr);
  120. X
  121. Xsocket(S, $DOMAIN, $STYLE, $proto) || die $!;
  122. Xconnect(S, $sock) || die $!;
  123. Xselect(S); $| = 1; select(STDOUT);
  124. X#set up for select
  125. Xvec($rin, fileno(S), 1) = 1;
  126. X#this select will block until the server gives us something.
  127. Xselect($rout=$rin, undef, undef, undef);
  128. X$_ = <S>; #Read one line to see if we got a good connection.
  129. Xif ($_ !~ /^2../)
  130. X{
  131. X    print;
  132. X    die "Service unavailable";
  133. X}
  134. Xopen(GRP, "< $newsfile") || die "Could not open $newsfile: $!";
  135. Xopen(NGRP, "> $nnewsfile") || die "Could not open $nnewsfile: $!";
  136. Xselect(NGRP); $| = 1; select(STDOUT);
  137. Xwhile(<GRP>)
  138. X{
  139. X    chop;
  140. X    ($grp, $lgot, $file) = split;
  141. X    print(S "group $grp\n");
  142. X    #this select will block until the server gives us something.
  143. X    select($rout=$rin, undef, undef, undef);
  144. X    $_ = <S>; #Make sure the group change worked...
  145. X    ($stat, $num, $first, $last) = split;
  146. X    if( $stat !~ /^2../ )
  147. X    {
  148. X        print;
  149. X        warn "Bad group";
  150. X        print(NGRP "$grp $lgot $file\n");
  151. X        next;
  152. X    }
  153. X    # good group open output file...
  154. X    open(OUTFILE, ">>$file") || die "Could not open $file";
  155. X
  156. X    if ( $first > $lgot )
  157. X    {
  158. X        $lgot = $first;
  159. X    }
  160. X    if ( $lgot <= $last )
  161. X    {
  162. X        foreach $art ($lgot..$last)
  163. X        {
  164. X            print(S "article $art\n");
  165. X            #this select will block until the server gives us something.
  166. X            select($rout=$rin, undef, undef, undef);
  167. X            $_ = <S>; #get error if one exists
  168. X            if($_ !~ /^2../)
  169. X            {
  170. X                print;
  171. X                warn "No article by that number";
  172. X            }
  173. X            else
  174. X            {
  175. X                do
  176. X                {
  177. X                    $lgot = $art;
  178. X                    $_ = <S>;
  179. X                    s/^Path:/From/;
  180. X                    s/\r//;
  181. X                    if( $_ ne ".\n")
  182. X                    {
  183. X                        s/^\.//;
  184. X                        print OUTFILE;
  185. X                        s/^\./../;
  186. X                    }    
  187. X                    else
  188. X                    {
  189. X                        print OUTFILE "\n";
  190. X                    }
  191. X                } until $_ eq ".\n";
  192. X            }
  193. X        }    
  194. X    }
  195. X    else
  196. X    {
  197. X        $lgot -= 1;
  198. X    }
  199. X    close(OUTFILE);
  200. X    $lgot += 1;
  201. X    print(NGRP "$grp $lgot $file\n");
  202. X}
  203. Xclose(NGRP);
  204. Xclose(GRP);
  205. Xsystem("mv $nnewsfile $newsfile");
  206. Xprint( S "quit\n");
  207. Xclose(S);
  208. END_OF_FILE
  209. if test 4527 -ne `wc -c <'pgnews'`; then
  210.     echo shar: \"'pgnews'\" unpacked with wrong size!
  211. fi
  212. chmod +x 'pgnews'
  213. # end of 'pgnews'
  214. fi
  215. echo shar: End of shell archive.
  216. exit 0
  217. -- 
  218. Lator,                                       We cheat the other guy,
  219.                                              and pass the savings on to you.
  220. Jeffrey B. McGough
  221. WR-ALC UNIX Systems Administrator                    (mcgough@wrdis01.af.mil)
  222.  
  223. exit 0 # Just in case...
  224.