1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see http://www.gnu.org/licenses/ .
19 * Authors: Carl Worth <cworth@cworth.org>
20 * Keith Packard <keithp@keithp.com>
23 #include "notmuch-client.h"
24 #include "gmime-filter-reply.h"
27 reply_part(GMimeObject *part, int *part_count)
29 GMimeContentDisposition *disposition;
30 GMimeContentType *content_type;
31 GMimeDataWrapper *wrapper;
34 disposition = g_mime_object_get_content_disposition (part);
36 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
38 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
39 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
41 printf ("Attachment: %s (%s)\n", filename,
42 g_mime_content_type_to_string (content_type));
46 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
48 if (g_mime_content_type_is_type (content_type, "text", "*") &&
49 !g_mime_content_type_is_type (content_type, "text", "html"))
51 GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
52 stream_stdout = g_mime_stream_file_new (stdout);
54 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
55 stream_filter = g_mime_stream_filter_new(stream_stdout);
57 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
58 g_mime_filter_reply_new(TRUE));
59 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
60 if (wrapper && stream_filter)
61 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
63 g_object_unref(stream_filter);
65 g_object_unref(stream_stdout);
69 printf ("Non-text part: %s\n",
70 g_mime_content_type_to_string (content_type));
75 notmuch_reply_command (void *ctx, int argc, char *argv[])
77 void *local = talloc_new (ctx);
79 notmuch_database_t *notmuch = NULL;
80 notmuch_query_t *query = NULL;
81 notmuch_messages_t *messages;
82 notmuch_message_t *message;
85 const char *headers[] = {
86 "Subject", "From", "To", "Cc", "Bcc", "Date",
87 "In-Reply-To", "References"
89 const char *name, *value;
92 notmuch = notmuch_database_open (NULL);
93 if (notmuch == NULL) {
98 query_string = query_string_from_args (local, argc, argv);
99 if (query_string == NULL) {
100 fprintf (stderr, "Out of memory\n");
105 query = notmuch_query_create (notmuch, query_string);
107 fprintf (stderr, "Out of memory\n");
112 for (messages = notmuch_query_search_messages (query);
113 notmuch_messages_has_more (messages);
114 notmuch_messages_advance (messages))
116 message = notmuch_messages_get (messages);
118 for (i = 0; i < ARRAY_SIZE (headers); i++) {
120 value = notmuch_message_get_header (message, name);
122 printf ("%s: %s\n", name, value);
125 show_message_body (notmuch_message_get_filename (message), reply_part);
127 notmuch_message_destroy (message);
135 notmuch_query_destroy (query);
138 notmuch_database_close (notmuch);