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