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