+ fputs (format->message_set_end, stdout);
+}
+
+/* Formatted output of single message */
+static int
+do_show_single (void *ctx,
+ notmuch_query_t *query,
+ const notmuch_show_format_t *format,
+ notmuch_show_params_t *params)
+{
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+
+ 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;
+ }
+
+ notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
+
+ /* Special case for --format=raw of full single message, just cat out file */
+ if (params->raw && 0 == params->part) {
+
+ const char *filename;
+ FILE *file;
+ size_t size;
+ char buf[4096];
+
+ filename = notmuch_message_get_filename (message);
+ if (filename == NULL) {
+ fprintf (stderr, "Error: Cannot message filename.\n");
+ return 1;
+ }
+
+ file = fopen (filename, "r");
+ if (file == NULL) {
+ fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
+ return 1;
+ }
+
+ while (!feof (file)) {
+ size = fread (buf, 1, sizeof (buf), file);
+ if (ferror (file)) {
+ fprintf (stderr, "Error: Read failed from %s\n", filename);
+ fclose (file);
+ return 1;
+ }
+
+ if (fwrite (buf, size, 1, stdout) != 1) {
+ fprintf (stderr, "Error: Write failed\n");
+ fclose (file);
+ return 1;
+ }
+ }
+
+ fclose (file);
+
+ } else {
+
+ show_message (ctx, format, message, 0, params);
+
+ }
+
+ return 0;
+}
+
+/* Formatted output of threads */
+static int
+do_show (void *ctx,
+ notmuch_query_t *query,
+ const notmuch_show_format_t *format,
+ notmuch_show_params_t *params)
+{
+ notmuch_threads_t *threads;
+ notmuch_thread_t *thread;
+ notmuch_messages_t *messages;
+ int first_toplevel = 1;
+
+ fputs (format->message_set_start, stdout);
+
+ for (threads = notmuch_query_search_threads (query);
+ notmuch_threads_valid (threads);
+ notmuch_threads_move_to_next (threads))
+ {
+ thread = notmuch_threads_get (threads);
+
+ messages = notmuch_thread_get_toplevel_messages (thread);
+
+ if (messages == NULL)
+ INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
+ notmuch_thread_get_thread_id (thread));
+
+ if (!first_toplevel)
+ fputs (format->message_set_sep, stdout);
+ first_toplevel = 0;
+
+ show_messages (ctx, format, messages, 0, params);
+
+ notmuch_thread_destroy (thread);
+
+ }
+
+ fputs (format->message_set_end, stdout);
+
+ return 0;
+}
+
+int
+notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
+{
+ notmuch_config_t *config;
+ notmuch_database_t *notmuch;
+ notmuch_query_t *query;
+ char *query_string;
+ char *opt;
+ const notmuch_show_format_t *format = &format_text;
+ notmuch_show_params_t params;
+ int mbox = 0;
+ int format_specified = 0;
+ int i;
+
+ params.entire_thread = 0;
+ params.raw = 0;
+ params.part = -1;
+ params.cryptoctx = NULL;
+ params.decrypt = 0;
+
+ argc--; argv++; /* skip subcommand argument */
+
+ for (i = 0; i < argc && argv[i][0] == '-'; i++) {
+ if (strcmp (argv[i], "--") == 0) {
+ i++;
+ break;
+ }
+ if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
+ opt = argv[i] + sizeof ("--format=") - 1;
+ if (strcmp (opt, "text") == 0) {
+ format = &format_text;
+ } else if (strcmp (opt, "json") == 0) {
+ format = &format_json;
+ params.entire_thread = 1;
+ } else if (strcmp (opt, "mbox") == 0) {
+ format = &format_mbox;
+ mbox = 1;
+ } else if (strcmp (opt, "raw") == 0) {
+ format = &format_raw;
+ params.raw = 1;
+ } else {
+ fprintf (stderr, "Invalid value for --format: %s\n", opt);
+ return 1;
+ }
+ format_specified = 1;
+ } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
+ params.part = atoi(argv[i] + sizeof ("--part=") - 1);
+ } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
+ params.entire_thread = 1;
+ } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||
+ (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
+ if (params.cryptoctx == NULL) {
+#ifdef GMIME_ATLEAST_26
+ /* TODO: GMimePasswordRequestFunc */
+ if (NULL == (params.cryptoctx = g_mime_gpg_context_new(NULL, "gpg")))
+#else
+ GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
+ if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
+#endif
+ fprintf (stderr, "Failed to construct gpg context.\n");
+ else
+ g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
+#ifndef GMIME_ATLEAST_26
+ g_object_unref (session);
+ session = NULL;
+#endif
+ }
+ if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)
+ params.decrypt = 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 (mbox && params.part > 0) {
+ fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
+ return 1;
+ }
+
+ if (*query_string == '\0') {
+ fprintf (stderr, "Error: notmuch show 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 part was requested and format was not specified, use format=raw */
+ if (params.part >= 0 && !format_specified)
+ format = &format_raw;
+
+ /* If --format=raw specified without specifying part, we can only
+ * output single message, so set part=0 */
+ if (params.raw && params.part < 0)
+ params.part = 0;
+
+ if (params.part >= 0)
+ return do_show_single (ctx, query, format, ¶ms);
+ else
+ return do_show (ctx, query, format, ¶ms);
+