RSS feed generator is basically complete
[citadel.git] / webcit / feed_generator.c
1 /*
2  * RSS/Atom feed generator
3  *
4  * Copyright (c) 2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23
24
25 /*
26  * RSS feed generator -- do one message
27  */
28 void feed_rss_one_message(long msgnum) {
29         char buf[1024];
30         int in_body = 0;
31         int found_title = 0;
32         char pubdate[128];
33
34         serv_printf("MSG0 %ld", msgnum);                /* FIXME we want msg4 eventually */
35         serv_getln(buf, sizeof buf);
36         if (buf[0] != '1') return;
37
38         wc_printf("<item>");
39         wc_printf("<link>%s/readfwd?gotofirst=", ChrPtr(site_prefix));
40         urlescputs(ChrPtr(WC->CurRoom.name));
41         wc_printf("?start_reading_at=%ld</link>", msgnum);
42
43         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
44                 if (in_body) {
45                         escputs(buf);
46                 }
47                 else if (!strncasecmp(buf, "subj=", 5)) {
48                         wc_printf("<title>");
49                         escputs(&buf[5]);
50                         wc_printf("</title>");
51                         ++found_title;
52                 }
53                 else if (!strncasecmp(buf, "exti=", 5)) {
54                         wc_printf("<guid>");
55                         escputs(&buf[5]);
56                         wc_printf("</guid>");
57                 }
58                 else if (!strncasecmp(buf, "time=", 5)) {
59                         http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
60                         wc_printf("<pubDate>%s</pubDate>", pubdate);
61                 }
62                 else if (!strncasecmp(buf, "text", 4)) {
63                         if (!found_title) {
64                                 wc_printf("<title>Message #%ld</title>", msgnum);
65                         }
66                         wc_printf("<description>");
67                         in_body = 1;
68                 }
69         }
70
71         if (in_body) {
72                 wc_printf("</description>");
73         }
74
75         wc_printf("</item>");
76 }
77
78 /*
79  * RSS feed generator -- go through the message list
80  */
81 void feed_rss_do_messages(void) {
82         char buf[1024];
83         long *msgs = NULL;
84         int num_msgs = 0;
85         int num_msgs_alloc = 0;
86         int i;
87
88         serv_puts("MSGP text/html|text/plain");         /* identify our preferred mime types */
89         serv_getln(buf, sizeof buf);                    /* don't care about the result */
90
91         serv_puts("MSGS ALL");                          /* ask for all messages in the room */
92         serv_getln(buf, sizeof buf);
93         if (buf[0] != '1') return;
94
95         while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))
96         {
97                 if (num_msgs >= num_msgs_alloc) {
98                         num_msgs_alloc += 1024;
99                         msgs = realloc(msgs, num_msgs_alloc*sizeof(long) );
100                 }
101                 msgs[num_msgs++] = atol(buf);
102         }
103
104         i = num_msgs;                                   /* convention is to feed newest-to-oldest */
105         while (i > 0) {
106                 feed_rss_one_message(msgs[i-1]);
107                 --i;
108         }
109
110         free(msgs);
111
112 }
113
114 /*
115  * Entry point for RSS feed generator
116  */
117 void feed_rss(void) {
118
119         output_headers(0, 0, 0, 1, 1, 0);
120         hprintf("Content-type: text/xml\r\n");
121         hprintf(
122                 "Server: %s / %s\r\n"
123                 "Connection: close\r\n"
124         ,
125                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
126         );
127         begin_burst();
128
129         wc_printf("<?xml version=\"1.0\"?>"
130                 "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">"
131                 "<channel>"
132         );
133
134         wc_printf("<title>");
135         escputs(ChrPtr(WC->CurRoom.name));
136         wc_printf("</title>");
137
138         wc_printf("<link>");
139         urlescputs(ChrPtr(site_prefix));
140         wc_printf("</link>");
141
142         //      <language>en-us</language>
143         //      <description>Linux Today News Service</description>
144         //      <atom:link href="http://linuxtoday.com/biglt.rss" rel="self" type="application/rss+xml" />
145
146         //    <image>
147         //      <title>Linux Today</title>
148         //      <url>http://linuxtoday.com/pics/ltnet.png</url>
149         //      <link>http://linuxtoday.com</link>
150         //    </image>
151
152         feed_rss_do_messages();
153
154         wc_printf("</channel>"
155                 "</rss>"
156                 "\r\n\r\n"
157         );
158
159         wDumpContent(0);
160         end_webcit_session();
161 }
162
163
164 void 
165 InitModule_RSS
166 (void)
167 {
168         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
169 }