Back to index

webcit  8.12-dfsg
sitemap.c
Go to the documentation of this file.
00001 /*
00002  * XML sitemap generator
00003  *
00004  * Copyright (c) 2010-2012 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License, version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "webcit.h"
00016 #include "webserver.h"
00017 
00018 
00019 /*
00020  * XML sitemap generator -- go through the message list for a BBS room
00021  */
00022 void sitemap_do_bbs(void) {
00023        wcsession *WCC = WC;
00024        int num_msgs = 0;
00025        int i;
00026        SharedMessageStatus Stat;
00027        message_summary *Msg = NULL;
00028 
00029        memset(&Stat, 0, sizeof Stat);
00030        Stat.maxload = INT_MAX;
00031        Stat.lowest_found = (-1);
00032        Stat.highest_found = (-1);
00033        num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
00034        if (num_msgs < 1) return;
00035 
00036        for (i=0; i<num_msgs; i+=20) {
00037               Msg = GetMessagePtrAt(i, WCC->summ);
00038               if (Msg != NULL) {
00039                      wc_printf("<url><loc>%s/readfwd", ChrPtr(site_prefix));
00040                      wc_printf("?go=");
00041                      urlescputs(ChrPtr(WC->CurRoom.name));
00042                      wc_printf("?start_reading_at=%ld", Msg->msgnum);
00043                      wc_printf("</loc></url>\r\n");
00044               }
00045        }
00046 }
00047 
00048 
00049 /*
00050  * XML sitemap generator -- go through the message list for a Blog room
00051  */
00052 void sitemap_do_blog(void) {
00053        wcsession *WCC = WC;
00054        int num_msgs = 0;
00055        int i;
00056        SharedMessageStatus Stat;
00057        message_summary *Msg = NULL;
00058 
00059        memset(&Stat, 0, sizeof Stat);
00060        Stat.maxload = INT_MAX;
00061        Stat.lowest_found = (-1);
00062        Stat.highest_found = (-1);
00063        num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
00064        if (num_msgs < 1) return;
00065 
00066        for (i=0; i<num_msgs; ++i) {
00067               Msg = GetMessagePtrAt(i, WCC->summ);
00068               if (Msg != NULL) {
00069                      struct bltr bltr = blogview_learn_thread_references(Msg->msgnum);
00070                      /* Show only top level posts, not comments */
00071                      if ((bltr.id != 0) && (bltr.refs == 0)) {
00072                             WC->bptlid = bltr.id;
00073                             wc_printf("<url><loc>%s", ChrPtr(site_prefix));
00074                             tmplput_blog_permalink(NULL, NULL);
00075                             wc_printf("</loc></url>\r\n");
00076                      }
00077               }
00078        }
00079 }
00080 
00081 
00082 /*
00083  * XML sitemap generator -- go through the message list for a wiki room
00084  */
00085 void sitemap_do_wiki(void) {
00086        wcsession *WCC = WC;
00087        int num_msgs = 0;
00088        int i;
00089        SharedMessageStatus Stat;
00090        message_summary *Msg = NULL;
00091        char buf[256];
00092 
00093        memset(&Stat, 0, sizeof Stat);
00094        Stat.maxload = INT_MAX;
00095        Stat.lowest_found = (-1);
00096        Stat.highest_found = (-1);
00097        num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
00098        if (num_msgs < 1) return;
00099 
00100        for (i=0; i<num_msgs; ++i) {
00101               Msg = GetMessagePtrAt(i, WCC->summ);
00102               if (Msg != NULL) {
00103 
00104                      serv_printf("MSG0 %ld|3", Msg->msgnum);
00105                      serv_getln(buf, sizeof buf);
00106                      if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00107                             if (   (!strncasecmp(buf, "exti=", 5))
00108                                    && (!bmstrcasestr(buf, "_HISTORY_"))
00109                             ) {
00110                                    wc_printf("<url><loc>%s/wiki", ChrPtr(site_prefix));
00111                                    wc_printf("?go=");
00112                                    urlescputs(ChrPtr(WC->CurRoom.name));
00113                                    wc_printf("?page=%s", &buf[5]);
00114                                    wc_printf("</loc></url>\r\n");
00115                             }
00116                      }
00117               }
00118        }
00119 }
00120 
00121 
00122 struct sitemap_room_list {
00123        struct sitemap_room_list *next;
00124        StrBuf *roomname;
00125        int defview;
00126 };
00127 
00128 
00129 /*
00130  * Load the room list for the sitemap
00131  */
00132 struct sitemap_room_list *sitemap_load_roomlist(void) {
00133        char buf[SIZ];
00134        char roomname_plain[SIZ];
00135        struct sitemap_room_list *roomlist = NULL;
00136 
00137        serv_puts("LKRA");
00138        serv_getln(buf, sizeof buf);
00139        if (buf[0] == '1') while(serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00140               struct sitemap_room_list *ptr = malloc(sizeof(struct sitemap_room_list));
00141               extract_token(roomname_plain, buf, 0, '|', sizeof roomname_plain);
00142               ptr->roomname = NewStrBufPlain(roomname_plain, -1);
00143               ptr->defview = extract_int(buf, 6);
00144               ptr->next = roomlist;
00145               roomlist = ptr;
00146        }
00147 
00148        return(roomlist);
00149 }
00150 
00151 
00152 /*
00153  * Entry point for RSS feed generator
00154  */
00155 void sitemap(void) {
00156        struct sitemap_room_list *roomlist = NULL;
00157        output_headers(0, 0, 0, 0, 1, 0);
00158        hprintf("Content-type: text/xml\r\n");
00159        hprintf(
00160               "Server: %s / %s\r\n"
00161               "Connection: close\r\n"
00162        ,
00163               PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
00164        );
00165        begin_burst();
00166 
00167        wc_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
00168        wc_printf("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n");
00169 
00170        roomlist = sitemap_load_roomlist();
00171 
00172        while (roomlist != NULL)
00173        {
00174               gotoroom(roomlist->roomname);
00175 
00176               /* Output the messages in this room only if it's a room type we can make sense of */
00177               switch(roomlist->defview) {
00178                      case VIEW_BBS:
00179                             sitemap_do_bbs();
00180                             break;
00181                      case VIEW_WIKI:
00182                             sitemap_do_wiki();
00183                             break;
00184                      case VIEW_BLOG:
00185                             sitemap_do_blog();
00186                             break;
00187                      default:
00188                             break;
00189               }
00190 
00191               struct sitemap_room_list *ptr = roomlist;
00192               roomlist = roomlist->next;
00193               FreeStrBuf(&ptr->roomname);
00194               free(ptr);
00195        }
00196 
00197        wc_printf("</urlset>\r\n");
00198        wDumpContent(0);
00199 }
00200 
00201 
00202 void 
00203 InitModule_SITEMAP
00204 (void)
00205 {
00206        WebcitAddUrlHandler(HKEY("sitemap"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
00207        WebcitAddUrlHandler(HKEY("sitemap.xml"), "", 0, sitemap, ANONYMOUS|COOKIEUNNEEDED);
00208 }