notmuch: Add a 'part' subcommand

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.
This commit is contained in:
David Edmondson 2010-03-24 07:21:20 +00:00
parent 930a47935f
commit 2e9c7aba99
5 changed files with 210 additions and 0 deletions

View 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);

View 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;
}

View file

@ -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.

View file

@ -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.",

View 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;
}