	Retrieving a newsgroup

	[[get_group()]] retrieves a list of all articles in a
	newsgroup, by repeatedly sending [[HEAD]] commands, until the
	subject and author of all articles have been retrieved.
	Arguments are: [[f]] = the socket; [[group]] = the name of the
	group.

	The output is a list with 5 columns separated by tabs: (1) the
	ID without [[<>]] brackets; (2) date; (3) subject; (4) number
	of lines; (5) author.

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

EXPORT void get_group(FILE *f, char *newsgroup)
{
    char line[MAXLINELEN], id[MAXLINELEN];
    long first, last, dummy;
    MIME_header header;

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

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

    if (sscanf(line, "211%ld%ld%ld", &dummy, &first, &last) != 3)
	errexit(line);

    printf("200 OK\r\n");
    printf("Newsgroups: %s\r\n", newsgroup);
    printf("Base: news:%s\r\n", newsgroup);
    printf("MIME-Version: 1.0\r\n");
    printf("Content-Type: text/x-article-list\r\n\r\n");

    for (; first <= last; first++) {
	/* debug("Sending \"HEAD %s\" command\n", first); */
	fflush(f);
	fprintf(f, "HEAD %ld\r\n", first);
	fflush(f);

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

	if (sscanf(line, "221%ld <%s", &dummy, &id) == 2) {
	    id[strlen(id)-1] = '\0';		/* Remove '>' */
	    read_header(f, &header, ".\r\n");
	    printf("%s\t%s\t%s\t%s\t%s\r\n", id, header.head[Date],
		   header.head[Subject], header.head[Lines],
		   header.head[Author]);
	}
    }
    debug("Done\n");
}
