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;
34 StrBuf *messagetext = NULL;
36 /* FIXME if this is a blog room we only want to include top-level messages */
38 serv_printf("MSG4 %ld", msgnum);
39 serv_getln(buf, sizeof buf);
40 if (buf[0] != '1') return;
43 wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
44 urlescputs(ChrPtr(WC->CurRoom.name));
45 wc_printf("?start_reading_at=%ld</link>", msgnum);
47 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
50 StrBufAppendBufPlain(messagetext, buf, -1, 0);
51 StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
53 else if (IsEmptyStr(buf)) {
57 else if (!strncasecmp(buf, "subj=", 5)) {
60 wc_printf("</title>");
63 else if (!strncasecmp(buf, "exti=", 5)) {
68 else if (!strncasecmp(buf, "time=", 5)) {
69 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
70 wc_printf("<pubDate>%s</pubDate>", pubdate);
72 else if (!strncasecmp(buf, "text", 4)) {
74 wc_printf("<title>Message #%ld</title>", msgnum);
76 wc_printf("<description>");
78 messagetext = NewStrBuf();
83 cdataout((char*)ChrPtr(messagetext));
84 FreeStrBuf(&messagetext);
85 wc_printf("</description>");
92 * RSS feed generator -- go through the message list
94 void feed_rss_do_messages(void) {
98 SharedMessageStatus Stat;
99 message_summary *Msg = NULL;
101 memset(&Stat, 0, sizeof Stat);
102 Stat.maxload = INT_MAX;
103 Stat.lowest_found = (-1);
104 Stat.highest_found = (-1);
105 num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
106 if (num_msgs < 1) return;
108 i = num_msgs; /* convention is to feed newest-to-oldest */
110 Msg = GetMessagePtrAt(i-1, WCC->summ);
112 feed_rss_one_message(Msg->msgnum);
120 * Entry point for RSS feed generator
122 void feed_rss(void) {
125 output_headers(0, 0, 0, 0, 1, 0);
126 hprintf("Content-type: text/xml\r\n");
128 "Server: %s / %s\r\n"
129 "Connection: close\r\n"
131 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
135 wc_printf("<?xml version=\"1.0\"?>"
136 "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">"
140 wc_printf("<title>");
141 escputs(ChrPtr(WC->CurRoom.name));
142 wc_printf("</title>");
145 urlescputs(ChrPtr(site_prefix));
146 wc_printf("</link>");
149 serv_getln(buf, sizeof buf);
151 wc_printf("<description>\r\n");
152 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
156 wc_printf("</description>");
159 wc_printf("<image><title>");
160 escputs(ChrPtr(WC->CurRoom.name));
161 wc_printf("</title><url>");
162 urlescputs(ChrPtr(site_prefix));
163 wc_printf("/image?name=_roompic_?go=");
164 urlescputs(ChrPtr(WC->CurRoom.name));
165 wc_printf("</url><link>");
166 urlescputs(ChrPtr(site_prefix));
167 wc_printf("</link></image>\r\n");
169 feed_rss_do_messages();
171 wc_printf("</channel>"
181 * Offer the RSS feed meta tag for this room
183 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP)
186 char feed_link[1024];
187 char encoded_link[1024];
189 strcpy(feed_link, "/feed_rss?go=");
190 urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
191 CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
193 StrBufAppendPrintf(Target,
194 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
201 * Offer the RSS feed button for this room
203 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP)
206 char feed_link[1024];
207 char encoded_link[1024];
209 strcpy(feed_link, "/feed_rss?go=");
210 urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
211 CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
213 StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"/B64%s\">", encoded_link);
214 StrBufAppendPrintf(Target, "<img border=\"0\" src=\"static/rss_16x.png\" alt=\"RSS\">");
215 StrBufAppendPrintf(Target, "</a>");
223 WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
224 RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
225 RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);