home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3028 / fixnews.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-09  |  5.4 KB  |  234 lines

  1. /*
  2.  *  Fixnews
  3.  *  Mon Feb 26 12:25:27 1990 -- Scott "TCB" Turner
  4.  *
  5.  *  This is a simple little hack that updates your .newsrc so that you
  6.  *  have no more than <arg> articles to read in every newsgroup.  If
  7.  *  <arg> is omitted, then 50 articles are left.  If you have less
  8.  *  than <arg> articles to read in a newsgroup, the .newsrc line
  9.  *  passes through unchanged.  If you are unsubscribed to a newsgroup
  10.  *  it is removed from your .newsrc.
  11.  *
  12.  *  Note that the latest version of rn expects you to have every
  13.  *  available newsgroup listed in your .newsrc.  (Which I find silly.)
  14.  *  If you delete lines from your .newsrc, the next time a checkgroups
  15.  *  message rolls around, you'll get asked about every deleted newsgroup.
  16.  *  You can avoid this by using the -q option to rn, but then you
  17.  *  won't be informed about legitimate new newsgroups.
  18.  *
  19.  *  Alternately, you can call fixnews with the -d option to turn off
  20.  *  deletion of unsubscribed newsgroups.
  21.  *
  22.  */
  23. #include <sys/types.h>
  24. #include <stdio.h>
  25. #include <sys/file.h>
  26. #include "server.h"
  27. #include "config.h"
  28.  
  29. /*
  30.  *  Globals
  31.  *
  32.  *    save -- number of articles to save.
  33.  *    delete -- whether to delete unsubscribed newsgroups.
  34.  */
  35. int save = SAVE;
  36. int delete = 1;
  37.  
  38. void filternewsrc()
  39. {
  40.   FILE *in, *out;
  41.   char *home;
  42.   char newsrc[256],newsrcold[256],newsrc_line[256],
  43.        name[256],command[256],ser_line[256];
  44.   int i,j;
  45.   long lastread,num,first,last;
  46.   char *getenv();
  47.  
  48.   /*
  49.    *  Determine user's home directory and location of ~/.newsrc
  50.    *
  51.    */
  52.   home = getenv("HOME");
  53.   if (home == NULL) {
  54.     fprintf(stderr,"Unable to determine home directory.\n");
  55.     exit(1);
  56.   };
  57.   (void) strcpy(newsrc,home);
  58.   (void) strcat(newsrc,NEWSRC);
  59.   in = fopen(newsrc, "r");
  60.  
  61.   if ( in == NULL ) {
  62.     fprintf(stderr,"Unable to open %s.\n",newsrc);
  63.     exit(1);
  64.   };
  65.  
  66.   /*
  67.    *   Rename .newsrc to .newsrc.old
  68.    *
  69.    */
  70.   (void) strcpy(newsrcold,newsrc);
  71.   (void) strcat(newsrcold,".old");
  72.   (void) rename(newsrc,newsrcold);
  73.   
  74.   /*
  75.    *  Open the new version.
  76.    *
  77.    */
  78.   out = fopen(newsrc, "w");
  79.  
  80.   if ( out == NULL ) {
  81.     fprintf(stderr,"Unable to open %s for output.\n",newsrc);
  82.     exit(1);
  83.   };
  84.  
  85.   /*
  86.    *  Read and process.
  87.    *
  88.    */
  89.   while(fgets(newsrc_line,80,in) != NULL) {
  90.  
  91.     /*
  92.      *  This might possibly be an "options" line, in which case it
  93.      *  should be written out unchanged.
  94.      *
  95.      */
  96.     if (strncmp("option",newsrc_line,6) == 0) {
  97.       fprintf(out,"%s",newsrc_line);
  98.       continue;
  99.     };
  100.     
  101.     /*
  102.      *  Copy the name from the beginning of the .newsrc line to a
  103.      *  colon or a bang.  If a bang, drop it from the .newsrc.
  104.      *
  105.      */
  106.     for(i = 0;newsrc_line[i] != ':' && newsrc_line[i] != '!';i++)
  107.       name[i] = newsrc_line[i];
  108.     if (newsrc_line[i] == '!') {
  109.       if (!delete) fprintf(out,"%s",newsrc_line);
  110.       continue;
  111.     };
  112.     name[i] = '\0';
  113.     
  114.     /*
  115.      *  Get the last read article.  Search backwards for the start
  116.      *  of the number.  Skip over the individually marked articles.
  117.      *  If you think about it, this isn't quite correct (well, it
  118.      *  it isn't quite correct even if you don't think about it) but
  119.      *  it is easy and behaves reasonably.
  120.      *
  121.      */
  122.     for(i=strlen(newsrc_line);
  123.     newsrc_line[i] != '-' && newsrc_line[i] != ':';
  124.     i--);
  125.     i++;
  126.     lastread = atol(&(newsrc_line[i]));
  127.     
  128.     /*
  129.      *   Get the last article from the NNTP server.  The command for
  130.      *   this is "GROUP <groupname>" and the response is either:
  131.      *
  132.      *     211 <number> <first> <last> <name>
  133.      *
  134.      *                   or
  135.      *
  136.      *     411 No such newsgroup
  137.      *
  138.      */
  139.     sprintf(command,"GROUP %s",name);
  140.     put_server(command);
  141.     (void) get_server(ser_line, sizeof(ser_line));
  142.     
  143.     /*
  144.      *  Were we successful?
  145.      *
  146.      */
  147.     i = atoi(ser_line);
  148.     if (i != GROUP_SUCCESS) {
  149.       fprintf(stderr,"No response from NNTP for newsgroup: %s.\n",name);
  150.       continue;
  151.     };
  152.  
  153.     /*
  154.      *  Else get the last article number available.
  155.      *
  156.      */
  157.     sscanf(ser_line,"%ld %ld %ld %ld",&i, &num,&first,&last);
  158.     if (lastread < (last - save)) {
  159.       fprintf(out,"%s: 1-%d\n",name,(last-save));
  160.     } else {
  161.       fprintf(out,"%s",newsrc_line);
  162.     };
  163.   };
  164. };
  165.  
  166. main(argc,argv)
  167. register int argc; char **argv;
  168. {
  169.   int        response;
  170.   register char    *server;
  171.  
  172.   /*
  173.    *  If there's a single argument, it's the number of articles to
  174.    *  save in each newsgroup.
  175.    *
  176.    */
  177.   while (argc > 1) {
  178.     /*
  179.      *  -d turns off line deletion.
  180.      *
  181.      */
  182.     if (!strcmp(argv[argc],"-d")) {
  183.       delete = 0;
  184.     } else {
  185.     /*
  186.      *  Otherwise, a number that indicates how many articles to save.
  187.      *
  188.      */
  189.       sscanf(argv[argc],"%d",&save);
  190.       if (save == 0) {
  191.     fprintf(stderr,"Unexpected argument to fixnews: %s.\n",argv[argc]);
  192.     exit(1);
  193.       };
  194.     };
  195.     argc--;
  196.   };
  197.  
  198.   /*
  199.    *  Find the NNTP server.
  200.    *
  201.    */
  202.   server = getserverbyfile(SERVER_FILE);
  203.   if (server == NULL) {
  204.     fprintf(stderr, "Couldn't get name of news server from %s\n",
  205.         SERVER_FILE);
  206.     fprintf(stderr,
  207.         "Either fix this file, or put NNTPSERVER in your environment.\n");
  208.     exit(1);
  209.   }
  210.  
  211.   /*
  212.    *  Initialize the server.
  213.    *
  214.    */
  215.   response = server_init(server);
  216.   if (response < 0) {
  217.     fprintf(stderr, "getactive: Can't get active file from server %s.\n",
  218.         server);
  219.     exit(1);
  220.   }
  221.  
  222.   /*
  223.    *  Set up the server handler.
  224.    *
  225.    */
  226.   if (handle_server_response(response, server) < 0)
  227.     exit(1);
  228.   
  229.   filternewsrc();
  230.   close_server();
  231.   exit(0);
  232. };
  233.  
  234.