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