Added template element THISROOM:FEED:RSS to offer an RSS feed button for a room,...
[citadel.git] / webcit / feed_generator.c
1 /*
2  * RSS/Atom feed generator
3  *
4  * Copyright (c) 2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (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         serv_printf("MSG0 %ld", msgnum);                /* FIXME we want msg4 eventually */
35         serv_getln(buf, sizeof buf);
36         if (buf[0] != '1') return;
37
38         wc_printf("<item>");
39         wc_printf("<link>%s/readfwd?gotofirst=", ChrPtr(site_prefix));
40         urlescputs(ChrPtr(WC->CurRoom.name));
41         wc_printf("?start_reading_at=%ld</link>", msgnum);
42
43         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
44                 if (in_body) {
45                         escputs(buf);
46                         wc_printf("\r\n");
47                 }
48                 else if (!strncasecmp(buf, "subj=", 5)) {
49                         wc_printf("<title>");
50                         escputs(&buf[5]);
51                         wc_printf("</title>");
52                         ++found_title;
53                 }
54                 else if (!strncasecmp(buf, "exti=", 5)) {
55                         wc_printf("<guid>");
56                         escputs(&buf[5]);
57                         wc_printf("</guid>");
58                 }
59                 else if (!strncasecmp(buf, "time=", 5)) {
60                         http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
61                         wc_printf("<pubDate>%s</pubDate>", pubdate);
62                 }
63                 else if (!strncasecmp(buf, "text", 4)) {
64                         if (!found_title) {
65                                 wc_printf("<title>Message #%ld</title>", msgnum);
66                         }
67                         wc_printf("<description>");
68                         in_body = 1;
69                 }
70         }
71
72         if (in_body) {
73                 wc_printf("</description>");
74         }
75
76         wc_printf("</item>");
77 }
78
79 /*
80  * RSS feed generator -- go through the message list
81  */
82 void feed_rss_do_messages(void) {
83         char buf[1024];
84         long *msgs = NULL;
85         int num_msgs = 0;
86         int num_msgs_alloc = 0;
87         int i;
88
89         serv_puts("MSGP text/html|text/plain");         /* identify our preferred mime types */
90         serv_getln(buf, sizeof buf);                    /* don't care about the result */
91
92         serv_puts("MSGS ALL");                          /* ask for all messages in the room */
93         serv_getln(buf, sizeof buf);
94         if (buf[0] != '1') return;
95
96         while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))
97         {
98                 if (num_msgs >= num_msgs_alloc) {
99                         num_msgs_alloc += 1024;
100                         msgs = realloc(msgs, num_msgs_alloc*sizeof(long) );
101                 }
102                 msgs[num_msgs++] = atol(buf);
103         }
104
105         i = num_msgs;                                   /* convention is to feed newest-to-oldest */
106         while (i > 0) {
107                 feed_rss_one_message(msgs[i-1]);
108                 --i;
109         }
110
111         free(msgs);
112
113 }
114
115 /*
116  * Entry point for RSS feed generator
117  */
118 void feed_rss(void) {
119         char buf[1024];
120
121         output_headers(0, 0, 0, 1, 1, 0);
122         hprintf("Content-type: text/xml\r\n");
123         hprintf(
124                 "Server: %s / %s\r\n"
125                 "Connection: close\r\n"
126         ,
127                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
128         );
129         begin_burst();
130
131         wc_printf("<?xml version=\"1.0\"?>"
132                 "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">"
133                 "<channel>"
134         );
135
136         wc_printf("<title>");
137         escputs(ChrPtr(WC->CurRoom.name));
138         wc_printf("</title>");
139
140         wc_printf("<link>");
141         urlescputs(ChrPtr(site_prefix));
142         wc_printf("</link>");
143
144         serv_puts("RINF");
145         serv_getln(buf, sizeof buf);
146         if (buf[0] == '1') {
147                 wc_printf("<description>\r\n");
148                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
149                         escputs(buf);
150                         wc_printf("\r\n");
151                 }
152                 wc_printf("</description>");
153         }
154
155         wc_printf("<image><title>");
156         escputs(ChrPtr(WC->CurRoom.name));
157         wc_printf("</title><url>");
158         urlescputs(ChrPtr(site_prefix));
159         wc_printf("/image?name=_roompic_?gotofirst=");
160         urlescputs(ChrPtr(WC->CurRoom.name));
161         wc_printf("</url><link>");
162         urlescputs(ChrPtr(site_prefix));
163         wc_printf("</link></image>\r\n");
164
165         feed_rss_do_messages();
166
167         wc_printf("</channel>"
168                 "</rss>"
169                 "\r\n\r\n"
170         );
171
172         wDumpContent(0);
173 }
174
175
176 /*
177  * Offer the RSS feed button for this room
178  */
179 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP) 
180 {
181         wcsession *WCC = WC;
182         char feed_link[1024];
183         char encoded_link[1024];
184
185         strcpy(feed_link, "/feed_rss?gotofirst=");
186         urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
187         CtdlEncodeBase64(encoded_link, feed_link, strlen(feed_link), 0);
188
189         StrBufAppendPrintf(Target, "<a href=\"/B64%s\">", encoded_link);
190         StrBufAppendPrintf(Target, "<img border=\"0\" src=\"static/rss_16x.png\">");
191         StrBufAppendPrintf(Target, "</a>");
192 }
193
194
195
196 void 
197 InitModule_RSS
198 (void)
199 {
200         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
201         RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
202 }