0.8.10claws34
[claws.git] / src / procheader.c
index 9a280cdcb9a9b59650d4b47fe5b8e291be3fd382..9bd66a78afd6140ceecf201209310b297c57806c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
  *
  * 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
 
 #define BUFFSIZE       8192
 
-/*
-  procheader_get_one_field
-  - reads fp and puts the header and the corresponding content into buf
-    if one of these is one of hentry table.
-  - if hentry is NULL, ignores no headers
- */
-
 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                              HeaderEntry hentry[])
 {
@@ -77,6 +70,11 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                gboolean folded = FALSE;
                gchar *bufp = buf + strlen(buf);
 
+               for (; bufp > buf &&
+                    (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+                    bufp--)
+                       *(bufp - 1) = '\0';
+
                while (1) {
                        nexthead = fgetc(fp);
 
@@ -86,22 +84,28 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                        else if (nexthead == EOF)
                                break;
                        else if (folded == TRUE) {
-                               /* concatenate next line */
+                               if (nexthead == '\r' || nexthead == '\n') {
+                                       folded = FALSE;
+                                       continue;
+                               }
+
                                if ((len - (bufp - buf)) <= 2) break;
 
                                /* replace return code on the tail end
                                   with space */
-                               *(bufp - 1) = ' ';
+                               *bufp++ = ' ';
                                *bufp++ = nexthead;
                                *bufp = '\0';
-                               if (nexthead == '\r' || nexthead == '\n') {
-                                       folded = FALSE;
-                                       continue;
-                               }
+                               /* concatenate next line */
                                if (fgets(bufp, len - (bufp - buf), fp)
                                    == NULL) break;
                                bufp += strlen(bufp);
 
+                               for (; bufp > buf &&
+                                    (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+                                    bufp--)
+                                       *(bufp - 1) = '\0';
+
                                folded = FALSE;
                        } else {
                                ungetc(nexthead, fp);
@@ -109,9 +113,6 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                        }
                }
 
-               /* remove trailing return code */
-               strretchomp(buf);
-
                return hnum;
        }
 
@@ -154,6 +155,11 @@ gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
        if (buf[0] == '\r' || buf[0] == '\n') return NULL;
        bufp = buf + strlen(buf);
 
+       for (; bufp > buf &&
+            (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+            bufp--)
+               *(bufp - 1) = '\0';
+
        while (1) {
                nexthead = fgetc(fp);
 
@@ -163,22 +169,29 @@ gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
                else if (nexthead == EOF)
                        break;
                else if (folded == TRUE) {
-                       /* concatenate next line */
+                       if (nexthead == '\r' || nexthead == '\n') {
+                               folded = FALSE;
+                               continue;
+                       }
+
                        if ((len - (bufp - buf)) <= 2) break;
 
                        /* replace return code on the tail end
                           with space */
-                       *(bufp - 1) = ' ';
+                       *bufp++ = ' ';
                        *bufp++ = nexthead;
                        *bufp = '\0';
-                       if (nexthead == '\r' || nexthead == '\n') {
-                               folded = FALSE;
-                               continue;
-                       }
+
+                       /* concatenate next line */
                        if (fgets(bufp, len - (bufp - buf), fp)
                            == NULL) break;
                        bufp += strlen(bufp);
 
+                       for (; bufp > buf &&
+                            (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+                            bufp--)
+                               *(bufp - 1) = '\0';
+
                        folded = FALSE;
                } else {
                        ungetc(nexthead, fp);
@@ -192,20 +205,34 @@ gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
        return buf;
 }
 
-GSList *procheader_get_header_list(const gchar *file)
+GSList *procheader_get_header_list_from_file(const gchar *file)
 {
        FILE *fp;
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *p;
-       GSList *hlist = NULL;
-       Header *header;
+       GSList *hlist;
 
-       if ((fp = fopen(file, "r")) == NULL) {
+       if ((fp = fopen(file, "rb")) == 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];
+       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)) != NULL)
+                       hlist = g_slist_append(hlist, header);
+               /*
                if (*buf == ':') continue;
                for (p = buf; *p && *p != ' '; p++) {
                        if (*p == ':') {
@@ -220,26 +247,167 @@ GSList *procheader_get_header_list(const gchar *file)
                                break;
                        }
                }
+               */
        }
 
-       fclose(fp);
        return hlist;
 }
 
+GPtrArray *procheader_get_header_array(FILE *fp)
+{
+       gchar buf[BUFFSIZE];
+       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)) != 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++;
+                               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;
+}
+
+GPtrArray *procheader_get_header_array_asis(FILE *fp)
+{
+       gchar buf[BUFFSIZE];
+       GPtrArray *headers;
+       Header *header;
+
+       g_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)) != 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;
-
-               g_free(header->name);
-               g_free(header->body);
-               g_free(header);
+               procheader_header_free(header);
                hlist = g_slist_remove(hlist, header);
        }
 }
 
