indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / feed_generator.c
1
2 /*
3  * RSS feed generator (could be adapted in the future to feed both RSS and Atom)
4  *
5  * Copyright (c) 2010-2012 by the citadel.org team
6  *
7  * This program is open source software.  You can redistribute it and/or
8  * modify it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "webcit.h"
17 #include "webserver.h"
18
19 /*
20  * RSS feed generator -- do one message
21  */
22 void feed_rss_one_message(long msgnum) {
23         int in_body = 0;
24         int in_messagetext = 0;
25         int found_title = 0;
26         int found_guid = 0;
27         char pubdate[128];
28         StrBuf *messagetext = NULL;
29         int is_top_level_post = 1;
30         const char *BufPtr = NULL;
31         StrBuf *Line = NewStrBufPlain(NULL, 1024);
32         char buf[1024];
33         int permalink_hash = 0;
34
35         /* Phase 1: read the message into memory */
36         serv_printf("MSG4 %ld", msgnum);
37         serv_getln(buf, sizeof buf);
38         if (buf[0] != '1')
39                 return;
40         StrBuf *ServerResponse = NewStrBuf();
41         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
42                 StrBufAppendPrintf(ServerResponse, "%s\n", buf);
43         }
44
45         /* Phase 2: help SkyNet become self-aware */
46         BufPtr = NULL;
47         while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr != StrBufNOTNULL) && (BufPtr != NULL))) {
48                 if (in_body) {
49                         /* do nothing */
50                 }
51                 else if (StrLength(Line) == 0) {
52                         ++in_body;
53                 }
54                 else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "wefw=", 5))) {
55                         is_top_level_post = 0;  /* presence of references means it's a reply/comment */
56                 }
57                 else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "msgn=", 5))) {
58                         StrBufCutLeft(Line, 5);
59                         permalink_hash = ThreadIdHash(Line);
60                 }
61         }
62
63         /*
64          * Phase 3: output the message in RSS <item> form
65          * (suppress replies [comments] if this is a blog room)
66          */
67         if ((WC->CurRoom.view != VIEW_BLOG) || (is_top_level_post == 1)) {
68                 wc_printf("<item>");
69                 wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
70                 urlescputs(ChrPtr(WC->CurRoom.name));
71                 if ((WC->CurRoom.view == VIEW_BLOG) && (permalink_hash != 0)) {
72                         wc_printf("?p=%d", permalink_hash);
73                 }
74                 else {
75                         wc_printf("?start_reading_at=%ld", msgnum);
76                 }
77                 wc_printf("</link>");
78
79                 BufPtr = NULL;
80                 in_body = 0;
81                 in_messagetext = 0;
82                 while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr != StrBufNOTNULL) && (BufPtr != NULL))) {
83                         safestrncpy(buf, ChrPtr(Line), sizeof buf);
84
85                         /* XML parsers can be picky; strip out nonprintable header characters */
86                         if ((strlen(buf) >= 6) && (buf[4] == '=')) {
87                                 char *p = &buf[5];
88                                 while (*p) {
89                                         if (!isprint(*p)) {
90                                                 *p = 0;
91                                         }
92                                         ++p;
93                                 }
94                         }
95
96                         /* Now output fields */
97                         if (in_body) {
98                                 if (in_messagetext) {
99                                         StrBufAppendBufPlain(messagetext, buf, -1, 0);
100                                         StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
101                                 }
102                                 else if (IsEmptyStr(buf)) {
103                                         in_messagetext = 1;
104                                 }
105                         }
106                         else if (!strncasecmp(buf, "subj=", 5)) {
107                                 wc_printf("<title>");
108                                 escputs(&buf[5]);
109                                 wc_printf("</title>");
110                                 ++found_title;
111                         }
112                         else if (!strncasecmp(buf, "exti=", 5)) {
113                                 wc_printf("<guid isPermaLink=\"false\">");
114                                 escputs(&buf[5]);
115                                 wc_printf("</guid>");
116                                 ++found_guid;
117                         }
118                         else if (!strncasecmp(buf, "time=", 5)) {
119                                 http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
120                                 wc_printf("<pubDate>%s</pubDate>", pubdate);
121                         }
122                         else if (!strncasecmp(buf, "text", 4)) {
123                                 if (!found_title) {
124                                         wc_printf("<title>Message #%ld</title>", msgnum);
125                                 }
126                                 if (!found_guid) {
127                                         wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
128                                                   msgnum, ChrPtr(WC->serv_info->serv_humannode)
129                                             );
130                                 }
131                                 wc_printf("<description>");
132                                 in_body = 1;
133                                 messagetext = NewStrBuf();
134                         }
135                 }
136
137                 if (in_body) {
138                         cdataout((char *) ChrPtr(messagetext));
139                         FreeStrBuf(&messagetext);
140                         wc_printf("</description>");
141                 }
142
143                 wc_printf("</item>");
144         }
145
146         FreeStrBuf(&Line);
147         FreeStrBuf(&ServerResponse);
148         return;
149 }
150
151
152 /*
153  * RSS feed generator -- go through the message list
154  */
155 void feed_rss_do_messages(void) {
156         wcsession *WCC = WC;
157         int num_msgs = 0;
158         int i;
159         SharedMessageStatus Stat;
160         message_summary *Msg = NULL;
161
162         memset(&Stat, 0, sizeof Stat);
163         Stat.maxload = INT_MAX;
164         Stat.lowest_found = (-1);
165         Stat.highest_found = (-1);
166         num_msgs = load_msg_ptrs("MSGS ALL", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0);
167         if (num_msgs < 1)
168                 return;
169
170         i = num_msgs;           /* convention is to feed newest-to-oldest */
171         while (i > 0) {
172                 Msg = GetMessagePtrAt(i - 1, WCC->summ);
173                 if (Msg != NULL) {
174                         feed_rss_one_message(Msg->msgnum);
175                 }
176                 --i;
177         }
178 }
179
180
181 /*
182  * Output the room info file of the current room as a <description> for the channel
183  */
184 void feed_rss_do_room_info_as_description(void) {
185         wc_printf("<description>");
186         escputs(ChrPtr(WC->CurRoom.name));      /* FIXME use the output of RINF instead */
187         wc_printf("</description>\r\n");
188 }
189
190
191 /*
192  * Entry point for RSS feed generator
193  */
194 void feed_rss(void) {
195         char buf[1024];
196
197         output_headers(0, 0, 0, 0, 1, 0);
198         hprintf("Content-type: text/xml; charset=utf-8\r\n");
199         hprintf("Server: %s / %s\r\n" "Connection: close\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
200             );
201         begin_burst();
202
203         wc_printf("<?xml version=\"1.0\"?>" "<rss version=\"2.0\">" "<channel>");
204
205         wc_printf("<title>");
206         escputs(ChrPtr(WC->CurRoom.name));
207         wc_printf("</title>");
208
209         wc_printf("<link>");
210         escputs(ChrPtr(site_prefix));
211         wc_printf("/</link>");
212
213         serv_puts("RINF");
214         serv_getln(buf, sizeof buf);
215         if (buf[0] == '1') {
216                 wc_printf("<description>\r\n");
217                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
218                         escputs(buf);
219                         wc_printf("\r\n");
220                 }
221                 wc_printf("</description>");
222         }
223
224         wc_printf("<image><title>");
225         escputs(ChrPtr(WC->CurRoom.name));
226         wc_printf("</title><url>");
227         escputs(ChrPtr(site_prefix));
228         wc_printf("/roompic?room=");
229         urlescputs(ChrPtr(WC->CurRoom.name));
230         wc_printf("</url><link>");
231         escputs(ChrPtr(site_prefix));
232         wc_printf("/</link></image>\r\n");
233
234         feed_rss_do_room_info_as_description();
235         feed_rss_do_messages();
236
237         wc_printf("</channel>" "</rss>" "\r\n\r\n");
238
239         wDumpContent(0);
240 }
241
242
243 /*
244  * Offer the RSS feed meta tag for this room
245  */
246 void tmplput_rssmeta(StrBuf * Target, WCTemplputParams * TP) {
247         wcsession *WCC = WC;
248         char feed_link[1024];
249
250         strcpy(feed_link, "/feed_rss?go=");
251         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *) ChrPtr(WCC->CurRoom.name));
252         StrBufAppendPrintf(Target, "<link rel=\"alternate\" title=\"RSS\" href=\"%s\" type=\"application/rss+xml\">", feed_link);
253 }
254
255
256 /*
257  * Offer the RSS feed button for this room
258  */
259 void tmplput_rssbutton(StrBuf * Target, WCTemplputParams * TP) {
260         StrBuf *FeedLink = NULL;
261
262         FeedLink = NewStrBufPlain(HKEY("/feed_rss?go="));
263         StrBufUrlescAppend(FeedLink, WC->CurRoom.name, NULL);
264
265         StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"");
266         StrBufAppendBuf(Target, FeedLink, 0);
267         StrBufAppendPrintf(Target, "\"><img src=\"static/webcit_icons/essen/16x16/rss.png\" alt=\"RSS\">");
268         StrBufAppendPrintf(Target, "</a>");
269         FreeStrBuf(&FeedLink);
270 }
271
272
273 void InitModule_RSS(void) {
274         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS | COOKIEUNNEEDED);
275         RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
276         RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);
277 }