]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-reply.c
show/reply: Unify the code that extracts text parts
[notmuch] / notmuch-reply.c
index f1478ccf9bd06c5ee2b882a7665303e2a460863e..2f5ed3dc9ea4c16c09b48cc7062b18f39845e5ce 100644 (file)
@@ -21,7 +21,6 @@
  */
 
 #include "notmuch-client.h"
-#include "gmime-filter-reply.h"
 #include "gmime-filter-headers.h"
 
 static void
@@ -106,29 +105,10 @@ reply_part_content (GMimeObject *part)
     else if (g_mime_content_type_is_type (content_type, "text", "*") &&
        !g_mime_content_type_is_type (content_type, "text", "html"))
     {
-       GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
-       GMimeDataWrapper *wrapper;
-       const char *charset;
-
-       charset = g_mime_object_get_content_type_parameter (part, "charset");
-       stream_stdout = g_mime_stream_file_new (stdout);
-       if (stream_stdout) {
-           g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
-           stream_filter = g_mime_stream_filter_new(stream_stdout);
-           if (charset) {
-               g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
-                                        g_mime_filter_charset_new(charset, "UTF-8"));
-           }
-       }
-       g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
-                                g_mime_filter_reply_new(TRUE));
-       wrapper = g_mime_part_get_content_object (GMIME_PART (part));
-       if (wrapper && stream_filter)
-           g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
-       if (stream_filter)
-           g_object_unref(stream_filter);
-       if (stream_stdout)
-           g_object_unref(stream_stdout);
+       GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
+       g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
+       show_text_part_content (part, stream_stdout, NOTMUCH_SHOW_TEXT_PART_REPLY);
+       g_object_unref(stream_stdout);
     }
     else
     {
@@ -604,6 +584,51 @@ notmuch_reply_format_default(void *ctx,
     return 0;
 }
 
+static int
+notmuch_reply_format_json(void *ctx,
+                         notmuch_config_t *config,
+                         notmuch_query_t *query,
+                         notmuch_show_params_t *params,
+                         notmuch_bool_t reply_all)
+{
+    GMimeMessage *reply;
+    notmuch_messages_t *messages;
+    notmuch_message_t *message;
+    mime_node_t *node;
+
+    if (notmuch_query_count_messages (query) != 1) {
+       fprintf (stderr, "Error: search term did not match precisely one message.\n");
+       return 1;
+    }
+
+    messages = notmuch_query_search_messages (query);
+    message = notmuch_messages_get (messages);
+    if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,
+                       &node) != NOTMUCH_STATUS_SUCCESS)
+       return 1;
+
+    reply = create_reply_message (ctx, config, message, reply_all);
+    if (!reply)
+       return 1;
+
+    /* The headers of the reply message we've created */
+    printf ("{\"reply-headers\": ");
+    format_headers_json (ctx, reply, TRUE);
+    g_object_unref (G_OBJECT (reply));
+    reply = NULL;
+
+    /* Start the original */
+    printf (", \"original\": ");
+
+    format_part_json (ctx, node, TRUE);
+
+    /* End */
+    printf ("}\n");
+    notmuch_message_destroy (message);
+
+    return 0;
+}
+
 /* This format is currently tuned for a git send-email --notmuch hook */
 static int
 notmuch_reply_format_headers_only(void *ctx,
@@ -666,6 +691,7 @@ notmuch_reply_format_headers_only(void *ctx,
 
 enum {
     FORMAT_DEFAULT,
+    FORMAT_JSON,
     FORMAT_HEADERS_ONLY,
 };
 
@@ -685,6 +711,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
     notmuch_opt_desc_t options[] = {
        { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
          (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
+                                 { "json", FORMAT_JSON },
                                  { "headers-only", FORMAT_HEADERS_ONLY },
                                  { 0, 0 } } },
        { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
@@ -703,6 +730,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
 
     if (format == FORMAT_HEADERS_ONLY)
        reply_format_func = notmuch_reply_format_headers_only;
+    else if (format == FORMAT_JSON)
+       reply_format_func = notmuch_reply_format_json;
     else
        reply_format_func = notmuch_reply_format_default;