When generating an RSS feed for a Blog room, omit comments; feed items are top level...
[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-2011 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 as
8  * published by the Free Software Foundation; either version 3 of the
9  * License, or (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         int in_body = 0;
30         int in_messagetext = 0;
31         int found_title = 0;
32         int found_guid = 0;
33         char pubdate[128];
34         StrBuf *messagetext = NULL;
35         int is_top_level_post = 1;
36         const char *BufPtr = NULL;
37         StrBuf *Line = NewStrBufPlain(NULL, 1024);
38         char buf[1024];
39
40         /* Phase 1: read the message into memory */
41         serv_printf("MSG4 %ld", msgnum);
42         serv_getln(buf, sizeof buf);
43         if (buf[0] != '1') return;
44         StrBuf *ServerResponse = NewStrBuf();
45         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
46                 StrBufAppendPrintf(ServerResponse, "%s\n", buf);
47         }
48
49         /* Phase 2: help SkyNet become self-aware */
50         BufPtr = NULL;
51         while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
52                 if (StrLength(Line) == 0) ++in_body;
53                 if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "wefw=", 5))) {
54                         is_top_level_post = 0;  /* presence of references means it's a reply/comment */
55                 }
56         }
57
58         /*
59          * Phase 3: output the message in RSS <item> form
60          * (suppress replies [comments] if this is a blog room)
61          */
62         if ( (WC->CurRoom.view != VIEW_BLOG) || (is_top_level_post == 1) ) {
63                 wc_printf("<item>");
64                 wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
65                 urlescputs(ChrPtr(WC->CurRoom.name));
66                 wc_printf("?start_reading_at=%ld</link>", msgnum);
67         
68                 BufPtr = NULL;
69                 in_body = 0;
70                 in_messagetext = 0;
71                 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
72                         safestrncpy(buf, ChrPtr(Line), sizeof buf);
73                         if (in_body) {
74                                 if (in_messagetext) {
75                                         StrBufAppendBufPlain(messagetext, buf, -1, 0);
76                                         StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
77                                 }
78                                 else if (IsEmptyStr(buf)) {
79                                         in_messagetext = 1;
80                                 }
81                         }
82                         else if (!strncasecmp(buf, "subj=", 5)) {
83                                 wc_printf("<title>");
84                                 escputs(&buf[5]);
85                                 wc_printf("</title>");
86                                 ++found_title;
87                         }
88                         else if (!strncasecmp(buf, "exti=", 5)) {
89                                 wc_printf("<guid isPermaLink=\"false\">");
90                                 escputs(&buf[5]);
91                                 wc_printf("</guid>");
92                                 ++found_guid;
93                         }
94                         else if (!strncasecmp(buf, "time=", 5)) {
95                                 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
96                                 wc_printf("<pubDate>%s</pubDate>", pubdate);
97                         }
98                         else if (!strncasecmp(buf, "text", 4)) {
99                                 if (!found_title) {
100                                         wc_printf("<title>Message #%ld</title>", msgnum);
101                                 }
102                                 if (!found_guid) {
103                                         wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
104                                                 msgnum,
105                                                 ChrPtr(WC->serv_info->serv_humannode)
106                                         );
107                                 }
108                                 wc_printf("<description>");
109                                 in_body = 1;
110                                 messagetext = NewStrBuf();
111                         }
112                 }
113         
114                 if (in_body) {
115                         cdataout((char*)ChrPtr(messagetext));
116                         FreeStrBuf(&messagetext);
117                         wc_printf("</description>");
118                 }
119
120                 wc_printf("</item>");
121         }
122
123         FreeStrBuf(&Line);
124         FreeStrBuf(&ServerResponse);
125         return;
126 }
127
128 /*
129  * RSS feed generator -- go through the message list
130  */
131 void feed_rss_do_messages(void) {
132         wcsession *WCC = WC;
133         int num_msgs = 0;
134         int i;
135         SharedMessageStatus Stat;
136         message_summary *Msg = NULL;
137
138         memset(&Stat, 0, sizeof Stat);
139         Stat.maxload = INT_MAX;
140         Stat.lowest_found = (-1);
141         Stat.highest_found = (-1);
142         num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
143         if (num_msgs < 1) return;
144
145         i = num_msgs;                                   /* convention is to feed newest-to-oldest */
146         while (i > 0) {
147                 Msg = GetMessagePtrAt(i-1, WCC->summ);
148                 if (Msg != NULL) {
149                         feed_rss_one_message(Msg->msgnum);
150                 }
151                 --i;
152         }
153 }
154
155
156 /*
157  * Output the room info file of the current room as a <description> for the channel
158  */
159 void feed_rss_do_room_info_as_description(void)
160 {
161         wc_printf("<description>");
162         escputs(ChrPtr(WC->CurRoom.name));      /* FIXME use the output of RINF instead */
163         wc_printf("</description>\r\n");
164 }
165
166
167 /*
168  * Entry point for RSS feed generator
169  */
170 void feed_rss(void) {
171         char buf[1024];
172
173         output_headers(0, 0, 0, 0, 1, 0);
174         hprintf("Content-type: text/xml; charset=utf-8\r\n");
175         hprintf(
176                 "Server: %s / %s\r\n"
177                 "Connection: close\r\n"
178         ,
179                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
180         );
181         begin_burst();
182
183         wc_printf("<?xml version=\"1.0\"?>"
184                 "<rss version=\"2.0\">"
185                 "<channel>"
186         );
187
188         wc_printf("<title>");
189         escputs(ChrPtr(WC->CurRoom.name));
190         wc_printf("</title>");
191
192         wc_printf("<link>");
193         escputs(ChrPtr(site_prefix));
194         wc_printf("/</link>");
195
196         serv_puts("RINF");
197         serv_getln(buf, sizeof buf);
198         if (buf[0] == '1') {
199                 wc_printf("<description>\r\n");
200                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
201                         escputs(buf);
202                         wc_printf("\r\n");
203                 }
204                 wc_printf("</description>");
205         }
206
207         wc_printf("<image><title>");
208         escputs(ChrPtr(WC->CurRoom.name));
209         wc_printf("</title><url>");
210         escputs(ChrPtr(site_prefix));
211         wc_printf("/image?name=_roompic_?go=");
212         urlescputs(ChrPtr(WC->CurRoom.name));
213         wc_printf("</url><link>");
214         escputs(ChrPtr(site_prefix));
215         wc_printf("/</link></image>\r\n");
216
217         feed_rss_do_room_info_as_description();
218         feed_rss_do_messages();
219
220         wc_printf("</channel>"
221                 "</rss>"
222                 "\r\n\r\n"
223         );
224
225         wDumpContent(0);
226 }
227
228
229 /*
230  * Offer the RSS feed meta tag for this room
231  */
232 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP) 
233 {
234         wcsession *WCC = WC;
235         char feed_link[1024];
236         char encoded_link[1024];
237
238         strcpy(feed_link, "/feed_rss?go=");
239         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
240         CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
241
242         StrBufAppendPrintf(Target,
243                 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
244                 encoded_link
245         );
246 }
247
248
249 /*
250  * Offer the RSS feed button for this room
251  */
252 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP) 
253 {
254         wcsession *WCC = WC;
255         char feed_link[1024];
256         char encoded_link[1024];
257
258         strcpy(feed_link, "/feed_rss?go=");
259         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
260         CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
261
262         StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"/B64%s\">", encoded_link);
263         StrBufAppendPrintf(Target, "<img border=\"0\" src=\"static/rss_16x.png\" alt=\"RSS\">");
264         StrBufAppendPrintf(Target, "</a>");
265 }
266
267
268 void 
269 InitModule_RSS
270 (void)
271 {
272         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
273         RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
274         RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);
275 }