sync with 0.7.5cvs11
[claws.git] / src / procheader.c
index e40fc1108a01fa7259f45423708954a22489f79c..93fc862c5f5f1806127f43d2daa4d4d7e99ce6c9 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-2002 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
@@ -191,7 +191,7 @@ GSList *procheader_get_header_list_from_file(const gchar *file)
        FILE *fp;
        GSList *hlist;
 
-       if ((fp = fopen(file, "r")) == NULL) {
+       if ((fp = fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return NULL;
        }
@@ -437,25 +437,33 @@ enum
        H_RETURN_RECEIPT_TO = 16
 };
 
-MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
+MsgInfo *procheader_parse(const gchar *file, MsgFlags flags,
+                         gboolean full, gboolean decrypted)
 {
        FILE *fp;
        MsgInfo *msginfo;
 
-       if ((fp = fopen(file, "r")) == NULL) {
+       if ((fp = fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return NULL;
        }
 
-       msginfo = procheader_file_parse(fp, flags, full);
+       msginfo = procheader_file_parse(fp, flags, full, decrypted);
 
        fclose(fp);
 
        return msginfo;
 }
 
+
+/* FIXME: we should not allow headers in messages to change the sylpheed marks
+ * so we're currently disabling setting any MsgFlags when detecting X-Seen,
+ * Seen, X-Status, Status. See macro ALLOW_HEADER_HINT */  
+
+/* #define ALLOW_HEADER_HINT */
+
 MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
-                              gboolean full)
+                              gboolean full, gboolean decrypted)
 {
        static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
                                           {"From:",            NULL, TRUE},
@@ -508,7 +516,15 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
        }
 
        msginfo = g_new0(MsgInfo, 1);
-       msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
+       
+       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))
@@ -572,6 +588,7 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
                        msginfo->msgid = g_strdup(hp);
                        break;
                case H_REFERENCES:
+               case H_IN_REPLY_TO:
                        if (!reference) {
                                msginfo->references = g_strdup(hp);
                                eliminate_parenthesis(hp, '(', ')');
@@ -584,23 +601,27 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
                                }
                        }
                        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) &&
+                                   strncasecmp(hp, "multipart/signed", 16)) {
+                                       MSG_SET_TMP_FLAGS(msginfo->flags,
+                                                         MSG_MIME);
+                               }
+                       }
+                       else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
+                               MSG_SET_TMP_FLAGS(msginfo->flags,
+                                                 MSG_ENCRYPTED);
+                       }
+                       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);
@@ -608,32 +629,36 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
                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_FLAGS(msginfo->flags, MSG_UNREAD);
+                               MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
                        if (strchr(hp, 'O') != NULL)
-                               MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW);
+                               MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
                        if (strchr(hp, 'U') != NULL)
-                               MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
+                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
                        break;
                case H_X_STATUS:
                        if (strchr(hp, 'D') != NULL)
-                               MSG_SET_FLAGS(msginfo->flags,
+                               MSG_SET_PERM_FLAGS(msginfo->flags,
                                              MSG_REALLY_DELETED);
                        if (strchr(hp, 'F') != NULL)
-                               MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
+                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
                        if (strchr(hp, 'd') != NULL)
-                               MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
+                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
                        if (strchr(hp, 'r') != NULL)
-                               MSG_SET_FLAGS(msginfo->flags, MSG_REPLIED);
+                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
                        if (strchr(hp, 'f') != NULL)
-                               MSG_SET_FLAGS(msginfo->flags, MSG_FORWARDED);
+                               MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
                        break;
+#endif                 
                case H_FROM_SPACE:
                        if (msginfo->fromspace) break;
                        msginfo->fromspace = g_strdup(hp);
@@ -651,8 +676,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, '\"');
@@ -678,43 +702,58 @@ gchar *procheader_get_fromname(const gchar *str)
        return name;
 }
 
+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;
+}
+
 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 {
        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 (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 :) */
@@ -725,6 +764,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 {
@@ -763,3 +803,28 @@ 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_if_fail(msginfo != NULL);
+       file = procmsg_get_message_file_path(msginfo);
+       if ((fp = fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               g_free(file);
+               return;
+        }
+       val=procheader_get_one_field(buf,len, fp, hentry);
+       if (fclose(fp) == EOF) {
+               FILE_OP_ERROR(file, "fclose");
+               unlink(file);
+               return -1;
+       }
+       if (val == -1)
+          return -1;
+       return 0;
+}