2011-01-06 [colin] 3.7.8cvs31
[claws.git] / src / procheader.c
index bca7e19ef8cfc72f79dbcfe6ca7eaaec65a950ee..e14953ad60bd083033ec0b1a3be06b3a4d6e2852 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2009 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,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #include <time.h>
 #include <sys/stat.h>
 
+#ifdef G_OS_WIN32
+#  include <w32lib.h>
+#endif
+
 #include "procheader.h"
 #include "procmsg.h"
 #include "codeconv.h"
@@ -37,6 +41,8 @@
 
 #define BUFFSIZE       8192
 
+static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
+
 typedef char *(*getlinefunc) (char *, size_t, void *);
 typedef int (*peekcharfunc) (void *);
 typedef int (*getcharfunc) (void *);
@@ -61,7 +67,7 @@ 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, (peekcharfunc)file_peekchar,
+                                    (getlinefunc)fgets_crlf, (peekcharfunc)file_peekchar,
                                     TRUE);
 }
 
@@ -76,12 +82,25 @@ static gint string_get_one_field(gchar *buf, size_t len, char **str,
 
 static char *string_getline(char *buf, size_t len, char **str)
 {
+       gboolean is_cr = FALSE;
+       gboolean last_was_cr = FALSE;
+
        if (!*str || !**str)
                return NULL;
 
-       for (; **str && len > 1; --len)
-               if ((*buf++ = *(*str)++) == '\n')
+       for (; **str && len > 1; --len) {
+               is_cr = (**str == '\r');
+               if ((*buf++ = *(*str)++) == '\n') {
+                   break;
+               }
+               if (last_was_cr) {
+                       *(--buf) = '\n';
+                       buf++;
                    break;
+               }
+               last_was_cr = is_cr;
+       }
+               
        *buf = '\0';
 
        return buf;
@@ -134,6 +153,7 @@ static gint generic_get_one_field(gchar *buf, size_t len, void *data,
                /* ([*WSP CRLF] 1*WSP) */
                if (nexthead == ' ' || nexthead == '\t') {
                        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))
@@ -145,6 +165,9 @@ static gint generic_get_one_field(gchar *buf, size_t len, void *data,
                        if ((len - buflen) > 2) {
                                if (getline(buf + buflen, len - buflen, data) == NULL)
                                        break;
+                               if (skiptab) { /* replace tab with space */
+                                       *(buf + buflen) = ' ';
+                               }
                        } else
                                break;
                } else {
@@ -160,7 +183,7 @@ static gint generic_get_one_field(gchar *buf, size_t len, void *data,
 gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
 {
        return generic_get_one_field(buf, len, fp, NULL,
-                                    (getlinefunc)fgets, 
+                                    (getlinefunc)fgets_crlf
                                     (peekcharfunc)file_peekchar,
                                     FALSE);
 }
@@ -171,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;
@@ -248,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;
@@ -261,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;
                }
        }
@@ -304,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;
        }
@@ -358,6 +392,12 @@ enum
        H_SC_PARTIALLY_RETRIEVED = 20,
        H_SC_ACCOUNT_SERVER = 21,
        H_SC_ACCOUNT_LOGIN = 22,
+       H_LIST_POST        = 23,
+       H_LIST_SUBSCRIBE   = 24,
+       H_LIST_UNSUBSCRIBE = 25,
+       H_LIST_HELP        = 26,
+       H_LIST_ARCHIVE     = 27,
+       H_LIST_OWNER       = 28,
 };
 
 static HeaderEntry hentry_full[] = {{"Date:",          NULL, FALSE},
@@ -383,6 +423,12 @@ static HeaderEntry hentry_full[] = {{"Date:",              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},
                                   {NULL,               NULL, FALSE}};
 
 static HeaderEntry hentry_short[] = {{"Date:",         NULL, FALSE},
@@ -403,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;
 }
@@ -423,6 +469,8 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
        gchar *hp;
        HeaderEntry *hentry;
        gint hnum;
+       void *orig_data = data;
+
        get_one_field_func get_one_field =
                isstring ? (get_one_field_func)string_get_one_field
                         : (get_one_field_func)procheader_get_one_field;
@@ -430,8 +478,24 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
        hentry = procheader_get_headernames(full);
 
        if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
-               while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
-                       ; /* loop */
+               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 = procmsg_msginfo_new();
@@ -472,13 +536,14 @@ 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);
-                       replace_returns(msginfo->from);
-                       replace_returns(msginfo->fromname);
+                       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;
                                msginfo->to =
