3 * RSS feed generator (could be adapted in the future to feed both RSS and Atom)
5 * Copyright (c) 2010-2012 by the citadel.org team
7 * This program is open source software. You can redistribute it and/or
8 * modify it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include "webserver.h"
20 * RSS feed generator -- do one message
22 void feed_rss_one_message(long msgnum) {
24 int in_messagetext = 0;
28 StrBuf *messagetext = NULL;
29 int is_top_level_post = 1;
30 const char *BufPtr = NULL;
31 StrBuf *Line = NewStrBufPlain(NULL, 1024);
33 int permalink_hash = 0;
35 /* Phase 1: read the message into memory */
36 serv_printf("MSG4 %ld", msgnum);
37 serv_getln(buf, sizeof buf);
40 StrBuf *ServerResponse = NewStrBuf();
41 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
42 StrBufAppendPrintf(ServerResponse, "%s\n", buf);
45 /* Phase 2: help SkyNet become self-aware */
47 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr != StrBufNOTNULL) && (BufPtr != NULL))) {
51 else if (StrLength(Line) == 0) {
54 else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "wefw=", 5))) {
55 is_top_level_post = 0; /* presence of references means it's a reply/comment */
57 else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "msgn=", 5))) {
58 StrBufCutLeft(Line, 5);
59 permalink_hash = ThreadIdHash(Line);
64 * Phase 3: output the message in RSS <item> form
65 * (suppress replies [comments] if this is a blog room)
67 if ((WC->CurRoom.view != VIEW_BLOG) || (is_top_level_post == 1)) {
69 wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
70 urlescputs(ChrPtr(WC->CurRoom.name));
71 if ((WC->CurRoom.view == VIEW_BLOG) && (permalink_hash != 0)) {
72 wc_printf("?p=%d", permalink_hash);
75 wc_printf("?start_reading_at=%ld", msgnum);
82 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr != StrBufNOTNULL) && (BufPtr != NULL))) {
83 safestrncpy(buf, ChrPtr(Line), sizeof buf);
85 /* XML parsers can be picky; strip out nonprintable header characters */
86 if ((strlen(buf) >= 6) && (buf[4] == '=')) {
96 /* Now output fields */
99 StrBufAppendBufPlain(messagetext, buf, -1, 0);
100 StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
102 else if (IsEmptyStr(buf)) {
106 else if (!strncasecmp(buf, "subj=", 5)) {
107 wc_printf("<title>");
109 wc_printf("</title>");
112 else if (!strncasecmp(buf, "exti=", 5)) {
113 wc_printf("<guid isPermaLink=\"false\">");
115 wc_printf("</guid>");
118 else if (!strncasecmp(buf, "time=", 5)) {
119 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
120 wc_printf("<pubDate>%s</pubDate>", pubdate);
122 else if (!strncasecmp(buf, "text", 4)) {
124 wc_printf("<title>Message #%ld</title>", msgnum);
127 wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
128 msgnum, ChrPtr(WC->serv_info->serv_humannode)
131 wc_printf("<description>");
133 messagetext = NewStrBuf();
138 cdataout((char *) ChrPtr(messagetext));
139 FreeStrBuf(&messagetext);
140 wc_printf("</description>");
143 wc_printf("</item>");
147 FreeStrBuf(&ServerResponse);
153 * RSS feed generator -- go through the message list
155 void feed_rss_do_messages(void) {
159 SharedMessageStatus Stat;
160 message_summary *Msg = NULL;
162 memset(&Stat, 0, sizeof Stat);
163 Stat.maxload = INT_MAX;
164 Stat.lowest_found = (-1);
165 Stat.highest_found = (-1);
166 num_msgs = load_msg_ptrs("MSGS ALL", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0);
170 i = num_msgs; /* convention is to feed newest-to-oldest */
172 Msg = GetMessagePtrAt(i - 1, WCC->summ);
174 feed_rss_one_message(Msg->msgnum);
182 * Output the room info file of the current room as a <description> for the channel
184 void feed_rss_do_room_info_as_description(void) {
185 wc_printf("<description>");
186 escputs(ChrPtr(WC->CurRoom.name)); /* FIXME use the output of RINF instead */
187 wc_printf("</description>\r\n");
192 * Entry point for RSS feed generator
194 void feed_rss(void) {
197 output_headers(0, 0, 0, 0, 1, 0);
198 hprintf("Content-type: text/xml; charset=utf-8\r\n");
199 hprintf("Server: %s / %s\r\n" "Connection: close\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
203 wc_printf("<?xml version=\"1.0\"?>" "<rss version=\"2.0\">" "<channel>");
205 wc_printf("<title>");
206 escputs(ChrPtr(WC->CurRoom.name));
207 wc_printf("</title>");
210 escputs(ChrPtr(site_prefix));
211 wc_printf("/</link>");
214 serv_getln(buf, sizeof buf);
216 wc_printf("<description>\r\n");
217 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
221 wc_printf("</description>");
224 wc_printf("<image><title>");
225 escputs(ChrPtr(WC->CurRoom.name));
226 wc_printf("</title><url>");
227 escputs(ChrPtr(site_prefix));
228 wc_printf("/roompic?room=");
229 urlescputs(ChrPtr(WC->CurRoom.name));
230 wc_printf("</url><link>");
231 escputs(ChrPtr(site_prefix));
232 wc_printf("/</link></image>\r\n");
234 feed_rss_do_room_info_as_description();
235 feed_rss_do_messages();
237 wc_printf("</channel>" "</rss>" "\r\n\r\n");
244 * Offer the RSS feed meta tag for this room
246 void tmplput_rssmeta(StrBuf * Target, WCTemplputParams * TP) {
248 char feed_link[1024];
250 strcpy(feed_link, "/feed_rss?go=");
251 urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *) ChrPtr(WCC->CurRoom.name));
252 StrBufAppendPrintf(Target, "<link rel=\"alternate\" title=\"RSS\" href=\"%s\" type=\"application/rss+xml\">", feed_link);
257 * Offer the RSS feed button for this room
259 void tmplput_rssbutton(StrBuf * Target, WCTemplputParams * TP) {
260 StrBuf *FeedLink = NULL;
262 FeedLink = NewStrBufPlain(HKEY("/feed_rss?go="));
263 StrBufUrlescAppend(FeedLink, WC->CurRoom.name, NULL);
265 StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"");
266 StrBufAppendBuf(Target, FeedLink, 0);
267 StrBufAppendPrintf(Target, "\"><img src=\"static/webcit_icons/essen/16x16/rss.png\" alt=\"RSS\">");
268 StrBufAppendPrintf(Target, "</a>");
269 FreeStrBuf(&FeedLink);
273 void InitModule_RSS(void) {
274 WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS | COOKIEUNNEEDED);
275 RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
276 RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);