* Applied matt's patch to have it show a loading graphic for ajax operations
[citadel.git] / webcit / html2html.c
index 110d9682e31d937ffa35a47ee621b75f860431eb..abd5904e846a1577fa6c2af0c5a2d3fa3e72eed6 100644 (file)
@@ -1,50 +1,83 @@
 /*
  * $Id$
- *
- * Output an HTML message, modifying it slightly to make sure it plays nice
+ */
+/**
+ * \defgroup HTML2HTML Output an HTML message, modifying it slightly to make sure it plays nice
  * with the rest of our web framework.
+ * \ingroup WebcitHttpServer
+ */
+/*@{*/
+#include "webcit.h"
+#include "vcard.h"
+#include "webserver.h"
+
+
+/**
+ * \brief      Strip surrounding single or double quotes from a string.
  *
+ * \param s    String to be stripped.
  */
+void stripquotes(char *s)
+{
+       int len;
 
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <limits.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
+       if (!s) return;
 
-#ifdef HAVE_ICONV
-#include <iconv.h>
-#endif
+       len = strlen(s);
+       if (len < 2) return;
 
-#include "webcit.h"
-#include "vcard.h"
-#include "webserver.h"
+       if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
+               s[len-1] = 0;
+               strcpy(s, &s[1]);
+       }
+}
 
 
-/*
- * Sanitize and enhance an HTML message for display.
- * Also convert weird character sets to UTF-8 if necessary.
+/**
+ * \brief Check to see if a META tag has overridden the declared MIME character set.
+ *
+ * \param charset              Character set name (left unchanged if we don't do anything)
+ * \param meta_http_equiv      Content of the "http-equiv" portion of the META tag
+ * \param meta_content         Content of the "content" portion of the META tag
  */
