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