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