diff --git a/src/config.h b/src/config.h index 0d03f42c..b5f182d9 100644 --- a/src/config.h +++ b/src/config.h @@ -13,7 +13,7 @@ #define VERSION "0.1.19" -#define BUILD 675 +#define BUILD 677 #define HOSTID "mailarchiver" diff --git a/src/parser_utils.c b/src/parser_utils.c index ce8548d0..3deb1afd 100644 --- a/src/parser_utils.c +++ b/src/parser_utils.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include #include #include "trans.h" #include "html.h" @@ -218,8 +220,13 @@ int extract_boundary(char *p, struct _state *state){ void fixupEncodedHeaderLine(char *buf){ - char *sb, *sq, *p, *q, *r, *s, v[SMALLBUFSIZE], puf[MAXBUFSIZE]; - char *start, *end; + char *sb, *sq, *p, *q, *r, *s, *e, *start, *end; + char v[SMALLBUFSIZE], puf[MAXBUFSIZE], encoding[SMALLBUFSIZE], tmpbuf[2*SMALLBUFSIZE]; + iconv_t cd; + size_t size, inbytesleft, outbytesleft; + char *inbuf, *outbuf; + int need_encoding; + memset(puf, 0, sizeof(puf)); @@ -230,6 +237,8 @@ void fixupEncodedHeaderLine(char *buf){ p = v; + memset(encoding, 0, sizeof(encoding)); + do { start = strstr(p, "=?"); if(start){ @@ -240,6 +249,13 @@ void fixupEncodedHeaderLine(char *buf){ start++; + e = strchr(start+2, '?'); + if(e){ + *e = '\0'; + snprintf(encoding, sizeof(encoding)-1, "%s", start+1); + *e = '?'; + } + s = NULL; sb = strcasestr(start, "?B?"); if(sb) s = sb; sq = strcasestr(start, "?Q?"); if(sq) s = sq; @@ -253,9 +269,30 @@ void fixupEncodedHeaderLine(char *buf){ if(sq){ decodeQP(s+3); r = s + 3; for(; *r; r++){ if(*r == '_') *r = ' '; } } /* encode everything if it's not utf-8 encoded */ - if(strncasecmp(start+1, "utf-8", 5)) utf8_encode((unsigned char*)s+3); + //if(strncasecmp(start+1, "utf-8", 5)) utf8_encode((unsigned char*)s+3); + //strncat(puf, s+3, sizeof(puf)-1); - strncat(puf, s+3, sizeof(puf)-1); + size = need_encoding = 0; + + if(strlen(encoding) > 2 && strcasecmp(encoding, "utf-8")){ + need_encoding = 1; + + cd = iconv_open("utf-8", encoding); + + memset(tmpbuf, 0, sizeof(tmpbuf)); + + inbuf = s+3; + outbuf = &tmpbuf[0]; + inbytesleft = strlen(s+3); + outbytesleft = sizeof(tmpbuf)-1; + size = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + iconv_close(cd); + } + + if(need_encoding == 1 && size >= 0) + strncat(puf, tmpbuf, sizeof(puf)-1); + else + strncat(puf, s+3, sizeof(puf)-1); p = end + 2; } diff --git a/webui/model/search/message.php b/webui/model/search/message.php index d515d6e1..cc58f8a4 100644 --- a/webui/model/search/message.php +++ b/webui/model/search/message.php @@ -346,6 +346,8 @@ class ModelSearchMessage extends Model { $what = preg_replace("/^\=\?/", "", $what); $what = preg_replace("/\?\=$/", "", $what); + $encoding = substr($what, 0, strpos($what, '?')); + if(preg_match("/\?Q\?/i", $what)){ $x = preg_replace("/^([\w\-]+)\?Q\?/i", "", $what); @@ -360,9 +362,8 @@ class ModelSearchMessage extends Model { $s = preg_replace('/\0/', "*", $s); } - - if(!preg_match("/utf-8/i", $what)){ - $s = utf8_encode($s); + if(!preg_match("/utf-8/i", $encoding)){ + $s = iconv($encoding, 'utf-8', $s); } }