* don't double libcitadel header function for safestrncpy in webcit.h
[citadel.git] / citadel / html.c
index 4b910252859367a9c4f43bf647261d1ad6e713dc..9795a0c8bf4b4523ab98623523e7887b5eb5dd1e 100644 (file)
@@ -2,14 +2,10 @@
  * $Id$
  *
  * Functions which handle translation between HTML and plain text
- * Copyright (c) 2000-2001 by Art Cancro and others.   This program is
+ * Copyright (c) 2000-2005 by Art Cancro and others.   This program is
  * released under the terms of the GNU General Public License.
  */
 
-#ifdef DLL_EXPORT
-#define IN_LIBCIT
-#endif
-
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
-#include "serv_extensions.h"
 #include "control.h"
 #include "sysdep_decls.h"
 #include "support.h"
 #include "config.h"
 #include "msgbase.h"
-#include "tools.h"
 #include "room_ops.h"
 #include "html.h"
  
@@ -52,8 +47,9 @@
  * screenwidth   = desired output screenwidth
  * do_citaformat = set to 1 to indent newlines with spaces
  */
-char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
+char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
        char inbuf[SIZ];
+       int inbuf_len = 0;
        char outbuf[SIZ];
        char tag[1024];
        int done_reading = 0;
@@ -61,12 +57,19 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
        char *outptr;
        size_t outptr_buffer_size;
        size_t output_len = 0;
-       int i, j, ch, did_out, rb;
+       int i, j, ch, did_out, rb, scanch;
        int nest = 0;           /* Bracket nesting level */
+       int blockquote = 0;     /* BLOCKQUOTE nesting level */
+       int styletag = 0;       /* STYLE tag nesting level */
+       int styletag_start = 0;
+       int bytes_processed = 0;
+       char nl[128];
 
+       strcpy(nl, "\n");
        inptr = inputmsg;
        strcpy(inbuf, "");
        strcpy(outbuf, "");
+       if (msglen == 0) msglen = strlen(inputmsg);
 
        outptr_buffer_size = strlen(inptr) + SIZ;
        outptr = malloc(outptr_buffer_size);
@@ -76,38 +79,45 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
 
        do {
                /* Fill the input buffer */
-               if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
+               inbuf_len = strlen(inbuf);
+               if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
 
                        ch = *inptr++;
                        if (ch != 0) {
-                               inbuf[strlen(inbuf)+1] = 0;
-                               inbuf[strlen(inbuf)] = ch;
+                               inbuf[inbuf_len++] = ch;
+                               inbuf[inbuf_len] = 0;
                        } 
                        else {
                                done_reading = 1;
                        }
 
+                       ++bytes_processed;
+                       if (bytes_processed > msglen) {
+                               done_reading = 1;
+                       }
+
                }
 
                /* Do some parsing */
-               if (strlen(inbuf)>0) {
+               if (!IsEmptyStr(inbuf)) {
+
 
                    /* Fold in all the spacing */
-                   for (i=0; i<strlen(inbuf); ++i) {
+                   for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
                        if (inbuf[i]==10) inbuf[i]=32;
                        if (inbuf[i]==13) inbuf[i]=32;
                        if (inbuf[i]==9) inbuf[i]=32;
+                       /*** we like foreign characters now.
                        if ((inbuf[i]<32) || (inbuf[i]>126)) {
                                inbuf[i] = '?';
-                               /* strcpy(&inbuf[i], &inbuf[i+1]); */
-                       }
+                       } */
                    }
-                   for (i=0; i<strlen(inbuf); ++i) {
+                   for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
                        while ((inbuf[i]==32)&&(inbuf[i+1]==32))
                                strcpy(&inbuf[i], &inbuf[i+1]);
                    }
 
-                   for (i=0; i<strlen(inbuf); ++i) {
+                   for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
 
                        ch = inbuf[i];
 
@@ -116,66 +126,117 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                                strcpy(tag, "");
                        }
 
