]> git.cworth.org Git - notmuch/commitdiff
Merge remote branch 'dme/dme-for-cworth'
authorCarl Worth <cworth@cworth.org>
Fri, 2 Apr 2010 22:40:13 +0000 (15:40 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 2 Apr 2010 22:47:27 +0000 (15:47 -0700)
Conflicts:
notmuch.c (Simply the change in documentation indentation level).

notmuch-client.h
notmuch-show.c
notmuch.1
notmuch.c
show-message.c

index 08d0a15b79675a6b45397295bf429f9ca3c68aa3..a090589ef6ef28f350d1364f488b1b8f711bc0bf 100644 (file)
@@ -110,6 +110,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]);
 int
 notmuch_search_tags_command (void *ctx, int argc, char *argv[]);
 
+int
+notmuch_part_command (void *ctx, int argc, char *argv[]);
+
 const char *
 notmuch_time_relative_date (const void *ctx, time_t then);
 
@@ -126,6 +129,9 @@ notmuch_status_t
 show_message_body (const char *filename,
                   void (*show_part) (GMimeObject *part, int *part_count));
 
+notmuch_status_t
+show_one_part (const char *filename, int part);
+
 char *
 json_quote_str (const void *ctx, const char *str);
 
index 96647c1205d32ad036d6920f07dae7aee9547a4c..e317d19855e9eed69483b051a04de4c7749ac62d 100644 (file)
@@ -139,11 +139,17 @@ format_message_json (const void *ctx, notmuch_message_t *message, unused (int in
     notmuch_tags_t *tags;
     int first = 1;
     void *ctx_quote = talloc_new (ctx);
+    time_t date;
+    const char *relative_date;
+
+    date = notmuch_message_get_date (message);
+    relative_date = notmuch_time_relative_date (ctx, date);
 
-    printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"tags\": [",
+    printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"date_unix\": %ld, \"date_relative\": \"%s\", \"tags\": [",
            json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
            notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
-           json_quote_str (ctx_quote, notmuch_message_get_filename (message)));
+           json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
+           date, relative_date);
 
     for (tags = notmuch_message_get_tags (message);
         notmuch_tags_valid (tags);
@@ -496,3 +502,78 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 
     return 0;
 }
+
+int
+notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
+{
+       notmuch_config_t *config;
+       notmuch_database_t *notmuch;
+       notmuch_query_t *query;
+       notmuch_messages_t *messages;
+       notmuch_message_t *message;
+       char *query_string;
+       int i;
+       int part = 0;
+
+       for (i = 0; i < argc && argv[i][0] == '-'; i++) {
+               if (strcmp (argv[i], "--") == 0) {
+                       i++;
+                       break;
+               }
+               if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
+                       part = atoi(argv[i] + sizeof ("--part=") - 1);
+               } else {
+                       fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+                       return 1;
+               }
+       }
+
+       argc -= i;
+       argv += i;
+
+       config = notmuch_config_open (ctx, NULL, NULL);
+       if (config == NULL)
+               return 1;
+
+       query_string = query_string_from_args (ctx, argc, argv);
+       if (query_string == NULL) {
+               fprintf (stderr, "Out of memory\n");
+               return 1;
+       }
+
+       if (*query_string == '\0') {
+               fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
+               return 1;
+       }
+
+       notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+                                        NOTMUCH_DATABASE_MODE_READ_ONLY);
+       if (notmuch == NULL)
+               return 1;
+
+       query = notmuch_query_create (notmuch, query_string);
+       if (query == NULL) {
+               fprintf (stderr, "Out of memory\n");
+               return 1;
+       }
+
+       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 (message == NULL) {
+               fprintf (stderr, "Error: cannot find matching message.\n");
+               return 1;
+       }
+
+       show_one_part (notmuch_message_get_filename (message), part);
+
+       notmuch_query_destroy (query);
+       notmuch_database_close (notmuch);
+
+       return 0;
+}
index e573749cd5988391c8d537ff3b8e5d90c6bbfde6..9d0473d5941423b310bf3c0af9389c2d49399064 100644 (file)
--- a/notmuch.1
+++ b/notmuch.1
@@ -353,6 +353,32 @@ So if you've previously been using sup for mail, then the
 .B "notmuch restore"
 command provides you a way to import all of your tags (or labels as
 sup calls them).
