2006-02-05 [colin] 2.0.0cvs13
[claws.git] / src / procheader.c
index 15b9e0bce833c1edfd61cb0f34d2f1d8fc619060..3782d6b89db40110255b7cc5764143f16030c602 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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
@@ -14,7 +14,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -76,7 +76,7 @@ static gint string_get_one_field(gchar *buf, size_t len, char **str,
 
 static char *string_getline(char *buf, size_t len, char **str)
 {
-       if (!**str)
+       if (!*str || !**str)
                return NULL;
 
        for (; **str && len > 1; --len)
@@ -233,7 +233,7 @@ GSList *procheader_get_header_list_from_file(const gchar *file)
        FILE *fp;
        GSList *hlist;
 
-       if ((fp = fopen(file, "rb")) == NULL) {
+       if ((fp = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return NULL;
        }
@@ -472,7 +472,7 @@ MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
        if (!S_ISREG(s.st_mode))
                return NULL;
 
-       if ((fp = fopen(file, "rb")) == NULL) {
+       if ((fp = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return NULL;
        }
@@ -512,12 +512,13 @@ enum
        H_FROM_SPACE    = 13,
        H_SC_PLANNED_DOWNLOAD = 14,
        H_SC_MESSAGE_SIZE = 15,
-       H_X_FACE        = 16,
-       H_DISPOSITION_NOTIFICATION_TO = 17,
-       H_RETURN_RECEIPT_TO = 18,
-       H_SC_PARTIALLY_RETRIEVED = 19,
-       H_SC_ACCOUNT_SERVER = 20,
-       H_SC_ACCOUNT_LOGIN = 21,
+       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,
 };
 
 static HeaderEntry hentry_full[] = {{"Date:",          NULL, FALSE},
@@ -536,6 +537,7 @@ static HeaderEntry hentry_full[] = {{"Date:",               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},
@@ -578,7 +580,6 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
 {
        MsgInfo *msginfo;
        gchar buf[BUFFSIZE];
-       gchar *reference = NULL;
        gchar *p, *tmp;
        gchar *hp;
        HeaderEntry *hentry;
@@ -619,6 +620,8 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        if (msginfo->from) break;
                         msginfo->from = conv_unmime_header(hp, NULL);
                        msginfo->fromname = procheader_get_fromname(msginfo->from);
+                       replace_returns(msginfo->from);
+                       replace_returns(msginfo->fromname);
                        break;
                case H_TO:
                         tmp = conv_unmime_header(hp, NULL);
@@ -654,6 +657,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                case H_SUBJECT:
                        if (msginfo->subject) break;
                         msginfo->subject = conv_unmime_header(hp, NULL);
+                       replace_returns(msginfo->subject);
                        break;
                case H_MSG_ID:
                        if (msginfo->msgid) break;
@@ -663,17 +667,20 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        msginfo->msgid = g_strdup(hp);
                        break;
                case H_REFERENCES:
+                       msginfo->references =
+                               references_list_prepend(msginfo->references,
+                                                       hp);
+                       break;
                case H_IN_REPLY_TO:
-                       if (!reference) {
-                               msginfo->references = g_strdup(hp);
-                               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);
-                               }
+                       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:
@@ -686,6 +693,10 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
                        break;
 #endif                 
+               case H_FACE:
+                       if (msginfo->face) break;
+                       msginfo->face = g_strdup(hp);
+                       break;
                case H_X_FACE:
                        if (msginfo->xface) break;
                        msginfo->xface = g_strdup(hp);
@@ -748,7 +759,10 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                }
        }
-       msginfo->inreplyto = reference;
+
+       if (!msginfo->inreplyto && msginfo->references)
+               msginfo->inreplyto =
+                       g_strdup((gchar *)msginfo->references->data);
 
        return msginfo;
 }
@@ -907,7 +921,6 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 
        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;
@@ -928,8 +941,6 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
                        break;
                }
        }
-       if (*p == '\0')
-               g_warning("Invalid month: %s\n", month);
 
        t.tm_sec = ss;
        t.tm_min = mm;
@@ -990,7 +1001,7 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,
        
        g_return_val_if_fail(msginfo != NULL, -1);
        file = procmsg_get_message_file_path(msginfo);
-       if ((fp = fopen(file, "rb")) == NULL) {
+       if ((fp = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                g_free(file);
                return -1;
@@ -998,7 +1009,7 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,
        val = procheader_get_one_field(buf,len, fp, hentry);
        if (fclose(fp) == EOF) {
                FILE_OP_ERROR(file, "fclose");
-               unlink(file);
+               g_unlink(file);
                g_free(file);
                return -1;
        }