+
+ /** Convert foreign character sets to UTF-8 if necessary. */
+#ifdef HAVE_ICONV
+ if ( (strcasecmp(charset, "us-ascii"))
+ && (strcasecmp(charset, "UTF-8"))
+ && (strcasecmp(charset, ""))
+ ) {
+ syslog(LOG_DEBUG, "Converting %s to UTF-8\n", charset);
+ ctdl_iconv_open("UTF-8", charset, &ic);
+ if (ic == (iconv_t)(-1) ) {
+ syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s\n",
+ __FILE__, __LINE__, strerror(errno));
+ }
+ }
+ 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
+
+ /*
+ * At this point, the message has been stripped down to
+ * only the content inside the <BODY></BODY> tags, and has
+ * been converted to UTF-8 if it was originally in a foreign
+ * character set. The text is also guaranteed to be null
+ * terminated now.
+ */
+
+ 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);