Remove APPDIR support in database_cleanup.sh
[citadel] / webcit / sitemap.c
1 /*
2  * XML sitemap generator
3  *
4  * Copyright (c) 2010-2012 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, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17
18
19 /*
20  * XML sitemap generator -- go through the message list for a BBS room
21  */
22 void sitemap_do_bbs(void) {
23         wcsession *WCC = WC;
24         int num_msgs = 0;
25         int i;
26         SharedMessageStatus Stat;
27         message_summary *Msg = NULL;
28
29         memset(&Stat, 0, sizeof Stat);
30         Stat.maxload = INT_MAX;
31         Stat.lowest_found = (-1);
32         Stat.highest_found = (-1);
33         num_msgs = load_msg_ptrs("MSGS ALL", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0);
34         if (num_msgs < 1) return;
35
36         for (i=0; i<num_msgs; i+=20) {
37                 Msg = GetMessagePtrAt(i, WCC->summ);
38                 if (Msg != NULL) {
39                         wc_printf("<url><loc>%s/readfwd", ChrPtr(site_prefix));
40                         wc_printf("?go=");
41                         urlescputs(ChrPtr(WC->CurRoom.name));
42                         wc_printf("?start_reading_at=%ld", Msg->msgnum);
43                         wc_printf("</loc></url>\r\n");
44                 }
45         }
46 }
47
48
49 /*
50  * XML sitemap generator -- go through the message list for a wiki room
51  */
52 void sitemap_do_wiki(void) {
53         wcsession *WCC = WC;
54         int num_msgs = 0;
55         int i;
56         SharedMessageStatus Stat;
57         message_summary *Msg = NULL;
58         char buf[256];
59
60         memset(&Stat, 0, sizeof Stat);
61         Stat.maxload = INT_MAX;
62         Stat.lowest_found = (-1);
63         Stat.highest_found = (-1);
64         num_msgs = load_msg_ptrs("MSGS ALL", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0);
65         if (num_msgs < 1) return;
66
67         for (i=0; i<num_msgs; ++i) {
68                 Msg = GetMessagePtrAt(i, WCC->summ);
69                 if (Msg != NULL) {
70
71                         serv_printf("MSG0 %ld|3", Msg->msgnum);
72                         serv_getln(buf, sizeof buf);
73                         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
74                                 if (    (!strncasecmp(buf, "exti=", 5))
75                                         && (!bmstrcasestr(buf, "_HISTORY_"))
76                                 ) {
77                                         wc_printf("<url><loc>%s/wiki", ChrPtr(site_prefix));
78                                         wc_printf("?go=");
79                                         urlescputs(ChrPtr(WC->CurRoom.name));
80                                         wc_printf("?page=%s", &buf[5]);
81                                         wc_printf("</loc></url>\r\n");
82                                 }
83                         }
84                 }
85         }
86 }
87
88
89 struct sitemap_room_list {
90         struct sitemap_room_list *next;
91         StrBuf *roomname;
92         int defview;
93 };
94
95
96 /*
97  * Load the room list for the sitemap
98  */
99 struct sitemap_room_list *sitemap_load_roomlist(void) {
100         char buf[SIZ];
101         char roomname_plain[SIZ];
102         struct sitemap_room_list *roomlist = NULL;
103
104         serv_puts("LKRA");
105         serv_getln(buf, sizeof buf);
106         if (buf[0] == '1') while(serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
107                 struct sitemap_room_list *ptr = malloc(sizeof(struct sitemap_room_list));
108                 extract_token(roomname_plain, buf, 0, '|', sizeof roomname_plain);
109                 ptr->roomname = NewStrBufPlain(roomname_plain, -1);
110                 ptr->defview = extract_int(buf, 6);
111                 ptr->next = roomlist;
112                 roomlist = ptr;
113         }
114
115         return(roomlist);
116 }
117
118 extern void sitemap_do_blog(void);
119
120 /*
121  * Entry point for RSS feed generator
122  */
123 void sitemap(void) {
124         struct sitemap_room_list *roomlist = NULL;
125         output_headers(0, 0, 0, 0, 1, 0);
126         hprintf("Content-type: text/xml\r\n");
127         hprintf(
128                 "Server: %s / %s\r\n"
129                 "Connection: close\r\n"
130         ,
131                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
132         );
133         begin_burst();
134
135         wc_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
136         wc_printf("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n");
137
138         roomlist = sitemap_load_roomlist();
139
140         while (roomlist != NULL)
141         {
142                 struct sitemap_room_list *ptr;
143
144                 gotoroom(roomlist->roomname);
145
146                 /* Output the messages in this room only if it's a room type we can make sense of */
147                 switch(roomlist->defview) {
148                 case VIEW_BBS:
149                         sitemap_do_bbs();
150                         break;
151                 case VIEW_WIKI:
152                 case VIEW_WIKIMD:
153                         sitemap_do_wiki();
154                         break;
155                 case VIEW_BLOG:
156                         sitemap_do_blog();
157                         break;
158                 default:
159                         break;
160                 }
161
162                 ptr = roomlist;
163                 roomlist = roomlist->next;
164                 FreeStrBuf(&ptr->roomname);
165                 free(ptr);
166         }
167
168         wc_printf("</urlset>\r\n");
169         wDumpContent(0);
170 }
171
172
173 void 
174 InitModule_SITEMAP
175 (void)
176 {
177         WebcitAddUrlHandler(HKEY("sitemap"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
178         WebcitAddUrlHandler(HKEY("sitemap.xml"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
179 }