Back to index

webcit  8.12-dfsg
feed_generator.c
Go to the documentation of this file.
00001 /*
00002  * RSS feed generator (could be adapted in the future to feed both RSS and Atom)
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
00007  * modify 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  * RSS feed generator -- do one message
00020  */
00021 void feed_rss_one_message(long msgnum) {
00022        int in_body = 0;
00023        int in_messagetext = 0;
00024        int found_title = 0;
00025        int found_guid = 0;
00026        char pubdate[128];
00027        StrBuf *messagetext = NULL;
00028        int is_top_level_post = 1;
00029        const char *BufPtr = NULL;
00030        StrBuf *Line = NewStrBufPlain(NULL, 1024);
00031        char buf[1024];
00032        int permalink_hash = 0;
00033 
00034        /* Phase 1: read the message into memory */
00035        serv_printf("MSG4 %ld", msgnum);
00036        serv_getln(buf, sizeof buf);
00037        if (buf[0] != '1') return;
00038        StrBuf *ServerResponse = NewStrBuf();
00039        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00040               StrBufAppendPrintf(ServerResponse, "%s\n", buf);
00041        }
00042 
00043        /* Phase 2: help SkyNet become self-aware */
00044        BufPtr = NULL;
00045        while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
00046               if (in_body) {
00047                      /* do nothing */
00048               }
00049               else if (StrLength(Line) == 0) {
00050                      ++in_body;
00051               }
00052               else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "wefw=", 5))) {
00053                      is_top_level_post = 0;      /* presence of references means it's a reply/comment */
00054               }
00055               else if ((StrLength(Line) > 5) && (!strncasecmp(ChrPtr(Line), "msgn=", 5))) {
00056                      StrBufCutLeft(Line, 5);
00057                      permalink_hash = ThreadIdHash(Line);
00058               }
00059        }
00060 
00061        /*
00062         * Phase 3: output the message in RSS <item> form
00063         * (suppress replies [comments] if this is a blog room)
00064         */
00065        if ( (WC->CurRoom.view != VIEW_BLOG) || (is_top_level_post == 1) ) {
00066               wc_printf("<item>");
00067               wc_printf("<link>%s/readfwd?go=", ChrPtr(site_prefix));
00068               urlescputs(ChrPtr(WC->CurRoom.name));
00069               if ((WC->CurRoom.view == VIEW_BLOG) && (permalink_hash != 0)) {
00070                      wc_printf("?p=%d", permalink_hash);
00071               }
00072               else {
00073                      wc_printf("?start_reading_at=%ld", msgnum);
00074               }
00075               wc_printf("</link>");
00076        
00077               BufPtr = NULL;
00078               in_body = 0;
00079               in_messagetext = 0;
00080               while (StrBufSipLine(Line, ServerResponse, &BufPtr), ((BufPtr!=StrBufNOTNULL)&&(BufPtr!=NULL)) ) {
00081                      safestrncpy(buf, ChrPtr(Line), sizeof buf);
00082 
00083                      /* XML parsers can be picky; strip out nonprintable header characters */
00084                      if ((strlen(buf)>=6) && (buf[4]=='=')) {
00085                             char *p = &buf[5];
00086                             while (*p) {
00087                                    if (!isprint(*p)) {
00088                                           *p = 0;
00089                                    }
00090                                    ++p;
00091                             }
00092                      }
00093 
00094                      /* Now output fields */
00095                      if (in_body) {
00096                             if (in_messagetext) {
00097                                    StrBufAppendBufPlain(messagetext, buf, -1, 0);
00098                                    StrBufAppendBufPlain(messagetext, HKEY("\r\n"), 0);
00099                             }
00100                             else if (IsEmptyStr(buf)) {
00101                                    in_messagetext = 1;
00102                             }
00103                      }
00104                      else if (!strncasecmp(buf, "subj=", 5)) {
00105                             wc_printf("<title>");
00106                             escputs(&buf[5]);
00107                             wc_printf("</title>");
00108                             ++found_title;
00109                      }
00110                      else if (!strncasecmp(buf, "exti=", 5)) {
00111                             wc_printf("<guid isPermaLink=\"false\">");
00112                             escputs(&buf[5]);
00113                             wc_printf("</guid>");
00114                             ++found_guid;
00115                      }
00116                      else if (!strncasecmp(buf, "time=", 5)) {
00117                             http_datestring(pubdate, sizeof pubdate, atol(&buf[5]));
00118                             wc_printf("<pubDate>%s</pubDate>", pubdate);
00119                      }
00120                      else if (!strncasecmp(buf, "text", 4)) {
00121                             if (!found_title) {
00122                                    wc_printf("<title>Message #%ld</title>", msgnum);
00123                             }
00124                             if (!found_guid) {
00125                                    wc_printf("<guid isPermaLink=\"false\">%ld@%s</guid>",
00126                                           msgnum,
00127                                           ChrPtr(WC->serv_info->serv_humannode)
00128                                    );
00129                             }
00130                             wc_printf("<description>");
00131                             in_body = 1;
00132                             messagetext = NewStrBuf();
00133                      }
00134               }
00135        
00136               if (in_body) {
00137                      cdataout((char*)ChrPtr(messagetext));
00138                      FreeStrBuf(&messagetext);
00139                      wc_printf("</description>");
00140               }
00141 
00142               wc_printf("</item>");
00143        }
00144 
00145        FreeStrBuf(&Line);
00146        FreeStrBuf(&ServerResponse);
00147        return;
00148 }
00149 
00150 
00151 /*
00152  * RSS feed generator -- go through the message list
00153  */
00154 void feed_rss_do_messages(void) {
00155        wcsession *WCC = WC;
00156        int num_msgs = 0;
00157        int i;
00158        SharedMessageStatus Stat;
00159        message_summary *Msg = NULL;
00160 
00161        memset(&Stat, 0, sizeof Stat);
00162        Stat.maxload = INT_MAX;
00163        Stat.lowest_found = (-1);
00164        Stat.highest_found = (-1);
00165        num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
00166        if (num_msgs < 1) return;
00167 
00168        i = num_msgs;                             /* convention is to feed newest-to-oldest */
00169        while (i > 0) {
00170               Msg = GetMessagePtrAt(i-1, WCC->summ);
00171               if (Msg != NULL) {
00172                      feed_rss_one_message(Msg->msgnum);
00173               }
00174               --i;
00175        }
00176 }
00177 
00178 
00179 /*
00180  * Output the room info file of the current room as a <description> for the channel
00181  */
00182 void feed_rss_do_room_info_as_description(void)
00183 {
00184        wc_printf("<description>");
00185        escputs(ChrPtr(WC->CurRoom.name)); /* FIXME use the output of RINF instead */
00186        wc_printf("</description>\r\n");
00187 }
00188 
00189 
00190 /*
00191  * Entry point for RSS feed generator
00192  */
00193 void feed_rss(void) {
00194        char buf[1024];
00195 
00196        output_headers(0, 0, 0, 0, 1, 0);
00197        hprintf("Content-type: text/xml; charset=utf-8\r\n");
00198        hprintf(
00199               "Server: %s / %s\r\n"
00200               "Connection: close\r\n"
00201        ,
00202               PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
00203        );
00204        begin_burst();
00205 
00206        wc_printf("<?xml version=\"1.0\"?>"
00207               "<rss version=\"2.0\">"
00208               "<channel>"
00209        );
00210 
00211        wc_printf("<title>");
00212        escputs(ChrPtr(WC->CurRoom.name));
00213        wc_printf("</title>");
00214 
00215        wc_printf("<link>");
00216        escputs(ChrPtr(site_prefix));
00217        wc_printf("/</link>");
00218 
00219        serv_puts("RINF");
00220        serv_getln(buf, sizeof buf);
00221        if (buf[0] == '1') {
00222               wc_printf("<description>\r\n");
00223               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00224                      escputs(buf);
00225                      wc_printf("\r\n");
00226               }
00227               wc_printf("</description>");
00228        }
00229 
00230        wc_printf("<image><title>");
00231        escputs(ChrPtr(WC->CurRoom.name));
00232        wc_printf("</title><url>");
00233        escputs(ChrPtr(site_prefix));
00234        wc_printf("/image?name=_roompic_?go=");
00235        urlescputs(ChrPtr(WC->CurRoom.name));
00236        wc_printf("</url><link>");
00237        escputs(ChrPtr(site_prefix));
00238        wc_printf("/</link></image>\r\n");
00239 
00240        feed_rss_do_room_info_as_description();
00241        feed_rss_do_messages();
00242 
00243        wc_printf("</channel>"
00244               "</rss>"
00245               "\r\n\r\n"
00246        );
00247 
00248        wDumpContent(0);
00249 }
00250 
00251 
00252 /*
00253  * Offer the RSS feed meta tag for this room
00254  */
00255 void tmplput_rssmeta(StrBuf *Target, WCTemplputParams *TP) 
00256 {
00257        wcsession *WCC = WC;
00258        char feed_link[1024];
00259 
00260        strcpy(feed_link, "/feed_rss?go=");
00261        urlesc(&feed_link[20], sizeof(feed_link) - 20, (char *)ChrPtr(WCC->CurRoom.name) );
00262        StrBufAppendPrintf(Target,
00263               "<link rel=\"alternate\" title=\"RSS\" href=\"%s\" type=\"application/rss+xml\">",
00264               feed_link
00265        );
00266 }
00267 
00268 
00269 /*
00270  * Offer the RSS feed button for this room
00271  */
00272 void tmplput_rssbutton(StrBuf *Target, WCTemplputParams *TP) 
00273 {
00274        StrBuf *FeedLink = NULL;
00275 
00276        FeedLink = NewStrBufPlain(HKEY("/feed_rss?go="));
00277        StrBufUrlescAppend(FeedLink, WC->CurRoom.name, NULL);
00278 
00279        StrBufAppendPrintf(Target, "<a type=\"application/rss+xml\" href=\"");
00280        StrBufAppendBuf(Target, FeedLink, 0);
00281        StrBufAppendPrintf(Target, "\"><img src=\"static/webcit_icons/essen/16x16/rss.png\" alt=\"RSS\">");
00282        StrBufAppendPrintf(Target, "</a>");
00283        FreeStrBuf(&FeedLink);
00284 }
00285 
00286 
00287 void 
00288 InitModule_RSS
00289 (void)
00290 {
00291        WebcitAddUrlHandler(HKEY("feed_rss"), "", 0, feed_rss, ANONYMOUS|COOKIEUNNEEDED);
00292        RegisterNamespace("THISROOM:FEED:RSS", 0, 0, tmplput_rssbutton, NULL, CTX_NONE);
00293        RegisterNamespace("THISROOM:FEED:RSSMETA", 0, 0, tmplput_rssmeta, NULL, CTX_NONE);
00294 }