2 * RSS feed generator (could be adapted in the future to feed both RSS and Atom)
4 * Copyright (c) 2010-2011 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 as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
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.
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
22 #include "webserver.h"
26 * RSS feed generator -- do one message
28 void feed_rss_one_message(long msgnum) {
30 int in_messagetext = 0;
34 StrBuf *messagetext = NULL;
35 int is_top_level_post = 1;
36 const char *BufPtr = NULL;
37 StrBuf *Line = NewStrBufPlain(NULL, 1024);
40 /* FIXME if this is a blog room we only want to include top-level messages */
42 serv_printf("MSG4 %ld", msgnum);
43 serv_getln(buf, sizeof buf);
44 if (buf[0] != '1') return;
45 StrBuf *ServerResponse = NewStrBuf();
46 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
47 StrBufAppendPrintf(ServerResponse, "%s\n", buf);
51 wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
52 urlescputs(ChrPtr(WC->CurRoom.name));
53 wc_printf("?start_reading_at=%ld</link>", msgnum);
55 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
56 safestrncpy(buf, ChrPtr(Line), sizeof buf);
59 StrBufAppendBufPlain(messagetext, buf, -1, 0);
60 StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
62 else if (IsEmptyStr(buf)) {
66 else if (!strncasecmp(buf, "subj=", 5)) {
69 wc_printf("</title>");
72 else if (!strncasecmp(buf, "exti=", 5)) {
73 wc_printf("<guid isPermaLink=\"false\">");
78 else if (!strncasecmp(buf, "time=", 5)) {
79 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
80 wc_printf("<pubDate>%s</pubDate>", pubdate);
82 else if (!strncasecmp(buf, "wefw=", 5)) {
83 is_top_level_post = 0; /* presence of references means it's a reply/comment */
85 else if (!strncasecmp(buf, "text", 4)) {
87 wc_printf("<title>Message #%ld</title>", msgnum);
90 wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
92 ChrPtr(WC->serv_info->serv_humannode)
95 wc_printf("<description>");
97 messagetext = NewStrBuf();
102 cdataout((char*)ChrPtr(messagetext));
103 FreeStrBuf(&messagetext);
104 wc_printf("</description>");
107 wc_printf("</item>");
109 FreeStrBuf(&ServerResponse);
114 * RSS feed generator -- go through the message list
116 void feed_rss_do_messages(void) {
120 SharedMessageStatus Stat;
121 message_summary *Msg = NULL;
123 memset(&Stat, 0, sizeof Stat);
124 Stat.maxload = INT_MAX;
125 Stat.lowest_found = (-1);
126 Stat.highest_found = (-1);
127 num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
128 if (num_msgs < 1) return;
130 i = num_msgs; /* convention is to feed newest-to-oldest */
132 Msg = GetMessagePtrAt(i-1, WCC->summ);
134 feed_rss_one_message(Msg->msgnum);
142 * Output the room info file of the current room as a <description> for the channel
144 void feed_rss_do_room_info_as_description(void)
146 wc_printf("<description>");
147 escputs(ChrPtr(WC->CurRoom.name)); /* FIXME use the output of RINF instead */
148 wc_printf("</description>\r\n");
153 * Entry point for RSS feed generator
155 void feed_rss(void) {
158 output_headers(0, 0, 0, 0, 1, 0);
159 hprintf("Content-type: text/xml; charset=utf-8\r\n");
161 "Server: %s / %s\r\n"
162 "Connection: close\r\n"
164 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
168 wc_printf("<?xml version=\"1.0\"?>"
169 "<rss version=\"2.0\">"
173 wc_printf("<title>");
174 escputs(ChrPtr(WC->CurRoom.name));
175 wc_printf("</title>");
178 escputs(ChrPtr(site_prefix));
179 wc_printf("/</link>");
182 serv_getln(buf, sizeof buf);
184 wc_printf("<description>\r\n");
185 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
189 wc_printf("</description>");
192 wc_printf("<image><title>");
193 escputs(ChrPtr(WC->CurRoom.name));
194 wc_printf("</title><url>");
195 escputs(ChrPtr(site_prefix));
196 wc_printf("/image?name=_roompic_?go=");
197 urlescputs(ChrPtr(WC->CurRoom.name));
198 wc_printf("</url><link>");
199 escputs(ChrPtr(site_prefix));
200 wc_printf("/</link></image>\r\n");
202 feed_rss_do_room_info_as_description();
203 feed_rss_do_messages();
205 wc_printf("</channel>"
215 * Offer the RSS feed meta tag for this room
217 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP)
220 char feed_link[1024];
221 char encoded_link[1024];
223 strcpy(feed_link, "/feed_rss?go=");
224 urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
225 CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
227 StrBufAppendPrintf(Target,
228 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
235 * Offer the RSS feed button for this room
237 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP)
240 char feed_link[1024];
241 char encoded_link[1024];
243 strcpy(feed_link, "/feed_rss?go=");
244 urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
245 CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
247 StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"/B64%s\">", encoded_link);
248 StrBufAppendPrintf(Target, "<img border=\"0\" src=\"static/rss_16x.png\" alt=\"RSS\">");
249 StrBufAppendPrintf(Target, "</a>");
257 WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
258 RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
259 RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);