+void procheader_header_array_destroy(GPtrArray *harray)
+{
+       gint i;
+       Header *header;
+
+       for (i = 0; i < harray->len; i++) {
+               header = g_ptr_array_index(harray, i);
+               procheader_header_free(header);
+       }
+
+       g_ptr_array_free(harray, TRUE);
+}
+
+void procheader_header_free(Header *header)
+{
+       if (!header) return;
+
+       g_free(header->name);
+       g_free(header->body);
+       g_free(header);
+}
+
+/*
+  tests whether two headers' names are equal
+  remove the trailing ':' or ' ' before comparing
+*/
+
+gboolean procheader_headername_equal(char * hdr1, char * hdr2)
+{
+       int len1;
+       int len2;
+
+       len1 = strlen(hdr1);
+       len2 = strlen(hdr2);
+       if (hdr1[len1 - 1] == ':')
+               len1--;
+       if (hdr2[len2 - 1] == ':')
+               len2--;
+       if (len1 != len2)
+               return 0;
+
+       return (g_strncasecmp(hdr1, hdr2, len1) == 0);
+}
+
+/*
+  parse headers, for example :
+  From: dinh@enseirb.fr becomes :
+  header->name = "From:"
+  header->body = "dinh@enseirb.fr"
+ */
+
+Header * procheader_parse_header(gchar * buf)
+{
+       gchar tmp[BUFFSIZE];
+       gchar *p = buf;
+       Header * header;
+
+       if ((*buf == ':') || (*buf == ' '))
+               return NULL;
+
+       for (p = buf; *p ; p++) {
+               if ((*p == ':') || (*p == ' ')) {
+                       header = g_new(Header, 1);
+                       header->name = g_strndup(buf, p - buf + 1);
+                       p++;
+                       while (*p == ' ' || *p == '\t') p++;
+                       conv_unmime_header(tmp, sizeof(tmp), p, NULL);
+                       if(tmp == NULL) 
+                               header->body = g_strdup(p);
+                       else    
+                               header->body = g_strdup(tmp);
+                       return header;
+               }
+       }
+       return NULL;
+}
+
 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
 {
        gchar buf[BUFFSIZE];
@@ -258,8 +426,8 @@ void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
 
                if (hp->body == NULL)
                        hp->body = g_strdup(p);
-               else if (!strcasecmp(hp->name, "To:") ||
-                        !strcasecmp(hp->name, "Cc:")) {
+               else if (procheader_headername_equal(hp->name, "To") ||
+                        procheader_headername_equal(hp->name, "Cc")) {
                        gchar *tp = hp->body;
                        hp->body = g_strconcat(tp, ", ", p, NULL);
                        g_free(tp);
@@ -267,72 +435,125 @@ void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
        }
 }
 
+MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
+                              gboolean full, gboolean decrypted)
+{
+       FILE *fp;
+       MsgInfo *msginfo;
+
+       if ((fp = fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               return NULL;
+       }
+
+       msginfo = procheader_parse_stream(fp, flags, full, decrypted);
+       fclose(fp);
+       return msginfo;
+}
+
+MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
+                             gboolean decrypted)
+{
+       FILE *fp;
+       MsgInfo *msginfo;
+
+       if ((fp = str_open_as_stream(str)) == NULL)
+               return NULL;
+
+       msginfo = procheader_parse_stream(fp, flags, full, decrypted);
+       fclose(fp);
+       return msginfo;
+}
+
 enum
 {
        H_DATE          = 0,
        H_FROM          = 1,
        H_TO            = 2,
-       H_NEWSGROUPS    = 3,
-       H_SUBJECT       = 4,
-       H_MSG_ID        = 5,
-       H_REFERENCES    = 6,
-       H_IN_REPLY_TO   = 7,
-       H_CONTENT_TYPE  = 8,
-       H_SEEN          = 9,
-       H_X_FACE        = 10,
-       H_DISPOSITION_NOTIFICATION_TO = 11
+       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_X_FACE        = 14,
+       H_DISPOSITION_NOTIFICATION_TO = 15,
+       H_RETURN_RECEIPT_TO = 16
 };
 
