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