diff options
author | Keith Packard <keithp@keithp.com> | 2009-11-06 10:00:38 -0800 |
---|---|---|
committer | Carl Worth <cworth@cworth.org> | 2009-11-10 13:32:02 -0800 |
commit | 357aba3ec8177c11a7ce22cbe26d92482f6a5e53 (patch) | |
tree | 33f04c027b4de0b525a695878540bfc14e5da563 /notmuch-reply.c | |
parent | 50144f95cababfb73027ca95ad1fb303c235a893 (diff) |
notmuch reply: Add (incomplete) reply command
Reviewed-by: Carl Worth <cworth@cworth.org>
Keith wrote all the code here against notmuch before notmuch.c was
split up into multiple files. So I've pushed the code around in
various ways to match the new code structure, but have generally tried
to avoid making any changes to the behavior of the code.
I did fix one bug---a missing call to g_mime_stream_file_set_owner in
show_part which would cause "notmuch show" to go off into the weeds
when trying to show multiple messages, (since the first stream would
fclose stdout).
Diffstat (limited to 'notmuch-reply.c')
-rw-r--r-- | notmuch-reply.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/notmuch-reply.c b/notmuch-reply.c new file mode 100644 index 0000000..0c3ea13 --- /dev/null +++ b/notmuch-reply.c @@ -0,0 +1,141 @@ +/* notmuch - Not much of an email program, (just index and search) + * + * Copyright © 2009 Carl Worth + * Copyright © 2009 Keith Packard + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/ . + * + * Authors: Carl Worth <cworth@cworth.org> + * Keith Packard <keithp@keithp.com> + */ + +#include "notmuch-client.h" +#include "gmime-filter-reply.h" + +static void +reply_part(GMimeObject *part, int *part_count) +{ + GMimeContentDisposition *disposition; + GMimeContentType *content_type; + GMimeDataWrapper *wrapper; + + (void) part_count; + disposition = g_mime_object_get_content_disposition (part); + if (disposition && + strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) + { + const char *filename = g_mime_part_get_filename (GMIME_PART (part)); + content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); + + printf ("Attachment: %s (%s)\n", filename, + g_mime_content_type_to_string (content_type)); + return; + } + + content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); + + 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; + 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); + } + 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); + } + else + { + printf ("Non-text part: %s\n", + g_mime_content_type_to_string (content_type)); + } +} + +int +notmuch_reply_command (void *ctx, int argc, char *argv[]) +{ + void *local = talloc_new (ctx); + char *query_string; + notmuch_database_t *notmuch = NULL; + notmuch_query_t *query = NULL; + notmuch_messages_t *messages; + notmuch_message_t *message; + int ret = 0; + + const char *headers[] = { + "Subject", "From", "To", "Cc", "Bcc", "Date", + "In-Reply-To", "References" + }; + const char *name, *value; + unsigned int i; + + notmuch = notmuch_database_open (NULL); + if (notmuch == NULL) { + ret = 1; + goto DONE; + } + + query_string = query_string_from_args (local, argc, argv); + if (query_string == NULL) { + fprintf (stderr, "Out of memory\n"); + ret = 1; + goto DONE; + } + + query = notmuch_query_create (notmuch, query_string); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); + ret = 1; + goto DONE; + } + + for (messages = notmuch_query_search_messages (query); + notmuch_messages_has_more (messages); + notmuch_messages_advance (messages)) + { + message = notmuch_messages_get (messages); + + for (i = 0; i < ARRAY_SIZE (headers); i++) { + name = headers[i]; + value = notmuch_message_get_header (message, name); + if (value) + printf ("%s: %s\n", name, value); + } + + show_message_body (notmuch_message_get_filename (message), reply_part); + + notmuch_message_destroy (message); + } + + DONE: + if (local) + talloc_free (local); + + if (query) + notmuch_query_destroy (query); + + if (notmuch) + notmuch_database_close (notmuch); + + return ret; +} |