add patch RFE 4244, 'Handle additional Date header formats'
[claws.git] / src / procheader.c
index b6f186f939e022b64addc8efb26c64f16a398519..f5755b6a395694be369d68aa363a4a918629e301 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include <glib.h>
 #include <time.h>
 #include <sys/stat.h>
 
+#ifdef G_OS_WIN32
+#  include <w32lib.h>
+#endif
+
 #include "procheader.h"
 #include "procmsg.h"
 #include "codeconv.h"
 #include "prefs_common.h"
+#include "hooks.h"
 #include "utils.h"
+#include "defs.h"
+#include "file-utils.h"
 
 #define BUFFSIZE       8192
 
+static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
+
 typedef char *(*getlinefunc) (char *, size_t, void *);
 typedef int (*peekcharfunc) (void *);
 typedef int (*getcharfunc) (void *);
-typedef gint (*get_one_field_func) (gchar *, size_t, void *, HeaderEntry[]);
+typedef gint (*get_one_field_func) (gchar **, void *, HeaderEntry[]);
 
-static gint string_get_one_field(gchar *buf, size_t len, char **str,
+static gint string_get_one_field(gchar **buf, char **str,
                                 HeaderEntry hentry[]);
 
 static char *string_getline(char *buf, size_t len, char **str);
 static int string_peekchar(char **str);
 static int file_peekchar(FILE *fp);
-static gint generic_get_one_field(gchar *buf, size_t len, void *data,
+static gint generic_get_one_field(gchar **bufptr, void *data,
                                  HeaderEntry hentry[],
                                  getlinefunc getline, 
                                  peekcharfunc peekchar,
@@ -57,31 +67,62 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                             gboolean full, gboolean decrypted);
 
 
-gint procheader_get_one_field(gchar *buf, size_t len, FILE *fp,
+gint procheader_get_one_field(gchar **buf, FILE *fp,
                              HeaderEntry hentry[])
 {
-       return generic_get_one_field(buf, len, fp, hentry,
-                                    (getlinefunc)fgets, (peekcharfunc)file_peekchar,
+       return generic_get_one_field(buf, fp, hentry,
+                                    (getlinefunc)fgets_crlf, (peekcharfunc)file_peekchar,
                                     TRUE);
 }
 
-static gint string_get_one_field(gchar *buf, size_t len, char **str,
+static gint string_get_one_field(gchar **buf, char **str,
                                 HeaderEntry hentry[])
 {
-       return generic_get_one_field(buf, len, str, hentry,
+       return generic_get_one_field(buf, str, hentry,
                                     (getlinefunc)string_getline,
                                     (peekcharfunc)string_peekchar,
                                     TRUE);
 }
 
+gboolean procheader_skip_headers(FILE *fp)
+{
+       gchar *buf = g_malloc(BUFFSIZE);
+       do {
+               if (fgets_crlf(buf, BUFFSIZE - 1, fp) == NULL) {
+                       g_free(buf);
+                       return FALSE;
+               }
+               if (buf[0] == '\r' || buf[0] == '\n') {
+                       break;
+               }
+       } while (TRUE);
+       g_free(buf);
+
+       return TRUE;
+}
+
+
 static char *string_getline(char *buf, size_t len, char **str)
 {
+       gboolean is_cr = FALSE;
+       gboolean last_was_cr = FALSE;
+
        if (!*str || !**str)
                return NULL;
 
-       for (; **str && len > 1; --len)
-               if ((*buf++ = *(*str)++) == '\n')
+       for (; **str && len > 1; --len) {
+               is_cr = (**str == '\r');
+               if ((*buf++ = *(*str)++) == '\n') {
+                   break;
+               }
+               if (last_was_cr) {
+                       *(--buf) = '\n';
+                       buf++;
                    break;
+               }
+               last_was_cr = is_cr;
+       }
+               
        *buf = '\0';
 
        return buf;
@@ -97,23 +138,44 @@ static int file_peekchar(FILE *fp)
        return ungetc(getc(fp), fp);
 }
 
-static gint generic_get_one_field(gchar *buf, size_t len, void *data,
+static gint generic_get_one_field(gchar **bufptr, void *data,
                          HeaderEntry *hentry,
                          getlinefunc getline, peekcharfunc peekchar,
                          gboolean unfold)
 {
+       /* returns -1 in case of failure of any kind, whatever it's a parsing error
+          or an allocation error. if returns -1, *bufptr is always NULL, and vice-versa,
+          and if returning 0 (OK), *bufptr is always non-NULL, so callers just have to
+          test the return value
+       */
        gint nexthead;
        gint hnum = 0;
        HeaderEntry *hp = NULL;
+       size_t len;
+       gchar *buf;
+
+       cm_return_val_if_fail(bufptr != NULL, -1);
+
+       len = BUFFSIZE;
+       buf = g_malloc(len);
 
        if (hentry != NULL) {
                /* skip non-required headers */
+               /* and get hentry header line */
                do {
                        do {
-                               if (getline(buf, len, data) == NULL)
+                               if (getline(buf, len, data) == NULL) {
+                                       debug_print("generic_get_one_field: getline\n");
+                                       g_free(buf);
+                                       *bufptr = NULL;
                                        return -1;
-                               if (buf[0] == '\r' || buf[0] == '\n')
+                               }
+                               if (buf[0] == '\r' || buf[0] == '\n') {
+                                       debug_print("generic_get_one_field: empty line\n");
+                                       g_free(buf);
+                                       *bufptr = NULL;
                                        return -1;
+                               }
                        } while (buf[0] == ' ' || buf[0] == '\t');
 
                        for (hp = hentry, hnum = 0; hp->name != NULL;
@@ -124,8 +186,27 @@ static gint generic_get_one_field(gchar *buf, size_t len, void *data,
                        }
                } while (hp->name == NULL);
        } else {
-               if (getline(buf, len, data) == NULL) return -1;
-               if (buf[0] == '\r' || buf[0] == '\n') return -1;
+               /* read first line */
+               if (getline(buf, len, data) == NULL) {
+                       debug_print("generic_get_one_field: getline\n");
+                       g_free(buf);
+                       *bufptr = NULL;
+                       return -1;
+               }
+               if (buf[0] == '\r' || buf[0] == '\n') {
+                       debug_print("generic_get_one_field: empty line\n");
+                       g_free(buf);
+                       *bufptr = NULL;
+                       return -1;
+               }
+       }
+       /* reduce initial buffer to its useful part */
+       len = strlen(buf)+1;
+       buf = g_realloc(buf, len);
+       if (buf == NULL) {
+               debug_print("generic_get_one_field: reallocation error\n");
+               *bufptr = NULL;
+               return -1;
        }
 
        /* unfold line */
@@ -134,6 +215,9 @@ static gint generic_get_one_field(gchar *buf, size_t len, void *data,
                /* ([*WSP CRLF] 1*WSP) */
                if (nexthead == ' ' || nexthead == '\t') {
                        size_t buflen;
+                       gchar *tmpbuf;
+                       size_t tmplen;
+
                        gboolean skiptab = (nexthead == '\t');
                        /* trim previous trailing \n if requesting one header or
                         * unfolding was requested */
@@ -142,15 +226,29 @@ static gint generic_get_one_field(gchar *buf, size_t len, void *data,
 
                        buflen = strlen(buf);
                        
-                       /* concatenate next line */
-                       if ((len - buflen) > 2) {
-                               if (getline(buf + buflen, len - buflen, data) == NULL)
-                                       break;
-                               if (skiptab) { /* replace tab with space */
-                                       *(buf + buflen) = ' ';
-                               }
-                       } else
+                       /* read next line */
+                       tmpbuf = g_malloc(BUFFSIZE);
+
+                       if (getline(tmpbuf, BUFFSIZE, data) == NULL) {
+                               g_free(tmpbuf);
                                break;
+                       }
+                       tmplen = strlen(tmpbuf)+1;
+
+                       /* extend initial buffer and concatenate next line */
+                       len += tmplen;
+                       buf = g_realloc(buf, len);
+                       if (buf == NULL) {
+                               debug_print("generic_get_one_field: reallocation error\n");
+                               g_free(buf);
+                               *bufptr = NULL;
+                               return -1;
+                       }
+                       memcpy(buf+buflen, tmpbuf, tmplen);
+                       g_free(tmpbuf);
+                       if (skiptab) { /* replace tab with space */
+                               *(buf + buflen) = ' ';
+                       }
                } else {
                        /* remove trailing new line */
                        strretchomp(buf);
@@ -158,45 +256,34 @@ static gint generic_get_one_field(gchar *buf, size_t len, void *data,
                }
        }
 
+       *bufptr = buf;
+
        return hnum;
 }
 
-gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
+gint procheader_get_one_field_asis(gchar **buf, FILE *fp)
 {
-       return generic_get_one_field(buf, len, fp, NULL,
-                                    (getlinefunc)fgets, 
+       return generic_get_one_field(buf, fp, NULL,
+                                    (getlinefunc)fgets_crlf
                                     (peekcharfunc)file_peekchar,
                                     FALSE);
 }
 
 GPtrArray *procheader_get_header_array_asis(FILE *fp)
 {
-       gchar buf[BUFFSIZE];
+       gchar *buf = NULL;
        GPtrArray *headers;
        Header *header;
 
-       g_return_val_if_fail(fp != NULL, NULL);
+       cm_return_val_if_fail(fp != NULL, NULL);
 
        headers = g_ptr_array_new();
 
-       while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
+       while (procheader_get_one_field_asis(&buf, fp) != -1) {
                if ((header = procheader_parse_header(buf)) != NULL)
                        g_ptr_array_add(headers, header);
-                       /*
-               if (*buf == ':') continue;
-               for (p = buf; *p && *p != ' '; p++) {
-                       if (*p == ':') {
-                               header = g_new(Header, 1);
-                               header->name = g_strndup(buf, p - buf);
-                               p++;
-                               conv_unmime_header(tmp, sizeof(tmp), p, NULL);
-                               header->body = g_strdup(tmp);
-
-                               g_ptr_array_add(headers, header);
-                               break;
-                       }
-               }
-                       */
+               g_free(buf);
+               buf = NULL;
        }
 
        return headers;
@@ -207,6 +294,8 @@ void procheader_header_array_destroy(GPtrArray *harray)
        gint i;
        Header *header;
 
+       cm_return_if_fail(harray != NULL);
+
        for (i = 0; i < harray->len; i++) {
                header = g_ptr_array_index(harray, i);
                procheader_header_free(header);
@@ -252,11 +341,38 @@ gboolean procheader_headername_equal(char * hdr1, char * hdr2)
   header->name = "From:"
   header->body = "dinh@enseirb.fr"
  */
+static gboolean header_is_addr_field(const gchar *hdr)
+{
+       static char *addr_headers[] = {
+                               "To:",
+                               "Cc:",
+                               "Bcc:",
+                               "From:",
+                               "Reply-To:",
+                               "Followup-To:",
+                               "Followup-and-Reply-To:",
+                               "Disposition-Notification-To:",
+                               "Return-Receipt-To:",
+                               NULL};
+       int i;
+
+       if (!hdr)
+               return FALSE;
+
+       for (i = 0; addr_headers[i] != NULL; i++)
+               if (!strcasecmp(hdr, addr_headers[i]))
+                       return FALSE;
+
+       return FALSE;
+}
 
 Header * procheader_parse_header(gchar * buf)
 {
-       gchar *p = buf;
+       gchar *p;
        Header * header;
+       gboolean addr_field = FALSE;
+
+       cm_return_val_if_fail(buf != NULL, NULL);
 
        if ((*buf == ':') || (*buf == ' '))
                return NULL;
@@ -265,9 +381,10 @@ Header * procheader_parse_header(gchar * buf)
                if ((*p == ':') || (*p == ' ')) {
                        header = g_new(Header, 1);
                        header->name = g_strndup(buf, p - buf + 1);
+                       addr_field = header_is_addr_field(header->name);
                        p++;
                        while (*p == ' ' || *p == '\t') p++;
-                        header->body = conv_unmime_header(p, NULL);
+                       header->body = conv_unmime_header(p, NULL, addr_field);
                        return header;
                }
        }
@@ -276,15 +393,14 @@ Header * procheader_parse_header(gchar * buf)
 
 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
 {
-       gchar buf[BUFFSIZE];
+       gchar *buf = NULL;
        HeaderEntry *hp;
        gint hnum;
        gchar *p;
 
        if (hentry == NULL) return;
 
-       while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
-              != -1) {
+       while ((hnum = procheader_get_one_field(&buf, fp, hentry)) != -1) {
                hp = hentry + hnum;
 
                p = buf + strlen(hp->name);
@@ -298,36 +414,76 @@ void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
                        hp->body = g_strconcat(tp, ", ", p, NULL);
                        g_free(tp);
                }
+               g_free(buf);
+               buf = NULL;
        }
 }
 
 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
                               gboolean full, gboolean decrypted)
 {
-       struct stat s;
+#ifdef G_OS_WIN32
+       GFile *f;
+       GFileInfo *fi;
+       GTimeVal tv;
+       GError *error = NULL;
+#else
+       GStatBuf s;
+#endif
        FILE *fp;
        MsgInfo *msginfo;
 
-       if (stat(file, &s) < 0) {
+#ifdef G_OS_WIN32
+       f = g_file_new_for_path(file);
+       fi = g_file_query_info(f, "standard::size,standard::type,time::modified",
+                       G_FILE_QUERY_INFO_NONE, NULL, &error);
+       if (error != NULL) {
+               g_warning(error->message);
+               g_error_free(error);
+               g_object_unref(f);
+       }
+#else
+       if (g_stat(file, &s) < 0) {
                FILE_OP_ERROR(file, "stat");
                return NULL;
        }
+#endif
+
+#ifdef G_OS_WIN32
+       if (g_file_info_get_file_type(fi) != G_FILE_TYPE_REGULAR) {
+               g_object_unref(fi);
+               g_object_unref(f);
+               return NULL;
+       }
+#else
        if (!S_ISREG(s.st_mode))
                return NULL;
+#endif
 
-       if ((fp = g_fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                return NULL;
        }
 
        msginfo = procheader_parse_stream(fp, flags, full, decrypted);
-       fclose(fp);
+       claws_fclose(fp);
 
        if (msginfo) {
+#ifdef G_OS_WIN32
+               msginfo->size = g_file_info_get_size(fi);
+               g_file_info_get_modification_time(fi, &tv);
+               msginfo->mtime = tv.tv_sec;
+#else
                msginfo->size = s.st_size;
                msginfo->mtime = s.st_mtime;
+#endif
        }
 
+#ifdef G_OS_WIN32
+       g_object_unref(fi);
+       g_object_unref(f);
+#endif
+
        return msginfo;
 }
 
@@ -339,38 +495,39 @@ MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
 
 enum
 {
-       H_DATE          = 0,
-       H_FROM          = 1,
-       H_TO            = 2,
-       H_CC            = 3,
-       H_NEWSGROUPS    = 4,
-       H_SUBJECT       = 5,
-       H_MSG_ID        = 6,
-       H_REFERENCES    = 7,
-       H_IN_REPLY_TO   = 8,
-       H_CONTENT_TYPE  = 9,
-       H_SEEN          = 10,
-       H_STATUS        = 11,
-       H_X_STATUS      = 12,
-       H_FROM_SPACE    = 13,
-       H_SC_PLANNED_DOWNLOAD = 14,
-       H_SC_MESSAGE_SIZE = 15,
-       H_FACE          = 16,
-       H_X_FACE        = 17,
-       H_DISPOSITION_NOTIFICATION_TO = 18,
-       H_RETURN_RECEIPT_TO = 19,
-       H_SC_PARTIALLY_RETRIEVED = 20,
-       H_SC_ACCOUNT_SERVER = 21,
-       H_SC_ACCOUNT_LOGIN = 22,
-       H_LIST_POST        = 23,
-       H_LIST_SUBSCRIBE   = 24,
-       H_LIST_UNSUBSCRIBE = 25,
-       H_LIST_HELP        = 26,
-       H_LIST_ARCHIVE     = 27,
-       H_LIST_OWNER       = 28,
+       H_DATE = 0,
+       H_FROM,
+       H_TO,
+       H_CC,
+       H_NEWSGROUPS,
+       H_SUBJECT,
+       H_MSG_ID,
+       H_REFERENCES,
+       H_IN_REPLY_TO,
+       H_CONTENT_TYPE,
+       H_SEEN,
+       H_STATUS,
+       H_FROM_SPACE,
+       H_SC_PLANNED_DOWNLOAD,
+       H_SC_MESSAGE_SIZE,
+       H_FACE,
+       H_X_FACE,
+       H_DISPOSITION_NOTIFICATION_TO,
+       H_RETURN_RECEIPT_TO,
+       H_SC_PARTIALLY_RETRIEVED,
+       H_SC_ACCOUNT_SERVER,
+       H_SC_ACCOUNT_LOGIN,
+       H_LIST_POST,
+       H_LIST_SUBSCRIBE,
+       H_LIST_UNSUBSCRIBE,
+       H_LIST_HELP,
+       H_LIST_ARCHIVE,
+       H_LIST_OWNER,
+       H_RESENT_FROM,
 };
 
-static HeaderEntry hentry_full[] = {{"Date:",          NULL, FALSE},
+static HeaderEntry hentry_full[] = {
+                                  {"Date:",            NULL, FALSE},
                                   {"From:",            NULL, TRUE},
                                   {"To:",              NULL, TRUE},
                                   {"Cc:",              NULL, TRUE},
@@ -382,7 +539,6 @@ static HeaderEntry hentry_full[] = {{"Date:",               NULL, FALSE},
                                   {"Content-Type:",    NULL, FALSE},
                                   {"Seen:",            NULL, FALSE},
                                   {"Status:",          NULL, FALSE},
-                                  {"X-Status:",        NULL, FALSE},
                                   {"From ",            NULL, FALSE},
                                   {"SC-Marked-For-Download:", NULL, FALSE},
                                   {"SC-Message-Size:", NULL, FALSE},
@@ -399,9 +555,11 @@ static HeaderEntry hentry_full[] = {{"Date:",              NULL, FALSE},
                                   {"List-Help:",       NULL, TRUE},
                                   {"List-Archive:",    NULL, TRUE},
                                   {"List-Owner:",      NULL, TRUE},
+                                  {"Resent-From:",     NULL, TRUE},
                                   {NULL,               NULL, FALSE}};
 
-static HeaderEntry hentry_short[] = {{"Date:",         NULL, FALSE},
+static HeaderEntry hentry_short[] = {
+                                   {"Date:",           NULL, FALSE},
                                    {"From:",           NULL, TRUE},
                                    {"To:",             NULL, TRUE},
                                    {"Cc:",             NULL, TRUE},
@@ -413,13 +571,12 @@ static HeaderEntry hentry_short[] = {{"Date:",            NULL, FALSE},
                                    {"Content-Type:",   NULL, FALSE},
                                    {"Seen:",           NULL, FALSE},
                                    {"Status:",         NULL, FALSE},
-                                   {"X-Status:",       NULL, FALSE},
                                    {"From ",           NULL, FALSE},
                                    {"SC-Marked-For-Download:", NULL, FALSE},
                                    {"SC-Message-Size:",NULL, FALSE},
                                    {NULL,              NULL, FALSE}};
 
-HeaderEntry* procheader_get_headernames(gboolean full)
+static HeaderEntry* procheader_get_headernames(gboolean full)
 {
        return full ? hentry_full : hentry_short;
 }
@@ -430,11 +587,33 @@ MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
        return parse_stream(fp, FALSE, flags, full, decrypted);
 }
 
+static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
+{
+       AvatarCaptureData *acd = (AvatarCaptureData *)source;
+       
+       if (*(acd->content) == '\0') /* won't be null, but may be empty */
+               return FALSE;
+
+       if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
+               debug_print("avatar_from_some_face: found 'Face' header\n");
+               procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
+       }
+#if HAVE_LIBCOMPFACE
+       else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
+               debug_print("avatar_from_some_face: found 'X-Face' header\n");
+               procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
+       }
+#endif
+       return FALSE;
+}
+
+static gulong avatar_hook_id = HOOK_NONE;
+
 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                             gboolean full, gboolean decrypted)
 {
        MsgInfo *msginfo;
-       gchar buf[BUFFSIZE];
+       gchar *buf = NULL;
        gchar *p, *tmp;
        gchar *hp;
        HeaderEntry *hentry;
@@ -448,12 +627,15 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
        hentry = procheader_get_headernames(full);
 
        if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
-               while (get_one_field(buf, sizeof(buf), data, NULL) != -1) {
+               while (get_one_field(&buf, data, NULL) != -1) {
                        if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
                                strlen("X-Claws-End-Special-Headers:"))) ||
                            (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
-                               strlen("X-Sylpheed-End-Special-Headers:"))))
+                               strlen("X-Sylpheed-End-Special-Headers:")))) {
+                               g_free(buf);
+                               buf = NULL;
                                break;
+                       }
                        /* from other mailers */
                        if (!strncmp(buf, "Date: ", 6)
                        ||  !strncmp(buf, "To: ", 4)
@@ -463,8 +645,12 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                                        data = orig_data;
                                else 
                                        rewind((FILE *)data);
+                               g_free(buf);
+                               buf = NULL;
                                break;
                        }
+                       g_free(buf);
+                       buf = NULL;
                }
        }
 
@@ -477,8 +663,14 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
        
        msginfo->inreplyto = NULL;
 
-       while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
-              != -1) {
+       if (avatar_hook_id == HOOK_NONE && (prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
+               avatar_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
+       } else if (avatar_hook_id != HOOK_NONE && !(prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
+               hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_hook_id);
+               avatar_hook_id = HOOK_NONE;
+       }
+
+       while ((hnum = get_one_field(&buf, data, hentry)) != -1) {
                hp = buf + strlen(hentry[hnum].name);
                while (*hp == ' ' || *hp == '\t') hp++;
 
@@ -506,13 +698,13 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_FROM:
                        if (msginfo->from) break;
-                        msginfo->from = conv_unmime_header(hp, NULL);
+                       msginfo->from = conv_unmime_header(hp, NULL, TRUE);
                        msginfo->fromname = procheader_get_fromname(msginfo->from);
                        remove_return(msginfo->from);
                        remove_return(msginfo->fromname);
                        break;
                case H_TO:
-                        tmp = conv_unmime_header(hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
                        remove_return(tmp);
                        if (msginfo->to) {
                                p = msginfo->to;
@@ -524,7 +716,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                         g_free(tmp);                                
                        break;
                case H_CC:
-                        tmp = conv_unmime_header(hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
                        remove_return(tmp);
                        if (msginfo->cc) {
                                p = msginfo->cc;
@@ -546,8 +738,9 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_SUBJECT:
                        if (msginfo->subject) break;
-                        msginfo->subject = conv_unmime_header(hp, NULL);
-                       break;
+                       msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
+                       unfold_line(msginfo->subject);
+                       break;
                case H_MSG_ID:
                        if (msginfo->msgid) break;
 
@@ -576,24 +769,6 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        if (!g_ascii_strncasecmp(hp, "multipart/", 10))
                                MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
                        break;
-#ifdef ALLOW_HEADER_HINT                       
-               case H_SEEN:
-                       /* mnews Seen header */
-                       MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
-                       break;
-#endif                 
-               case H_FACE:
-                       if (!msginfo->extradata)
-                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
-                       if (msginfo->extradata->face) break;
-                       msginfo->extradata->face = g_strdup(hp);
-                       break;
-               case H_X_FACE:
-                       if (!msginfo->extradata)
-                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
-                       if (msginfo->extradata->xface) break;
-                       msginfo->extradata->xface = g_strdup(hp);
-                       break;
                case H_DISPOSITION_NOTIFICATION_TO:
                        if (!msginfo->extradata)
                                msginfo->extradata = g_new0(MsgInfoExtraData, 1);
@@ -633,29 +808,6 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        msginfo->planned_download = atoi(hp);
                        break;
 /* end partial download infos */
-#ifdef ALLOW_HEADER_HINT                       
-               case H_STATUS:
-                       if (strchr(hp, 'R') != NULL)
-                               MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
-                       if (strchr(hp, 'O') != NULL)
-                               MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
-                       if (strchr(hp, 'U') != NULL)
-                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
-                       break;
-               case H_X_STATUS:
-                       if (strchr(hp, 'D') != NULL)
-                               MSG_SET_PERM_FLAGS(msginfo->flags,
-                                             MSG_REALLY_DELETED);
-                       if (strchr(hp, 'F') != NULL)
-                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
-                       if (strchr(hp, 'd') != NULL)
-                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
-                       if (strchr(hp, 'r') != NULL)
-                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
-                       if (strchr(hp, 'f') != NULL)
-                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
-                       break;
-#endif                 
                case H_FROM_SPACE:
                        if (msginfo->fromspace) break;
                        msginfo->fromspace = g_strdup(hp);
@@ -698,10 +850,30 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        if (msginfo->extradata->list_owner) break;
                        msginfo->extradata->list_owner = g_strdup(hp);
                        break;
+               case H_RESENT_FROM:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->resent_from) break;
+                       msginfo->extradata->resent_from = g_strdup(hp);
+                       break;
 /* end list infos */
                default:
                        break;
                }
+               /* to avoid performance penalty hooklist is invoked only for
+                  headers known to be able to generate avatars */
+               if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
+                       AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
+                       /* no extra memory is wasted, hooks are expected to
+                          take care of copying members when needed */
+                       acd->msginfo = msginfo;
+                       acd->header  = hentry_full[hnum].name;
+                       acd->content = hp;
+                       hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
+                       g_free(acd);
+               }
+               g_free(buf);
+               buf = NULL;
        }
 
        if (!msginfo->inreplyto && msginfo->references)
@@ -748,16 +920,29 @@ static gint procheader_scan_date_string(const gchar *str,
                                        gchar *zone)
 {
        gint result;
+       gint month_n;
+       gint secfract;
+       gint zone1 = 0, zone2 = 0;
+       gchar offset_sign, zonestr[7];
+       gchar sep1;
+
+       if (str == NULL)
+               return -1;
 
-       result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %6s",
                        weekday, day, month, year, hh, mm, ss, zone);
        if (result == 8) return 0;
 
-       result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
+       /* RFC2822 */
+       result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %6s",
                        weekday, day, month, year, hh, mm, ss, zone);
        if (result == 8) return 0;
 
-       result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
+       result = sscanf(str, "%3s %3s %d %2d:%2d:%2d %d %6s",
+                       weekday, month, day, hh, mm, ss, year, zone);
+       if (result == 8) return 0;
+
+       result = sscanf(str, "%d %9s %d %2d:%2d:%2d %6s",
                        day, month, year, hh, mm, ss, zone);
        if (result == 7) return 0;
 
@@ -766,12 +951,16 @@ static gint procheader_scan_date_string(const gchar *str,
                        weekday, day, month, year, hh, mm, ss);
        if (result == 7) return 0;
 
+       result = sscanf(str, "%3s %3s %d %2d:%2d:%2d %d",
+                       weekday, month, day, hh, mm, ss, year);
+       if (result == 7) return 0;
+
        result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
                        day, month, year, hh, mm, ss);
        if (result == 6) return 0;
 
        *ss = 0;
-       result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d %6s",
                        weekday, day, month, year, hh, mm, zone);
        if (result == 7) return 0;
 
@@ -788,6 +977,66 @@ static gint procheader_scan_date_string(const gchar *str,
                        day, month, year, hh, mm);
        if (result == 5) return 0;
 
+       *weekday = '\0';
+
+       /* RFC3339 subset, with fraction of second */
+       result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%d%6s",
+                       year, &month_n, day, &sep1, hh, mm, ss, &secfract, zonestr);
+       if (result == 9
+                       && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
+               if (month_n >= 1 && month_n <= 12) {
+                       strncpy2(month, monthstr+((month_n-1)*3), 4);
+                       if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
+                               strcat(zone, "+00:00");
+                       } else if (sscanf(zonestr, "%c%2d:%2d",
+                                               &offset_sign, &zone1, &zone2) == 3) {
+                               strcat(zone, zonestr);
+                       }
+                       return 0;
+               }
+       }
+
+       /* RFC3339 subset, no fraction of second */
+       result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%6s",
+                       year, &month_n, day, &sep1, hh, mm, ss, zonestr);
+       if (result == 8
+                       && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
+               if (month_n >= 1 && month_n <= 12) {
+                       strncpy2(month, monthstr+((month_n-1)*3), 4);
+                       if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
+                               strcat(zone, "+00:00");
+                       } else if (sscanf(zonestr, "%c%2d:%2d",
+                                               &offset_sign, &zone1, &zone2) == 3) {
+                               strcat(zone, zonestr);
+                       }
+                       return 0;
+               }
+       }
+
+       *zone = '\0';
+
+       /* RFC3339 subset, no fraction of second, and no timezone offset */
+       /* This particular "subset" is invalid, RFC requires the offset */
+       result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
+                       year, &month_n, day, hh, mm, ss);
+       if (result == 6) {
+               if (1 <= month_n && month_n <= 12) {
+                       strncpy2(month, monthstr+((month_n-1)*3), 4);
+                       return 0;
+               }
+       }
+
+       /* ISO8601 format with just date (YYYY-MM-DD) */
+       result = sscanf(str, "%4d-%2d-%2d",
+                       year, &month_n, day);
+       if (result == 3) {
+               *hh = *mm = *ss = 0;
+               if (1 <= month_n && month_n <= 12) {
+                       strncpy2(month, monthstr+((month_n-1)*3), 4);
+                       return 0;
+               }
+       }
+
        return -1;
 }
 
@@ -797,7 +1046,6 @@ static gint procheader_scan_date_string(const gchar *str,
  */
 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
 {
-       static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
        gchar weekday[11];
        gint day;
        gchar month[10];
@@ -813,7 +1061,7 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
 
        if (procheader_scan_date_string(src, weekday, &day, month, &year,
                                        &hh, &mm, &ss, zone) < 0) {
-               g_warning("Invalid date: %s\n", src);
+               g_warning("Invalid date: %s", src);
                return FALSE;
        }
 
@@ -829,7 +1077,7 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
        if ((p = strstr(monthstr, month)) != NULL)
                dmonth = (gint)(p - monthstr) / 3 + 1;
        else {
-               g_warning("Invalid month: %s\n", month);
+               g_warning("Invalid month: %s", month);
                dmonth = G_DATE_BAD_MONTH;
        }
 
@@ -850,18 +1098,15 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
 
 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 {
-       static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
        gchar weekday[11];
        gint day;
        gchar month[10];
        gint year;
        gint hh, mm, ss;
-       gchar zone[6];
+       gchar zone[7];
        GDateMonth dmonth = G_DATE_BAD_MONTH;
-       struct tm t;
        gchar *p;
        time_t timer;
-       time_t tz_offset;
 
        if (procheader_scan_date_string(src, weekday, &day, month, &year,
                                        &hh, &mm, &ss, zone) < 0) {
@@ -870,14 +1115,6 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
                return 0;
        }
 
-       /* Y2K compliant :) */
-       if (year < 1000) {
-               if (year < 50)
-                       year += 2000;
-               else
-                       year += 1900;
-       }
-
        month[3] = '\0';
        for (p = monthstr; *p != '\0'; p += 3) {
                if (!g_ascii_strncasecmp(p, month, 3)) {
@@ -886,6 +1123,31 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
                }
        }
 
+#ifdef G_OS_WIN32
+       GTimeZone *tz;
+       GDateTime *dt, *dt2;
+
+       tz = g_time_zone_new(zone); // can't return NULL no need to check for it
+       dt = g_date_time_new(tz, 1, 1, 1, 0, 0, 0);
+       g_time_zone_unref(tz);
+       dt2 = g_date_time_add_full(dt, year-1, dmonth-1, day-1, hh, mm, ss);
+       g_date_time_unref(dt);
+
+       timer = g_date_time_to_unix(dt2);
+       g_date_time_unref(dt2);
+
+#else
+       struct tm t;
+       time_t tz_offset;
+
+       /* Y2K compliant :) */
+       if (year < 1000) {
+               if (year < 50)
+                       year += 2000;
+               else
+                       year += 1900;
+       }
+
        t.tm_sec = ss;
        t.tm_min = mm;
        t.tm_hour = hh;
@@ -903,6 +1165,7 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 
        if (dest)
                procheader_date_get_localtime(dest, len, timer);
+#endif
 
        return timer;
 }
@@ -913,8 +1176,14 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
        gchar *default_format = "%y/%m/%d(%a) %H:%M";
        gchar *str;
        const gchar *src_codeset, *dest_codeset;
+       struct tm buf;
 
-       lt = localtime(&timer);
+       if (timer > 0)
+               lt = localtime_r(&timer, &buf);
+       else {
+               time_t dummy = 1;
+               lt = localtime_r(&dummy, &buf);
+       }
 
        if (prefs_common.date_format)
                fast_strftime(dest, len, prefs_common.date_format, lt);
@@ -934,34 +1203,120 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
 
 /* Added by Mel Hadasht on 27 Aug 2001 */
 /* Get a header from msginfo */
-gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
+gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *header)
 {
        gchar *file;
        FILE *fp;
        HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
-                               { NULL, NULL, FALSE } };
+                                                  { NULL, NULL, FALSE } };
        gint val;
 
-        hentry[0].name = header;
-       
-       g_return_val_if_fail(msginfo != NULL, -1);
+       cm_return_val_if_fail(msginfo != NULL, -1);
+       cm_return_val_if_fail(buf != NULL, -1);
+       cm_return_val_if_fail(header != NULL, -1);
+
+       hentry[0].name = header;
+
        file = procmsg_get_message_file_path(msginfo);
-       if ((fp = g_fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               g_free(file);
-               return -1;
-       }
-       val = procheader_get_one_field(buf,len, fp, hentry);
-       if (fclose(fp) == EOF) {
-               FILE_OP_ERROR(file, "fclose");
-               g_unlink(file);
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                g_free(file);
+               g_free(*buf);
+               *buf = NULL;
+               return -1;
+       }
+       val = procheader_get_one_field(buf, fp, hentry);
+
+       if (claws_fclose(fp) == EOF) {
+               FILE_OP_ERROR(file, "claws_fclose");
+               claws_unlink(file);
+               g_free(file);
+               g_free(*buf);
+               *buf = NULL;
                return -1;
        }
 
        g_free(file);
-        if (val == -1)
+       if (val == -1) {
+               /* *buf is already NULL in that case, see procheader_get_one_field() */
                return -1;
+       }
 
        return 0;
 }
+
+HeaderEntry *procheader_entries_from_str(const gchar *str)
+{
+       HeaderEntry *entries = NULL, *he;
+       int numh = 0, i = 0;
+       gchar **names = NULL;
+       const gchar *s = str;
+
+       if (s == NULL) {
+               return NULL;
+       }
+       while (*s != '\0') {
+               if (*s == ' ') ++numh;
+               ++s;
+       }
+       if (numh == 0) {
+               return NULL;
+       }
+       entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
+       s = str;
+       ++s; /* skip first space */
+       names = g_strsplit(s, " ", numh);
+       he = entries;
+       while (names[i]) {
+               he->name = g_strdup_printf("%s:", names[i]);
+               he->body = NULL;
+               he->unfold = FALSE;
+               ++i, ++he;
+       }
+       he->name = NULL;
+       g_strfreev(names);
+       return entries;
+}
+
+void procheader_entries_free (HeaderEntry *entries)
+{
+       if (entries != NULL) {
+               HeaderEntry *he = entries;
+               while (he->name != NULL) {
+                       g_free(he->name);
+                       if (he->body != NULL)
+                               g_free(he->body);
+                       ++he;                   
+               }
+               g_free(entries);
+       }
+}
+
+gboolean procheader_header_is_internal(const gchar *hdr_name)
+{
+       const gchar *internal_hdrs[] = {
+               "AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", "CFG:",
+               "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", "R:", "MAID:",
+               "SCF:", "RMID:", "FMID:", "NAID:",
+               "X-Claws-Account-Id:",
+               "X-Claws-Sign:",
+               "X-Claws-Encrypt:",
+               "X-Claws-Privacy-System:",
+               "X-Claws-Auto-Wrapping:",
+               "X-Claws-Auto-Indent:",
+               "X-Claws-End-Special-Headers:",
+               "X-Sylpheed-Account-Id:",
+               "X-Sylpheed-Sign:",
+               "X-Sylpheed-Encrypt:",
+               "X-Sylpheed-Privacy-System:",
+               "X-Sylpheed-End-Special-Headers:",
+                NULL
+       };
+       int i;
+
+       for (i = 0; internal_hdrs[i]; i++) {
+               if (!strcmp(hdr_name, internal_hdrs[i]))
+                       return TRUE;
+       }
+       return FALSE;
+}