-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},
+                                  {"X-Status:",        NULL, FALSE},
+                                  {"From ",            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},
+                                   {"Status:",         NULL, FALSE},
+                                   {"X-Status:",       NULL, FALSE},
+                                   {"From ",           NULL, FALSE},
+                                   {NULL,              NULL, FALSE}};
+
+const HeaderEntry* procheader_get_headernames(gboolean full)
 {
-       static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
-                                          {"From:",            NULL, TRUE},
-                                          {"To:",              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},
-                                          {NULL,               NULL, FALSE}};
-
-       static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
-                                           {"From:",           NULL, TRUE},
-                                           {"To:",             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)
+{
        MsgInfo *msginfo;
        gchar buf[BUFFSIZE], tmp[BUFFSIZE];
        gchar *reference = NULL;
        gchar *p;
        gchar *hp;
-       HeaderEntry *hentry;
+       const HeaderEntry *hentry;
        gint hnum;
 
-       hentry = full ? hentry_full : hentry_short;
+       hentry = procheader_get_headernames(full);
 
-       if ((fp = fopen(file, "r")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return NULL;
-       }
-       if (MSG_IS_QUEUED(flags)) {
+       if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
                while (fgets(buf, sizeof(buf), fp) != NULL)
                        if (buf[0] == '\r' || buf[0] == '\n') 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);
+       
+       if (decrypted)
+               MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
+
        msginfo->inreplyto = NULL;
 
        while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
@@ -363,6 +584,16 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                        } else
                                msginfo->to = g_strdup(tmp);
                        break;
+               case H_CC:
+                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
+                       if (msginfo->cc) {
+                               p = msginfo->cc;
+                               msginfo->cc =
+                                       g_strconcat(p, ", ", tmp, NULL);
+                               g_free(p);
+                       } else
+                               msginfo->cc = g_strdup(tmp);
+                       break;
                case H_NEWSGROUPS:
                        if (msginfo->newsgroups) {
                                p = msginfo->newsgroups;
@@ -370,7 +601,7 @@ 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;
@@ -385,7 +616,9 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                        msginfo->msgid = g_strdup(hp);
                        break;
                case H_REFERENCES:
+               case H_IN_REPLY_TO:
                        if (!reference) {
+                               msginfo->references = g_strdup(hp);
                                eliminate_parenthesis(hp, '(', ')');
                                if ((p = strrchr(hp, '<')) != NULL &&
                                    strchr(p + 1, '>') != NULL) {
@@ -396,23 +629,35 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                                }
                        }
                        break;
-               case H_IN_REPLY_TO:
-                       if (!reference) {
-                               eliminate_parenthesis(hp, '(', ')');
-                               extract_parenthesis(hp, '<', '>');
-                               remove_space(hp);
-                               if (*hp != '\0')
-                                       reference = g_strdup(hp);
-                       }
-                       break;
                case H_CONTENT_TYPE:
-                       if (!strncasecmp(hp, "multipart", 9))
-                               msginfo->flags |= MSG_MIME;
+                       if (decrypted) {
+                               if (!strncasecmp(hp, "multipart", 9)) {
+                                       if (strncasecmp(hp, "multipart/signed", 16)) {
+                                               MSG_SET_TMP_FLAGS(msginfo->flags,
+                                                         MSG_MIME);
+                                       } else {
+                                               MSG_SET_TMP_FLAGS(msginfo->flags,
+                                                         MSG_SIGNED);
+                                       }
+                               }
+                       }
+                       else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
+                               MSG_SET_TMP_FLAGS(msginfo->flags,
+                                                 MSG_ENCRYPTED);
+                       } 
+                       else if (!strncasecmp(hp, "multipart", 9) &&
+                                  !strncasecmp(hp, "multipart/signed", 16)) {
+                               MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
+                       } 
+                       else if (!strncasecmp(hp, "multipart", 9))
+                               MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
                        break;
+#ifdef ALLOW_HEADER_HINT                       
                case H_SEEN:
                        /* mnews Seen header */
-                       MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
+                       MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
                        break;
+#endif                 
                case H_X_FACE:
                        if (msginfo->xface) break;
                        msginfo->xface = g_strdup(hp);
@@ -420,14 +665,46 @@ MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
                case H_DISPOSITION_NOTIFICATION_TO:
                        if (msginfo->dispositionnotificationto) break;
                        msginfo->dispositionnotificationto = g_strdup(hp);
+                       MSG_SET_PERM_FLAGS(msginfo->flags, MSG_RETRCPT_PENDING);        
+                       break;
+               case H_RETURN_RECEIPT_TO:
+                       if (msginfo->returnreceiptto) break;
+                       msginfo->returnreceiptto = g_strdup(hp);
+                       MSG_SET_PERM_FLAGS(msginfo->flags, MSG_RETRCPT_PENDING);        
+                       break;
+#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);
                        break;
                default:
+                       break;
                }
        }
        msginfo->inreplyto = reference;
 
