+ /** Sometimes, badly formed messages contain strings which were simply
+ * written out directly in some foreign character set instead of
+ * using RFC2047 encoding. This is illegal but we will attempt to
+ * handle it anyway by converting from a user-specified default
+ * charset to UTF-8 if we see any nonprintable characters.
+ */
+ for (i=0; i<strlen(buf); ++i) {
+ if ((buf[i] < 32) || (buf[i] > 126)) {
+ illegal_non_rfc2047_encoding = 1;
+ }
+ }
+ if (illegal_non_rfc2047_encoding) {
+ char default_header_charset[128];
+ get_preference("default_header_charset", default_header_charset, sizeof default_header_charset);
+ if ( (strcasecmp(default_header_charset, "UTF-8")) && (strcasecmp(default_header_charset, "us-ascii")) ) {
+ ic = ctdl_iconv_open("UTF-8", default_header_charset);
+ if (ic != (iconv_t)(-1) ) {
+ ibuf = malloc(1024);
+ isav = ibuf;
+ safestrncpy(ibuf, buf, 1024);
+ ibuflen = strlen(ibuf);
+ obuflen = 1024;
+ obuf = (char *) malloc(obuflen);
+ osav = obuf;
+ iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
+ osav[1024-obuflen] = 0;
+ strcpy(buf, osav);
+ free(osav);
+ iconv_close(ic);
+ free(isav);
+ }
+ }
+ }
+
+ /** Now we handle foreign character sets properly encoded
+ * in RFC2047 format.
+ */