+ void *local = talloc_new (NULL);
+ char *query_string;
+ notmuch_database_t *notmuch = NULL;
+ notmuch_query_t *query = NULL;
+ notmuch_message_results_t *messages;
+ notmuch_message_t *message;
+ const char *filename;
+ FILE *file;
+ int ret = 0;
+ int c;
+
+ const char *headers[] = {
+ "Subject", "From", "To", "Cc", "Bcc", "Date"
+ };
+ const char *name, *value;
+ unsigned int i;
+
+ if (argc != 1) {
+ fprintf (stderr, "Error: \"notmuch show\" requires exactly one thread-ID argument.\n");
+ ret = 1;
+ goto DONE;
+ }
+
+ notmuch = notmuch_database_open (NULL);
+ if (notmuch == NULL) {
+ ret = 1;
+ goto DONE;
+ }
+
+ query_string = talloc_asprintf (local, "thread:%s", argv[0]);
+ 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_message_results_has_more (messages);
+ notmuch_message_results_advance (messages))
+ {
+ message = notmuch_message_results_get (messages);
+
+ printf ("%%message{\n");
+
+ printf ("%%header{\n");
+
+ printf ("%s\n", _get_one_line_summary (local, message));
+
+ 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);
+ }
+
+ printf ("%%header}\n");
+
+ filename = notmuch_message_get_filename (message);
+
+ file = fopen (filename, "r");
+ if (file) {
+ size_t header_size = notmuch_message_get_header_size (message);
+ fseek (file, header_size + 1, SEEK_SET);
+ while (1) {
+ c = fgetc (file);
+ if (c == EOF)
+ break;
+ putchar (c);
+ }
+ }
+ fclose (file);
+
+ printf ("%%message}\n");
+
+ notmuch_message_destroy (message);
+ }
+
+ DONE:
+ if (local)
+ talloc_free (local);
+
+ if (query)
+ notmuch_query_destroy (query);
+
+ if (notmuch)
+ notmuch_database_close (notmuch);
+
+ return ret;