]> code.citadel.org Git - citadel.git/blobdiff - citadel/html.c
* Removed all of the thread cancellation cruft that is no longer necessary
[citadel.git] / citadel / html.c
index d11e2198489044575d28cb04e7b0332193aa5759..a4d67929511cc14c55f23d2d13e44f8d686ec165 100644 (file)
@@ -3,34 +3,71 @@
  * $Id$
  */
 
+#include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <time.h>
 #include <ctype.h>
 #include <string.h>
-
+#include <errno.h>
+#include <limits.h>
+#include <syslog.h>
+#include "citadel.h"
+#include "server.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"
 
 /*
  * Convert HTML to plain text.
+ *
+ * inputmsg      = pointer to raw HTML message
+ * screenwidth   = desired output screenwidth
+ * do_citaformat = set to 1 to indent newlines with spaces
  */
-void html_to_ascii(int screenwidth) {
+char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
        char inbuf[256];
        char outbuf[256];
        char tag[1024];
        int done_reading = 0;
-       char *ptr;
-       int i, ch, did_out, rb;
+       char *inptr;
+       char *outptr;
+       size_t outlen;
+       int i, j, ch, did_out, rb;
        int nest = 0;           /* Bracket nesting level */
 
+       inptr = inputmsg;
        strcpy(inbuf, "");
        strcpy(outbuf, "");
 
+       outptr = mallok(strlen(inptr) + 256);
+       if (outptr == NULL) return NULL;
+       strcpy(outptr, "");
+       outlen = 0;
+
        do {
                /* Fill the input buffer */
                if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
+
                        /* FIX ... genericize this */
-                       ptr = fgets(&inbuf[strlen(inbuf)], 127, stdin);
-                       if (ptr == NULL) done_reading = 1;
+                       ch = *inputmsg++;
+                       if (ch > 0) {
+                               inbuf[strlen(inbuf)+1] = 0;
+                               inbuf[strlen(inbuf)] = ch;
+                       } 
+                       else {
+                               done_reading = 1;
+                       }
+
                }
 
                /* Do some parsing */
@@ -63,6 +100,10 @@ void html_to_ascii(int screenwidth) {
                                        strcat(outbuf, "\n\n");
                                }
 
+                               if (!strcasecmp(tag, "/DIV")) {
+                                       strcat(outbuf, "\n\n");
+                               }
+
                                else if (!strcasecmp(tag, "H1")) {
                                        strcat(outbuf, "\n\n");
                                }
@@ -97,7 +138,7 @@ void html_to_ascii(int screenwidth) {
 
                                else if (!strcasecmp(tag, "HR")) {
                                        strcat(outbuf, "\n ");
-                                       for (i=0; i<screenwidth-2; ++i)
+                                       for (j=0; j<screenwidth-2; ++j)
                                                strcat(outbuf, "-");
                                        strcat(outbuf, "\n");
                                }
@@ -137,12 +178,12 @@ void html_to_ascii(int screenwidth) {
                                strcpy(&outbuf[i+1], &outbuf[i+6]);
                        }
 
-                       else if (!strncasecmp(&outbuf[i], "&lb;", 4)) {
+                       else if (!strncasecmp(&outbuf[i], "&lt;", 4)) {
                                outbuf[i] = '<';
                                strcpy(&outbuf[i+1], &outbuf[i+4]);
                        }
 
-                       else if (!strncasecmp(&outbuf[i], "&rb;", 4)) {
+                       else if (!strncasecmp(&outbuf[i], "&gt;", 4)) {
                                outbuf[i] = '>';
                                strcpy(&outbuf[i+1], &outbuf[i+4]);
                        }
@@ -156,6 +197,26 @@ void html_to_ascii(int screenwidth) {
                                strcpy(&outbuf[i+1], &outbuf[i+6]);
                        }
 
+                       else if (!strncasecmp(&outbuf[i], "&copy;", 6)) {
+                               outbuf[i] = '(';
+                               outbuf[i+1] = 'c';
+                               outbuf[i+2] = ')';
+                               strcpy(&outbuf[i+3], &outbuf[i+6]);
+                       }
+
+                       else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
+                               outbuf[i] = '(';
+                               outbuf[i+1] = 'r';
+                               outbuf[i+2] = ')';
+                               strcpy(&outbuf[i+3], &outbuf[i+5]);
+                       }
+
+               }
+
+               /* Make sure the output buffer is big enough */
+               if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
+                       outlen += 128;
+                       outptr = realloc(outptr, outlen);
                }
 
                /* Output any lines terminated with hard line breaks */
@@ -164,7 +225,10 @@ void html_to_ascii(int screenwidth) {
                        if (strlen(outbuf)>0)
                            for (i = 0; i<strlen(outbuf); ++i) {
                                if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
-                                       fwrite(outbuf, i+1, 1, stdout);
+                                       strncat(outptr, outbuf, i+1);
+                                       strcat(outptr, "\n");
+                                       if (do_citaformat)
+                                               strcat(outptr, " ");
                                        strcpy(outbuf, &outbuf[i+1]);
                                        i = 0;
                                        did_out = 1;
@@ -179,27 +243,27 @@ void html_to_ascii(int screenwidth) {
                                if (outbuf[i]==32) rb = i;
                        }
                        if (rb>=0) {
-                               fwrite(outbuf, rb, 1, stdout);
-                               fwrite("\n", 1, 1, stdout);
+                               strncat(outptr, outbuf, rb);
+                               strcat(outptr, "\n");
+                               if (do_citaformat)
+                                       strcat(outptr, " ");
                                strcpy(outbuf, &outbuf[rb+1]);
                        } else {
-                               fwrite(outbuf, screenwidth-2, 1, stdout);
-                               fwrite("\n", 1, 1, stdout);
+
+                               strncat(outptr, outbuf, screenwidth-2);
+                               strcat(outptr, "\n");
+                               if (do_citaformat)
+                                       strcat(outptr, " ");
                                strcpy(outbuf, &outbuf[screenwidth-2]);
                        }
                }
 
        } while (done_reading == 0);
-       fwrite(outbuf, strlen(outbuf), 1, stdout);
-       fwrite("\n", 1, 1, stdout);
 
-}
+       strcat(outptr, outbuf);
+       strcat(outptr, "\n");
 
+       return outptr;
 
-/*
- * Temporary main loop for testing
- */
-int main() {
-       html_to_ascii(80);
-       return 0;
 }
+