Fix wrong binary logic
[claws.git] / src / procheader.c
index 8148317c7c2a296b94e95cb39ab89a49a03420c6..330a3107a0d8f4adb1ce3a1a1b6a51b189b2f209 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2012 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <time.h>
+#include <sys/stat.h>
+
+#ifdef G_OS_WIN32
+#  include <w32lib.h>
+#endif
 
-#include "intl.h"
 #include "procheader.h"
 #include "procmsg.h"
 #include "codeconv.h"
 #include "prefs_common.h"
+#include "hooks.h"
 #include "utils.h"
+#include "defs.h"
 
 #define BUFFSIZE       8192
 
-gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
+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[]);
+
+static gint string_get_one_field(gchar *buf, size_t len, 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,
+                                 HeaderEntry hentry[],
+                                 getlinefunc getline, 
+                                 peekcharfunc peekchar,
+                                 gboolean unfold);
+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,
                              HeaderEntry hentry[])
+{
+       return generic_get_one_field(buf, len, fp, hentry,
+                                    (getlinefunc)fgets_crlf, (peekcharfunc)file_peekchar,
+                                    TRUE);
+}
+
+static gint string_get_one_field(gchar *buf, size_t len, char **str,
+                                HeaderEntry hentry[])
+{
+       return generic_get_one_field(buf, len, str, hentry,
+                                    (getlinefunc)string_getline,
+                                    (peekcharfunc)string_peekchar,
+                                    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) {
+               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;
+}
+
+static int string_peekchar(char **str)
+{
+       return **str;
+}
+
+static int file_peekchar(FILE *fp)
+{
+       return ungetc(getc(fp), fp);
+}
+
+static gint generic_get_one_field(gchar *buf, size_t len, void *data,
+                         HeaderEntry *hentry,
+                         getlinefunc getline, peekcharfunc peekchar,
+                         gboolean unfold)
 {
        gint nexthead;
        gint hnum = 0;
@@ -47,7 +132,7 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                /* skip non-required headers */
                do {
                        do {
-                               if (fgets(buf, len, fp) == NULL)
+                               if (getline(buf, len, data) == NULL)
                                        return -1;
                                if (buf[0] == '\r' || buf[0] == '\n')
                                        return -1;
@@ -55,265 +140,75 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
 
                        for (hp = hentry, hnum = 0; hp->name != NULL;
                             hp++, hnum++) {
-                               if (!strncasecmp(hp->name, buf,
+                               if (!g_ascii_strncasecmp(hp->name, buf,
                                                 strlen(hp->name)))
                                        break;
                        }
                } while (hp->name == NULL);
        } else {
-               if (fgets(buf, len, fp) == NULL) return -1;
+               if (getline(buf, len, data) == NULL) return -1;
                if (buf[0] == '\r' || buf[0] == '\n') return -1;
        }
 
-       /* unfold the specified folded line */
-       if (hp && hp->unfold) {
-               gboolean folded = FALSE;
-               gchar *bufp = buf + strlen(buf);
-
-               while (1) {
-                       nexthead = fgetc(fp);
-
-                       /* folded */
-                       if (nexthead == ' ' || nexthead == '\t')
-                               folded = TRUE;
-                       else if (nexthead == EOF)
-                               break;
-                       else if (folded == TRUE) {
-                               /* concatenate next line */
-                               if ((len - (bufp - buf)) <= 2) break;
-
-                               /* replace return code on the tail end
-                                  with space */
-                               *(bufp - 1) = ' ';
-                               *bufp++ = nexthead;
-                               *bufp = '\0';
-                               if (nexthead == '\r' || nexthead == '\n') {
-                                       folded = FALSE;
-                                       continue;
-                               }
-                               if (fgets(bufp, len - (bufp - buf), fp)
-                                   == NULL) break;
-                               bufp += strlen(bufp);
-
-                               folded = FALSE;
-                       } else {
-                               ungetc(nexthead, fp);
-                               break;
-                       }
-               }
-
-               /* remove trailing return code */
-               strretchomp(buf);
-
-               return hnum;
-       }
-
+       /* unfold line */
        while (1) {
-               nexthead = fgetc(fp);
+               nexthead = peekchar(data);
+               /* ([*WSP CRLF] 1*WSP) */
                if (nexthead == ' ' || nexthead == '\t') {
-                       size_t buflen = strlen(buf);
-
+                       size_t buflen;
+                       gboolean skiptab = (nexthead == '\t');
+                       /* trim previous trailing \n if requesting one header or
+                        * unfolding was requested */
+                       if ((!hentry && unfold) || (hp && hp->unfold))
+                               strretchomp(buf);
+
+                       buflen = strlen(buf);
+                       
                        /* concatenate next line */
                        if ((len - buflen) > 2) {
-                               gchar *p = buf + buflen;
-
-                               *p++ = nexthead;
-                               *p = '\0';
-                               buflen++;
-                               if (fgets(p, len - buflen, fp) == NULL)
+                               if (getline(buf + buflen, len - buflen, data) == NULL)
                                        break;
+                               if (skiptab) { /* replace tab with space */
+                                       *(buf + buflen) = ' ';
+                               }
                        } else
                                break;
                } else {
-                       if (nexthead != EOF)
-                               ungetc(nexthead, fp);
+                       /* remove trailing new line */
+                       strretchomp(buf);
                        break;
                }
        }
 
-       /* remove trailing return code */
-       strretchomp(buf);
-
        return hnum;
 }
 
-gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
+gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
 {
-       gboolean folded = FALSE;
-       gint nexthead;
-       gchar *bufp;
-
-       if (fgets(buf, len, fp) == NULL) return NULL;
-       if (buf[0] == '\r' || buf[0] == '\n') return NULL;
-       bufp = buf + strlen(buf);
-
-       while (1) {
-               nexthead = fgetc(fp);
-
-               /* folded */
-               if (nexthead == ' ' || nexthead == '\t')
-                       folded = TRUE;
-               else if (nexthead == EOF)
-                       break;
-               else if (folded == TRUE) {
-                       /* concatenate next line */
-                       if ((len - (bufp - buf)) <= 2) break;
-
-                       /* replace return code on the tail end
-                          with space */
-                       *(bufp - 1) = ' ';
-                       *bufp++ = nexthead;
-                       *bufp = '\0';
-                       if (nexthead == '\r' || nexthead == '\n') {
-                               folded = FALSE;
-                               continue;
-                       }
-                       if (fgets(bufp, len - (bufp - buf), fp)
-                           == NULL) break;
-                       bufp += strlen(bufp);
-
-                       folded = FALSE;
-               } else {
-                       ungetc(nexthead, fp);
-                       break;
-               }
-       }
-
-       /* remove trailing return code */
-       strretchomp(buf);
-
-       return buf;
-}
-
-GSList *procheader_get_header_list_from_file(const gchar *file)
-{
-       FILE *fp;
-       GSList *hlist;
-
-       if ((fp = fopen(file, "r")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return NULL;
-       }
-
-       hlist = procheader_get_header_list(fp);
-
-       fclose(fp);
-       return hlist;
-}
-
-GSList *procheader_get_header_list(FILE *fp)
-{
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *p;
-       GSList *hlist = NULL;
-       Header *header;
-
-       g_return_val_if_fail(fp != NULL, NULL);
-
-       while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
-               if (header = procheader_parse_header(buf))
-                       hlist = g_slist_append(hlist, 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++;
-                               while (*p == ' ' || *p == '\t') p++;
-                               conv_unmime_header(tmp, sizeof(tmp), p, NULL);
-                               header->body = g_strdup(tmp);
-
-                               hlist = g_slist_append(hlist, header);
-                               break;
-                       }
-               }
-               */
-       }
-
-       return hlist;
-}
-
-GPtrArray *procheader_get_header_array(FILE *fp)
-{
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *p;
-       GPtrArray *headers;
-       Header *header;
-
-       g_return_val_if_fail(fp != NULL, NULL);
-
-       headers = g_ptr_array_new();
-
-       while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
-               if (header = procheader_parse_header(buf))
-                       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++;
-                               while (*p == ' ' || *p == '\t') p++;
-                               conv_unmime_header(tmp, sizeof(tmp), p, NULL);
-                               header->body = g_strdup(tmp);
-
-                               g_ptr_array_add(headers, header);
-                               break;
-                       }
-               }
-               */
-       }
-
-       return headers;
+       return generic_get_one_field(buf, len, fp, NULL,
+                                    (getlinefunc)fgets_crlf, 
+                                    (peekcharfunc)file_peekchar,
+                                    FALSE);
 }
 
 GPtrArray *procheader_get_header_array_asis(FILE *fp)
 {
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *p;
+       gchar buf[BUFFSIZE];
        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(buf, sizeof(buf), fp, NULL) != -1) {
-               if (header = procheader_parse_header(buf))
+       while (procheader_get_one_field_asis(buf, sizeof(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;
-                       }
-               }
-                       */
        }
 
        return headers;
 }
 
