removed a bunch of blank comment lines
[citadel.git] / 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, &Stat, NULL);
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 Blog room
51  */
52 void sitemap_do_blog(void) {
53         wcsession *WCC = WC;
54         int num_msgs = 0;
55         int i;
56         SharedMessageStatus Stat;
57         message_summary *Msg = NULL;
58
59         memset(&Stat, 0, sizeof Stat);
60         Stat.maxload = INT_MAX;
61         Stat.lowest_found = (-1);
62         Stat.highest_found = (-1);
63         num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
64         if (num_msgs < 1) return;
65
66         for (i=0; i<num_msgs; ++i) {
67                 Msg = GetMessagePtrAt(i, WCC->summ);
68                 if (Msg != NULL) {
69                         struct bltr bltr = blogview_learn_thread_references(Msg->msgnum);
70                         /* Show only top level posts, not comments */
71                         if ((bltr.id != 0) && (bltr.refs == 0)) {
72                                 WC->bptlid = bltr.id;
73                                 wc_printf("<url><loc>%s", ChrPtr(site_prefix));
74                                 tmplput_blog_permalink(NULL, NULL);
75                                 wc_printf("</loc></url>\r\n");
76                         }
77                 }
78         }
79 }
80
81
82 /*
83  * XML sitemap generator -- go through the message list for a wiki room
84  */
85 void sitemap_do_wiki(void) {
86         wcsession *WCC = WC;
87         int num_msgs = 0;
88         int i;
89         SharedMessageStatus Stat;
90         message_summary *Msg = NULL;
91         char buf[256];
92
93         memset(&Stat, 0, sizeof Stat);
94         Stat.maxload = INT_MAX;
95         Stat.lowest_found = (-1);
96         Stat.highest_found = (-1);
97         num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
98         if (num_msgs < 1) return;
99
100         for (i=0; i<num_msgs; ++i) {
101                 Msg = GetMessagePtrAt(i, WCC->summ);
102                 if (Msg != NULL) {
103
104                         serv_printf("MSG0 %ld|3", Msg->msgnum);
105                         serv_getln(buf, sizeof buf);
106                         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
107                                 if (    (!strncasecmp(buf, "exti=", 5))
108                                         && (!bmstrcasestr(buf, "_HISTORY_"))
109                                 ) {
110                                         wc_printf("<url><loc>%s/wiki", ChrPtr(site_prefix));
111                                         wc_printf("?go=");
112                                         urlescputs(ChrPtr(WC->CurRoom.name));
113                                         wc_printf("?page=%s", &buf[5]);
114                                         wc_printf("</loc></url>\r\n");
115                                 }
116                         }
117                 }
118         }
119 }
120
121
122 struct sitemap_room_list {
123         struct sitemap_room_list *next;
124         StrBuf *roomname;
125         int defview;
126 };
127
128
129 /*
130  * Load the room list for the sitemap
131  */
132 struct sitemap_room_list *sitemap_load_roomlist(void) {
133         char buf[SIZ];
134         char roomname_plain[SIZ];
135         struct sitemap_room_list *roomlist = NULL;
136
137         serv_puts("LKRA");
138         serv_getln(buf, sizeof buf);
139         if (buf[0] == '1') while(serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
140                 struct sitemap_room_list *ptr = malloc(sizeof(struct sitemap_room_list));
141                 extract_token(roomname_plain, buf, 0, '|', sizeof roomname_plain);
142                 ptr->roomname = NewStrBufPlain(roomname_plain, -1);
143                 ptr->defview = extract_int(buf, 6);
144                 ptr->next = roomlist;
145                 roomlist = ptr;
146         }
147
148         return(roomlist);
149 }
150
151
152 /*
153  * Entry point for RSS feed generator
154  */
155 void sitemap(void) {
156         struct sitemap_room_list *roomlist = NULL;
157         output_headers(0, 0, 0, 0, 1, 0);
158         hprintf("Content-type: text/xml\r\n");
159         hprintf(
160                 "Server: %s / %s\r\n"
161                 "Connection: close\r\n"
162         ,
163                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
164         );
165         begin_burst();
166
167         wc_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
168         wc_printf("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n");
169
170         roomlist = sitemap_load_roomlist();
171
172         while (roomlist != NULL)
173         {
174                 gotoroom(roomlist->roomname);
175
176                 /* Output the messages in this room only if it's a room type we can make sense of */
177                 switch(roomlist->defview) {
178                         case VIEW_BBS:
179                                 sitemap_do_bbs();
180                                 break;
181                         case VIEW_WIKI:
182                                 sitemap_do_wiki();
183                                 break;
184                         case VIEW_BLOG:
185                                 sitemap_do_blog();
186                                 break;
187                         default:
188                                 break;
189                 }
190
191                 struct sitemap_room_list *ptr = roomlist;
192                 roomlist = roomlist->next;
193                 FreeStrBuf(&ptr->roomname);
194                 free(ptr);
195         }
196
197         wc_printf("</urlset>\r\n");
198         wDumpContent(0);
199 }
200
201
202 void 
203 InitModule_SITEMAP
204 (void)
205 {
206         WebcitAddUrlHandler(HKEY("sitemap"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
207         WebcitAddUrlHandler(HKEY("sitemap.xml"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
208 }