More RSS cleanup based on the suggestions of http://www.feedvalidator.org
[citadel.git] / webcit / feed_generator.c
1 /*
2  * RSS feed generator (could be adapted in the future to feed both RSS and Atom)
3  *
4  * Copyright (c) 2010-2011 by the citadel.org team
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23
24
25 /*
26  * RSS feed generator -- do one message
27  */
28 void feed_rss_one_message(long msgnum) {
29         char buf[1024];
30         int in_body = 0;
31         int in_messagetext = 0;
32         int found_title = 0;
33         int found_guid = 0;
34         char pubdate[128];
35         StrBuf *messagetext = NULL;
36
37         /* FIXME if this is a blog room we only want to include top-level messages */
38
39         serv_printf("MSG4 %ld", msgnum);
40         serv_getln(buf, sizeof buf);
41         if (buf[0] != '1') return;
42
43         wc_printf("<item>");
44         wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
45         urlescputs(ChrPtr(WC->CurRoom.name));
46         wc_printf("?start_reading_at=%ld</link>", msgnum);
47
48         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
49                 if (in_body) {
50                         if (in_messagetext) {
51                                 StrBufAppendBufPlain(messagetext, buf, -1, 0);
52                                 StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
53                         }
54                         else if (IsEmptyStr(buf)) {
55                                 in_messagetext = 1;
56                         }
57                 }
58                 else if (!strncasecmp(buf, "subj=", 5)) {
59                         wc_printf("<title>");
60                         escputs(&buf[5]);
61                         wc_printf("</title>");
62                         ++found_title;
63                 }
64                 else if (!strncasecmp(buf, "exti=", 5)) {
65                         wc_printf("<guid isPermaLink=\"false\">");
66                         escputs(&buf[5]);
67                         wc_printf("</guid>");
68                         ++found_guid;
69                 }
70                 else if (!strncasecmp(buf, "time=", 5)) {
71                         http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
72                         wc_printf("<pubDate>%s</pubDate>", pubdate);
73                 }
74                 else if (!strncasecmp(buf, "text", 4)) {
75                         if (!found_title) {
76                                 wc_printf("<title>Message #%ld</title>", msgnum);
77                         }
78                         if (!found_guid) {
79                                 wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
80                                         msgnum,
81                                         ChrPtr(WC->serv_info->serv_humannode)
82                                 );
83                         }
84                         wc_printf("<description>");
85                         in_body = 1;
86                         messagetext = NewStrBuf();
87                 }
88         }
89
90         if (in_body) {
91                 cdataout((char*)ChrPtr(messagetext));
92                 FreeStrBuf(&messagetext);
93                 wc_printf("</description>");
94         }
95
96         wc_printf("</item>");
97 }
98
99 /*
100  * RSS feed generator -- go through the message list
101  */
102 void feed_rss_do_messages(void) {
103         wcsession *WCC = WC;
104         int num_msgs = 0;
105         int i;
106         SharedMessageStatus Stat;
107         message_summary *Msg = NULL;
108
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;
115
116         i = num_msgs;                                   /* convention is to feed newest-to-oldest */
117         while (i > 0) {
118                 Msg = GetMessagePtrAt(i-1, WCC->summ);
119                 if (Msg != NULL) {
120                         feed_rss_one_message(Msg->msgnum);
121                 }
122                 --i;
123         }
124 }
125
126
127 /*
128  * Output the room info file of the current room as a <description> for the channel
129  */
130 void feed_rss_do_room_info_as_description(void)
131 {
132         wc_printf("<description>");
133         escputs(ChrPtr(WC->CurRoom.name));      /* FIXME use the output of RINF instead */
134         wc_printf("</description>\r\n");
135 }
136
137
138 /*
139  * Entry point for RSS feed generator
140  */
141 void feed_rss(void) {
142         char buf[1024];
143
144         output_headers(0, 0, 0, 0, 1, 0);
145         hprintf("Content-type: text/xml; charset=utf-8\r\n");
146         hprintf(
147                 "Server: %s / %s\r\n"
148                 "Connection: close\r\n"
149         ,
150                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
151         );
152         begin_burst();
153
154         wc_printf("<?xml version=\"1.0\"?>"
155                 "<rss version=\"2.0\">"
156                 "<channel>"
157         );
158
159         wc_printf("<title>");
160         escputs(ChrPtr(WC->CurRoom.name));
161         wc_printf("</title>");
162
163         wc_printf("<link>");
164         escputs(ChrPtr(site_prefix));
165         wc_printf("/</link>");
166
167         serv_puts("RINF");
168         serv_getln(buf, sizeof buf);
169         if (buf[0] == '1') {
170                 wc_printf("<description>\r\n");
171                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
172                         escputs(buf);
173                         wc_printf("\r\n");
174                 }
175                 wc_printf("</description>");
176         }
177
178         wc_printf("<image><title>");
179         escputs(ChrPtr(WC->CurRoom.name));
180         wc_printf("</title><url>");
181         escputs(ChrPtr(site_prefix));
182         wc_printf("/image?name=_roompic_?go=");
183         urlescputs(ChrPtr(WC->CurRoom.name));
184         wc_printf("</url><link>");
185         escputs(ChrPtr(site_prefix));
186         wc_printf("/</link></image>\r\n");
187
188         feed_rss_do_room_info_as_description();
189         feed_rss_do_messages();
190
191         wc_printf("</channel>"
192                 "</rss>"
193                 "\r\n\r\n"
194         );
195
196         wDumpContent(0);
197 }
198
199
200 /*
201  * Offer the RSS feed meta tag for this room
202  */
203 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP) 
204 {
205         wcsession *WCC = WC;
206         char feed_link[1024];
207         char encoded_link[1024];
208
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);
212
213         StrBufAppendPrintf(Target,
214                 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
215                 encoded_link
216         );
217 }
218
219
220 /*
221  * Offer the RSS feed button for this room
222  */
223 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP) 
224 {
225         wcsession *WCC = WC;
226         char feed_link[1024];
227         char encoded_link[1024];
228
229         strcpy(feed_link, "/feed_rss?go=");
230         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
231         CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
232
233         StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"/B64%s\">", encoded_link);
234         StrBufAppendPrintf(Target, "<img border=\"0\" src=\"static/rss_16x.png\" alt=\"RSS\">");
235         StrBufAppendPrintf(Target, "</a>");
236 }
237
238
239 void 
240 InitModule_RSS
241 (void)
242 {
243         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
244         RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
245         RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);
246 }