-void procheader_header_list_destroy(GSList *hlist)
-{
-       Header *header;
-
-       while (hlist != NULL) {
-               header = hlist->data;
-               procheader_header_free(header);
-               hlist = g_slist_remove(hlist, header);
-       }
-}
-
 void procheader_header_array_destroy(GPtrArray *harray)
 {
        gint i;
@@ -348,13 +243,14 @@ gboolean procheader_headername_equal(char * hdr1, char * hdr2)
 
        len1 = strlen(hdr1);
        len2 = strlen(hdr2);
-       if ((hdr1[len1 - 1] == ':') || (hdr1[len1 - 1] == ' '))
+       if (hdr1[len1 - 1] == ':')
                len1--;
-       if ((hdr2[len2 - 1] == ':') || (hdr2[len2 - 1] == ' '))
+       if (hdr2[len2 - 1] == ':')
                len2--;
        if (len1 != len2)
                return 0;
-       return (g_strncasecmp(hdr1, hdr2, len1) == 0);
+
+       return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
 }
 
 /*
@@ -363,12 +259,36 @@ 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 tmp[BUFFSIZE];
-       gchar *p = buf;
+       gchar *p;
        Header * header;
+       gboolean addr_field = FALSE;
 
        if ((*buf == ':') || (*buf == ' '))
                return NULL;
@@ -377,10 +297,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++;
-                       conv_unmime_header(tmp, sizeof(tmp), p, NULL);
-                       header->body = g_strdup(tmp);
+                       header->body = conv_unmime_header(p, NULL, addr_field);
                        return header;
                }
        }
@@ -414,80 +334,213 @@ void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
        }
 }
 
+MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
+                              gboolean full, gboolean decrypted)
+{
+       struct stat s;
+       FILE *fp;
+       MsgInfo *msginfo;
+
+       if (g_stat(file, &s) < 0) {
+               FILE_OP_ERROR(file, "stat");
+               return NULL;
+       }
+       if (!S_ISREG(s.st_mode))
+               return NULL;
+
+       if ((fp = g_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               return NULL;
+       }
+
+       msginfo = procheader_parse_stream(fp, flags, full, decrypted);
+       fclose(fp);
+
+       if (msginfo) {
+               msginfo->size = s.st_size;
+               msginfo->mtime = s.st_mtime;
+       }
+
+       return msginfo;
+}
+
+MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
+                             gboolean decrypted)
+{
+       return parse_stream(&str, TRUE, flags, full, decrypted);
+}
+
 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_X_FACE        = 11,
-       H_DISPOSITION_NOTIFICATION_TO = 12,
-       H_RETURN_RECEIPT_TO = 13
+       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,
 };
 
-MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
+static HeaderEntry hentry_full[] = {
+                                  {"Date:",            NULL, FALSE},
+                                  {"From:",            NULL, TRUE},
+                                  {"To:",              NULL, TRUE},
+                                  {"Cc:",              NULL, TRUE},
+                                  {"Newsgroups:",      NULL, TRUE},
+                                  {"Subject:",         NULL, TRUE},
+                                  {"Message-ID:",      NULL, FALSE},
+                                  {"References:",      NULL, FALSE},
+                                  {"In-Reply-To:",     NULL, FALSE},
+                                  {"Content-Type:",    NULL, FALSE},
+                                  {"Seen:",            NULL, FALSE},
+                                  {"Status:",          NULL, FALSE},
+                                  {"From ",            NULL, FALSE},
+                                  {"SC-Marked-For-Download:", NULL, FALSE},
+                                  {"SC-Message-Size:", NULL, FALSE},
+                                  {"Face:",            NULL, FALSE},
+                                  {"X-Face:",          NULL, FALSE},
+                                  {"Disposition-Notification-To:", NULL, FALSE},
+                                  {"Return-Receipt-To:", NULL, FALSE},
+                                  {"SC-Partially-Retrieved:", NULL, FALSE},
+                                  {"SC-Account-Server:", NULL, FALSE},
+                                  {"SC-Account-Login:",NULL, FALSE},
+                                  {"List-Post:",       NULL, TRUE},
+                                  {"List-Subscribe:",  NULL, TRUE},
+                                  {"List-Unsubscribe:",NULL, TRUE},
+                                  {"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},
+                                   {"From:",           NULL, TRUE},
+                                   {"To:",             NULL, TRUE},
+                                   {"Cc:",             NULL, TRUE},
+                                   {"Newsgroups:",     NULL, TRUE},
+                                   {"Subject:",        NULL, TRUE},
+                                   {"Message-ID:",     NULL, FALSE},
+                                   {"References:",     NULL, FALSE},
+                                   {"In-Reply-To:",    NULL, FALSE},
+                                   {"Content-Type:",   NULL, FALSE},
+                                   {"Seen:",           NULL, FALSE},
+                                   {"Status:",         NULL, FALSE},
+                                   {"From ",           NULL, FALSE},
+                                   {"SC-Marked-For-Download:", NULL, FALSE},
+                                   {"SC-Message-Size:",NULL, FALSE},
+                                   {NULL,              NULL, FALSE}};
+
+static HeaderEntry* procheader_get_headernames(gboolean full)
 {
-       static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
-                                          {"From:",            NULL, TRUE},
-                                          {"To:",              NULL, TRUE},
-                                          {"Cc:",              NULL, TRUE},
-                                          {"Newsgroups:",      NULL, TRUE},
-                                          {"Subject:",         NULL, TRUE},
-                                          {"Message-Id:",      NULL, FALSE},
-                                          {"References:",      NULL, FALSE},
-                                          {"In-Reply-To:",     NULL, FALSE},
-                                          {"Content-Type:",    NULL, FALSE},
-                                          {"Seen:",            NULL, FALSE},
-                                          {"X-Face:",          NULL, FALSE},
-                                          {"Disposition-Notification-To:", NULL, FALSE},
-                                          {"Return-Receipt-To:", NULL, FALSE},
-                                          {NULL,               NULL, FALSE}};
-
-       static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
-                                           {"From:",           NULL, TRUE},
-                                           {"To:",             NULL, TRUE},
-                                           {"Cc:",             NULL, TRUE},
-                                           {"Newsgroups:",     NULL, TRUE},
-                                           {"Subject:",        NULL, TRUE},
-                                           {"Message-Id:",     NULL, FALSE},
-                                           {"References:",     NULL, FALSE},
-                                           {"In-Reply-To:",    NULL, FALSE},
-                                           {"Content-Type:",   NULL, FALSE},
-                                           {"Seen:",           NULL, FALSE},
-                                           {NULL,              NULL, FALSE}};
+       return full ? hentry_full : hentry_short;
+}
 
-       FILE *fp;
+MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
+                                gboolean decrypted)
+{
+       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 guint avatar_hook_id = 0;
+
+static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
+                            gboolean full, gboolean decrypted)
+{
        MsgInfo *msginfo;
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *reference = NULL;
-       gchar *p;
+       gchar buf[BUFFSIZE];
+       gchar *p, *tmp;
        gchar *hp;
        HeaderEntry *hentry;
        gint hnum;
+       void *orig_data = data;
 
-       hentry = full ? hentry_full : hentry_short;
+       get_one_field_func get_one_field =
+               isstring ? (get_one_field_func)string_get_one_field
+                        : (get_one_field_func)procheader_get_one_field;
 
-       if ((fp = fopen(file, "r")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return NULL;
-       }
-       if (MSG_IS_QUEUED(flags)) {
-               while (fgets(buf, sizeof(buf), fp) != NULL)
-                       if (buf[0] == '\r' || buf[0] == '\n') break;
+       hentry = procheader_get_headernames(full);
+
+       if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
+               while (get_one_field(buf, sizeof(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:"))))
+                               break;
+                       /* from other mailers */
+                       if (!strncmp(buf, "Date: ", 6)
+                       ||  !strncmp(buf, "To: ", 4)
+                       ||  !strncmp(buf, "From: ", 6)
+                       ||  !strncmp(buf, "Subject: ", 9)) {
+                               if (isstring)
+                                       data = orig_data;
+                               else 
+                                       rewind((FILE *)data);
+                               break;
+                       }
+               }
        }
 