-void output_html(char *charset) {
+void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
+{
+       char *ptr;
+       char buf[64];
+
+       if (!charset) return;
+       if (!meta_http_equiv) return;
+       if (!meta_content) return;
+
+
+       if (strcasecmp(meta_http_equiv, "Content-type")) return;
+
+       ptr = strchr(meta_content, ';');
+       if (!ptr) return;
+
+       safestrncpy(buf, ++ptr, sizeof buf);
+       striplt(buf);
+       if (!strncasecmp(buf, "charset=", 8)) {
+               strcpy(charset, &buf[8]);
+       }
+}
+
+
+
+/**
+ * \brief Sanitize and enhance an HTML message for display.
+ *        Also convert weird character sets to UTF-8 if necessary.
+ *
+ * \param supplied_charset the input charset as declared in the MIME headers
+ */
+void output_html(char *supplied_charset, int treat_as_wiki) {
        char buf[SIZ];
        char *msg;
        char *ptr;
        char *msgstart;
        char *msgend;
        char *converted_msg;
+       size_t converted_alloc = 0;
        int buffer_length = 1;
        int line_length = 0;
        int content_length = 0;
@@ -54,71 +87,103 @@ void output_html(char *charset) {
        int alevel = 0;
        int i;
        int linklen;
+       char charset[128];
 #ifdef HAVE_ICONV
        iconv_t ic = (iconv_t)(-1) ;
-       char *ibuf;                   /* Buffer of characters to be converted */
-       char *obuf;                   /* Buffer for converted characters      */
-       size_t ibuflen;               /* Length of input buffer               */
-       size_t obuflen;               /* Length of output buffer              */
-       char *osav;                   /* Saved pointer to output buffer       */
+       char *ibuf;                   /**< Buffer of characters to be converted */
+       char *obuf;                   /**< Buffer for converted characters      */
+       size_t ibuflen;               /**< Length of input buffer               */
+       size_t obuflen;               /**< Length of output buffer              */
+       char *osav;                   /**< Saved pointer to output buffer       */
 #endif
 
+       safestrncpy(charset, supplied_charset, sizeof charset);
        msg = strdup("");
-       sprintf(new_window, "<A TARGET=\"%s\" HREF=", TARGET);
+       sprintf(new_window, "<a target=\"%s\" href=", TARGET);
 
        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                line_length = strlen(buf);
                buffer_length = content_length + line_length + 2;
-               msg = realloc(msg, buffer_length);
-               if (msg == NULL) {
-                       wprintf("<B>realloc() error!  "
-                               "couldn't get %d bytes: %s</B><br /><br />\n",
+               ptr = realloc(msg, buffer_length);
+               if (ptr == NULL) {
+                       wprintf("<b>");
+                       wprintf(_("realloc() error! couldn't get %d bytes: %s"),
                                buffer_length + 1,
                                strerror(errno));
+                       wprintf("</b><br /><br />\n");
+                       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                               /** flush */
+                       }
+                       free(msg);
                        return;
                }
+               msg = ptr;
                strcpy(&msg[content_length], buf);
                content_length += line_length;
                strcpy(&msg[content_length], "\n");
                content_length += 1;
        }
 
-#ifdef HAVE_ICONV
-       if ( (strcasecmp(charset, "us-ascii"))
-          && (strcasecmp(charset, "UTF-8")) ) {
-               ic = iconv_open("UTF-8", charset);
-               if (ic == (iconv_t)(-1) ) {
-                       lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
-               }
-       }
-       if (ic != (iconv_t)(-1) ) {
-               ibuf = msg;
-               ibuflen = content_length;
-               obuflen = content_length + (content_length / 2) ;
-               obuf = (char *) malloc(obuflen);
-               osav = obuf;
-               iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
-               content_length = content_length + (content_length / 2) - obuflen;
-               osav[content_length] = 0;
-               free(msg);
-               msg = osav;
-               iconv_close(ic);
-       }
-#endif
-
+       /** Do a first pass to isolate the message body */
        ptr = msg;
        msgstart = msg;
        msgend = &msg[content_length];
 
        while (ptr < msgend) {
 
-               /* Advance to next tag */
+               /** Advance to next tag */
                ptr = strchr(ptr, '<');
                if ((ptr == NULL) || (ptr >= msgend)) break;
                ++ptr;
                if ((ptr == NULL) || (ptr >= msgend)) break;
 
-               /* Any of these tags cause everything up to and including
+               /**
+                *  Look for META tags.  Some messages (particularly in
+                *  Asian locales) illegally declare a message's character
+                *  set in the HTML instead of in the MIME headers.  This
+                *  is wrong but we have to work around it anyway.
+                */
+               if (!strncasecmp(ptr, "META", 4)) {
+
+                       char *meta_start;
+                       char *meta_end;
+                       int meta_length;
+                       char *meta;
+                       char *meta_http_equiv;
+                       char *meta_content;
+                       char *spaceptr;
+
+                       meta_start = &ptr[4];
+                       meta_end = strchr(ptr, '>');
+                       if ((meta_end != NULL) && (meta_end <= msgend)) {
+                               meta_length = meta_end - meta_start + 1;
+                               meta = malloc(meta_length + 1);
+                               safestrncpy(meta, meta_start, meta_length);
+                               meta[meta_length] = 0;
+                               striplt(meta);
+                               if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
+                                       meta_http_equiv = strdup(&meta[11]);
+                                       spaceptr = strchr(meta_http_equiv, ' ');
+                                       if (spaceptr != NULL) {
+                                               *spaceptr = 0;
+                                               meta_content = strdup(++spaceptr);
+                                               if (!strncasecmp(meta_content, "content=", 8)) {
+                                                       strcpy(meta_content, &meta_content[8]);
+                                                       stripquotes(meta_http_equiv);
+                                                       stripquotes(meta_content);
+                                                       extract_charset_from_meta(charset,
+                                                               meta_http_equiv, meta_content);
+                                               }
+                                               free(meta_content);
+                                       }
+                                       free(meta_http_equiv);
+                               }
+                               free(meta);
+                       }
+               }
+
+               /**
+                * Any of these tags cause everything up to and including
                 * the tag to be removed.
                 */     
                if ( (!strncasecmp(ptr, "HTML", 4))
@@ -132,7 +197,8 @@ void output_html(char *charset) {
                        msgstart = ptr;
                }
 
-               /* Any of these tags cause everything including and following
+               /**
+                * Any of these tags cause everything including and following
                 * the tag to be removed.
                 */
                if ( (!strncasecmp(ptr, "/HTML", 5))
@@ -145,42 +211,125 @@ void output_html(char *charset) {
 
                ++ptr;
        }
+       if (msgstart > msg) {
+               strcpy(msg, msgstart);
+       }
 
-       converted_msg = malloc(content_length);
+       /** Convert foreign character sets to UTF-8 if necessary. */
+#ifdef HAVE_ICONV
+       if ( (strcasecmp(charset, "us-ascii"))
+          && (strcasecmp(charset, "UTF-8"))
+          && (strcasecmp(charset, ""))
+       ) {
+               lprintf(9, "Converting %s to UTF-8\n", charset);
+               ic = ctdl_iconv_open("UTF-8", charset);
+               if (ic == (iconv_t)(-1) ) {
+                       lprintf(5, "%s:%d iconv_open() failed: %s\n",
+                               __FILE__, __LINE__, strerror(errno));
+               }
+       }
+       if (ic != (iconv_t)(-1) ) {
+               ibuf = msg;
+               ibuflen = content_length;
+               obuflen = content_length + (content_length / 2) ;
+               obuf = (char *) malloc(obuflen);
+               osav = obuf;
+               iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
+               content_length = content_length + (content_length / 2) - obuflen;
+               osav[content_length] = 0;
+               free(msg);
+               msg = osav;
+               iconv_close(ic);
+       }
+#endif
+
+       /**
+        *      At this point, the message has been stripped down to
+        *      only the content inside the <BODY></BODY> tags, and has
+        *      been converted to UTF-8 if it was originally in a foreign
+        *      character set.  The text is also guaranteed to be null
+        *      terminated now.
+        */
+
+       /** Now go through the message, parsing tags as necessary. */
+       converted_alloc = content_length + 8192;
+       converted_msg = malloc(converted_alloc);
+       if (converted_msg == NULL) {
+               abort();                        /* FIXME */
+       }
        strcpy(converted_msg, "");
-       ptr = msgstart;
+       ptr = msg;
+       msgend = strchr(msg, 0);
        while (ptr < msgend) {
-               /* Change mailto: links to WebCit mail, by replacing the
+
+               /**
+                * Change mailto: links to WebCit mail, by replacing the
                 * link with one that points back to our mail room.  Due to
                 * the way we parse URL's, it'll even handle mailto: links
                 * that have "?subject=" in them.
                 */
-               if (!strncasecmp(ptr, "<A HREF=\"mailto:", 16)) {
+               if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
                        content_length += 64;
-                       converted_msg = realloc(converted_msg, content_length);
+                       if (content_length >= converted_alloc) {
+                               converted_alloc += 8192;
+                               converted_msg = realloc(converted_msg, converted_alloc);
+                               if (converted_msg == NULL) {
+                                       abort();
+                               }
+                       }
                        sprintf(&converted_msg[output_length],
-                               "<A HREF=\"/display_enter"
+                               "<a href=\"display_enter"
                                "?force_room=_MAIL_&recp=");
                        output_length += 47;
                        ptr = &ptr[16];
                        ++alevel;
                }
-               /* Make links open in a separate window */
-               else if (!strncasecmp(ptr, "<A HREF=", 8)) {
-                       content_length += 64;
-                       converted_msg = realloc(converted_msg, content_length);
-                       sprintf(&converted_msg[output_length], new_window);
-                       output_length += strlen(new_window);
-                       ptr = &ptr[8];
+               /** Make external links open in a separate window */
+               else if (!strncasecmp(ptr, "<a href=\"", 9)) {
                        ++alevel;
+                       if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
+                            &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
+                            ) {
+                               /* open external links to new window */
+                               content_length += 64;
+                               if (content_length >= converted_alloc) {
+                                       converted_alloc += 8192;
+                                       converted_msg = realloc(converted_msg, converted_alloc);
+                                       if (converted_msg == NULL) {
+                                               abort();
+                                       }
+                               }
+                               sprintf(&converted_msg[output_length], new_window);
+                               output_length += strlen(new_window);
+                               ptr = &ptr[8];
+                       }
+                       else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
+                               content_length += 64;
+                               if (content_length >= converted_alloc) {
+                                       converted_alloc += 8192;
+                                       converted_msg = realloc(converted_msg, converted_alloc);
+                                       if (converted_msg == NULL) {
+                                               abort();
+                                       }
+                               }
+                               sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
+                               output_length += 19;
+                               ptr = &ptr[9];
+                       }
+                       else {
+                               sprintf(&converted_msg[output_length], "<a href=\"");
+                               output_length += 9;
+                               ptr = &ptr[9];
+                       }
                }
-               /* Turn anything that looks like a URL into a real link, as long
+               /**
+                * Turn anything that looks like a URL into a real link, as long
                 * as it's not inside a tag already
                 */
                else if ( (brak == 0) && (alevel == 0)
                     && (!strncasecmp(ptr, "http://", 7))) {
                                linklen = 0;
-                               /* Find the end of the link */
+                               /** Find the end of the link */
                                for (i=0; i<=strlen(ptr); ++i) {
                                        if ((ptr[i]==0)
                                           ||(isspace(ptr[i]))
@@ -197,7 +346,13 @@ void output_html(char *charset) {
                                }
                                if (linklen > 0) {
                                        content_length += (32 + linklen);
-                                       converted_msg = realloc(converted_msg, content_length);
+                                       if (content_length >= converted_alloc) {
+                                               converted_alloc += 8192;
+                                               converted_msg = realloc(converted_msg, converted_alloc);
+                                               if (converted_msg == NULL) {
+                                                       abort();
+                                               }
+                                       }
                                        sprintf(&converted_msg[output_length], new_window);
                                        output_length += strlen(new_window);
                                        converted_msg[output_length] = '\"';
@@ -217,7 +372,7 @@ void output_html(char *charset) {
                                }
                }
                else {
-                       /*
+                       /**
                         * We need to know when we're inside a tag,
                         * so we don't turn things that look like URL's into
                         * links, when they're already links - or image sources.
@@ -230,14 +385,19 @@ void output_html(char *charset) {
                }
        }
 
-       /* Output our big pile of markup */
+       /**     uncomment these two lines to override conversion        */
+       /**     memcpy(converted_msg, msg, content_length);             */
+       /**     output_length = content_length;                         */
+
+       /** Output our big pile of markup */
        client_write(converted_msg, output_length);
 
-       /* A little trailing vertical whitespace... */
+       /** A little trailing vertical whitespace... */
        wprintf("<br /><br />\n");
 
-       /* Now give back the memory */
-       free(converted_msg);
-       free(msg);
+       /** Now give back the memory */
+       if (converted_msg != NULL) free(converted_msg);
+       if (msg != NULL) free(msg);
 }
 
+/*@}*/