-                       else if (ch == '>') {
+                       else if (ch == '>') {   /* We have a tag. */
                                if (nest > 0) --nest;
+
+                               /* Unqualify the tag (truncate at first space) */
+                               if (strchr(tag, ' ') != NULL) {
+                                       strcpy(strchr(tag, ' '), "");
+                               }
                                
                                if (!strcasecmp(tag, "P")) {
-                                       strcat(outbuf, "\n\n");
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, nl);
                                }
 
                                if (!strcasecmp(tag, "/DIV")) {
-                                       strcat(outbuf, "\n\n");
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, nl);
+                               }
+
+                               if (!strcasecmp(tag, "LI")) {
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, " * ");
+                               }
+
+                               else if (!strcasecmp(tag, "/UL")) {
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "H1")) {
-                                       strcat(outbuf, "\n\n");
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "H2")) {
-                                       strcat(outbuf, "\n\n");
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "H3")) {
-                                       strcat(outbuf, "\n\n");
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "H4")) {
-                                       strcat(outbuf, "\n\n");
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "/H1")) {
-                                       strcat(outbuf, "\n");
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "/H2")) {
-                                       strcat(outbuf, "\n");
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "/H3")) {
-                                       strcat(outbuf, "\n");
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "/H4")) {
-                                       strcat(outbuf, "\n");
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "HR")) {
-                                       strcat(outbuf, "\n ");
+                                       strcat(outbuf, nl);
+                                       strcat(outbuf, " ");
                                        for (j=0; j<screenwidth-2; ++j)
                                                strcat(outbuf, "-");
-                                       strcat(outbuf, "\n");
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "BR")) {
-                                       strcat(outbuf, "\n");
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "TR")) {
-                                       strcat(outbuf, "\n");
+                                       strcat(outbuf, nl);
                                }
 
                                else if (!strcasecmp(tag, "/TABLE")) {
+                                       strcat(outbuf, nl);
+                               }
+
+                               else if (!strcasecmp(tag, "BLOCKQUOTE")) {
+                                       ++blockquote;
+                                       strcpy(nl, "\n");
+                                       for (j=0; j<blockquote; ++j) strcat(nl, ">");
+                                       strcat(outbuf, nl);
+                               }
+
+                               else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
                                        strcat(outbuf, "\n");
+                                       --blockquote;
+                                       strcpy(nl, "\n");
+                                       for (j=0; j<blockquote; ++j) strcat(nl, ">");
+                                       strcat(outbuf, nl);
+                               }
+
+                               else if (!strcasecmp(tag, "STYLE")) {
+                                       ++styletag;
+                                       if (styletag == 1) {
+                                               styletag_start = strlen(outbuf);
+                                       }
+                               }
+
+                               else if (!strcasecmp(tag, "/STYLE")) {
+                                       --styletag;
+                                       if (styletag == 0) {
+                                               outbuf[styletag_start] = 0;
+                                       }
                                }
 
                        }
@@ -194,13 +255,29 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                }
 
                /* Convert &; tags to the forbidden characters */
-               if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
+               if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
 
+                       /* Character entity references */
                        if (!strncasecmp(&outbuf[i], "&nbsp;", 6)) {
                                outbuf[i] = ' ';
                                strcpy(&outbuf[i+1], &outbuf[i+6]);
                        }
 