-       msginfo = g_new0(MsgInfo, 1);
-       msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
+       msginfo = procmsg_msginfo_new();
+       
+       if (flags.tmp_flags || flags.perm_flags) 
+               msginfo->flags = flags;
+       else 
+               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
+       
        msginfo->inreplyto = NULL;
 
-       while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
+       if (avatar_hook_id == 0 && (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 != 0 && !(prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
+               hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_hook_id);
+               avatar_hook_id = 0;
+       }
+
+       while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
               != -1) {
                hp = buf + strlen(hentry[hnum].name);
                while (*hp == ' ' || *hp == '\t') hp++;
@@ -497,16 +550,33 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                        if (msginfo->date) break;
                        msginfo->date_t =
                                procheader_date_parse(NULL, hp, 0);
-                       msginfo->date = g_strdup(hp);
+                       if (g_utf8_validate(hp, -1, NULL)) {
+                               msginfo->date = g_strdup(hp);
+                       } else {
+                               gchar *utf = conv_codeset_strdup(
+                                       hp, 
+                                       conv_get_locale_charset_str_no_utf8(),
+                                       CS_INTERNAL);
+                               if (utf == NULL || 
+                                   !g_utf8_validate(utf, -1, NULL)) {
+                                       g_free(utf);
+                                       utf = g_malloc(strlen(buf)*2+1);
+                                       conv_localetodisp(utf, 
+                                               strlen(hp)*2+1, hp);
+                               }
+                               msginfo->date = utf;
+                       }
                        break;
                case H_FROM:
                        if (msginfo->from) break;
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
-                       msginfo->from = g_strdup(tmp);
-                       msginfo->fromname = procheader_get_fromname(tmp);
+                       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:
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
+                       remove_return(tmp);
                        if (msginfo->to) {
                                p = msginfo->to;
                                msginfo->to =
@@ -514,9 +584,11 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                                g_free(p);
                        } else
                                msginfo->to = g_strdup(tmp);
+                        g_free(tmp);                                
                        break;
                case H_CC:
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
+                       remove_return(tmp);
                        if (msginfo->cc) {
                                p = msginfo->cc;
                                msginfo->cc =
@@ -524,6 +596,7 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                                g_free(p);
                        } else
                                msginfo->cc = g_strdup(tmp);
+                        g_free(tmp);                                
                        break;
                case H_NEWSGROUPS:
                        if (msginfo->newsgroups) {
@@ -532,13 +605,13 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                                        g_strconcat(p, ",", hp, NULL);
                                g_free(p);
                        } else
-                               msginfo->newsgroups = g_strdup(buf + 12);
+                               msginfo->newsgroups = g_strdup(hp);
                        break;
                case H_SUBJECT:
                        if (msginfo->subject) break;
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
-                       msginfo->subject = g_strdup(tmp);
-                       break;
+                       msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
+                       unfold_line(msginfo->subject);
+                       break;
                case H_MSG_ID:
                        if (msginfo->msgid) break;
 
@@ -547,52 +620,134 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                        msginfo->msgid = g_strdup(hp);
                        break;
                case H_REFERENCES:
-                       if (!reference) {
-                               eliminate_parenthesis(hp, '(', ')');
-                               if ((p = strrchr(hp, '<')) != NULL &&
-                                   strchr(p + 1, '>') != NULL) {
-                                       extract_parenthesis(p, '<', '>');
-                                       remove_space(p);
-                                       if (*p != '\0')
-                                               reference = g_strdup(p);
-                               }
-                       }
+                       msginfo->references =
+                               references_list_prepend(msginfo->references,
+                                                       hp);
                        break;
                case H_IN_REPLY_TO:
-                       if (!reference) {
-                               eliminate_parenthesis(hp, '(', ')');
-                               extract_parenthesis(hp, '<', '>');
-                               remove_space(hp);
-                               if (*hp != '\0')
-                                       reference = g_strdup(hp);
+                       if (msginfo->inreplyto) break;
+
+                       eliminate_parenthesis(hp, '(', ')');
+                       if ((p = strrchr(hp, '<')) != NULL &&
+                           strchr(p + 1, '>') != NULL) {
+                               extract_parenthesis(p, '<', '>');
+                               remove_space(p);
+                               if (*p != '\0')
+                                       msginfo->inreplyto = g_strdup(p);
                        }
                        break;
                case H_CONTENT_TYPE:
-                       if (!strncasecmp(hp, "multipart", 9))
-                               msginfo->flags |= MSG_MIME;
-                       break;
-               case H_SEEN:
-                       /* mnews Seen header */
-                       MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
-                       break;
-               case H_X_FACE:
-                       if (msginfo->xface) break;
-                       msginfo->xface = g_strdup(hp);
+                       if (!g_ascii_strncasecmp(hp, "multipart/", 10))
+                               MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
                        break;
                case H_DISPOSITION_NOTIFICATION_TO:
-                       if (msginfo->dispositionnotificationto) break;
-                       msginfo->dispositionnotificationto = g_strdup(hp);
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->dispositionnotificationto) break;
+                       msginfo->extradata->dispositionnotificationto = g_strdup(hp);
                        break;
                case H_RETURN_RECEIPT_TO:
-                       if (msginfo->returnreceiptto) break;
-                       msginfo->returnreceiptto = g_strdup(hp);
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->returnreceiptto) break;
+                       msginfo->extradata->returnreceiptto = g_strdup(hp);
+                       break;
+/* partial download infos */                   
+               case H_SC_PARTIALLY_RETRIEVED:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->partial_recv) break;
+                       msginfo->extradata->partial_recv = g_strdup(hp);
+                       break;
+               case H_SC_ACCOUNT_SERVER:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->account_server) break;
+                       msginfo->extradata->account_server = g_strdup(hp);
+                       break;
+               case H_SC_ACCOUNT_LOGIN:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->account_login) break;
+                       msginfo->extradata->account_login = g_strdup(hp);
+                       break;
+               case H_SC_MESSAGE_SIZE:
+                       if (msginfo->total_size) break;
+                       msginfo->total_size = atoi(hp);
+                       break;
+               case H_SC_PLANNED_DOWNLOAD:
+                       msginfo->planned_download = atoi(hp);
+                       break;
+/* end partial download infos */
+               case H_FROM_SPACE:
+                       if (msginfo->fromspace) break;
+                       msginfo->fromspace = g_strdup(hp);
+                       remove_return(msginfo->fromspace);
+                       break;
+/* list infos */
+               case H_LIST_POST:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->list_post) break;
+                       msginfo->extradata->list_post = g_strdup(hp);
+                       break;
+               case H_LIST_SUBSCRIBE:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->list_subscribe) break;
+                       msginfo->extradata->list_subscribe = g_strdup(hp);
+                       break;
+               case H_LIST_UNSUBSCRIBE:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->list_unsubscribe) break;
+                       msginfo->extradata->list_unsubscribe = g_strdup(hp);
+                       break;
+               case H_LIST_HELP:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->list_help) break;
+                       msginfo->extradata->list_help = g_strdup(hp);
                        break;
