Retrieving News groups and articles The [[ag-nntp]] agent has a single argument: a URL of the form [[news:*]], [[news:group]], [[news:articleID]] or [[news:group/articlenumber]]. Instead of `news', the name `nntp' may also be used. Which NNTP server to contact is determined by the [[NNTPSERVER]] environment variable, or by the compiled-in default. The default is defined in [["../config.h"]]. If the argument is of the first form, [[news:*]], the agent contacts the NNTP server and asks for a list of all available newsgroups. The program prefixes the list with a status code and a MIME header and prints it on standard output. The MIME Content-Type of such a list is [[text/x-newsgroup-list]]. The status code will be 200 if all goes well. If the argument is of the form [[news:group]], where group is any newsgroup name, then the agent sends a series of commands to the NNTP server to retrieve a list of all articles in that group. For each article, the agent sends the [[HEAD]] command to get the header information, of which the subject and author are retained and the rest is thrown away. The list is then prefixed with a status code and a MIME header and printed to stdout. The MIME type is [[text/x-article-list]] If the argument is of the form [[news:group/number]], the agent retrieves the indicated article from the group. If the header of that article does not include a MIME type, a header line for [[Content-Type: text/plain]] is added. The complete article, header plus body, is then printed on standard output, prefixed with a status code. Instead of a number, the message ID can also be used. In that case the [[group]] is redundant. When the URL is of the form [[news:articleID]], then the indicated article will be retrieved. The ID must be given without [[<]] and [[>]] brackets. The agent distinguishes between [[news:group]] and [[news:articleID]] by looking for a [[@]] character. Such a character occurs iff it is an ID. NOTE: In this version, the second and third command line argument (method, resp. title) are ignored. This nees to be changed. Especially the second argument (method) should be taken into account, since it may be a [[POST]] method. <<*>>= #include #include #include #include #include #include "get-all.e" #include "get-group.e" #include "get-article.e" #include "get-artID.e" void main(int argc, char *argv[]) { int s; FILE *f; char *server, *p, line[MAXLINELEN], *method = NULL, *title = "no title"; char *url; switch (argc) { case 4: title = argv[3]; /* fall through */ case 3: method = argv[2]; /* fall through */ case 2: url = argv[1]; break; default: errexit("400 Incorrect number of arguments\r\n"); } if (strncmp(url, "news:", 5) != 0 && strncmp(url, "nntp:", 5) != 0) errexit("400 Bad URL syntax\r\n"); if (! (server = getenv("NNTPSERVER"))) server = NNTPSERVER; debug("Connecting to %s\n", server); if ((s = connectTCP(server, "nntp")) == -1) errexit("500 Could not connect\r\n"); if (! (f = fdopen(s, "r+"))) errexit("500 I/O error\r\n"); if (! fgets(line, sizeof(line), f)) errexit("500 No response from server\r\n"); if (line[0] != '2') errexit(line); debug("Sending commands\n"); if (strcmp(url + 5, "*") == 0) { get_all_groups(f); /* news:* */ } else if (strchr(url + 5, '@')) { get_article_by_ID(f, url + 5); /* news:articleID */ } else if (! (p = strchr(url + 5, '/'))) { get_group(f, url + 5); /* news:group */ } else { *p = '\0'; get_article(f, url + 5, p + 1); /* news:group/number */ } exit(0); }