indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / sitemap.c
1
2 /*
3  * XML sitemap generator
4  *
5  * Copyright (c) 2010-2021 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "webcit.h"
17 #include "webserver.h"
18
19
20 /*
21  * XML sitemap generator -- go through the message list for a BBS room
22  */
23 void sitemap_do_bbs(void) {
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)
35                 return;
36
37         for (i = 0; i < num_msgs; i += 20) {
38                 Msg = GetMessagePtrAt(i, WC->summ);
39                 if (Msg != NULL) {
40                         wc_printf("<url><loc>%s/readfwd", ChrPtr(site_prefix));
41                         wc_printf("?go=");
42                         urlescputs(ChrPtr(WC->CurRoom.name));
43                         wc_printf("?start_reading_at=%ld", Msg->msgnum);
44                         wc_printf("</loc></url>\r\n");
45                 }
46         }
47 }
48
49
50 /*
51  * XML sitemap generator -- go through the message list for a wiki room
52  */
53 void sitemap_do_wiki(void) {
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)
66                 return;
67
68         for (i = 0; i < num_msgs; ++i) {
69                 Msg = GetMessagePtrAt(i, WC->summ);
70                 if (Msg != NULL) {
71
72                         serv_printf("MSG0 %ld|3", Msg->msgnum);
73                         serv_getln(buf, sizeof buf);
74                         if (buf[0] == '1')
75                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
76                                         if ((!strncasecmp(buf, "exti=", 5))
77                                             && (!bmstrcasestr(buf, "_HISTORY_"))
78                                             ) {
79                                                 wc_printf("<url><loc>%s/wiki", ChrPtr(site_prefix));
80                                                 wc_printf("?go=");
81                                                 urlescputs(ChrPtr(WC->CurRoom.name));
82                                                 wc_printf("?page=%s", &buf[5]);
83                                                 wc_printf("</loc></url>\r\n");
84                                         }
85                                 }
86                 }
87         }
88 }
89
90
91 struct sitemap_room_list {
92         struct sitemap_room_list *next;
93         StrBuf *roomname;
94         int defview;
95 };
96
97
98 /*
99  * Load the room list for the sitemap
100  */
101 struct sitemap_room_list *sitemap_load_roomlist(void) {
102         char buf[SIZ];
103         char roomname_plain[SIZ];
104         struct sitemap_room_list *roomlist = NULL;
105
106         serv_puts("LKRA");
107         serv_getln(buf, sizeof buf);
108         if (buf[0] == '1')
109                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
110                         struct sitemap_room_list *ptr = malloc(sizeof(struct sitemap_room_list));
111                         extract_token(roomname_plain, buf, 0, '|', sizeof roomname_plain);
112                         ptr->roomname = NewStrBufPlain(roomname_plain, -1);
113                         ptr->defview = extract_int(buf, 6);
114                         ptr->next = roomlist;
115                         roomlist = ptr;
116                 }
117
118         return (roomlist);
119 }
120
121 extern void sitemap_do_blog(void);
122
123 /*
124  * Entry point for RSS feed generator
125  */
126 void sitemap(void) {
127         struct sitemap_room_list *roomlist = NULL;
128         output_headers(0, 0, 0, 0, 1, 0);
129         hprintf("Content-type: text/xml\r\n");
130         hprintf("Server: %s / %s\r\n" "Connection: close\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
131             );
132         begin_burst();
133
134         wc_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
135         wc_printf("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n");
136
137         roomlist = sitemap_load_roomlist();
138
139         while (roomlist != NULL) {
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                         sitemap_do_wiki();
151                         break;
152                 case VIEW_BLOG:
153                         sitemap_do_blog();
154                         break;
155                 default:
156                         break;
157                 }
158
159                 ptr = roomlist;
160                 roomlist = roomlist->next;
161                 FreeStrBuf(&ptr->roomname);
162                 free(ptr);
163         }
164
165         wc_printf("</urlset>\r\n");
166         wDumpContent(0);
167 }
168
169
170 void InitModule_SITEMAP(void) {
171         WebcitAddUrlHandler(HKEY("sitemap"), "", 0, sitemap, ANONYMOUS | COOKIEUNNEEDED);
172         WebcitAddUrlHandler(HKEY("sitemap.xml"), "", 0, sitemap, ANONYMOUS | COOKIEUNNEEDED);
173 }