+               case H_LIST_ARCHIVE:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->list_archive) break;
+                       msginfo->extradata->list_archive = g_strdup(hp);
+                       break;
+               case H_LIST_OWNER:
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       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);
                }
        }
-       msginfo->inreplyto = reference;
 
-       fclose(fp);
+       if (!msginfo->inreplyto && msginfo->references)
+               msginfo->inreplyto =
+                       g_strdup((gchar *)msginfo->references->data);
 
        return msginfo;
 }
@@ -601,8 +756,7 @@ gchar *procheader_get_fromname(const gchar *str)
 {
        gchar *tmp, *name;
 
-       Xalloca(tmp, strlen(str) + 1, return NULL);
-       strcpy(tmp, str);
+       Xstrdup_a(tmp, str, return NULL);
 
        if (*tmp == '\"') {
                extract_quote(tmp, '\"');
@@ -628,43 +782,112 @@ gchar *procheader_get_fromname(const gchar *str)
        return name;
 }
 
-time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
+static gint procheader_scan_date_string(const gchar *str,
+                                       gchar *weekday, gint *day,
+                                       gchar *month, gint *year,
+                                       gint *hh, gint *mm, gint *ss,
+                                       gchar *zone)
 {
-       static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
-       gchar weekday[4];
+       gint result;
+       gint month_n;
+       gchar zone1[3];
+       gchar zone2[3];
+
+       if (str == NULL)
+               return -1;
+
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
+                       weekday, day, month, year, hh, mm, ss, zone);
+       if (result == 8) return 0;
+
+       /* RFC2822 */
+       result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
+                       weekday, day, month, year, hh, mm, ss, zone);
+       if (result == 8) return 0;
+
+       result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
+                       day, month, year, hh, mm, ss, zone);
+       if (result == 7) return 0;
+
+       *zone = '\0';
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
+                       weekday, day, month, year, hh, mm, ss);
+       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",
+                       weekday, day, month, year, hh, mm, zone);
+       if (result == 7) return 0;
+
+       result = sscanf(str, "%d %9s %d %2d:%2d %5s",
+                       day, month, year, hh, mm, zone);
+       if (result == 6) return 0;
+
+       *zone = '\0';
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d",
+                       weekday, day, month, year, hh, mm);
+       if (result == 6) return 0;
+
+       result = sscanf(str, "%d %9s %d %2d:%2d",
+                       day, month, year, hh, mm);
+       if (result == 5) return 0;
+
+       /* RFC3339 subset */
+       *weekday = '\0';
+       result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d+%2s:%2s",
+                       year, &month_n, day, hh, mm, ss, zone1, zone2);
+       if (result == 8) {
+               if (1 <= month_n && month_n <= 12) {
+                       strncpy2(month, monthstr+((month_n-1)*3), 4);
+                       *zone = '+';
+                       strncpy2(zone+1, zone1, 3);
+                       strncpy2(zone+3, zone2, 3);
+                       return 0;
+               }
+       }
+
+       /* RFC3339 subset */
+       *zone = '\0';
+       *weekday = '\0';
+       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;
+               }
+       }
+
+       return -1;
+}
+
+/*
+ * Hiro, most UNIXen support this function:
+ * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
+ */
+gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
+{
+       gchar weekday[11];
        gint day;
-       gchar month[4];
+       gchar month[10];
        gint year;
        gint hh, mm, ss;
-       gchar zone[6];
-       gint result;
        GDateMonth dmonth;
-       struct tm t;
        gchar *p;
-       time_t timer;
 
-       /* parsing date field... */
-       result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
-                       weekday, &day, month, &year, &hh, &mm, &ss, zone);
-       if (result != 8) {
-               result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
-                               &day, month, &year, &hh, &mm, &ss, zone);
-               if (result != 7) {
-                       ss = 0;
-                       result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
-                                       weekday, &day, month, &year, &hh, &mm, zone);
-                       if (result != 7) {
-                               result = sscanf(src, "%d %3s %d %2d:%2d %5s",
-                                               &day, month, &year, &hh, &mm,
-                                               zone);
-                               if (result != 6) {
-                                       g_warning("Invalid date: %s\n", src);
-                                       if (dest && len > 0)
-                                               strncpy2(dest, src, len);
-                                       return 0;
-                               }
-                       }
-               }
+       if (!t)
+               return FALSE;
+       
+       memset(t, 0, sizeof *t);        
+
+       if (procheader_scan_date_string(src, weekday, &day, month, &year,
+                                       &hh, &mm, &ss, zone) < 0) {
+               g_warning("Invalid date: %s\n", src);
+               return FALSE;
        }
 
        /* Y2K compliant :) */