+                       if (!strncasecmp(&outbuf[i], "&ensp;", 6)) {
+                               outbuf[i] = ' ';
+                               strcpy(&outbuf[i+1], &outbuf[i+6]);
+                       }
+
+                       if (!strncasecmp(&outbuf[i], "&emsp;", 6)) {
+                               outbuf[i] = ' ';
+                               strcpy(&outbuf[i+1], &outbuf[i+6]);
+                       }
+
+                       if (!strncasecmp(&outbuf[i], "&thinsp;", 8)) {
+                               outbuf[i] = ' ';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
                        else if (!strncasecmp(&outbuf[i], "&lt;", 4)) {
                                outbuf[i] = '<';
                                strcpy(&outbuf[i+1], &outbuf[i+4]);
@@ -220,6 +297,16 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                                strcpy(&outbuf[i+1], &outbuf[i+6]);
                        }
 
+                       else if (!strncasecmp(&outbuf[i], "&lsquo;", 7)) {
+                               outbuf[i] = '`';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&rsquo;", 7)) {
+                               outbuf[i] = '\'';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
                        else if (!strncasecmp(&outbuf[i], "&copy;", 6)) {
                                outbuf[i] = '(';
                                outbuf[i+1] = 'c';
@@ -227,6 +314,28 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                                strcpy(&outbuf[i+3], &outbuf[i+6]);
                        }
 
+                       else if (!strncasecmp(&outbuf[i], "&bull;", 6)) {
+                               outbuf[i] = ' ';
+                               outbuf[i+1] = '*';
+                               outbuf[i+2] = ' ';
+                               strcpy(&outbuf[i+3], &outbuf[i+6]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&hellip;", 8)) {
+                               outbuf[i] = '.';
+                               outbuf[i+1] = '.';
+                               outbuf[i+2] = '.';
+                               strcpy(&outbuf[i+3], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&trade;", 7)) {
+                               outbuf[i] = '(';
+                               outbuf[i+1] = 't';
+                               outbuf[i+2] = 'm';
+                               outbuf[i+3] = ')';
+                               strcpy(&outbuf[i+4], &outbuf[i+7]);
+                       }
+
                        else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
                                outbuf[i] = '(';
                                outbuf[i+1] = 'r';
@@ -234,29 +343,146 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                                strcpy(&outbuf[i+3], &outbuf[i+5]);
                        }
 
+                       else if (!strncasecmp(&outbuf[i], "&frac14;", 8)) {
+                               outbuf[i] = '1';
+                               outbuf[i+1] = '/';
+                               outbuf[i+2] = '4';
+                               strcpy(&outbuf[i+3], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&frac12;", 8)) {
+                               outbuf[i] = '1';
+                               outbuf[i+1] = '/';
+                               outbuf[i+2] = '2';
+                               strcpy(&outbuf[i+3], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&frac34;", 8)) {
+                               outbuf[i] = '3';
+                               outbuf[i+1] = '/';
+                               outbuf[i+2] = '4';
+                               strcpy(&outbuf[i+3], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&ndash;", 7)) {
+                               outbuf[i] = '-';
+                               outbuf[i+1] = '-';
+                               strcpy(&outbuf[i+2], &outbuf[i+7]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&mdash;", 7)) {
+                               outbuf[i] = '-';
+                               outbuf[i+1] = '-';
+                               outbuf[i+2] = '-';
+                               strcpy(&outbuf[i+3], &outbuf[i+7]);
+                       }
+
+                       else if (!strncmp(&outbuf[i], "&Ccedil;", 8)) {
+                               outbuf[i] = 'C';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&ccedil;", 8)) {
+                               outbuf[i] = 'c';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncmp(&outbuf[i], "&Egrave;", 8)) {
+                               outbuf[i] = 'E';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&egrave;", 8)) {
+                               outbuf[i] = 'e';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncmp(&outbuf[i], "&Ecirc;", 7)) {
+                               outbuf[i] = 'E';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&ecirc;", 7)) {
+                               outbuf[i] = 'e';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       else if (!strncmp(&outbuf[i], "&Eacute;", 8)) {
+                               outbuf[i] = 'E';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&eacute;", 8)) {
+                               outbuf[i] = 'e';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncmp(&outbuf[i], "&Agrave;", 8)) {
+                               outbuf[i] = 'A';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&agrave;", 8)) {
+                               outbuf[i] = 'a';
+                               strcpy(&outbuf[i+1], &outbuf[i+8]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&ldquo;", 7)) {
+                               outbuf[i] = '\"';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&rdquo;", 7)) {
+                               outbuf[i] = '\"';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&acute;", 7)) {
+                               outbuf[i] = '\'';
+                               strcpy(&outbuf[i+1], &outbuf[i+7]);
+                       }
+
+                       /* two-digit decimal equivalents */
+                       else if ((!strncmp(&outbuf[i], "&#", 2))
+                             && (outbuf[i+4] == ';') ) {
+                               scanch = 0;
+                               sscanf(&outbuf[i+2], "%02d", &scanch);
+                               outbuf[i] = scanch;
+                               strcpy(&outbuf[i+1], &outbuf[i+5]);
+                       }
+
+                       /* three-digit decimal equivalents */
+                       else if ((!strncmp(&outbuf[i], "&#", 2))
+                             && (outbuf[i+5] == ';') ) {
+                               scanch = 0;
+                               sscanf(&outbuf[i+2], "%03d", &scanch);
+                               outbuf[i] = scanch;
+                               strcpy(&outbuf[i+1], &outbuf[i+6]);
+                       }
+
                }
 
                /* Make sure the output buffer is big enough */
-               if ((output_len + strlen(outbuf) + SIZ)
-                  > outptr_buffer_size) {
+               if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
                        outptr_buffer_size += SIZ;
                        outptr = realloc(outptr, outptr_buffer_size);
+                       if (outptr == NULL) {
+                               abort();
+                       }
                }
 
                /* Output any lines terminated with hard line breaks */
                do {
                        did_out = 0;
-                       if (strlen(outbuf)>0)
+                       if (strlen(outbuf) > 0) {
                            for (i = 0; i<strlen(outbuf); ++i) {
                                if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
 
-                                       strncpy(&outptr[output_len],
-                                               outbuf, i+1);
+                                       strncpy(&outptr[output_len], outbuf, i+1);
                                        output_len += (i+1);
 
                                        if (do_citaformat) {
-                                               strcpy(&outptr[output_len],
-                                                       " ");
+                                               strcpy(&outptr[output_len], " ");
                                                ++output_len;
                                        }
 
@@ -265,10 +491,11 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                                        did_out = 1;
                                }
                        }
+                   }
                } while (did_out);
 
                /* Add soft line breaks */
-               if (strlen(outbuf) > (screenwidth - 2)) {
+               if (strlen(outbuf) > (screenwidth - 2 )) {
                        rb = (-1);
                        for (i=0; i<(screenwidth-2); ++i) {
                                if (outbuf[i]==32) rb = i;
@@ -276,8 +503,8 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                        if (rb>=0) {
                                strncpy(&outptr[output_len], outbuf, rb);
                                output_len += rb;
-                               strcpy(&outptr[output_len], "\n");
-                               output_len += 1;
+                               strcpy(&outptr[output_len], nl);
+                               output_len += strlen(nl);
                                if (do_citaformat) {
                                        strcpy(&outptr[output_len], " ");
                                        ++output_len;
@@ -287,8 +514,8 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                                strncpy(&outptr[output_len], outbuf,
                                        screenwidth-2);
                                output_len += (screenwidth-2);
-                               strcpy(&outptr[output_len], "\n");
-                               output_len += 1;
+                               strcpy(&outptr[output_len], nl);
+                               output_len += strlen(nl);
                                if (do_citaformat) {
                                        strcpy(&outptr[output_len], " ");
                                        ++output_len;