-       fclose(fp);
-
        return msginfo;
 }
 
@@ -435,8 +712,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, '\"');
@@ -462,43 +738,62 @@ 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)
+{
+       gint result;
+
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
+                       weekday, day, month, year, hh, mm, ss, zone);
+       if (result == 8) return 0;
+
+       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;
+
+       *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;
+
+       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)
 {
        static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
-       gchar weekday[4];
+       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 :) */
@@ -509,6 +804,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 {
@@ -516,6 +812,61 @@ 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)
+{
+       static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
+       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;
+
+       if (procheader_scan_date_string(src, weekday, &day, month, &year,
+                                       &hh, &mm, &ss, zone) < 0) {
+               g_warning("Invalid date: %s\n", src);
+               if (dest && len > 0)
+                       strncpy2(dest, src, len);
+               return 0;
+       }
+
+       /* Y2K compliant :) */
+       if (year < 100) {
+               if (year < 70)
+                       year += 2000;
+               else
+                       year += 1900;
+       }
+
+       month[3] = '\0';
+       for (p = monthstr; *p != '\0'; p += 3) {
+               if (!strncasecmp(p, month, 3)) {
+                       dmonth = (gint)(p - monthstr) / 3 + 1;
+                       break;
+               }
+       }
+       if (*p == '\0')
+               g_warning("Invalid month: %s\n", month);
+
        t.tm_sec = ss;
        t.tm_min = mm;
        t.tm_hour = hh;
@@ -547,3 +898,33 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
        else
                strftime(dest, len, default_format, lt);
 }
+
+gint get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
+{
+       gchar *file;
+       FILE *fp;
+       HeaderEntry hentry[]={ { header, NULL, TRUE  },
+                               { NULL,   NULL, FALSE } };
+       gint val;
+       
+       g_return_val_if_fail(msginfo != NULL, -1);
+       file = procmsg_get_message_file_path(msginfo);
+       if ((fp = 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");
+               unlink(file);
+               g_free(file);
+               return -1;
+       }
+
+       g_free(file);
+        if (val == -1)
+               return -1;
+
+       return 0;
+}