HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / citadel / html.c
index 3964dade2a6ab6f8f7cb502ed37b5ba5ca24137e..bc9006a3bbff4c0f54ac4a0f3494534f60068d45 100644 (file)
 #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"
  
@@ -50,6 +49,7 @@
  */
 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;
@@ -79,12 +79,13 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
 
        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;
@@ -98,10 +99,11 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                }
 
                /* 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;
@@ -110,12 +112,12 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                inbuf[i] = '?';
                        } */
                    }
-                   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];
 
@@ -253,7 +255,7 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                }
 
                /* 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)) {
@@ -312,6 +314,13 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                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] = '.';
@@ -418,6 +427,16 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                                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]);
+                       }
+
                        /* two-digit decimal equivalents */
                        else if ((!strncmp(&outbuf[i], "&#", 2))
                              && (outbuf[i+4] == ';') ) {
@@ -439,26 +458,26 @@ char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaform
                }
 
                /* 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;
                                        }