2 * RSS feed generator (could be adapted in the future to feed both RSS and Atom)
4 * Copyright (c) 2010-2012 by the citadel.org team
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.
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.
16 #include "webserver.h"
19 * RSS feed generator -- do one message
21 void feed_rss_one_message(long msgnum) {
23 int in_messagetext = 0;
27 StrBuf *messagetext = NULL;
28 int is_top_level_post = 1;
29 const char *BufPtr = NULL;
30 StrBuf *Line = NewStrBufPlain(NULL, 1024);
32 int permalink_hash = 0;
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);
43 /* Phase 2: help SkyNet become self-aware */
45 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
49 else if (StrLength(Line) == 0) {
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 */
55 else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "msgn=", 5))) {
56 StrBufCutLeft(Line, 5);
57 permalink_hash = ThreadIdHash(Line);
62 * Phase 3: output the message in RSS <item> form
63 * (suppress replies [comments] if this is a blog room)
65 if ( (WC->CurRoom.view != VIEW_BLOG) || (is_top_level_post == 1) ) {
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);
73 wc_printf("?start_reading_at=%ld", msgnum);
80 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
81 safestrncpy(buf, ChrPtr(Line), sizeof buf);
83 /* XML parsers can be picky; strip out nonprintable header characters */
84 if ((strlen(buf)>=6) && (buf[4]=='=')) {
94 /* Now output fields */
97 StrBufAppendBufPlain(messagetext, buf, -1, 0);
98 StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
100 else if (IsEmptyStr(buf)) {
104 else if (!strncasecmp(buf, "subj=", 5)) {
105 wc_printf("<title>");
107 wc_printf("</title>");
110 else if (!strncasecmp(buf, "exti=", 5)) {
111 wc_printf("<guid isPermaLink=\"false\">");
113 wc_printf("</guid>");
116 else if (!strncasecmp(buf, "time=", 5)) {
117 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
118 wc_printf("<pubDate>%s</pubDate>", pubdate);
120 else if (!strncasecmp(buf, "text", 4)) {
122 wc_printf("<title>Message #%ld</title>", msgnum);
125 wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
127 ChrPtr(WC->serv_info->serv_humannode)
130 wc_printf("<description>");
132 messagetext = NewStrBuf();
137 cdataout((char*)ChrPtr(messagetext));
138 FreeStrBuf(&messagetext);
139 wc_printf("</description>");
142 wc_printf("</item>");
146 FreeStrBuf(&ServerResponse);
152 * RSS feed generator -- go through the message list
154 void feed_rss_do_messages(void) {
158 SharedMessageStatus Stat;
159 message_summary *Msg = NULL;
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;
168 i = num_msgs; /* convention is to feed newest-to-oldest */
170 Msg = GetMessagePtrAt(i-1, WCC->summ);
172 feed_rss_one_message(Msg->msgnum);
180 * Output the room info file of the current room as a <description> for the channel
182 void feed_rss_do_room_info_as_description(void)
184 wc_printf("<description>");
185 escputs(ChrPtr(WC->CurRoom.name)); /* FIXME use the output of RINF instead */
186 wc_printf("</description>\r\n");
191 * Entry point for RSS feed generator
193 void feed_rss(void) {
196 output_headers(0, 0, 0, 0, 1, 0);
197 hprintf("Content-type: text/xml; charset=utf-8\r\n");
199 "Server: %s / %s\r\n"
200 "Connection: close\r\n"
202 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
206 wc_printf("<?xml version=\"1.0\"?>"
207 "<rss version=\"2.0\">"
211 wc_printf("<title>");
212 escputs(ChrPtr(WC->CurRoom.name));
213 wc_printf("</title>");
216 escputs(ChrPtr(site_prefix));
217 wc_printf("/</link>");
220 serv_getln(buf, sizeof buf);
222 wc_printf("<description>\r\n");
223 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
227 wc_printf("</description>");
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");
240 feed_rss_do_room_info_as_description();
241 feed_rss_do_messages();
243 wc_printf("</channel>"
253 * Offer the RSS feed meta tag for this room
255 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP)
258 char feed_link[1024];
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\">",
270 * Offer the RSS feed button for this room
272 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP)
274 StrBuf *FeedLink = NULL;
276 FeedLink = NewStrBufPlain(HKEY("/feed_rss?go="));
277 StrBufUrlescAppend(FeedLink, WC->CurRoom.name, NULL);
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);
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);