* This software is Copyright 1987 by Stan Barber.
* Permission is hereby granted to copy, reproduce, redistribute or otherwise
* use this software as long as: there is no monetary profit gained
* specifically from the use or reproduction or this software, it is not
* sold, rented, traded or otherwise marketed, and this copyright notice is
* included prominently in any copy made.
* The author make no claims as to the fitness or correctness of this software
* for any use whatsoever, and it is provided as is. Any use of this software
* is at the user's own risk.
* this is a file full of functions used by the various news client programs
static char *SccsId
= "@(#)nntp.c 2.2 10/15/87";
static char ser_line
[256];
static char active_file_name
[512];
static char article_file_name
[512];
static char last_group
[256];
* open_server open a connection to the server
/* open connection to nntpserver if appropriate */
nntpserver
= getserverbyfile(SERVER_FILE
);
if (nntpserver
== NULL
) {
fprintf(stderr
, "Can't get the name of the news server from %s\n",
fprintf(stderr
, "Either fix this file, or put NNTPSERVER in your environment.");
response
= server_init(nntpserver
);
fprintf(stderr
, "Couldn't connect to %s news server, try again later.\n",
if (handle_server_response(response
, nntpserver
) < 0) {
fprintf(stderr
, "Cannot handle response from nntpserver.\n");
* open_active gets the active file and returns an open file descriptor to
bzero(active_file_name
, sizeof(active_file_name
));
put_server("LIST"); /* tell server we want the active file */
(void) get_server(ser_line
, sizeof(ser_line
));
if (*ser_line
!= CHAR_OK
) { /* and then see if that's ok */
xerror("Can't get active file from server: \n%s\n", ser_line
);
strcpy(active_file_name
, "/tmp/nsact.XXXXXX");
(void) mktemp(active_file_name
); /* make a temporary name */
openfp
= fopen(active_file_name
, "w+"); /* and get ready */
while (get_server(ser_line
, sizeof(ser_line
)) >= 0) { /* while */
if (ser_line
[0] == '.' && strlen(ser_line
) == 1)
/* there's another line */
break; /* get it and write it to */
fseek(openfp
, 0L, 0); /* just get to the beginning */
* active_name() returns the name of the temporary file that contains the
* name of the current active file.
if (active_file_name
[0] == '\0')
return &active_file_name
[0];
* set_group() set the current group returns NULL if failure "string" if
if (newsgroup
== NULL
|| *newsgroup
== '\0')
if (strcmp(newsgroup
, last_group
)) {
(void) sprintf(nntpbfr
, "GROUP %s", newsgroup
);
(void) get_server(ser_line
, sizeof(ser_line
));
if (*ser_line
!= CHAR_OK
)
strcpy(last_group
, newsgroup
);
* getarticle() returns an open file descriptor to the requested article.
getarticle(newsgroup
, number
, command
)
char *newsgroup
, *command
;
bzero(article_file_name
, sizeof(article_file_name
));
if (set_group(newsgroup
) == NULL
)
strcpy(article_file_name
, "/tmp/nsart.XXXXXX");
if (mktemp(article_file_name
) == NULL
)
(void) sprintf(nntpbfr
, "%s %ld", command
, number
);
(void) get_server(ser_line
, sizeof(ser_line
));
if (*ser_line
!= CHAR_OK
)
if ((fp
= fopen(article_file_name
, "w+")) == NULL
) {
while (get_server(ser_line
, sizeof(ser_line
)) >= 0) { /* while */
if (ser_line
[0] == '.' && strlen(ser_line
) == 1)
/* there's another line */
break; /* get it and write it to */
fputs(ser_line
, fp
); /* the temp file */
fseek(fp
, 0L, 0); /* just get to the beginning */
* article_name() returns the name of the temporary file that contains the
* name of the current article file.
if (article_file_name
[0] == '\0')
return &article_file_name
[0];
* group_name() returns the name of the last group accessed from nntp
if (last_group
[0] == '\0')
* getartbyid retrieves an article by id number and returns an open file
* descriptor for that article
(void) sprintf(nntpbfr
, "ARTICLE %s", id
);
(void) get_server(ser_line
, sizeof(ser_line
));
if (*ser_line
!= CHAR_OK
) {
fprintf(stderr
, "Cannot fetch article %s\n", id
);
strcpy(article_file_name
, "/tmp/nsart.XXXXXX");
if (mktemp(article_file_name
) == NULL
)
if ((fp
= fopen(article_file_name
, "w+")) == NULL
) {
while (get_server(ser_line
, sizeof(ser_line
)) >= 0) { /* while */
if (ser_line
[0] == '.' && strlen(ser_line
) == 1)
/* there's another line */
break; /* get it and write it to */
fputs(ser_line
, fp
); /* the temp file */
fseek(fp
, 0L, 0); /* just get to the beginning */
* sync_server gobbles up the rest of the server output until it sees the .
* on the beginning of a line by itself
while (get_server(ser_line
, sizeof(ser_line
)) >= 0) { /* while */
if (ser_line
[0] == '.' && strlen(ser_line
) == 1)
/* there's another line */
break; /* get it and throw it away */
* strindex returns location of tx in sx
for (i
= 0; sx
[i
] != '\0'; i
++)
if (strncmp(sx
+ i
, tx
, n
) == 0)