More work on the RSS feed generator. The fetch loop is in place, now to just tune...
[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         wc_printf("<item>");
30         wc_printf("<title>title %ld title</title>", msgnum);
31         wc_printf("<pubDate>Wed, 08 Sep 2010 20:03:21 GMT</pubDate>");
32         wc_printf("<link>http://xxxxx.xxxx.xxxxxx.xxxx.xxx</link>");
33         wc_printf("<description>&#60;b&#62;foo bar baz:&#60;/b&#62; message %ld</description>", msgnum);
34         wc_printf("<guid>xxxx-xxxx-xxxx-xxxx-xxxx-xxxx</guid>");
35         wc_printf("</item>");
36 }
37
38 /*
39  * RSS feed generator -- go through the message list
40  */
41 void feed_rss_do_messages(void) {
42         char buf[1024];
43         long *msgs = NULL;
44         int num_msgs = 0;
45         int num_msgs_alloc = 0;
46         int i;
47
48         serv_puts("MSGS ALL");
49         serv_getln(buf, sizeof buf);
50         if (buf[0] != '1') return;
51
52         while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))
53         {
54                 if (num_msgs >= num_msgs_alloc) {
55                         num_msgs_alloc += 1024;
56                         msgs = realloc(msgs, num_msgs_alloc*sizeof(long) );
57                 }
58                 msgs[num_msgs++] = atol(buf);
59         }
60
61         i = num_msgs;
62         while (i > 0) {
63                 feed_rss_one_message(msgs[i-1]);
64                 --i;
65         }
66
67         free(msgs);
68
69 }
70
71 /*
72  * Entry point for RSS feed generator
73  */
74 void feed_rss(void) {
75
76         output_headers(0, 0, 0, 1, 1, 0);
77         hprintf("Content-type: text/xml\r\n");
78         hprintf(
79                 "Server: %s / %s\r\n"
80                 "Connection: close\r\n"
81         ,
82                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
83         );
84         begin_burst();
85
86         wc_printf("<?xml version=\"1.0\"?>"
87                 "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">"
88                 "<channel>"
89         );
90
91         wc_printf("<title>");
92         escputs(ChrPtr(WC->CurRoom.name));
93         wc_printf("</title>");
94
95         wc_printf("<link>");
96         urlescputs(ChrPtr(site_prefix));
97         wc_printf("</link>");
98
99         //      <language>en-us</language>
100         //      <description>Linux Today News Service</description>
101         //      <atom:link href="http://linuxtoday.com/biglt.rss" rel="self" type="application/rss+xml" />
102
103         //    <image>
104         //      <title>Linux Today</title>
105         //      <url>http://linuxtoday.com/pics/ltnet.png</url>
106         //      <link>http://linuxtoday.com</link>
107         //    </image>
108
109         feed_rss_do_messages();
110
111         wc_printf("</channel>"
112                 "</rss>"
113                 "\r\n\r\n"
114         );
115
116         wDumpContent(0);
117         end_webcit_session();
118 }
119
120
121 void 
122 InitModule_RSS
123 (void)
124 {
125         WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
126 }