Fix RSS bugs
[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Ă«rg</title>\n"
22                 "<link>%s#%s</link>\n"
23                 "<description>%s</description>\n",
24                 username,
25                 "http://blerg.dominionfawesome.com/",
26                 username,
27                 "Textual vomit"
28         );
29
30         while (i < record_count) {
31                 blerg_fetch(b, i, &data, &len);
32                 tmp = xml_escape_data(data, len);
33                 fprintf(f,
34                         "<item>\n"
35                         "<description>%s</description>\n"
36                         "</item>\n",
37                         tmp
38                 );
39                 free(tmp);
40                 free(data);
41                 i++;
42         }
43         blerg_close(b);
44
45         fprintf(f,
46                 "</channel>\n"
47                 "</rss>\n"
48         );
49 }
50
51 int main (int argc, char *argv) {
52         char *path;
53         char *request_method;
54         int ret;
55         struct url_info info;
56
57         request_method = getenv("REQUEST_METHOD");
58         if (request_method == NULL) {
59                 fprintf(stderr, "Request method is null!?\n");
60                 exit(0);
61         }
62
63         if (strncmp(request_method, "GET", 4) != 0) {
64                 respond_405();
65                 exit(0);
66         }
67
68         path = getenv("PATH_INFO");
69         if (path == NULL) {
70                 respond_404();
71                 exit(0);
72         }
73
74         if (path[0] != '/') {
75                 respond_404();
76                 exit(0);
77         }
78
79         ret = parse_url_info(path + 1, &info);
80         if ((ret & URL_INFO_AUTHOR) == 0) {
81                 respond_404();
82                 exit(0);
83         }
84
85         if (!blerg_exists(info.author)) {
86                 respond_404();
87                 exit(0);
88         }
89
90         printf("Content-type: application/rss+xml\r\n\r\n");
91
92         fprint_rss(stdout, "chip");
93
94 }