home *** CD-ROM | disk | FTP | other *** search
- /*
- * Fixnews
- * Mon Feb 26 12:25:27 1990 -- Scott "TCB" Turner
- *
- * This is a simple little hack that updates your .newsrc so that you
- * have no more than <arg> articles to read in every newsgroup. If
- * <arg> is omitted, then 50 articles are left. If you have less
- * than <arg> articles to read in a newsgroup, the .newsrc line
- * passes through unchanged. If you are unsubscribed to a newsgroup
- * it is removed from your .newsrc.
- *
- * Note that the latest version of rn expects you to have every
- * available newsgroup listed in your .newsrc. (Which I find silly.)
- * If you delete lines from your .newsrc, the next time a checkgroups
- * message rolls around, you'll get asked about every deleted newsgroup.
- * You can avoid this by using the -q option to rn, but then you
- * won't be informed about legitimate new newsgroups.
- *
- * Alternately, you can call fixnews with the -d option to turn off
- * deletion of unsubscribed newsgroups.
- *
- */
- #include <sys/types.h>
- #include <stdio.h>
- #include <sys/file.h>
- #include "server.h"
- #include "config.h"
-
- /*
- * Globals
- *
- * save -- number of articles to save.
- * delete -- whether to delete unsubscribed newsgroups.
- */
- int save = SAVE;
- int delete = 1;
-
- void filternewsrc()
- {
- FILE *in, *out;
- char *home;
- char newsrc[256],newsrcold[256],newsrc_line[256],
- name[256],command[256],ser_line[256];
- int i,j;
- long lastread,num,first,last;
- char *getenv();
-
- /*
- * Determine user's home directory and location of ~/.newsrc
- *
- */
- home = getenv("HOME");
- if (home == NULL) {
- fprintf(stderr,"Unable to determine home directory.\n");
- exit(1);
- };
- (void) strcpy(newsrc,home);
- (void) strcat(newsrc,NEWSRC);
- in = fopen(newsrc, "r");
-
- if ( in == NULL ) {
- fprintf(stderr,"Unable to open %s.\n",newsrc);
- exit(1);
- };
-
- /*
- * Rename .newsrc to .newsrc.old
- *
- */
- (void) strcpy(newsrcold,newsrc);
- (void) strcat(newsrcold,".old");
- (void) rename(newsrc,newsrcold);
-
- /*
- * Open the new version.
- *
- */
- out = fopen(newsrc, "w");
-
- if ( out == NULL ) {
- fprintf(stderr,"Unable to open %s for output.\n",newsrc);
- exit(1);
- };
-
- /*
- * Read and process.
- *
- */
- while(fgets(newsrc_line,80,in) != NULL) {
-
- /*
- * This might possibly be an "options" line, in which case it
- * should be written out unchanged.
- *
- */
- if (strncmp("option",newsrc_line,6) == 0) {
- fprintf(out,"%s",newsrc_line);
- continue;
- };
-
- /*
- * Copy the name from the beginning of the .newsrc line to a
- * colon or a bang. If a bang, drop it from the .newsrc.
- *
- */
- for(i = 0;newsrc_line[i] != ':' && newsrc_line[i] != '!';i++)
- name[i] = newsrc_line[i];
- if (newsrc_line[i] == '!') {
- if (!delete) fprintf(out,"%s",newsrc_line);
- continue;
- };
- name[i] = '\0';
-
- /*
- * Get the last read article. Search backwards for the start
- * of the number. Skip over the individually marked articles.
- * If you think about it, this isn't quite correct (well, it
- * it isn't quite correct even if you don't think about it) but
- * it is easy and behaves reasonably.
- *
- */
- for(i=strlen(newsrc_line);
- newsrc_line[i] != '-' && newsrc_line[i] != ':';
- i--);
- i++;
- lastread = atol(&(newsrc_line[i]));
-
- /*
- * Get the last article from the NNTP server. The command for
- * this is "GROUP <groupname>" and the response is either:
- *
- * 211 <number> <first> <last> <name>
- *
- * or
- *
- * 411 No such newsgroup
- *
- */
- sprintf(command,"GROUP %s",name);
- put_server(command);
- (void) get_server(ser_line, sizeof(ser_line));
-
- /*
- * Were we successful?
- *
- */
- i = atoi(ser_line);
- if (i != GROUP_SUCCESS) {
- fprintf(stderr,"No response from NNTP for newsgroup: %s.\n",name);
- continue;
- };
-
- /*
- * Else get the last article number available.
- *
- */
- sscanf(ser_line,"%ld %ld %ld %ld",&i, &num,&first,&last);
- if (lastread < (last - save)) {
- fprintf(out,"%s: 1-%d\n",name,(last-save));
- } else {
- fprintf(out,"%s",newsrc_line);
- };
- };
- };
-
- main(argc,argv)
- register int argc; char **argv;
- {
- int response;
- register char *server;
-
- /*
- * If there's a single argument, it's the number of articles to
- * save in each newsgroup.
- *
- */
- while (argc > 1) {
- /*
- * -d turns off line deletion.
- *
- */
- if (!strcmp(argv[argc],"-d")) {
- delete = 0;
- } else {
- /*
- * Otherwise, a number that indicates how many articles to save.
- *
- */
- sscanf(argv[argc],"%d",&save);
- if (save == 0) {
- fprintf(stderr,"Unexpected argument to fixnews: %s.\n",argv[argc]);
- exit(1);
- };
- };
- argc--;
- };
-
- /*
- * Find the NNTP server.
- *
- */
- server = getserverbyfile(SERVER_FILE);
- if (server == NULL) {
- fprintf(stderr, "Couldn't get name of news server from %s\n",
- SERVER_FILE);
- fprintf(stderr,
- "Either fix this file, or put NNTPSERVER in your environment.\n");
- exit(1);
- }
-
- /*
- * Initialize the server.
- *
- */
- response = server_init(server);
- if (response < 0) {
- fprintf(stderr, "getactive: Can't get active file from server %s.\n",
- server);
- exit(1);
- }
-
- /*
- * Set up the server handler.
- *
- */
- if (handle_server_response(response, server) < 0)
- exit(1);
-
- filternewsrc();
- close_server();
- exit(0);
- };
-
-