@@ -675,6 +898,7 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
                        year += 1900;
        }
 
+       month[3] = '\0';
        if ((p = strstr(monthstr, month)) != NULL)
                dmonth = (gint)(p - monthstr) / 3 + 1;
        else {
@@ -682,6 +906,58 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
                dmonth = G_DATE_BAD_MONTH;
        }
 
+       t->tm_sec = ss;
+       t->tm_min = mm;
+       t->tm_hour = hh;
+       t->tm_mday = day;
+       t->tm_mon = dmonth - 1;
+       t->tm_year = year - 1900;
+       t->tm_wday = 0;
+       t->tm_yday = 0;
+       t->tm_isdst = -1;
+
+       mktime(t);
+
+       return TRUE;
+}
+
+time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
+{
+       gchar weekday[11];
+       gint day;
+       gchar month[10];
+       gint year;
+       gint hh, mm, ss;
+       gchar zone[6];
+       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) {
+               if (dest && len > 0)
+                       strncpy2(dest, src, 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)) {
+                       dmonth = (gint)(p - monthstr) / 3 + 1;
+                       break;
+               }
+       }
+
        t.tm_sec = ss;
        t.tm_min = mm;
        t.tm_hour = hh;
@@ -693,7 +969,9 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
        t.tm_isdst = -1;
 
        timer = mktime(&t);
