🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #380

Re: Cleaning up expired articles after a holiday.

#380
From: Rich Burridge
Date: Wed, 24 Jul 1991 07:56
330 lines
11238 bytes
In article <1991Jul20.211922.16995@ecst.csuchico.edu> warlock@ecst.csuchico.edu (John Kennedy) writes:
>In article <1991Jul20.060254.22139@sunaus.oz> Rich Burridge writes:
>
>-->	If I've been away from news for a while, rn has to "skip articles"
>-->	until it catches up with the news articles that are still available.
>-->	...  This can be very time consuming on high volume newgroups.
>
>  If you're clever, or have clever programming friends, a program could be
>written to ask NNTP to go to each group you subscribe to.

Well, I am a clever chappie, so I've done exactly this. No guarantees; it
seems to work okay for me, but so far it's been given minimal testing. It's
in my cron file, to do my complete ~/.newsrc file tonight, so that will be
a much better test.

You'll need to get nntp-clientlib.c, nntp-clientlib.h, nntp-conf.h and
nntp.h from an NNTP software distribution in order to get this to compile.

This was a one time hack. I don't really want to support it, but if anybody
finds any bugs or puts in any features, I'd like to hear about it.

The performce can certainly be improved. This has been a low priority so
far. Hey, the program will be running at 4:00am from cron.

Thanks to the people who replied to my original question.

------CUT HERE-----tidyrc.c------CUT HERE------

/*  @(#)tidyrc.c 1.2 91/07/24
 *
 *  Connect to a remote news server via NNTP, and tidy up the users ~/.newsrc
 *  based on articles still available for all subscribed groups.
 *
 *  Environment variables used:
 *
 *  NNTPSERVER - name of the remote NNTP server to connect to.
 *  HOME       - used to find the users .newsrc file.
 *
 *  The original .newsrc file is copied to ~/.newsrc.pretidy
 *
 *  Compile with:
 *
 *  cc -o tidyrc -O tidyrc.c nntp-clientlib.c
 *
 *  where nntp-clientlib.c comes from the NNTP software distribution.
 *
 *--------------------------------------------------------------------
 *
 *  Copyright (c) Rich Burridge - Sun Microsystems Australia.
 *                                All rights reserved.
 *
 *  Permission is given to distribute these sources, as long as the
 *  copyright messages are not removed, and no monies are exchanged.
 *
 *  No responsibility is taken for any errors on inaccuracies inherent
 *  either to the comments or the code of this program, but if reported
 *  to me, then an attempt will be made to fix them.
 */

#include <stdio.h>
#include <string.h>

#define  PATCHLEVEL  0

#define  FFLUSH           (void) fflush
#define  FPRINTF          (void) fprintf
#define  FPUTS            (void) fputs
#define  FREE             (void) free
#define  SPRINTF          (void) sprintf
#define  SSCANF           (void) sscanf
#define  STRCAT           (void) strcat
#define  STRCPY           (void) strcpy
#define  STRNCPY          (void) strncpy

#define  BUFSIZE          4096
#define  EQUAL(str, val)  !strncmp(str, val, strlen(val))
#define  MAXLINE          1000

char group[MAXLINE] ;         /* Name of current group. */
char *home ;                  /* Users home directory. */
char *machine ;               /* Name of the machine to connect to. */
char message[MAXLINE] ;       /* For sending messages to the NNTP server. */
char nextline[BUFSIZE] ;      /* Next line from users .newsrc file. */
char output[BUFSIZE] ;        /* For constructiong new .newsrc lines. */
char *progname ;              /* Name of this program. */
char *ptr ;
char reply[MAXLINE] ;         /* For getting replies from the NNTP server. */
char rfilename[MAXLINE] ;     /* Name of users .newsrc file. */
char *valid = NULL ;          /* Set if valid article. */
char wfilename[MAXLINE] ;     /* Name of users .newsrc file. */

int first ;           /* First article number in current group. */
int last ;            /* Last article number in current group. */
int len ;             /* Length of the current group name. */
int maxfound ;        /* Maximum article number per group found in .newsrc. */
int no ;              /* Number of articles in current group. */

