indicate GnuPG signed messages by displaying an icon in the Attachment column
[claws.git] / src / procheader.c
index 36084cc2cfe04db94ea1d2ac37265849fb48f9b3..514ff56e2ec12bfe527ff092c2c3f8070fc7269d 100644 (file)
@@ -56,9 +56,8 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                        for (hp = hentry, hnum = 0; hp->name != NULL;
                             hp++, hnum++) {
                                if (!strncasecmp(hp->name, buf,
-                                                strlen(hp->name))) {
+                                                strlen(hp->name)))
                                        break;
-                               }
                        }
                } while (hp->name == NULL);
        } else {
@@ -71,6 +70,11 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                gboolean folded = FALSE;
                gchar *bufp = buf + strlen(buf);
 
+               for (; bufp > buf &&
+                    (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+                    bufp--)
+                       *(bufp - 1) = '\0';
+
                while (1) {
                        nexthead = fgetc(fp);
 
@@ -80,22 +84,28 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                        else if (nexthead == EOF)
                                break;
                        else if (folded == TRUE) {
-                               /* concatenate next line */
+                               if (nexthead == '\r' || nexthead == '\n') {
+                                       folded = FALSE;
+                                       continue;
+                               }
+
                                if ((len - (bufp - buf)) <= 2) break;
 
                                /* replace return code on the tail end
                                   with space */
-                               *(bufp - 1) = ' ';
+                               *bufp++ = ' ';
                                *bufp++ = nexthead;
                                *bufp = '\0';
-                               if (nexthead == '\r' || nexthead == '\n') {
-                                       folded = FALSE;
-                                       continue;
-                               }
+                               /* 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);
@@ -103,9 +113,6 @@ gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
                        }
                }
 
-               /* remove trailing return code */
-               strretchomp(buf);
-
                return hnum;
        }
 
@@ -148,6 +155,11 @@ gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
        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);
 
@@ -157,22 +169,29 @@ gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
                else if (nexthead == EOF)
                        break;
                else if (folded == TRUE) {
-                       /* concatenate next line */
+                       if (nexthead == '\r' || nexthead == '\n') {
+                               folded = FALSE;
+                               continue;
+                       }
+
                        if ((len - (bufp - buf)) <= 2) break;
 
                        /* replace return code on the tail end
                           with space */
-                       *(bufp - 1) = ' ';
+                       *bufp++ = ' ';
                        *bufp++ = nexthead;
                        *bufp = '\0';
-                       if (nexthead == '\r' || nexthead == '\n') {
-                               folded = FALSE;
-                               continue;
-                       }
+
+                       /* 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);
@@ -204,15 +223,14 @@ GSList *procheader_get_header_list_from_file(const gchar *file)
 
 GSList *procheader_get_header_list(FILE *fp)
 {
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *p;
+       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))
+               if ((header = procheader_parse_header(buf)) != NULL)
                        hlist = g_slist_append(hlist, header);
                /*
                if (*buf == ':') continue;
@@ -237,8 +255,7 @@ GSList *procheader_get_header_list(FILE *fp)
 
 GPtrArray *procheader_get_header_array(FILE *fp)
 {
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *p;
+       gchar buf[BUFFSIZE];
        GPtrArray *headers;
        Header *header;
 
@@ -247,7 +264,7 @@ GPtrArray *procheader_get_header_array(FILE *fp)
        headers = g_ptr_array_new();
 
        while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
-               if (header = procheader_parse_header(buf))
+               if ((header = procheader_parse_header(buf)) != NULL)
                        g_ptr_array_add(headers, header);
                /*
                if (*buf == ':') continue;
@@ -272,8 +289,7 @@ GPtrArray *procheader_get_header_array(FILE *fp)
 
 GPtrArray *procheader_get_header_array_asis(FILE *fp)
 {
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gchar *p;
+       gchar buf[BUFFSIZE];
        GPtrArray *headers;
        Header *header;
 
@@ -282,7 +298,7 @@ GPtrArray *procheader_get_header_array_asis(FILE *fp)
        headers = g_ptr_array_new();
 
        while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
-               if (header = procheader_parse_header(buf))
+               if ((header = procheader_parse_header(buf)) != NULL)
                        g_ptr_array_add(headers, header);
                        /*
                if (*buf == ':') continue;
@@ -382,7 +398,10 @@ Header * procheader_parse_header(gchar * buf)
                        p++;
                        while (*p == ' ' || *p == '\t') p++;
                        conv_unmime_header(tmp, sizeof(tmp), p, NULL);
-                       header->body = g_strdup(tmp);
+                       if(tmp == NULL) 
+                               header->body = g_strdup(p);
+                       else    
+                               header->body = g_strdup(tmp);
                        return header;
                }
        }
@@ -416,6 +435,36 @@ void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
        }
 }
 
+MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
+                              gboolean full, gboolean decrypted)
+{
+       FILE *fp;
+       MsgInfo *msginfo;
+
+       if ((fp = fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               return NULL;
+       }
+
+       msginfo = procheader_parse_stream(fp, flags, full, decrypted);
+       fclose(fp);
+       return msginfo;
+}
+
+MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
+                             gboolean decrypted)
+{
+       FILE *fp;
+       MsgInfo *msginfo;
+
+       if ((fp = str_open_as_stream(str)) == NULL)
+               return NULL;
+
+       msginfo = procheader_parse_stream(fp, flags, full, decrypted);
+       fclose(fp);
+       return msginfo;
+}
+
 enum
 {
        H_DATE          = 0,
@@ -437,33 +486,8 @@ enum
        H_RETURN_RECEIPT_TO = 16
 };
 
-MsgInfo *procheader_parse(const gchar *file, MsgFlags flags,
-                         gboolean full, gboolean decrypted)
-{
-       FILE *fp;
-       MsgInfo *msginfo;
-
-       if ((fp = fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return NULL;
-       }
-
-       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 decrypted)
+MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full, 
+                                gboolean decrypted)
 {
        static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
                                           {"From:",            NULL, TRUE},
@@ -499,7 +523,7 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
                                            {"X-Status:",       NULL, FALSE},
                                            {"From ",           NULL, FALSE},
                                            {NULL,              NULL, FALSE}};
-       
+
        MsgInfo *msginfo;
        gchar buf[BUFFSIZE], tmp[BUFFSIZE];
        gchar *reference = NULL;
@@ -510,12 +534,12 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
 
        hentry = full ? hentry_full : hentry_short;
 
-       if (MSG_IS_QUEUED(flags)) {
+       if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
                while (fgets(buf, sizeof(buf), fp) != NULL)
                        if (buf[0] == '\r' || buf[0] == '\n') break;
        }
 
-       msginfo = g_new0(MsgInfo, 1);
+       msginfo = procmsg_msginfo_new();
        
        if (flags.tmp_flags || flags.perm_flags) 
                msginfo->flags = flags;
@@ -530,7 +554,6 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
        while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
               != -1) {
                hp = buf + strlen(hentry[hnum].name);
-
                while (*hp == ' ' || *hp == '\t') hp++;
 
                switch (hnum) {
@@ -573,7 +596,7 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
                                        g_strconcat(p, ",", hp, NULL);
                                g_free(p);
                        } else
-                               msginfo->newsgroups = g_strdup(buf + 12);
+                               msginfo->newsgroups = g_strdup(hp);
                        break;
                case H_SUBJECT:
                        if (msginfo->subject) break;
@@ -612,7 +635,11 @@ MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
                        else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
                                MSG_SET_TMP_FLAGS(msginfo->flags,
                                                  MSG_ENCRYPTED);
-                       }
+                       } 
+                       else if (!strncasecmp(hp, "multipart", 9) &&
+                                  !strncasecmp(hp, "multipart/signed", 16)) {
+                               MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
+                       } 
                        else if (!strncasecmp(hp, "multipart", 9))
                                MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
                        break;
@@ -748,7 +775,6 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
        gint hh, mm, ss;
        GDateMonth dmonth;
        gchar *p;
-       time_t timer;
 
        if (!t)
                return FALSE;
@@ -787,6 +813,8 @@ gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
        t->tm_yday = 0;
        t->tm_isdst = -1;
 
+       mktime(t);
+
        return TRUE;
 }
 
@@ -860,27 +888,32 @@ void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
                strftime(dest, len, default_format, lt);
 }
 
-gint get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,gchar *header)
+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) {
+       gchar *file;
+       FILE *fp;
+       HeaderEntry hentry[]={ { header, NULL, TRUE  },
+                               { NULL,   NULL, FALSE } };
+       gint val;
+       
+       g_return_val_if_fail(msginfo != NULL, -1);
+       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) {
+               return -1;
+       }
+       val = procheader_get_one_field(buf,len, fp, hentry);
+       if (fclose(fp) == EOF) {
                FILE_OP_ERROR(file, "fclose");
                unlink(file);
+               g_free(file);
                return -1;
        }
-       if (val == -1)
-          return -1;
-       return 0;
+
+       g_free(file);
+        if (val == -1)
+               return -1;
+
+       return 0;
 }