feed_generator.c: buffer message text into a StrBuf so we can output it as a single...
[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 found_title = 0;
32         char pubdate[128];
33         StrBuf *messagetext = NULL;
34
35         /* FIXME if this is a blog room we only want to include top-level messages */
36
37         serv_printf("MSG0 %ld", msgnum);                /* FIXME we want msg4 eventually */
38         serv_getln(buf, sizeof buf);
39         if (buf[0] != '1') return;
40
41         wc_printf("<item>");
42         wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
43         urlescputs(ChrPtr(WC->CurRoom.name));
44         wc_printf("?start_reading_at=%ld</link>", msgnum);
45
46         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
47                 if (in_body) {
48                         StrBufAppendBufPlain(messagetext, buf, -1, 0);
49                         StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
50                 }
51                 else if (!strncasecmp(buf, "subj=", 5)) {
52                         wc_printf("<title>");
53                         escputs(&buf[5]);
54                         wc_printf("</title>");
55                         ++found_title;
56                 }
57                 else if (!strncasecmp(buf, "exti=", 5)) {
58                         wc_printf("<guid>");
59                         escputs(&buf[5]);
60                         wc_printf("</guid>");
61                 }
62                 else if (!strncasecmp(buf, "time=", 5)) {
63                         http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
64                         wc_printf("<pubDate>%s</pubDate>", pubdate);
65                 }
66                 else if (!strncasecmp(buf, "text", 4)) {
67                         if (!found_title) {
68                                 wc_printf("<title>Message #%ld</title>", msgnum);
69                         }
70                         wc_printf("<description>");
71                         in_body = 1;
72                         messagetext = NewStrBuf();
73                 }
74         }
75
76         if (in_body) {
77                 cdataout((char*)ChrPtr(messagetext));
78                 FreeStrBuf(&messagetext);
79                 wc_printf("</description>");
80         }
81
82         wc_printf("</item>");
83 }
84
85 /*
86  * RSS feed generator -- go through the message list
87  */
88 void feed_rss_do_messages(void) {
89         wcsession *WCC = WC;
90         int num_msgs = 0;
91         int i;
92         SharedMessageStatus Stat;
93         message_summary *Msg = NULL;
94
95         memset(&Stat, 0, sizeof Stat);
96         Stat.maxload = INT_MAX;
97         Stat.lowest_found = (-1);
98         Stat.highest_found = (-1);
99         num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
100         if (num_msgs < 1) return;
101
102         i = num_msgs;                                   /* convention is to feed newest-to-oldest */
103         while (i > 0) {
104                 Msg = GetMessagePtrAt(i-1, WCC->summ);
105                 if (Msg != NULL) {
106                         feed_rss_one_message(Msg->msgnum);
107                 }
108                 --i;
109         }
110 }
111
112
113 /*
114  * Entry point for RSS feed generator
115  */
116 void feed_rss(void) {
117         char buf[1024];
118
119         output_headers(0, 0, 0, 0, 1, 0);
120         hprintf("Content-type: text/xml\r\n");
121         hprintf(
122                 "Server: %s / %s\r\n"
123                 "Connection: close\r\n"
124         ,
125                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
126         );
127         begin_burst();
128
129         wc_printf("<?xml version=\"1.0\"?>"
130                 "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">"
131                 "<channel>"
132         );
133
134         wc_printf("<title>");
135         escputs(ChrPtr(WC->CurRoom.name));
136         wc_printf("</title>");
137
138         wc_printf("<link>");
139         urlescputs(ChrPtr(site_prefix));
140         wc_printf("</link>");
141
142         serv_puts("RINF");
143         serv_getln(buf, sizeof buf);
144         if (buf[0] == '1') {
145                 wc_printf("<description>\r\n");
146                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
147                         escputs(buf);
148                         wc_printf("\r\n");
149                 }
150                 wc_printf("</description>");
151         }
152
153         wc_printf("<image><title>");
154         escputs(ChrPtr(WC->CurRoom.name));
155         wc_printf("</title><url>");
156         urlescputs(ChrPtr(site_prefix));
157         wc_printf("/image?name=_roompic_?go=");
158         urlescputs(ChrPtr(WC->CurRoom.name));
159         wc_printf("</url><link>");
160         urlescputs(ChrPtr(site_prefix));
161         wc_printf("</link></image>\r\n");
162
163         feed_rss_do_messages();
164
165         wc_printf("</channel>"
166                 "</rss>"
167                 "\r\n\r\n"
168         );
169
170         wDumpContent(0);
171 }
172
173
174 /*
175  * Offer the RSS feed meta tag for this room
176  */
177 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP) 
178 {
179         wcsession *WCC = WC;
180         char feed_link[1024];
181         char encoded_link[1024];
182
183         strcpy(feed_link, "/feed_rss?go=");
184         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
185         CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
186
187         StrBufAppendPrintf(Target,
188                 "<link rel=\"alternate\" title=\"RSS\" href=\"/B64%s\" type=\"application/rss+xml\">",
189                 encoded_link
190         );
191 }
192
193
194 /*
195  * Offer the RSS feed button for this room
196  */
197 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP) 
198 {
199         wcsession *WCC = WC;
200         char feed_link[1024];
201         char encoded_link[1024];
202
203         strcpy(feed_link, "/feed_rss?go=");
204         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
205         CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
206
207         StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"/B64%s\">", encoded_link);
208         StrBufAppendPrintf(Target, "<img border=\"0\" src=\"static/rss_16x.png\" alt=\"RSS\">");
209         StrBufAppendPrintf(Target, "</a>");
210 }
211
212
213 void 
214 InitModule_RSS
215 (void)
216 {
217         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
218         RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
219         RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);
220 }