-       timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
+       tz_offset = remote_tzoffset_sec(zone);
+       if (tz_offset != -1)
+               timer += tzoffset_sec(&timer) - tz_offset;
 
        if (dest)
                procheader_date_get_localtime(dest, len, timer);
@@ -705,11 +983,111 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
 {
        struct tm *lt;
        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)
-               strftime(dest, len, prefs_common.date_format, lt);
+               fast_strftime(dest, len, prefs_common.date_format, lt);
        else
-               strftime(dest, len, default_format, lt);
+               fast_strftime(dest, len, default_format, lt);
+
+       if (!g_utf8_validate(dest, -1, NULL)) {
+               src_codeset = conv_get_locale_charset_str_no_utf8();
+               dest_codeset = CS_UTF_8;
+               str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
+               if (str) {
+                       strncpy2(dest, str, len);
+                       g_free(str);
+               }
+       }
+}
+
+/* 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)
+{
+       gchar *file;
+       FILE *fp;
+       HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
+                               { NULL, NULL, FALSE } };
+       gint val;
+
+        hentry[0].name = header;
+       
+       cm_return_val_if_fail(msginfo != NULL, -1);
+       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");
+               claws_unlink(file);
+               g_free(file);
+               return -1;
+       }
+
+       g_free(file);
+        if (val == -1)
+               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);
+       }
+}
+