* add tags found in mail <body> tag to a <table> wrapped arround the mail.
[citadel.git] / webcit / html2html.c
index bfa53709cc5ab8994092d9419c85aa0c178518c8..12cd1b198007d9d90a518cf6047dc3cc59d6fd1a 100644 (file)
@@ -100,6 +100,7 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
        int i;
        int linklen;
        char charset[128];
+       StrBuf *BodyArea;
 #ifdef HAVE_ICONV
        iconv_t ic = (iconv_t)(-1) ;
        char *ibuf;                   /**< Buffer of characters to be converted */
@@ -139,6 +140,7 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
        }
        else {
                content_length = StrLength(Source);
+               free(msg);
                msg = (char*) ChrPtr(Source);/* TODO: remove cast */
                buffer_length = content_length;
        }
@@ -209,8 +211,52 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
                   ||(!strncasecmp(ptr, "HEAD", 4))
                   ||(!strncasecmp(ptr, "/HEAD", 5))
                   ||(!strncasecmp(ptr, "BODY", 4)) ) {
+                       char *pBody = NULL;
+                       
+                       if (!strncasecmp(ptr, "BODY", 4)) {
+                               pBody = ptr;
+                       }
                        ptr = strchr(ptr, '>');
                        if ((ptr == NULL) || (ptr >= msgend)) break;
+                       if ((pBody != NULL) && (ptr - pBody > 4)) {
+                               char* src;
+                               char *cid_start, *cid_end;
+
+                               *ptr = '\0';
+                               pBody += 4; 
+                               while ((isspace(*pBody)) && (pBody < ptr))
+                                       pBody ++;
+                               BodyArea = NewStrBufPlain(NULL,  ptr - pBody);
+
+                               if (pBody < ptr) {
+                                       src = strstr(pBody, "cid:");
+                                       if (src) {
+                                               cid_start = src + 4;
+                                               cid_end = cid_start;
+                                               while ((*cid_end != '"') && 
+                                                      !isspace(*cid_end) &&
+                                                      (cid_end < ptr))
+                                                       cid_end ++;
+
+                                               /* copy tag and attributes up to src="cid: */
+                                               StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
+
+                                               /* add in /webcit/mimepart/<msgno>/CID/ 
+                                                  trailing / stops dumb URL filters getting excited */
+                                               StrBufAppendPrintf(BodyArea,
+                                                                  "/webcit/mimepart/%d/",msgnum);
+                                               StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
+
+                                               if (ptr - cid_end > 0)
+                                                       StrBufAppendBufPlain(BodyArea, 
+                                                                            cid_end + 1, 
+                                                                            ptr - cid_end, 0);
+                                       }
+                                       else 
+                                               StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
+                               }
+                               *ptr = '>';
+                       }
                        ++ptr;
                        if ((ptr == NULL) || (ptr >= msgend)) break;
                        msgstart = ptr;
@@ -234,6 +280,10 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
                strcpy(msg, msgstart);
        }
 
+       /** Now go through the message, parsing tags as necessary. */
+       converted_msg = NewStrBufPlain(NULL, content_length + 8192);
+
+
        /** Convert foreign character sets to UTF-8 if necessary. */
 #ifdef HAVE_ICONV
        if ( (strcasecmp(charset, "us-ascii"))
@@ -247,19 +297,31 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
                                __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);
+       if  (Source == NULL) {
+               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);
+               }
+       }
+       else {
+               if (ic != (iconv_t)(-1) ) {
+                       StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
+                       StrBufConvert(Source, Buf, &ic);
+                       FreeStrBuf(&Buf);
+                       iconv_close(ic);
+                       msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
+               }
        }
+               
 #endif
 
        /**
@@ -270,13 +332,16 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
         *      terminated now.
         */
 
-       /** Now go through the message, parsing tags as necessary. */
-       converted_msg = NewStrBufPlain(NULL, content_length + 8192);
        if (converted_msg == NULL) {
                StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
                goto BAIL;
        }
 
+       if (BodyArea != NULL) {
+               StrBufAppendBufPlain(converted_msg, HKEY("<table "), 0);  
+               StrBufAppendBuf(converted_msg, BodyArea, 0);
+               StrBufAppendBufPlain(converted_msg, HKEY(" width=\"100%\"><tr><td>"), 0);
+       }
        ptr = msg;
        msgend = strchr(msg, 0);
        while (ptr < msgend) {
@@ -410,7 +475,7 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
 
                                        nbspreviewptr = strstr(ptr, "&nbsp;");
                                        if (nbspreviewptr != NULL) {
-                                               ///*nbspreviewptr = '\0';
+                                               /* nbspreviewptr = '\0'; */
                                                linklen = nbspreviewptr - ptr;
                                        }
                                        if (ltreviewptr != 0)
@@ -449,6 +514,8 @@ void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, St
                }
                if (!strncasecmp(ptr, "</A>", 3)) --alevel;
        }