+.RE
+
+The
+.B part
+command can used to output a single part of a multi-part MIME message.
+
+.RS 4
+.TP 4
+.BR part " --part=<part-number> <search-term>..."
+
+Output a single MIME part of a message.
+
+A single decoded MIME part, with no encoding or framing, is output to
+stdout. The search terms must match only a single message, otherwise
+this command will fail.
+
+The part number should match the part "id" field output by the
+"--format=json" option of "notmuch show". If the message specified by
+the search terms does not include a part with the specified "id" there
+will be no output.
+
+See the
+.B "SEARCH SYNTAX"
+section below for details of the supported syntax for <search-terms>.
+.RE
+
 .SH SEARCH SYNTAX
 Several notmuch commands accept a common syntax for search terms.
 
index b392c1c207833bc98d60499896a4b57eb162c559..f5669fcda2093cf5116f7bb99e7c5674745eb9d3 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -294,6 +294,17 @@ command_t commands[] = {
       "\tcontain tags only from messages that match the search-term(s).\n"
       "\n"
       "\tIn both cases the list will be alphabetically sorted." },
+    { "part", notmuch_part_command,
+      "--part=<num> <search-terms>",
+      "Output a single MIME part of a message.",
+      "\tA single decoded MIME part, with no encoding or framing,\n"
+      "\tis output to stdout. The search terms must match only a single\n"
+      "\tmessage, otherwise this command will fail.\n"
+      "\n"
+      "\tThe part number should match the part \"id\" field output\n"
+      "\tby the \"--format=json\" option of \"notmuch show\". If the\n"
+      "\tmessage specified by the search terms does not include a\n"
+      "\tpart with the specified \"id\" there will be no output." },
     { "help", notmuch_help_command,
       "[<command>]",
       "This message, or more detailed help for the named command.",
index 05ced9c421c5ec780563312765ea43fa2a5f87df..b1b61be40c2618fddcc56c46e683e517f9101f7d 100644 (file)
@@ -102,3 +102,95 @@ show_message_body (const char *filename,
 
     return ret;
 }
+
+static void
+show_one_part_output (GMimeObject *part)
+{
+    GMimeStream *stream_filter = NULL;
+    GMimeDataWrapper *wrapper;
+    GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
+
+    stream_filter = g_mime_stream_filter_new(stream_stdout);
+    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);
+}
+
+static void
+show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
+{
+    if (GMIME_IS_MULTIPART (part)) {
+       GMimeMultipart *multipart = GMIME_MULTIPART (part);
+       int i;
+
+       for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
+               show_one_part_worker (g_mime_multipart_get_part (multipart, i),
+                                     part_count, desired_part);
+       }
+       return;
+    }
+
+    if (GMIME_IS_MESSAGE_PART (part)) {
+       GMimeMessage *mime_message;
+
+       mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
+
+       show_one_part_worker (g_mime_message_get_mime_part (mime_message),
+                             part_count, desired_part);
+
+       return;
+    }
+
+    if (! (GMIME_IS_PART (part)))
+       return;
+
+    *part_count = *part_count + 1;
+
+    if (*part_count == desired_part)
+           show_one_part_output (part);
+}
+
+notmuch_status_t
+show_one_part (const char *filename, int part)
+{
+       GMimeStream *stream = NULL;
+       GMimeParser *parser = NULL;
+       GMimeMessage *mime_message = NULL;
+       notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
+       FILE *file = NULL;
+       int part_count = 0;
+
+       file = fopen (filename, "r");
+       if (! file) {
+               fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
+               ret = NOTMUCH_STATUS_FILE_ERROR;
+               goto DONE;
+       }
+
+       stream = g_mime_stream_file_new (file);
+       g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
+
+       parser = g_mime_parser_new_with_stream (stream);
+
+       mime_message = g_mime_parser_construct_message (parser);
+
+       show_one_part_worker (g_mime_message_get_mime_part (mime_message),
+                             &part_count, part);
+
+ DONE:
+       if (mime_message)
+               g_object_unref (mime_message);
+
+       if (parser)
+               g_object_unref (parser);
+
+       if (stream)
+               g_object_unref (stream);
+
+       if (file)
+               fclose (file);
+
+       return ret;
+}