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