2006-03-26 [colin] 2.0.0cvs172
[claws.git] / src / procheader.c
index 7aaae5e4ae8aa58395da2f4bb36c946b734ef684..bca7e19ef8cfc72f79dbcfe6ca7eaaec65a950ee 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 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
 #endif
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <time.h>
 #include <sys/stat.h>
 
-#include "intl.h"
 #include "procheader.h"
 #include "procmsg.h"
 #include "codeconv.h"
 
 #define BUFFSIZE       8192
 
-typedef char *(*getlinefunc)(char *, int, void *);
-typedef int (*peekcharfunc)(void *);
-typedef int (*getcharfunc)(void *);
-typedef gint (*get_one_field_func)(gchar *, gint, void *, HeaderEntry[]);
+typedef char *(*getlinefunc) (char *, size_t, void *);
+typedef int (*peekcharfunc) (void *);
+typedef int (*getcharfunc) (void *);
+typedef gint (*get_one_field_func) (gchar *, size_t, void *, HeaderEntry[]);
 
-static gint string_get_one_field(gchar *buf, gint len, char **str,
+static gint string_get_one_field(gchar *buf, size_t len, char **str,
                                 HeaderEntry hentry[]);
 
-static char *string_getline(char *buf, int len, char **str);
+static char *string_getline(char *buf, size_t len, char **str);
 static int string_peekchar(char **str);
 static int file_peekchar(FILE *fp);
-static gint generic_get_one_field(gchar *buf, gint len, void *data,
+static gint generic_get_one_field(gchar *buf, size_t len, void *data,
                                  HeaderEntry hentry[],
                                  getlinefunc getline, 
                                  peekcharfunc peekchar,
@@ -57,7 +57,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                             gboolean full, gboolean decrypted);
 
 
-gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
+gint procheader_get_one_field(gchar *buf, size_t len, FILE *fp,
                              HeaderEntry hentry[])
 {
        return generic_get_one_field(buf, len, fp, hentry,
@@ -65,7 +65,7 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                                     TRUE);
 }
 
-static gint string_get_one_field(gchar *buf, gint len, char **str,
+static gint string_get_one_field(gchar *buf, size_t len, char **str,
                                 HeaderEntry hentry[])
 {
        return generic_get_one_field(buf, len, str, hentry,
@@ -74,9 +74,9 @@ static gint string_get_one_field(gchar *buf, gint len, char **str,
                                     TRUE);
 }
 
-static char *string_getline(char *buf, int 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)
@@ -97,7 +97,7 @@ static int file_peekchar(FILE *fp)
        return ungetc(getc(fp), fp);
 }
 
-static gint generic_get_one_field(gchar *buf, gint len, void *data,
+static gint generic_get_one_field(gchar *buf, size_t len, void *data,
                          HeaderEntry *hentry,
                          getlinefunc getline, peekcharfunc peekchar,
                          gboolean unfold)
@@ -118,7 +118,7 @@ static gint generic_get_one_field(gchar *buf, gint len, void *data,
 
                        for (hp = hentry, hnum = 0; hp->name != NULL;
                             hp++, hnum++) {
-                               if (!g_strncasecmp(hp->name, buf,
+                               if (!g_ascii_strncasecmp(hp->name, buf,
                                                 strlen(hp->name)))
                                        break;
                        }
@@ -157,7 +157,7 @@ static gint generic_get_one_field(gchar *buf, gint len, void *data,
        return hnum;
 }
 
-gint procheader_get_one_field_asis(gchar *buf, gint len, FILE *fp)
+gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
 {
        return generic_get_one_field(buf, len, fp, NULL,
                                     (getlinefunc)fgets, 
@@ -165,154 +165,6 @@ gint procheader_get_one_field_asis(gchar *buf, gint len, FILE *fp)
                                     FALSE);
 }
 
-#if 0
-gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
-{
-       gboolean folded = FALSE;
-       gint nexthead;
-       gchar *bufp;
-
-       if (fgets(buf, len, fp) == NULL) return NULL;
-       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);
-
-               /* folded */
-               if (nexthead == ' ' || nexthead == '\t')
-                       folded = TRUE;
-               else if (nexthead == EOF)
-                       break;
-               else if (folded == TRUE) {
-                       if (nexthead == '\r' || nexthead == '\n') {
-                               folded = FALSE;
-                               continue;
-                       }
-
-                       if ((len - (bufp - buf)) <= 2) break;
-
-                       /* replace return code on the tail end
-                          with space */
-                       *bufp++ = ' ';
-                       *bufp++ = nexthead;
-                       *bufp = '\0';
-
-                       /* 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);
-                       break;
-               }
-       }
-
-       /* remove trailing return code */
-       strretchomp(buf);
-
-       return buf;
-}
-#endif
-
-#if 0
-GSList *procheader_get_header_list_from_file(const gchar *file)
-{
-       FILE *fp;
-       GSList *hlist;
-
-       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 == ':') {
-                               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);
-
-                               hlist = g_slist_append(hlist, header);
-                               break;
-                       }
-               }
-               */
-       }
-
-       return hlist;
-}
-#endif
-
-#if 0
-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;
-}
-#endif
-
 GPtrArray *procheader_get_header_array_asis(FILE *fp)
 {
        gchar buf[BUFFSIZE];
@@ -346,19 +198,6 @@ GPtrArray *procheader_get_header_array_asis(FILE *fp)
        return headers;
 }
 
-#if 0
-void procheader_header_list_destroy(GSList *hlist)
-{
-       Header *header;
-
-       while (hlist != NULL) {
-               header = hlist->data;
-               procheader_header_free(header);
-               hlist = g_slist_remove(hlist, header);
-       }
-}
-#endif
-
 void procheader_header_array_destroy(GPtrArray *harray)
 {
        gint i;
@@ -400,7 +239,7 @@ gboolean procheader_headername_equal(char * hdr1, char * hdr2)
        if (len1 != len2)
                return 0;
 
-       return (g_strncasecmp(hdr1, hdr2, len1) == 0);
+       return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
 }
 
 /*
@@ -412,7 +251,6 @@ gboolean procheader_headername_equal(char * hdr1, char * hdr2)
 
 Header * procheader_parse_header(gchar * buf)
 {
-       gchar tmp[BUFFSIZE];
        gchar *p = buf;
        Header * header;
 
@@ -425,11 +263,7 @@ Header * procheader_parse_header(gchar * buf)
                        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);
+                        header->body = conv_unmime_header(p, NULL);
                        return header;
                }
        }
@@ -477,7 +311,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;
        }
@@ -516,13 +350,14 @@ enum
        H_X_STATUS      = 12,
        H_FROM_SPACE    = 13,
        H_SC_PLANNED_DOWNLOAD = 14,
-       H_X_FACE        = 15,
-       H_DISPOSITION_NOTIFICATION_TO = 16,
-       H_RETURN_RECEIPT_TO = 17,
-       H_SC_PARTIALLY_RETRIEVED = 18,
-       H_SC_ACCOUNT_SERVER = 19,
-       H_SC_ACCOUNT_LOGIN = 20,
-       H_SC_MESSAGE_SIZE = 21
+       H_SC_MESSAGE_SIZE = 15,
+       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},
@@ -540,13 +375,14 @@ static HeaderEntry hentry_full[] = {{"Date:",             NULL, FALSE},
                                   {"X-Status:",        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},
                                   {"SC-Partially-Retrieved:", NULL, FALSE},
                                   {"SC-Account-Server:", NULL, FALSE},
                                   {"SC-Account-Login:",NULL, FALSE},
-                                  {"SC-Message-Size:", NULL, FALSE},
                                   {NULL,               NULL, FALSE}};
 
 static HeaderEntry hentry_short[] = {{"Date:",         NULL, FALSE},
@@ -564,6 +400,7 @@ static HeaderEntry hentry_short[] = {{"Date:",              NULL, FALSE},
                                    {"X-Status:",       NULL, FALSE},
                                    {"From ",           NULL, FALSE},
                                    {"SC-Marked-For-Download:", NULL, FALSE},
+                                   {"SC-Message-Size:",NULL, FALSE},
                                    {NULL,              NULL, FALSE}};
 
 HeaderEntry* procheader_get_headernames(gboolean full)
@@ -581,9 +418,8 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                             gboolean full, gboolean decrypted)
 {
        MsgInfo *msginfo;
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *reference = NULL;
-       gchar *p;
+       gchar buf[BUFFSIZE];
+       gchar *p, *tmp;
        gchar *hp;
        HeaderEntry *hentry;
        gint hnum;
@@ -617,16 +453,32 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        if (msginfo->date) break;
                        msginfo->date_t =
                                procheader_date_parse(NULL, hp, 0);
-                       msginfo->date = g_strdup(hp);
+                       if (g_utf8_validate(hp, -1, NULL)) {
+                               msginfo->date = g_strdup(hp);
+                       } else {
+                               gchar *utf = conv_codeset_strdup(
+                                       hp, 
+                                       conv_get_locale_charset_str_no_utf8(),
+                                       CS_INTERNAL);
+                               if (utf == NULL || 
+                                   !g_utf8_validate(utf, -1, NULL)) {
+                                       g_free(utf);
+                                       utf = g_malloc(strlen(buf)*2+1);
+                                       conv_localetodisp(utf, 
+                                               strlen(hp)*2+1, hp);
+                               }
+                               msginfo->date = utf;
+                       }
                        break;
                case H_FROM:
                        if (msginfo->from) break;
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
-                       msginfo->from = g_strdup(tmp);
-                       msginfo->fromname = procheader_get_fromname(tmp);
+                        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:
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
+                        tmp = conv_unmime_header(hp, NULL);
                        if (msginfo->to) {
                                p = msginfo->to;
                                msginfo->to =
@@ -634,9 +486,10 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                                g_free(p);
                        } else
                                msginfo->to = g_strdup(tmp);
+                        g_free(tmp);                                
                        break;
                case H_CC:
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
+                        tmp = conv_unmime_header(hp, NULL);
                        if (msginfo->cc) {
                                p = msginfo->cc;
                                msginfo->cc =
@@ -644,6 +497,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                                g_free(p);
                        } else
                                msginfo->cc = g_strdup(tmp);
+                        g_free(tmp);                                
                        break;
                case H_NEWSGROUPS:
                        if (msginfo->newsgroups) {
@@ -656,8 +510,8 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_SUBJECT:
                        if (msginfo->subject) break;
-                       conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
-                       msginfo->subject = g_strdup(tmp);
+                        msginfo->subject = conv_unmime_header(hp, NULL);
+                       replace_returns(msginfo->subject);
                        break;
                case H_MSG_ID:
                        if (msginfo->msgid) break;
@@ -667,21 +521,24 @@ 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:
-                       if (!g_strncasecmp(hp, "multipart/", 10))
+                       if (!g_ascii_strncasecmp(hp, "multipart/", 10))
                                MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
                        break;
 #ifdef ALLOW_HEADER_HINT                       
@@ -690,6 +547,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);
@@ -752,7 +613,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;
 }
@@ -812,6 +676,10 @@ static gint procheader_scan_date_string(const gchar *str,
                        weekday, day, month, year, hh, mm, ss);
        if (result == 7) return 0;
 
+       result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
+                       day, month, year, hh, mm, ss);
+       if (result == 6) return 0;
+
        *ss = 0;
        result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
                        weekday, day, month, year, hh, mm, zone);
@@ -821,6 +689,15 @@ static gint procheader_scan_date_string(const gchar *str,
                        day, month, year, hh, mm, zone);
        if (result == 6) return 0;
 
+       *zone = '\0';
+       result = sscanf(str, "%10s %d %9s %d %2d:%2d",
+                       weekday, day, month, year, hh, mm);
+       if (result == 6) return 0;
+
+       result = sscanf(str, "%d %9s %d %2d:%2d",
+                       day, month, year, hh, mm);
+       if (result == 5) return 0;
+
        return -1;
 }
 
@@ -898,7 +775,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;
@@ -914,13 +790,11 @@ time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
 
        month[3] = '\0';
        for (p = monthstr; *p != '\0'; p += 3) {
-               if (!g_strncasecmp(p, month, 3)) {
+               if (!g_ascii_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;
@@ -957,7 +831,7 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
        else
                strftime(dest, len, default_format, lt);
 
-       src_codeset = conv_get_current_charset_str();
+       src_codeset = conv_get_locale_charset_str();
        dest_codeset = CS_UTF_8;
        str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
        if (str) {
@@ -973,13 +847,15 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,
 {
        gchar *file;
        FILE *fp;
-       HeaderEntry hentry[]={ { header, NULL, TRUE  },
-                               { NULL,   NULL, FALSE } };
+       HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
+                               { NULL, NULL, FALSE } };
        gint val;
+
+        hentry[0].name = header;
        
        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;
@@ -987,7 +863,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;
        }