]> git.notmuchmail.org Git - notmuch/commitdiff
cli: replace use of g_mime_message_get_date_as_string
authorDavid Bremner <david@tethera.net>
Thu, 4 May 2017 12:48:44 +0000 (08:48 -0400)
committerDavid Bremner <david@tethera.net>
Fri, 14 Jul 2017 20:58:09 +0000 (17:58 -0300)
This function goes away in gmime-3.0. Also, the memory management is
apparently error prone, witness the memory leak in notmuch-reply.

notmuch-reply.c
notmuch-show.c
util/gmime-extra.c
util/gmime-extra.h

index e6c166414f1a72c376911b4e4dae63e26250e519..857e1e141121e7dc66562677e466400b6872cda0 100644 (file)
@@ -61,7 +61,7 @@ format_part_reply (GMimeStream *stream, mime_node_t *node)
                                  recipients_string);
        g_free (recipients_string);
        g_mime_stream_printf (stream, "> Subject: %s\n", g_mime_message_get_subject (message));
-       g_mime_stream_printf (stream, "> Date: %s\n", g_mime_message_get_date_as_string (message));
+       g_mime_stream_printf (stream, "> Date: %s\n", g_mime_message_get_date_string (node, message));
        g_mime_stream_printf (stream, ">\n");
     } else if (GMIME_IS_PART (node->part)) {
        GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
index 084b7dab78e59423a03137ad57bf18b6a68835e1..78d46f1ef1af335a9c677aa4a67eda5b55cea07d 100644 (file)
@@ -204,7 +204,6 @@ format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
     InternetAddressList *recipients;
     char *recipients_string;
     const char *reply_to_string;
-    char *date_string;
 
     sp->begin_map (sp);
 
@@ -252,9 +251,7 @@ format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
        sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
     } else {
        sp->map_key (sp, "Date");
-       date_string = g_mime_message_get_date_as_string (message);
-       sp->string (sp, date_string);
-       g_free (date_string);
+       sp->string (sp, g_mime_message_get_date_string (sp, message));
     }
 
     sp->end (sp);
@@ -532,9 +529,8 @@ format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
        if (recipients_string)
            g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
        g_free (recipients_string);
-       date_string = g_mime_message_get_date_as_string (message);
+       date_string = g_mime_message_get_date_string (node, message);
        g_mime_stream_printf (stream, "Date: %s\n", date_string);
-       g_free (date_string);
        g_mime_stream_printf (stream, "\fheader}\n");
 
        g_mime_stream_printf (stream, "\fbody{\n");
index f1538587bf5a429e0d1cedc11279ae9353f84a06..01fe9e35f979851c1184124a9fee248e42181e5b 100644 (file)
@@ -18,3 +18,38 @@ g_mime_stream_stdout_new()
 
     return stream_buffered;
 }
+
+/**
+ * copy a glib string into a talloc context, and free it.
+ */
+static char*
+g_string_talloc_strdup (void *ctx, char *g_string)
+{
+    char *new_str = talloc_strdup (ctx, g_string);
+    g_free (g_string);
+    return new_str;
+}
+
+#if (GMIME_MAJOR_VERSION < 3)
+
+char *
+g_mime_message_get_date_string (void *ctx, GMimeMessage *message)
+{
+    char *date = g_mime_message_get_date_as_string (message);
+    return g_string_talloc_strdup (ctx, date);
+}
+
+#else /* GMime >= 3.0 */
+
+char *
+g_mime_message_get_date_string (void *ctx, GMimeMessage *message)
+{
+    GDateTime* parsed_date = g_mime_message_get_date (message);
+    if (parsed_date) {
+       char *date = g_mime_utils_header_format_date (parsed_date);
+       return g_string_talloc_strdup (ctx, date);
+    } else {
+       return talloc_strdup(ctx, "Thu, 01 Jan 1970 00:00:00 +0000");
+    }
+}
+#endif
index e0432a942ffa0d0a8d3ea4e45ea95019bad876a3..6e2f6ca5c83cbfd864a1f08a6a6b5b5a0d2f44f4 100644 (file)
@@ -3,4 +3,12 @@
 #include <gmime/gmime.h>
 
 GMimeStream *g_mime_stream_stdout_new(void);
+
+#include <talloc.h>
+
+/**
+ * return talloc allocated date string
+ */
+char *g_mime_message_get_date_string (void *ctx, GMimeMessage *message);
+
 #endif