FILE *fp ;            /* File descriptor for reading old .newsrc file. */
FILE *nfp ;           /* File descriptor for writing new .newsrc file. */

extern char *fgets(), *getenv(), *index() ;
extern void nntp_close_server(), nntp_put_server() ;


/*ARGSUSED*/
main(argc, argv)
int argc ;
char *argv[] ;
{
  progname = argv[0] ;
  if ((home = getenv("HOME")) == NULL)
    {
      FPRINTF(stderr, "%s: couldn't get HOME environment variable.\n") ;
      exit(1) ;
    }
  if ((machine = getenv("NNTPSERVER")) == NULL)
    {
      FPRINTF(stderr, "%s: couldn't get NNTPSERVER environment variable.\n",
              progname) ;
      exit(1) ;
    }

  if (nntp_server_init(machine) == -1)
    {
      FPRINTF(stderr, "%s: couldn't connect to nntp server for %s\n",
              progname, machine) ;
      exit(1) ;
    }

  SPRINTF(rfilename, "%s/.newsrc", home) ;
  if ((fp = fopen(rfilename, "r")) == NULL)
    {
      FPRINTF(stderr, "%s: couldn't open %s for reading.\n",
                      progname, rfilename) ;
      exit(1) ;
    }

  SPRINTF(wfilename, "%s/.newsrc.NEW", home) ;
  if ((nfp = fopen(wfilename, "w")) == NULL)
    {
      FPRINTF(stderr, "%s: couldn't open %d for writing.\n",
                       progname, wfilename) ;
      exit(1) ;
    }

  while (fgets(nextline, BUFSIZE, fp) != NULL)    /* Read .newsrc until EOF. */
    {
      if ((ptr = index(nextline, ':')) != NULL)   /* Group subscribed to? */
        {
          len = ptr - nextline ;
          STRNCPY(group, nextline, ptr - nextline) ;
          group[len] = '\0' ;
          maxfound = 0 ;
          SPRINTF(message, "GROUP %s", group) ;
          nntp_put_server(message) ;
          read_reply() ;
          if (EQUAL(reply, "211"))                    /* Valid newsgroup? */
            {
              SSCANF(reply, "211 %d %d %d", &no, &first, &last) ;
              if (no == 0 && first == 0 && last == 0)
                FPUTS(nextline, nfp) ;
              else
                {
                  set_valid(ptr+1) ;  /* Set valid array from .newsrc. */
                  if (maxfound > last) FPUTS(nextline, nfp) ;
                  else
                    {
                      find_valid() ;  /* Find valid article ranges via NNTP. */
                      write_valid() ; /* Write out valid article ranges. */
                    }
                }
            }
        }
      else FPUTS(nextline, nfp) ;     /* Unsubscribed; just write it out. */
      FFLUSH(nfp) ;
    }
  nntp_close_server() ;

/* Move .newsrc ==> .newsrc.pretidy. */

  SPRINTF(wfilename, "%s/.newsrc.pretidy", home) ;
  if (rename(rfilename, wfilename) == 0)
    {

/* Move .newsrc.NEW ==> .newsrc if first rename successful. */

      SPRINTF(wfilename, "%s/.newsrc.NEW", home) ;
      rename(wfilename, rfilename) ;
    }
  exit(0) ;
/*NOTREACHED*/
}


find_valid()              /* Find valid article ranges. */
{
  int more = 1 ;          /* Cleared when no more articles in this group. */
  int article ;           /* Current article number found. */

  valid[first] += 2 ;     /* Set the first article. */
  while (more)
    {
      nntp_put_server("NEXT") ;
      read_reply() ;
           if (EQUAL(reply, "421")) more = 0 ;    /* No more articles. */
      else if (EQUAL(reply, "223"))
        {
          article = make_num(reply+3) ;
          valid[article] += 2 ;
        }
    }
}


