Minor tweaks to sitemap code
[citadel.git] / webcit / sitemap.c
1 /*
2  * XML sitemap generator
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 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  * XML sitemap generator -- go through the message list for a BBS room
27  */
28 void sitemap_do_bbs(void) {
29         wcsession *WCC = WC;
30         int num_msgs = 0;
31         int i;
32         SharedMessageStatus Stat;
33         message_summary *Msg = NULL;
34
35         memset(&Stat, 0, sizeof Stat);
36         Stat.maxload = INT_MAX;
37         Stat.lowest_found = (-1);
38         Stat.highest_found = (-1);
39         num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
40         if (num_msgs < 1) return;
41
42         for (i=0; i<num_msgs; i+=20) {
43                 Msg = GetMessagePtrAt(i, WCC->summ);
44                 if (Msg != NULL) {
45                         wc_printf("<url><loc>%s/readfwd", ChrPtr(site_prefix));
46                         wc_printf("?go=");
47                         urlescputs(ChrPtr(WC->CurRoom.name));
48                         wc_printf("?start_reading_at=%ld", Msg->msgnum);
49                         wc_printf("</loc></url>\r\n");
50                 }
51         }
52 }
53
54
55 /*
56  * XML sitemap generator -- go through the message list for a wiki room
57  */
58 void sitemap_do_wiki(void) {
59         wcsession *WCC = WC;
60         int num_msgs = 0;
61         int i;
62         SharedMessageStatus Stat;
63         message_summary *Msg = NULL;
64         char buf[256];
65
66         memset(&Stat, 0, sizeof Stat);
67         Stat.maxload = INT_MAX;
68         Stat.lowest_found = (-1);
69         Stat.highest_found = (-1);
70         num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
71         if (num_msgs < 1) return;
72
73         for (i=0; i<num_msgs; ++i) {
74                 Msg = GetMessagePtrAt(i, WCC->summ);
75                 if (Msg != NULL) {
76
77                         serv_printf("MSG0 %ld|3", Msg->msgnum);
78                         serv_getln(buf, sizeof buf);
79                         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
80                                 if (    (!strncasecmp(buf, "exti=", 5))
81                                         && (!bmstrcasestr(buf, "_HISTORY_"))
82                                 ) {
83                                         wc_printf("<url><loc>%s/wiki", ChrPtr(site_prefix));
84                                         wc_printf("?go=");
85                                         urlescputs(ChrPtr(WC->CurRoom.name));
86                                         wc_printf("?page=%s", &buf[5]);
87                                         wc_printf("</loc></url>\r\n");
88                                 }
89                         }
90                 }
91         }
92 }
93
94
95 /*
96  * Entry point for RSS feed generator
97  */
98 void sitemap(void) {
99         HashList *roomlist;
100         HashPos *it;
101         long HKlen;
102         const char *HashKey;
103         folder *room;
104
105         output_headers(0, 0, 0, 0, 1, 0);
106         hprintf("Content-type: text/xml\r\n");
107         hprintf(
108                 "Server: %s / %s\r\n"
109                 "Connection: close\r\n"
110         ,
111                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
112         );
113         begin_burst();
114
115         wc_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
116         wc_printf("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n");
117
118         roomlist = GetRoomListHashLKRA(NULL, NULL);
119         it = GetNewHashPos(roomlist, 0);
120
121         while (GetNextHashPos(roomlist, it, &HKlen, &HashKey, (void *)&room))
122         {
123                 gotoroom(room->name);
124
125                 /* Output the messages in this room only if it's a room type we can make sense of */
126                 switch(room->defview) {
127                         case VIEW_BBS:
128                                 sitemap_do_bbs();
129                                 break;
130                         case VIEW_WIKI:
131                                 sitemap_do_wiki();
132                                 break;
133                         default:
134                                 break;
135                 }
136         }
137
138         DeleteHashPos(&it);
139         /* No need to DeleteHash(&roomlist) -- it will be freed when the session closes */
140
141         wc_printf("</urlset>\r\n");
142         wDumpContent(0);
143 }
144
145
146 void 
147 InitModule_SITEMAP
148 (void)
149 {
150         WebcitAddUrlHandler(HKEY("sitemap"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
151         WebcitAddUrlHandler(HKEY("sitemap.xml"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
152 }