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) {
31 int in_messagetext = 0;
35 StrBuf *messagetext = NULL;
37 /* FIXME if this is a blog room we only want to include top-level messages */
39 serv_printf("MSG4 %ld", msgnum);
40 serv_getln(buf, sizeof buf);
41 if (buf[0] != '1') return;
44 wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
45 escputs(ChrPtr(WC->CurRoom.name));
46 wc_printf("?start_reading_at=%ld</link>", msgnum);
48 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
51 StrBufAppendBufPlain(messagetext, buf, -1, 0);
52 StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
54 else if (IsEmptyStr(buf)) {
58 else if (!strncasecmp(buf, "subj=", 5)) {
61 wc_printf("</title>");
64 else if (!strncasecmp(buf, "exti=", 5)) {
65 wc_printf("<guid isPermaLink=\"false\">");
70 else if (!strncasecmp(buf, "time=", 5)) {
71 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
72 wc_printf("<pubDate>%s</pubDate>", pubdate);
74 else if (!strncasecmp(buf, "text", 4)) {
76 wc_printf("<title>Message #%ld</title>", msgnum);
79 wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
81 ChrPtr(WC->serv_info->serv_humannode)
84 wc_printf("<description>");
86 messagetext = NewStrBuf();
91 cdataout((char*)ChrPtr(messagetext));
92 FreeStrBuf(&messagetext);
93 wc_printf("</description>");
100 * RSS feed generator -- go through the message list
102 void feed_rss_do_messages(void) {
106 SharedMessageStatus Stat;
107 message_summary *Msg = NULL;
109 memset(&Stat, 0, sizeof Stat);
110 Stat.maxload = INT_MAX;
111 Stat.lowest_found = (-1);
112 Stat.highest_found = (-1);
113 num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
114 if (num_msgs < 1) return;
116 i = num_msgs; /* convention is to feed newest-to-oldest */
118 Msg = GetMessagePtrAt(i-1, WCC->summ);
120 feed_rss_one_message(Msg->msgnum);
128 * Entry point for RSS feed generator
130 void feed_rss(void) {
133 output_headers(0, 0, 0, 0, 1, 0);
134 hprintf("Content-type: text/xml; charset=utf-8\r\n");
136 "Server: %s / %s\r\n"
137 "Connection: close\r\n"
139 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
143 wc_printf("<?xml version=\"1.0\"?>"
144 "<rss version=\"2.0\">"
148 wc_printf("<title>");
149 escputs(ChrPtr(WC->CurRoom.name));
150 wc_printf("</title>");
153 escputs(ChrPtr(site_prefix));
154 wc_printf("/</link>");
157 serv_getln(buf, sizeof buf);
159 wc_printf("<description>\r\n");
160 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
164 wc_printf("</description>");
167 wc_printf("<image><title>");
168 escputs(ChrPtr(WC->CurRoom.name));
169 wc_printf("</title><url>");
170 escputs(ChrPtr(site_prefix));
171 wc_printf("/image?name=_roompic_?go=");
172 urlescputs(ChrPtr(WC->CurRoom.name));
173 wc_printf("</url><link>");
174 escputs(ChrPtr(site_prefix));
175 wc_printf("/</link></image>\r\n");
177 feed_rss_do_messages();
179 wc_printf("</channel>"
189 * Offer the RSS feed meta tag for this room
191 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP)
194 char feed_link[1024];
195 char encoded_link[1024];
197 strcpy(feed_link, "/feed_rss?go=");
198 urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
199 CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
201 StrBufAppendPrintf(Target,
202 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
209 * Offer the RSS feed button for this room
211 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP)
214 char feed_link[1024];
215 char encoded_link[1024];
217 strcpy(feed_link, "/feed_rss?go=");
218 urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
219 CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
221 StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"/B64%s\">", encoded_link);
222 StrBufAppendPrintf(Target, "<img border=\"0\" src=\"static/rss_16x.png\" alt=\"RSS\">");
223 StrBufAppendPrintf(Target, "</a>");
231 WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
232 RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
233 RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);