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"
23 typedef struct show_format {
24 const char *message_set_start;
25 const char *message_start;
26 void (*message) (const void *ctx,
27 notmuch_message_t *message,
29 const char *header_start;
30 void (*header) (const void *ctx,
31 notmuch_message_t *message);
32 const char *header_end;
33 const char *body_start;
34 void (*part) (GMimeObject *part,
37 const char *message_end;
38 const char *message_set_sep;
39 const char *message_set_end;
43 format_message_text (unused (const void *ctx),
44 notmuch_message_t *message,
47 format_headers_text (const void *ctx,
48 notmuch_message_t *message);
50 format_part_text (GMimeObject *part,
52 static const show_format_t format_text = {
54 "\fmessage{ ", format_message_text,
55 "\fheader{\n", format_headers_text, "\fheader}\n",
56 "\fbody{\n", format_part_text, "\fbody}\n",
62 format_message_json (const void *ctx,
63 notmuch_message_t *message,
66 format_headers_json (const void *ctx,
67 notmuch_message_t *message);
69 format_part_json (GMimeObject *part,
71 static const show_format_t format_json = {
73 "{", format_message_json,
74 ", \"headers\": {", format_headers_json, "}",
75 ", \"body\": [", format_part_json, "]",
81 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
88 result = talloc_strdup (ctx, "");
92 for (tags = notmuch_message_get_tags (message);
93 notmuch_tags_valid (tags);
94 notmuch_tags_move_to_next (tags))
96 tag = notmuch_tags_get (tags);
98 result = talloc_asprintf_append (result, "%s%s",
99 first ? "" : " ", tag);
106 /* Get a nice, single-line summary of message. */
108 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
112 const char *relative_date;
115 from = notmuch_message_get_header (message, "from");
117 date = notmuch_message_get_date (message);
118 relative_date = notmuch_time_relative_date (ctx, date);
120 tags = _get_tags_as_string (ctx, message);
122 return talloc_asprintf (ctx, "%s (%s) (%s)",
123 from, relative_date, tags);
127 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
129 printf ("id:%s depth:%d match:%d filename:%s\n",
130 notmuch_message_get_message_id (message),
132 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
133 notmuch_message_get_filename (message));
137 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
139 notmuch_tags_t *tags;
141 void *ctx_quote = talloc_new (ctx);
143 const char *relative_date;
145 date = notmuch_message_get_date (message);
146 relative_date = notmuch_time_relative_date (ctx, date);
148 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"date_unix\": %ld, \"date_relative\": \"%s\", \"tags\": [",
149 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
150 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
151 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
152 date, relative_date);
154 for (tags = notmuch_message_get_tags (message);
155 notmuch_tags_valid (tags);
156 notmuch_tags_move_to_next (tags))
158 printf("%s%s", first ? "" : ",",
159 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
163 talloc_free (ctx_quote);
167 format_headers_text (const void *ctx, notmuch_message_t *message)
169 const char *headers[] = {
170 "Subject", "From", "To", "Cc", "Bcc", "Date"
172 const char *name, *value;
175 printf ("%s\n", _get_one_line_summary (ctx, message));
177 for (i = 0; i < ARRAY_SIZE (headers); i++) {
179 value = notmuch_message_get_header (message, name);
180 if (value && strlen (value))
181 printf ("%s: %s\n", name, value);
186 format_headers_json (const void *ctx, notmuch_message_t *message)
188 const char *headers[] = {
189 "Subject", "From", "To", "Cc", "Bcc", "Date"
191 const char *name, *value;
193 int first_header = 1;
194 void *ctx_quote = talloc_new (ctx);
196 for (i = 0; i < ARRAY_SIZE (headers); i++) {
198 value = notmuch_message_get_header (message, name);
202 fputs (", ", stdout);
206 json_quote_str (ctx_quote, name),
207 json_quote_str (ctx_quote, value));
211 talloc_free (ctx_quote);
215 show_part_content (GMimeObject *part, GMimeStream *stream_out)
217 GMimeStream *stream_filter = NULL;
218 GMimeDataWrapper *wrapper;
221 charset = g_mime_object_get_content_type_parameter (part, "charset");
224 stream_filter = g_mime_stream_filter_new(stream_out);
225 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
226 g_mime_filter_crlf_new(FALSE, FALSE));
228 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
229 g_mime_filter_charset_new(charset, "UTF-8"));
233 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
234 if (wrapper && stream_filter)
235 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
237 g_object_unref(stream_filter);
241 format_part_text (GMimeObject *part, int *part_count)
243 GMimeContentDisposition *disposition;
244 GMimeContentType *content_type;
246 disposition = g_mime_object_get_content_disposition (part);
248 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
250 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
251 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
253 printf ("\fattachment{ ID: %d, Content-type: %s\n",
255 g_mime_content_type_to_string (content_type));
256 printf ("Attachment: %s (%s)\n", filename,
257 g_mime_content_type_to_string (content_type));
259 if (g_mime_content_type_is_type (content_type, "text", "*") &&
260 !g_mime_content_type_is_type (content_type, "text", "html"))
262 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
263 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
264 show_part_content (part, stream_stdout);
265 g_object_unref(stream_stdout);
268 printf ("\fattachment}\n");
273 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
275 printf ("\fpart{ ID: %d, Content-type: %s\n",
277 g_mime_content_type_to_string (content_type));
279 if (g_mime_content_type_is_type (content_type, "text", "*") &&
280 !g_mime_content_type_is_type (content_type, "text", "html"))
282 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
283 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
284 show_part_content (part, stream_stdout);
285 g_object_unref(stream_stdout);
289 printf ("Non-text part: %s\n",
290 g_mime_content_type_to_string (content_type));
293 printf ("\fpart}\n");
297 format_part_json (GMimeObject *part, int *part_count)
299 GMimeContentType *content_type;
300 GMimeContentDisposition *disposition;
301 void *ctx = talloc_new (NULL);
302 GMimeStream *stream_memory = g_mime_stream_mem_new ();
303 GByteArray *part_content;
305 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
308 fputs (", ", stdout);
310 printf ("{\"id\": %d, \"content-type\": %s",
312 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
314 disposition = g_mime_object_get_content_disposition (part);
316 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
318 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
320 printf (", \"filename\": %s", json_quote_str (ctx, filename));
323 if (g_mime_content_type_is_type (content_type, "text", "*") &&
324 !g_mime_content_type_is_type (content_type, "text", "html"))
326 show_part_content (part, stream_memory);
327 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
329 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
336 g_object_unref (stream_memory);
340 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
342 fputs (format->message_start, stdout);
344 format->message(ctx, message, indent);
346 fputs (format->header_start, stdout);
348 format->header(ctx, message);
349 fputs (format->header_end, stdout);
351 fputs (format->body_start, stdout);
353 show_message_body (notmuch_message_get_filename (message), format->part);
354 fputs (format->body_end, stdout);
356 fputs (format->message_end, stdout);
361 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
362 notmuch_bool_t entire_thread)
364 notmuch_message_t *message;
365 notmuch_bool_t match;
369 fputs (format->message_set_start, stdout);
372 notmuch_messages_valid (messages);
373 notmuch_messages_move_to_next (messages))
376 fputs (format->message_set_sep, stdout);
379 fputs (format->message_set_start, stdout);
381 message = notmuch_messages_get (messages);
383 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
385 next_indent = indent;
387 if (match || entire_thread) {
388 show_message (ctx, format, message, indent);
389 next_indent = indent + 1;
391 fputs (format->message_set_sep, stdout);
394 show_messages (ctx, format, notmuch_message_get_replies (message),
395 next_indent, entire_thread);
397 notmuch_message_destroy (message);
399 fputs (format->message_set_end, stdout);
402 fputs (format->message_set_end, stdout);
406 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
408 notmuch_config_t *config;
409 notmuch_database_t *notmuch;
410 notmuch_query_t *query;
411 notmuch_threads_t *threads;
412 notmuch_thread_t *thread;
413 notmuch_messages_t *messages;
416 const show_format_t *format = &format_text;
417 int entire_thread = 0;
419 int first_toplevel = 1;
421 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
422 if (strcmp (argv[i], "--") == 0) {
426 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
427 opt = argv[i] + sizeof ("--format=") - 1;
428 if (strcmp (opt, "text") == 0) {
429 format = &format_text;
430 } else if (strcmp (opt, "json") == 0) {
431 format = &format_json;
434 fprintf (stderr, "Invalid value for --format: %s\n", opt);
437 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
440 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
448 config = notmuch_config_open (ctx, NULL, NULL);
452 query_string = query_string_from_args (ctx, argc, argv);
453 if (query_string == NULL) {
454 fprintf (stderr, "Out of memory\n");
458 if (*query_string == '\0') {
459 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
463 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
464 NOTMUCH_DATABASE_MODE_READ_ONLY);
468 query = notmuch_query_create (notmuch, query_string);
470 fprintf (stderr, "Out of memory\n");
474 fputs (format->message_set_start, stdout);
476 for (threads = notmuch_query_search_threads (query);
477 notmuch_threads_valid (threads);
478 notmuch_threads_move_to_next (threads))
480 thread = notmuch_threads_get (threads);
482 messages = notmuch_thread_get_toplevel_messages (thread);
484 if (messages == NULL)
485 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
486 notmuch_thread_get_thread_id (thread));
489 fputs (format->message_set_sep, stdout);
492 show_messages (ctx, format, messages, 0, entire_thread);
494 notmuch_thread_destroy (thread);
498 fputs (format->message_set_end, stdout);
500 notmuch_query_destroy (query);
501 notmuch_database_close (notmuch);
507 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
509 notmuch_config_t *config;
510 notmuch_database_t *notmuch;
511 notmuch_query_t *query;
512 notmuch_messages_t *messages;
513 notmuch_message_t *message;
518 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
519 if (strcmp (argv[i], "--") == 0) {
523 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
524 part = atoi(argv[i] + sizeof ("--part=") - 1);
526 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
534 config = notmuch_config_open (ctx, NULL, NULL);
538 query_string = query_string_from_args (ctx, argc, argv);
539 if (query_string == NULL) {
540 fprintf (stderr, "Out of memory\n");
544 if (*query_string == '\0') {
545 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
549 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
550 NOTMUCH_DATABASE_MODE_READ_ONLY);
554 query = notmuch_query_create (notmuch, query_string);
556 fprintf (stderr, "Out of memory\n");
560 if (notmuch_query_count_messages (query) != 1) {
561 fprintf (stderr, "Error: search term did not match precisely one message.\n");
565 messages = notmuch_query_search_messages (query);
566 message = notmuch_messages_get (messages);
568 if (message == NULL) {
569 fprintf (stderr, "Error: cannot find matching message.\n");
573 show_one_part (notmuch_message_get_filename (message), part);
575 notmuch_query_destroy (query);
576 notmuch_database_close (notmuch);