1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
24 _get_tags_as_string (void *ctx, notmuch_message_t *message)
31 result = talloc_strdup (ctx, "");
35 for (tags = notmuch_message_get_tags (message);
36 notmuch_tags_has_more (tags);
37 notmuch_tags_advance (tags))
39 tag = notmuch_tags_get (tags);
41 result = talloc_asprintf_append (result, "%s%s",
42 first ? "" : " ", tag);
49 /* Get a nice, single-line summary of message. */
51 _get_one_line_summary (void *ctx, notmuch_message_t *message)
55 const char *relative_date;
58 from = notmuch_message_get_header (message, "from");
60 date = notmuch_message_get_date (message);
61 relative_date = notmuch_time_relative_date (ctx, date);
63 tags = _get_tags_as_string (ctx, message);
65 return talloc_asprintf (ctx, "%s (%s) (%s)",
66 from, relative_date, tags);
70 show_part (GMimeObject *part, int *part_count)
72 GMimeContentDisposition *disposition;
73 GMimeContentType *content_type;
74 GMimeDataWrapper *wrapper;
76 disposition = g_mime_object_get_content_disposition (part);
78 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
80 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
81 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
83 printf ("\fattachment{ ID: %d, Content-type: %s\n",
85 g_mime_content_type_to_string (content_type));
86 printf ("Attachment: %s (%s)\n", filename,
87 g_mime_content_type_to_string (content_type));
88 printf ("\fattachment}\n");
93 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
95 printf ("\fpart{ ID: %d, Content-type: %s\n",
97 g_mime_content_type_to_string (content_type));
99 if (g_mime_content_type_is_type (content_type, "text", "*") &&
100 !g_mime_content_type_is_type (content_type, "text", "html"))
102 GMimeStream *stream = g_mime_stream_file_new (stdout);
103 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
105 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
106 if (wrapper && stream)
107 g_mime_data_wrapper_write_to_stream (wrapper, stream);
109 g_object_unref(stream);
113 printf ("Non-text part: %s\n",
114 g_mime_content_type_to_string (content_type));
117 printf ("\fpart}\n");
121 show_message (void *ctx, notmuch_message_t *message, int indent)
123 const char *headers[] = {
124 "Subject", "From", "To", "Cc", "Bcc", "Date"
126 const char *name, *value;
129 printf ("\fmessage{ id:%s depth:%d filename:%s\n",
130 notmuch_message_get_message_id (message),
132 notmuch_message_get_filename (message));
134 printf ("\fheader{\n");
136 printf ("%s\n", _get_one_line_summary (ctx, message));
138 for (i = 0; i < ARRAY_SIZE (headers); i++) {
140 value = notmuch_message_get_header (message, name);
142 printf ("%s: %s\n", name, value);
145 printf ("\fheader}\n");
146 printf ("\fbody{\n");
148 show_message_body (notmuch_message_get_filename (message), show_part);
150 printf ("\fbody}\n");
152 printf ("\fmessage}\n");
157 show_messages (void *ctx, notmuch_messages_t *messages, int indent)
159 notmuch_message_t *message;
162 notmuch_messages_has_more (messages);
163 notmuch_messages_advance (messages))
165 message = notmuch_messages_get (messages);
167 show_message (ctx, message, indent);
169 show_messages (ctx, notmuch_message_get_replies (message), indent + 1);
171 notmuch_message_destroy (message);
176 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
178 notmuch_config_t *config;
179 notmuch_database_t *notmuch;
180 notmuch_query_t *query;
181 notmuch_threads_t *threads;
182 notmuch_thread_t *thread;
183 notmuch_messages_t *messages;
186 config = notmuch_config_open (ctx, NULL, NULL);
190 notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
194 query_string = query_string_from_args (ctx, argc, argv);
195 if (query_string == NULL) {
196 fprintf (stderr, "Out of memory\n");
200 query = notmuch_query_create (notmuch, query_string);
202 fprintf (stderr, "Out of memory\n");
206 for (threads = notmuch_query_search_threads (query, 0, -1);
207 notmuch_threads_has_more (threads);
208 notmuch_threads_advance (threads))
210 thread = notmuch_threads_get (threads);
212 messages = notmuch_thread_get_toplevel_messages (thread);
214 show_messages (ctx, messages, 0);
216 notmuch_thread_destroy (thread);
219 notmuch_query_destroy (query);
220 notmuch_database_close (notmuch);