+#include <stdio.h>
+#include <stdlib.h>
+#include <cgi-util.h>
+#include "database.h"
+#include "escapery.h"
+#include "canned_responses.h"
+#include "app.h"
+
+int fprint_rss(FILE *f, const char *username) {
+ struct blerg *b = blerg_open(username);
+ uint64_t record_count = blerg_get_record_count(b);
+ uint64_t i = (record_count > 50 ? record_count - 50 : 0);
+ char *data;
+ char *tmp;
+ int len;
+
+ fprintf(f,
+ "<?xml version=\"1.0\" charset=\"utf8\">\n"
+ "<rss version=\"2.0\">\n"
+ "<channel>\n"
+ "<title>%s's blërg</title>\n"
+ "<link>%s</link>\n"
+ "<description>%s</description>\n",
+ "blërg",
+ "http://blerg.dominionfawesome.com/",
+ "Textual vomit"
+ );
+
+ while (i < record_count) {
+ blerg_fetch(b, i, &data, &len);
+ tmp = xml_escape_data(data, len);
+ fprintf(f,
+ "<item>\n"
+ "<description>%s</description>\n"
+ "</item>\n",
+ tmp
+ );
+ free(tmp);
+ free(data);
+ i++;
+ }
+ blerg_close(b);
+
+ fprintf(f,
+ "</channel>\n"
+ "</rss>\n"
+ );
+}
+
+int main (int argc, char *argv) {
+ char *path;
+ char *request_method;
+ int ret;
+ struct url_info info;
+
+ request_method = getenv("REQUEST_METHOD");
+ if (request_method == NULL) {
+ fprintf(stderr, "Request method is null!?\n");
+ exit(0);
+ }
+
+ if (strncmp(request_method, "GET", 4) != 0) {
+ respond_405();
+ exit(0);
+ }
+
+ path = getenv("PATH_INFO");
+ if (path == NULL) {
+ respond_404();
+ exit(0);
+ }
+
+ if (path[0] != '/') {
+ respond_404();
+ exit(0);
+ }
+
+ ret = parse_url_info(path + 1, &info);
+ if ((ret & URL_INFO_AUTHOR) == 0) {
+ respond_404();
+ exit(0);
+ }
+
+ if (!blerg_exists(info.author)) {
+ respond_404();
+ exit(0);
+ }
+
+ printf("Content-type: application/rss+xml\r\n\r\n");
+
+ fprint_rss(stdout, "chip");
+
+}