Merge remote branch 'dme/dme-for-cworth'

Conflicts:
	notmuch.c (Simply the change in documentation indentation level).
This commit is contained in:
Carl Worth 2010-04-02 15:40:13 -07:00
commit 44df403ccd
5 changed files with 218 additions and 2 deletions

View file

@ -110,6 +110,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]);
int int
notmuch_search_tags_command (void *ctx, int argc, char *argv[]); notmuch_search_tags_command (void *ctx, int argc, char *argv[]);
int
notmuch_part_command (void *ctx, int argc, char *argv[]);
const char * const char *
notmuch_time_relative_date (const void *ctx, time_t then); notmuch_time_relative_date (const void *ctx, time_t then);
@ -126,6 +129,9 @@ notmuch_status_t
show_message_body (const char *filename, show_message_body (const char *filename,
void (*show_part) (GMimeObject *part, int *part_count)); void (*show_part) (GMimeObject *part, int *part_count));
notmuch_status_t
show_one_part (const char *filename, int part);
char * char *
json_quote_str (const void *ctx, const char *str); json_quote_str (const void *ctx, const char *str);

View file

@ -139,11 +139,17 @@ format_message_json (const void *ctx, notmuch_message_t *message, unused (int in
notmuch_tags_t *tags; notmuch_tags_t *tags;
int first = 1; int first = 1;
void *ctx_quote = talloc_new (ctx); void *ctx_quote = talloc_new (ctx);
time_t date;
const char *relative_date;
printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"tags\": [", date = notmuch_message_get_date (message);
relative_date = notmuch_time_relative_date (ctx, date);
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)), json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false", 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); for (tags = notmuch_message_get_tags (message);
notmuch_tags_valid (tags); notmuch_tags_valid (tags);
@ -496,3 +502,78 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
return 0; 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" .B "notmuch restore"
command provides you a way to import all of your tags (or labels as command provides you a way to import all of your tags (or labels as
sup calls them). 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 .SH SEARCH SYNTAX
Several notmuch commands accept a common syntax for search terms. Several notmuch commands accept a common syntax for search terms.

View file

@ -294,6 +294,17 @@ command_t commands[] = {
"\tcontain tags only from messages that match the search-term(s).\n" "\tcontain tags only from messages that match the search-term(s).\n"
"\n" "\n"
"\tIn both cases the list will be alphabetically sorted." }, "\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, { "help", notmuch_help_command,
"[<command>]", "[<command>]",
"This message, or more detailed help for the named command.", "This message, or more detailed help for the named command.",

View file

@ -102,3 +102,95 @@ show_message_body (const char *filename,
return ret; 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;
}