Apply fixes suggested by 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         escputs(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  * Entry point for RSS feed generator
129  */
130 void feed_rss(void) {
131         char buf[1024];
132
133         output_headers(0, 0, 0, 0, 1, 0);
134         hprintf("Content-type: text/xml; charset=utf-8\r\n");
135         hprintf(
136                 "Server: %s / %s\r\n"
137                 "Connection: close\r\n"
138         ,
139                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
140         );
141         begin_burst();
142
143         wc_printf("<?xml version=\"1.0\"?>"
144                 "<rss version=\"2.0\">"
145                 "<channel>"
146         );
147
148         wc_printf("<title>");
149         escputs(ChrPtr(WC->CurRoom.name));
150         wc_printf("</title>");
151
152         wc_printf("<link>");
153         escputs(ChrPtr(site_prefix));
154         wc_printf("/</link>");
155
156         serv_puts("RINF");
157         serv_getln(buf, sizeof buf);
158         if (buf[0] == '1') {
159                 wc_printf("<description>\r\n");
160                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
161                         escputs(buf);
162                         wc_printf("\r\n");
163                 }
164                 wc_printf("</description>");
165         }
166
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");
176
177         feed_rss_do_messages();
178
179         wc_printf("</channel>"
180                 "</rss>"
181                 "\r\n\r\n"
182         );
183
184         wDumpContent(0);
185 }
186
187
188 /*
189  * Offer the RSS feed meta tag for this room
190  */
191 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP) 
192 {
193         wcsession *WCC = WC;
194         char feed_link[1024];
195         char encoded_link[1024];
196
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);
200
201         StrBufAppendPrintf(Target,
202                 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
203                 encoded_link
204         );
205 }
206
207
208 /*
209  * Offer the RSS feed button for this room
210  */
211 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP) 
212 {
213         wcsession *WCC = WC;
214         char feed_link[1024];
215         char encoded_link[1024];
216
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);
220
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>");
224 }
225
226
227 void 
228 InitModule_RSS
229 (void)
230 {
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);
234 }