@@ -489,7 +554,8 @@ 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;
                                msginfo->cc =
@@ -510,9 +576,9 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_SUBJECT:
                        if (msginfo->subject) break;
-                        msginfo->subject = conv_unmime_header(hp, NULL);
-                       replace_returns(msginfo->subject);
-                       break;
+                       msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
+                       unfold_line(msginfo->subject);
+                       break;
                case H_MSG_ID:
                        if (msginfo->msgid) break;
 
@@ -548,32 +614,47 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
 #endif                 
                case H_FACE:
-                       if (msginfo->face) break;
-                       msginfo->face = g_strdup(hp);
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->face) break;
+                       msginfo->extradata->face = g_strdup(hp);
                        break;
                case H_X_FACE:
-                       if (msginfo->xface) break;
-                       msginfo->xface = g_strdup(hp);
+                       if (!msginfo->extradata)
+                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
+                       if (msginfo->extradata->xface) break;
+                       msginfo->extradata->xface = g_strdup(hp);
                        break;
                case H_DISPOSITION_NOTIFICATION_TO:
-                       if (msginfo->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->partial_recv) break;
-                       msginfo->partial_recv = g_strdup(hp);
+                       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->account_server) break;
-                       msginfo->account_server = g_strdup(hp);
+                       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->account_login) break;
-                       msginfo->account_login = g_strdup(hp);
+                       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;
@@ -582,6 +663,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                case H_SC_PLANNED_DOWNLOAD:
                        msginfo->planned_download = atoi(hp);
                        break;
+/* end partial download infos */
 #ifdef ALLOW_HEADER_HINT                       
                case H_STATUS:
                        if (strchr(hp, 'R') != NULL)
@@ -608,7 +690,46 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                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;
+/* end list infos */
                default:
                        break;
                }
@@ -658,11 +779,18 @@ static gint procheader_scan_date_string(const gchar *str,
                                        gchar *zone)
 {
        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;
@@ -698,6 +826,32 @@ static gint procheader_scan_date_string(const gchar *str,
                        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;
 }
 
@@ -707,7 +861,6 @@ static gint procheader_scan_date_string(const gchar *str,
  */
 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
 {
-       static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
        gchar weekday[11];
        gint day;
        gchar month[10];
@@ -760,7 +913,6 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
 
 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 {
-       static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
        gchar weekday[11];
        gint day;
        gchar month[10];
@@ -823,21 +975,28 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
        gchar *default_format = "%y/%m/%d(%a) %H:%M";
        gchar *str;
        const gchar *src_codeset, *dest_codeset;
+       struct tm buf;
 
-       lt = localtime(&timer);
+       if (timer > 0)
+               lt = localtime_r(&timer, &buf);
+       else {
+               time_t dummy = 1;
+               lt = localtime_r(&dummy, &buf);
+       }
 
        if (prefs_common.date_format)
-               strftime(dest, len, prefs_common.date_format, lt);
+               fast_strftime(dest, len, prefs_common.date_format, lt);
        else
-               strftime(dest, len, default_format, lt);
-
-       src_codeset = conv_get_locale_charset_str();
-       dest_codeset = CS_UTF_8;
-       str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
-       if (str) {
-               g_snprintf(dest, len, "%s", str);
-               strncpy2(dest, str, len);
-               g_free(str);
+               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);
+               }
        }
 }
 
@@ -853,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");
@@ -863,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;
        }