Use MSG4 instead of MSG0 to output RSS feeds
[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         char pubdate[128];
34         StrBuf *messagetext = NULL;
35
36         /* FIXME if this is a blog room we only want to include top-level messages */
37
38         serv_printf("MSG4 %ld", msgnum);
39         serv_getln(buf, sizeof buf);
40         if (buf[0] != '1') return;
41
42         wc_printf("<item>");
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);
46
47         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
48                 if (in_body) {
49                         if (in_messagetext) {
50                                 StrBufAppendBufPlain(messagetext, buf, -1, 0);
51                                 StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
52                         }
53                         else if (IsEmptyStr(buf)) {
54                                 in_messagetext = 1;
55                         }
56                 }
57                 else if (!strncasecmp(buf, "subj=", 5)) {
58                         wc_printf("<title>");
59                         escputs(&buf[5]);
60                         wc_printf("</title>");
61                         ++found_title;
62                 }
63                 else if (!strncasecmp(buf, "exti=", 5)) {
64                         wc_printf("<guid>");
65                         escputs(&buf[5]);
66                         wc_printf("</guid>");
67                 }
68                 else if (!strncasecmp(buf, "time=", 5)) {
69                         http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
70                         wc_printf("<pubDate>%s</pubDate>", pubdate);
71                 }
72                 else if (!strncasecmp(buf, "text", 4)) {
73                         if (!found_title) {
74                                 wc_printf("<title>Message #%ld</title>", msgnum);
75                         }
76                         wc_printf("<description>");
77                         in_body = 1;
78                         messagetext = NewStrBuf();
79                 }
80         }
81
82         if (in_body) {
83                 cdataout((char*)ChrPtr(messagetext));
84                 FreeStrBuf(&messagetext);
85                 wc_printf("</description>");
86         }
87
88         wc_printf("</item>");
89 }
90
91 /*
92  * RSS feed generator -- go through the message list
93  */
94 void feed_rss_do_messages(void) {
95         wcsession *WCC = WC;
96         int num_msgs = 0;
97         int i;
98         SharedMessageStatus Stat;
99         message_summary *Msg = NULL;
100
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;
107
108         i = num_msgs;                                   /* convention is to feed newest-to-oldest */
109         while (i > 0) {
110                 Msg = GetMessagePtrAt(i-1, WCC->summ);
111                 if (Msg != NULL) {
112                         feed_rss_one_message(Msg->msgnum);
113                 }
114                 --i;
115         }
116 }
117
118
119 /*
120  * Entry point for RSS feed generator
121  */
122 void feed_rss(void) {
123         char buf[1024];
124
125         output_headers(0, 0, 0, 0, 1, 0);
126         hprintf("Content-type: text/xml\r\n");
127         hprintf(
128                 "Server: %s / %s\r\n"
129                 "Connection: close\r\n"
130         ,
131                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
132         );
133         begin_burst();
134
135         wc_printf("<?xml version=\"1.0\"?>"
136                 "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">"
137                 "<channel>"
138         );
139
140         wc_printf("<title>");
141         escputs(ChrPtr(WC->CurRoom.name));
142         wc_printf("</title>");
143
144         wc_printf("<link>");
145         urlescputs(ChrPtr(site_prefix));
146         wc_printf("</link>");
147
148         serv_puts("RINF");
149         serv_getln(buf, sizeof buf);
150         if (buf[0] == '1') {
151                 wc_printf("<description>\r\n");
152                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
153                         escputs(buf);
154                         wc_printf("\r\n");
155                 }
156                 wc_printf("</description>");
157         }
158
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");
168
169         feed_rss_do_messages();
170
171         wc_printf("</channel>"
172                 "</rss>"
173                 "\r\n\r\n"
174         );
175
176         wDumpContent(0);
177 }
178
179
180 /*
181  * Offer the RSS feed meta tag for this room
182  */
183 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP) 
184 {
185         wcsession *WCC = WC;
186         char feed_link[1024];
187         char encoded_link[1024];
188
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);
192
193         StrBufAppendPrintf(Target,
194                 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
195                 encoded_link
196         );
197 }
198
199
200 /*
201  * Offer the RSS feed button for this room
202  */
203 void tmplput_rssbutton(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, "<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>");
216 }
217
218
219 void 
220 InitModule_RSS
221 (void)
222 {
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);
226 }