* calendar_view.c event.c floors.c graphics.c html2html.c iconbar.c: i18n
[citadel.git] / webcit / html2html.c
index fdab14556feeb0dba4f808114ea061d1bbf7fa45..32f76777f561bae82db7431c205951a2d25efe53 100644 (file)
@@ -6,32 +6,16 @@
  *
  */
 
-#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>
 #include "webcit.h"
 #include "vcard.h"
 #include "webserver.h"
 
 
 /*
+ * Sanitize and enhance an HTML message for display.
+ * Also convert weird character sets to UTF-8 if necessary.
  */
-void output_html(void) {
+void output_html(char *charset) {
        char buf[SIZ];
        char *msg;
        char *ptr;
@@ -44,21 +28,31 @@ void output_html(void) {
        int output_length = 0;
        char new_window[SIZ];
        int brak = 0;
+       int alevel = 0;
        int i;
        int linklen;
+#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       */
+#endif
 
        msg = strdup("");
        sprintf(new_window, "<A TARGET=\"%s\" HREF=", TARGET);
 
-       while (serv_gets(buf), strcmp(buf, "000")) {
+       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",
+                       wprintf("<B>");
+                       wprintf(_("realloc() error! couldn't get %d bytes: %s"),
                                buffer_length + 1,
                                strerror(errno));
+                       wprintf("</B><br /><br />\n");
                        return;
                }
                strcpy(&msg[content_length], buf);
@@ -67,6 +61,29 @@ void output_html(void) {
                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
+
        ptr = msg;
        msgstart = msg;
        msgend = &msg[content_length];
@@ -124,7 +141,7 @@ void output_html(void) {
                                "?force_room=_MAIL_&recp=");
                        output_length += 47;
                        ptr = &ptr[16];
-                       ++brak;
+                       ++alevel;
                }
                /* Make links open in a separate window */
                else if (!strncasecmp(ptr, "<A HREF=", 8)) {
@@ -133,12 +150,12 @@ void output_html(void) {
                        sprintf(&converted_msg[output_length], new_window);
                        output_length += strlen(new_window);
                        ptr = &ptr[8];
-                       ++brak;
+                       ++alevel;
                }
                /* 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)
+               else if ( (brak == 0) && (alevel == 0)
                     && (!strncasecmp(ptr, "http://", 7))) {
                                linklen = 0;
                                /* Find the end of the link */
@@ -147,8 +164,11 @@ void output_html(void) {
                                           ||(isspace(ptr[i]))
                                           ||(ptr[i]==10)
                                           ||(ptr[i]==13)
+                                          ||(ptr[i]=='(')
                                           ||(ptr[i]==')')
+                                          ||(ptr[i]=='<')
                                           ||(ptr[i]=='>')
+                                          ||(ptr[i]=='[')
                                           ||(ptr[i]==']')
                                        ) linklen = i;
                                        if (linklen > 0) break;
@@ -182,6 +202,7 @@ void output_html(void) {
                         */
                        if (*ptr == '<') ++brak;
                        if (*ptr == '>') --brak;
+                       if (!strncasecmp(ptr, "</A>", 3)) --alevel;
                        converted_msg[output_length] = *ptr++;
                        converted_msg[++output_length] = 0;
                }
@@ -191,7 +212,7 @@ void output_html(void) {
        client_write(converted_msg, output_length);
 
        /* A little trailing vertical whitespace... */
-       wprintf("<BR><BR>\n");
+       wprintf("<br /><br />\n");
 
        /* Now give back the memory */
        free(converted_msg);