]> git.notmuchmail.org Git - notmuch/commitdiff
notmuch: Add a 'part' subcommand
authorDavid Edmondson <dme@dme.org>
Wed, 24 Mar 2010 07:21:20 +0000 (07:21 +0000)
committerDavid Edmondson <dme@dme.org>
Fri, 2 Apr 2010 08:43:03 +0000 (09:43 +0100)
A new 'part' subcommand allows the user to extract a single part from
a MIME message. Usage:
  notmuch part --part=<n> <search terms>
The search terms must match only a single message
(e.g. id:foo@bar.com). The part number specified refers to the part
identifiers output by `notmuch show'. The content of the part is
written the stdout with no formatting or identification marks. It is
not JSON formatted.

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

index c80b39cb4ce4faf5b5669b43c80540bce68f31b7..0fa8fa5d9700c4469c10e33a4092de27ab6423ad 100644 (file)
@@ -107,6 +107,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);
 
@@ -123,6 +126,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 3f2ce73491433ffa035edf58ba3bbfd6b962859b..e317d19855e9eed69483b051a04de4c7749ac62d 100644 (file)
@@ -502,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 95f057eae21b2289df61cc373b354fe0885a07a6..6ebe36dbc64ab9d167af8631f5e16614eb6b287f 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -294,6 +294,17 @@ command_t commands[] = {
       "\t\tcontain tags only from messages that match the search-term(s).\n"
       "\n"
       "\t\tIn both cases the list will be alphabetically sorted." },
+    { "part", notmuch_part_command,
+      "--part=<num> <search-terms>",
+      "\t\tOutput a single MIME part of a message.",
+      "\t\tA single decoded MIME part, with no encoding or framing,\n"
+      "\t\tis output to stdout. The search terms must match only a single\n"
+      "\t\tmessage, otherwise this command will fail.\n"
+      "\n"
+      "\t\tThe part number should match the part \"id\" field output\n"
+      "\t\tby the \"--format=json\" option of \"notmuch show\". If the\n"
+      "\t\tmessage specified by the search terms does not include a\n"
+      "\t\tpart with the specified \"id\" there will be no output." },
     { "help", notmuch_help_command,
       "[<command>]",
       "\t\tThis 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;
+}