make_num(str)     /* Extract next numeric field. XXX: can be improved. */
char *str ;
{
  int val ;

  SSCANF(str, "%d", &val) ;
  return(val) ;
}


read_reply()              /* Read a reply from the NNTP server. */
{
  if (nntp_get_server(reply, MAXLINE) == -1)
    {
      FPRINTF(stderr, "%s: error with reply from NNTP server\n",
                       progname) ;
      exit(1) ;
    }
}


set_range(str)      /* Set a range (or a single item) in the valid array. */
char *str ;
{
  char *ptr, temp[25] ;
  int i, len, start, end ;

  if ((ptr = index(str, '-')) != NULL)      /* Is it a range of articles? */
    {
      len = ptr - str ;
      STRNCPY(temp, str, len) ;
      temp[len] = '\0' ;
      start = make_num(temp) ;              /* Get start article number. */
      end   = make_num(ptr+1) ;             /* Get end article number. */
      for (i = start; i <= end; i++)
        if (i <= last) valid[i] = 1 ;
      if (end > maxfound) maxfound = end ;
    }   
  else
    {
      start = make_num(str) ;
      if (start <= last) valid[start] = 1 ;
      if (start > maxfound) maxfound = start ;
    }
}


set_valid(line)      /* Set indices for all the valid articles. */
char *line ;
{
  char *nptr, *ptr, temp[25] ;
  int len ;

  if (valid != NULL) FREE(valid) ;
  valid = (char *) calloc(1, last+1) ;
  ptr = line ;
  while (ptr)
    {
      if ((nptr = index(ptr, ',')) != NULL)
        {
          len = nptr - ptr ;
          STRNCPY(temp, ptr, len) ;
          temp[len] = '\0' ;
          set_range(temp) ;
          ptr = nptr + 1 ;
        }
      else
        {
          STRCPY(temp, ptr) ;                /* Convert last one. */
          set_range(temp) ;
          return ;
        }
    }
}


write_valid()           /* Write out valid article ranges. */
{
  char str[MAXLINE] ;   /* For creating current range of articles. */
  int entry ;           /* Number of "entries" written for this group. */
  int n ;
  int start ;           /* Start article number of possible range. */

  SPRINTF(output, "%s: ", group) ;
  entry = 0 ;
  n = 1 ;
  start = n ;
  while (n <= last)
    {
      if (valid[n] == 2)
        {
          if (entry) STRCAT(output, ",") ;       /* Put out separator. */
          if ((n - start) > 1)                    /* Range of articles? */
            {
              SPRINTF(str, "%d-%d", start, n-1) ;
              STRCAT(output, str) ;
            }
          else
            {
              SPRINTF(str, "%d", start) ;
              STRCAT(output, str) ;
            }
          entry++ ;
          while (n <= last && valid[n] == 2) n++ ;
          if (n > last) break ;
          start = n ;
        }
      n++ ;
    }
  if (!entry)
    {
      SPRINTF(str, "%d-%d", start, last) ;
      STRCAT(output, str) ;
    }
  FPRINTF(nfp, "%s\n", output) ;
}
Technical Details
Message-ID: <1991Jul24.075656.21217@sunaus.oz>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!gmdzi!unido!mcsun!uunet!olivea!protoa!sun-barr!newstop!sunaus.oz!richb
References: <1991Jul20.060254.22139@sunaus.oz> <1991Jul20.211922.16995@ecst.csuchico.edu>

Show Full Headers
Complete Article Headers
Message-ID: <1991Jul24.075656.21217@sunaus.oz>
Subject: Re: Cleaning up expired articles after a holiday.
Date: Wed, 24 Jul 1991 07:56:56 +0000
From: richb@Aus.Sun.COM (Rich Burridge)
References: <1991Jul20.060254.22139@sunaus.oz> <1991Jul20.211922.16995@ecst.csuchico.edu>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!gmdzi!unido!mcsun!uunet!olivea!protoa!sun-barr!newstop!sunaus.oz!richb
Newsgroups: news.software.readers
Sender: news@sunaus.oz
Organization: Sun Microsystems Australia
Lines: 330