	Retrieving a news article

	[[get_article]] sends a series of commands to the NNTP server
	over socket [[f]], in order to get article [[num]] from group
	[[group]]. The first command is [[GROUP]], the second
	[[ARTICLE]]. If either of these returns a status code outside
	the range 200 to 299, then the status code is printed and the
	agent is stopped.

<<*>>=
#include <config.h>
#include <str.h>
#include <mime.h>

EXPORT void get_article(FILE *f, char *group, char *num)
{
    char line[MAXLINELEN];
    int has_mime;

    debug("Sending \"GROUP %s\" command\n", group);
    fflush(f);
    fprintf(f, "GROUP %s\r\n", group);
    fflush(f);

    if (! fgets(line, sizeof(line), f))
	errexit("500 No response from server\r\n");

    if (line[0] != '2')
	errexit(line);

    debug("Sending \"ARTICLE %s\" command\n", num);
    fflush(f);
    fprintf(f, "ARTICLE %s\r\n", num);
    fflush(f);

    if (! fgets(line, sizeof(line), f))
	errexit("500 No response from server\r\n");

    if (line[0] != '2')
	errexit(line);

    /* Read header lines */

    debug("Receiving article\n");
    fputs("200 OK\r\n", stdout);
    has_mime = 0;
    while (fgets(line, sizeof(line), f) && line[0] != '\r') {
	if (strncmp(line, "Content-Type", 12) == 0)
	    has_mime = 1;
	fputs(line, stdout);
    }
    if (! has_mime) {
	fputs("MIME-Version: 1.0\r\n", stdout);
	fputs("Content-Type: text/plain\r\n", stdout);
    }
    fputs("\r\n", stdout);

    while (fgets(line, sizeof(line), f)
	   && (line[0] != '.' || line[1] != '\r' || line[2] != '\n'))
	fputs(line, stdout);

    fflush(f);
    fprintf(f, "QUIT\r\n");
    fflush(f);
    (void) fgets(line, sizeof(line), f);
    debug("Done\n");
}
