Fix XML encoding declaration
[blerg.git] / cgi / rss.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <cgi-util.h>
4 #include "database.h"
5 #include "escapery.h"
6 #include "canned_responses.h"
7 #include "app.h"
8
9 int fprint_rss(FILE *f, const char *username) {
10         struct blerg *b = blerg_open(username);
11         uint64_t record_count = blerg_get_record_count(b);
12         uint64_t i = (record_count > 50 ? record_count - 50 : 0);
13         char *data;
14         char *tmp;
15         int len;
16
17         fprintf(f,
18                 "<?xml version=\"1.0\" encoding=\"utf8\">\n"
19                 "<rss version=\"2.0\">\n"
20                 "<channel>\n"
21                 "<title>%s's bl&euml;rg</title>\n"
22                 "<link>%s</link>\n"
23                 "<description>%s</description>\n",
24                 "bl&euml;rg",
25                 "http://blerg.dominionfawesome.com/",
26                 "Textual vomit"
27         );
28
29         while (i < record_count) {
30                 blerg_fetch(b, i, &data, &len);
31                 tmp = xml_escape_data(data, len);
32                 fprintf(f,
33                         "<item>\n"
34                         "<description>%s</description>\n"
35                         "</item>\n",
36                         tmp
37                 );
38                 free(tmp);
39                 free(data);
40                 i++;
41         }
42         blerg_close(b);
43
44         fprintf(f,
45                 "</channel>\n"
46                 "</rss>\n"
47         );
48 }
49
50 int main (int argc, char *argv) {
51         char *path;
52         char *request_method;
53         int ret;
54         struct url_info info;
55
56         request_method = getenv("REQUEST_METHOD");
57         if (request_method == NULL) {
58                 fprintf(stderr, "Request method is null!?\n");
59                 exit(0);
60         }
61
62         if (strncmp(request_method, "GET", 4) != 0) {
63                 respond_405();
64                 exit(0);
65         }
66
67         path = getenv("PATH_INFO");
68         if (path == NULL) {
69                 respond_404();
70                 exit(0);
71         }
72
73         if (path[0] != '/') {
74                 respond_404();
75                 exit(0);
76         }
77
78         ret = parse_url_info(path + 1, &info);
79         if ((ret & URL_INFO_AUTHOR) == 0) {
80                 respond_404();
81                 exit(0);
82         }
83
84         if (!blerg_exists(info.author)) {
85                 respond_404();
86                 exit(0);
87         }
88
89         printf("Content-type: application/rss+xml\r\n\r\n");
90
91         fprint_rss(stdout, "chip");
92
93 }