Mailing list header changes (fuck you Google)
[citadel.git] / webcit / feed_generator.c
1 /*
2  * RSS feed generator (could be adapted in the future to feed both RSS and Atom)
3  *
4  * Copyright (c) 2010-2012 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17
18 /*
19  * RSS feed generator -- do one message
20  */
21 void feed_rss_one_message(long msgnum) {
22         int in_body = 0;
23         int in_messagetext = 0;
24         int found_title = 0;
25         int found_guid = 0;
26         char pubdate[128];
27         StrBuf *messagetext = NULL;
28         int is_top_level_post = 1;
29         const char *BufPtr = NULL;
30         StrBuf *Line = NewStrBufPlain(NULL, 1024);
31         char buf[1024];
32         int permalink_hash = 0;
33
34         /* Phase 1: read the message into memory */
35         serv_printf("MSG4 %ld", msgnum);
36         serv_getln(buf, sizeof buf);
37         if (buf[0] != '1') return;
38         StrBuf *ServerResponse = NewStrBuf();
39         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
40                 StrBufAppendPrintf(ServerResponse, "%s\n", buf);
41         }
42
43         /* Phase 2: help SkyNet become self-aware */
44         BufPtr = NULL;
45         while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
46                 if (in_body) {
47                         /* do nothing */
48                 }
49                 else if (StrLength(Line) == 0) {
50                         ++in_body;
51                 }
52                 else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "wefw=", 5))) {
53                         is_top_level_post = 0;  /* presence of references means it's a reply/comment */
54                 }
55                 else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "msgn=", 5))) {
56                         StrBufCutLeft(Line, 5);
57                         permalink_hash = ThreadIdHash(Line);
58                 }
59         }
60
61         /*
62          * Phase 3: output the message in RSS <item> form
63          * (suppress replies [comments] if this is a blog room)
64          */
65         if ( (WC->CurRoom.view != VIEW_BLOG) || (is_top_level_post == 1) ) {
66                 wc_printf("<item>");
67                 wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
68                 urlescputs(ChrPtr(WC->CurRoom.name));
69                 if ((WC->CurRoom.view == VIEW_BLOG) && (permalink_hash != 0)) {
70                         wc_printf("?p=%d", permalink_hash);
71                 }
72                 else {
73                         wc_printf("?start_reading_at=%ld", msgnum);
74                 }
75                 wc_printf("</link>");
76         
77                 BufPtr = NULL;
78                 in_body = 0;
79                 in_messagetext = 0;
80                 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
81                         safestrncpy(buf, ChrPtr(Line), sizeof buf);
82
83                         /* XML parsers can be picky; strip out nonprintable header characters */
84                         if ((strlen(buf)>=6) && (buf[4]=='=')) {
85                                 char *p = &buf[5];
86                                 while (*p) {
87                                         if (!isprint(*p)) {
88                                                 *p = 0;
89                                         }
90                                         ++p;
91                                 }
92                         }
93
94                         /* Now output fields */
95                         if (in_body) {
96                                 if (in_messagetext) {
97                                         StrBufAppendBufPlain(messagetext, buf, -1, 0);
98                                         StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
99                                 }
100                                 else if (IsEmptyStr(buf)) {
101                                         in_messagetext = 1;
102                                 }
103                         }
104                         else if (!strncasecmp(buf, "subj=", 5)) {
105                                 wc_printf("<title>");
106                                 escputs(&buf[5]);
107                                 wc_printf("</title>");
108                                 ++found_title;
109                         }
110                         else if (!strncasecmp(buf, "exti=", 5)) {
111                                 wc_printf("<guid isPermaLink=\"false\">");
112                                 escputs(&buf[5]);
113                                 wc_printf("</guid>");
114                                 ++found_guid;
115                         }
116                         else if (!strncasecmp(buf, "time=", 5)) {
117                                 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
118                                 wc_printf("<pubDate>%s</pubDate>", pubdate);
119                         }
120                         else if (!strncasecmp(buf, "text", 4)) {
121                                 if (!found_title) {
122                                         wc_printf("<title>Message #%ld</title>", msgnum);
123                                 }
124                                 if (!found_guid) {
125                                         wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
126                                                 msgnum,
127                                                 ChrPtr(WC->serv_info->serv_humannode)
128                                         );
129                                 }
130                                 wc_printf("<description>");
131                                 in_body = 1;
132                                 messagetext = NewStrBuf();
133                         }
134                 }
135         
136                 if (in_body) {
137                         cdataout((char*)ChrPtr(messagetext));
138                         FreeStrBuf(&messagetext);
139                         wc_printf("</description>");
140                 }
141
142                 wc_printf("</item>");
143         }
144
145         FreeStrBuf(&Line);
146         FreeStrBuf(&ServerResponse);
147         return;
148 }
149
150
151 /*
152  * RSS feed generator -- go through the message list
153  */
154 void feed_rss_do_messages(void) {
155         wcsession *WCC = WC;
156         int num_msgs = 0;
157         int i;
158         SharedMessageStatus Stat;
159         message_summary *Msg = NULL;
160
161         memset(&Stat, 0, sizeof Stat);
162         Stat.maxload = INT_MAX;
163         Stat.lowest_found = (-1);
164         Stat.highest_found = (-1);
165         num_msgs = load_msg_ptrs("MSGS ALL", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0);
166         if (num_msgs < 1) return;
167
168         i = num_msgs;                                   /* convention is to feed newest-to-oldest */
169         while (i > 0) {
170                 Msg = GetMessagePtrAt(i-1, WCC->summ);
171                 if (Msg != NULL) {
172                         feed_rss_one_message(Msg->msgnum);
173                 }
174                 --i;
175         }
176 }
177
178
179 /*
180  * Output the room info file of the current room as a <description> for the channel
181  */
182 void feed_rss_do_room_info_as_description(void)
183 {
184         wc_printf("<description>");
185         escputs(ChrPtr(WC->CurRoom.name));      /* FIXME use the output of RINF instead */
186         wc_printf("</description>\r\n");
187 }
188
189
190 /*
191  * Entry point for RSS feed generator
192  */
193 void feed_rss(void) {
194         char buf[1024];
195
196         output_headers(0, 0, 0, 0, 1, 0);
197         hprintf("Content-type: text/xml; charset=utf-8\r\n");
198         hprintf(
199                 "Server: %s / %s\r\n"
200                 "Connection: close\r\n"
201         ,
202                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
203         );
204         begin_burst();
205
206         wc_printf("<?xml version=\"1.0\"?>"
207                 "<rss version=\"2.0\">"
208                 "<channel>"
209         );
210
211         wc_printf("<title>");
212         escputs(ChrPtr(WC->CurRoom.name));
213         wc_printf("</title>");
214
215         wc_printf("<link>");
216         escputs(ChrPtr(site_prefix));
217         wc_printf("/</link>");
218
219         serv_puts("RINF");
220         serv_getln(buf, sizeof buf);
221         if (buf[0] == '1') {
222                 wc_printf("<description>\r\n");
223                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
224                         escputs(buf);
225                         wc_printf("\r\n");
226                 }
227                 wc_printf("</description>");
228         }
229
230         wc_printf("<image><title>");
231         escputs(ChrPtr(WC->CurRoom.name));
232         wc_printf("</title><url>");
233         escputs(ChrPtr(site_prefix));
234         wc_printf("/roompic?room=");
235         urlescputs(ChrPtr(WC->CurRoom.name));
236         wc_printf("</url><link>");
237         escputs(ChrPtr(site_prefix));
238         wc_printf("/</link></image>\r\n");
239
240         feed_rss_do_room_info_as_description();
241         feed_rss_do_messages();
242
243         wc_printf("</channel>"
244                 "</rss>"
245                 "\r\n\r\n"
246         );
247
248         wDumpContent(0);
249 }
250
251
252 /*
253  * Offer the RSS feed meta tag for this room
254  */
255 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP) 
256 {
257         wcsession *WCC = WC;
258         char feed_link[1024];
259
260         strcpy(feed_link, "/feed_rss?go=");
261         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
262         StrBufAppendPrintf(Target,
263                 "<link rel=\"alternate\" title=\"RSS\" href=\"%s\" type=\"application/rss+xml\">",
264                 feed_link
265         );
266 }
267
268
269 /*
270  * Offer the RSS feed button for this room
271  */
272 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP) 
273 {
274         StrBuf *FeedLink = NULL;
275
276         FeedLink = NewStrBufPlain(HKEY("/feed_rss?go="));
277         StrBufUrlescAppend(FeedLink, WC->CurRoom.name, NULL);
278
279         StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"");
280         StrBufAppendBuf(Target, FeedLink, 0);
281         StrBufAppendPrintf(Target, "\"><img src=\"static/webcit_icons/essen/16x16/rss.png\" alt=\"RSS\">");
282         StrBufAppendPrintf(Target, "</a>");
283         FreeStrBuf(&FeedLink);
284 }
285
286
287 void 
288 InitModule_RSS
289 (void)
290 {
291         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
292         RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
293         RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);
294 }