+       if (BodyArea != NULL) 
+               StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);  
 
        /**     uncomment these two lines to override conversion        */
        /**     memcpy(converted_msg, msg, content_length);             */
@@ -465,4 +532,147 @@ BAIL:     /** A little trailing vertical whitespace... */
        if ((msg != NULL) && (Source == NULL)) free(msg);
 }
 
+
+
+
+
+
+/*
+ * Look for URL's embedded in a buffer and make them linkable.  We use a
+ * target window in order to keep the Citadel session in its own window.
+ */
+void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
+{
+       int len, UrlLen, Offset, TrailerLen;
+       const char *start, *end, *pos;
+       
+       FlushStrBuf(Target);
+
+       start = NULL;
+       len = StrLength(Source);
+       end = ChrPtr(Source) + len;
+       for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
+               if (!strncasecmp(pos, "http://", 7))
+                       start = pos;
+               else if (!strncasecmp(pos, "ftp://", 6))
+                       start = pos;
+       }
+
+       if (start == NULL) {
+               StrBufAppendBuf(Target, Source, 0);
+               return;
+       }
+       FlushStrBuf(WrkBuf);
+
+       for (pos = ChrPtr(Source) + len; pos > start; --pos) {
+               if (  (!isprint(*pos))
+                  || (isspace(*pos))
+                  || (*pos == '{')
+                  || (*pos == '}')
+                  || (*pos == '|')
+                  || (*pos == '\\')
+                  || (*pos == '^')
+                  || (*pos == '[')
+                  || (*pos == ']')
+                  || (*pos == '`')
+                  || (*pos == '<')
+                  || (*pos == '>')
+                  || (*pos == '(')
+                  || (*pos == ')')
+               ) {
+                       end = pos;
+               }
+       }
+       
+       UrlLen = end - start;
+       StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
+
+       Offset = start - ChrPtr(Source);
+       if (Offset != 0)
+               StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
+       StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
+                          LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, 
+                          QU, RB, ChrPtr(WrkBuf), LB, RB);
+
+       TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
+       if (TrailerLen > 0)
+               StrBufAppendBufPlain(Target, end, TrailerLen, 0);
+}
+void url(char *buf, size_t bufsize)
+{
+       int len, UrlLen, Offset, TrailerLen, outpos;
+       char *start, *end, *pos;
+       char urlbuf[SIZ];
+       char outbuf[SIZ];
+
+       start = NULL;
+       len = strlen(buf);
+       if (len > bufsize) {
+               lprintf(1, "URL: content longer than buffer!");
+               return;
+       }
+       end = buf + len;
+       for (pos = buf; (pos < end) && (start == NULL); ++pos) {
+               if (!strncasecmp(pos, "http://", 7))
+                       start = pos;
+               if (!strncasecmp(pos, "ftp://", 6))
+                       start = pos;
+       }
+
+       if (start == NULL)
+               return;
+
+       for (pos = buf+len; pos > start; --pos) {
+               if (  (!isprint(*pos))
+                  || (isspace(*pos))
+                  || (*pos == '{')
+                  || (*pos == '}')
+                  || (*pos == '|')
+                  || (*pos == '\\')
+                  || (*pos == '^')
+                  || (*pos == '[')
+                  || (*pos == ']')
+                  || (*pos == '`')
+                  || (*pos == '<')
+                  || (*pos == '>')
+                  || (*pos == '(')
+                  || (*pos == ')')
+               ) {
+                       end = pos;
+               }
+       }
+       
+       UrlLen = end - start;
+       if (UrlLen > sizeof(urlbuf)){
+               lprintf(1, "URL: content longer than buffer!");
+               return;
+       }
+       memcpy(urlbuf, start, UrlLen);
+       urlbuf[UrlLen] = '\0';
+
+       Offset = start - buf;
+       if ((Offset != 0) && (Offset < sizeof(outbuf)))
+               memcpy(outbuf, buf, Offset);
+       outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,  
+                         "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
+                         LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
+       if (outpos >= sizeof(outbuf) - Offset) {
+               lprintf(1, "URL: content longer than buffer!");
+               return;
+       }
+
+       TrailerLen = len - (end - start);
+       if (TrailerLen > 0)
+               memcpy(outbuf + Offset + outpos, end, TrailerLen);
+       if (Offset + outpos + TrailerLen > bufsize) {
+               lprintf(1, "URL: content longer than buffer!");
+               return;
+       }
+       memcpy (buf, outbuf, Offset + outpos + TrailerLen);
+       *(buf + Offset + outpos + TrailerLen) = '\0';
+}
+
+
+
+
 /*@}*/