	Retrieving a news article by ID

	[[get_article_by_ID]] sends a command to the NNTP server over
	socket [[f]], in order to get the article with ID [[id]].

	The [[id]] is given here without [[<]] and [[>]] brackets, but
	it must be sent to the server with those brackets.

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

EXPORT void get_article_by_ID(FILE *f, char *id)
{
    char line[MAXLINELEN];
    int has_mime;

    debug("Sending \"ARTICLE %s\" command\n", id);
    fflush(f);
    fprintf(f, "ARTICLE <%s>\r\n", id);
    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);
    }
    printf("Base: news:%s\r\n", id);
    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");
}
