2011-11-18 [mones] 3.7.10cvs87
[claws.git] / src / procheader.c
index 672700bab6560b7b115d566d0595892e43516324..370539012284080207087ea730a5577fdc8dc492 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2011 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
@@ -194,28 +194,13 @@ GPtrArray *procheader_get_header_array_asis(FILE *fp)
        GPtrArray *headers;
        Header *header;
 
-       g_return_val_if_fail(fp != NULL, NULL);
+       cm_return_val_if_fail(fp != NULL, NULL);
 
        headers = g_ptr_array_new();
 
        while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
                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;
@@ -271,11 +256,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 *p = buf;
        Header * header;
+       gboolean addr_field = FALSE;
 
        if ((*buf == ':') || (*buf == ' '))
                return NULL;
@@ -284,9 +294,10 @@ Header * procheader_parse_header(gchar * buf)
                if ((*p == ':') || (*p == ' ')) {
                        header = g_new(Header, 1);
                        header->name = g_strndup(buf, p - buf + 1);
+                       addr_field = header_is_addr_field(header->name);
                        p++;
                        while (*p == ' ' || *p == '\t') p++;
-                        header->body = conv_unmime_header(p, NULL);
+                       header->body = conv_unmime_header(p, NULL, addr_field);
                        return header;
                }
        }
@@ -327,7 +338,7 @@ MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
        FILE *fp;
        MsgInfo *msginfo;
 
-       if (stat(file, &s) < 0) {
+       if (g_stat(file, &s) < 0) {
                FILE_OP_ERROR(file, "stat");
                return NULL;
        }
@@ -438,7 +449,7 @@ static HeaderEntry hentry_short[] = {{"Date:",              NULL, FALSE},
                                    {"SC-Message-Size:",NULL, FALSE},
                                    {NULL,              NULL, FALSE}};
 
-HeaderEntry* procheader_get_headernames(gboolean full)
+static HeaderEntry* procheader_get_headernames(gboolean full)
 {
        return full ? hentry_full : hentry_short;
 }
@@ -525,13 +536,13 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_FROM:
                        if (msginfo->from) break;
-                        msginfo->from = conv_unmime_header(hp, NULL);
+                       msginfo->from = conv_unmime_header(hp, NULL, TRUE);
                        msginfo->fromname = procheader_get_fromname(msginfo->from);
                        remove_return(msginfo->from);
                        remove_return(msginfo->fromname);
                        break;
                case H_TO:
-                        tmp = conv_unmime_header(hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
                        remove_return(tmp);
                        if (msginfo->to) {
                                p = msginfo->to;
@@ -543,7 +554,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                         g_free(tmp);                                
                        break;
                case H_CC:
-                        tmp = conv_unmime_header(hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
                        remove_return(tmp);
                        if (msginfo->cc) {
                                p = msginfo->cc;
@@ -565,7 +576,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_SUBJECT:
                        if (msginfo->subject) break;
-                       msginfo->subject = conv_unmime_header(hp, NULL);
+                       msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
                        unfold_line(msginfo->subject);
                        break;
                case H_MSG_ID:
@@ -772,6 +783,9 @@ static gint procheader_scan_date_string(const gchar *str,
        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;
@@ -963,7 +977,12 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
        const gchar *src_codeset, *dest_codeset;
        struct tm buf;
 
-       lt = localtime_r(&timer, &buf);
+       if (timer > 0)
+               lt = localtime_r(&timer, &buf);
+       else {
+               time_t dummy = 1;
+               lt = localtime_r(&dummy, &buf);
+       }
 
        if (prefs_common.date_format)
                fast_strftime(dest, len, prefs_common.date_format, lt);
@@ -993,7 +1012,7 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,
 
         hentry[0].name = header;
        
-       g_return_val_if_fail(msginfo != NULL, -1);
+       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");
@@ -1003,7 +1022,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");
-               g_unlink(file);
+               claws_unlink(file);
                g_free(file);
                return -1;
        }
@@ -1014,3 +1033,51 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,
 
        return 0;
 }
+
+HeaderEntry *procheader_entries_from_str(const gchar *str)
+{
+       HeaderEntry *entries = NULL, *he;
+       int numh = 0, i = 0;
+       gchar **names = NULL;
+       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);
+       }
+}
+