+ void *local = talloc_new (ctx);
+ char *query_string;
+ notmuch_database_t *notmuch = NULL;
+ notmuch_query_t *query = NULL;
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+ int ret = 0;
+
+ 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_messages_has_more (messages);
+ notmuch_messages_advance (messages))
+ {
+ message = notmuch_messages_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");
+
+ show_message_body (notmuch_message_get_filename (message));
+
+ 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;
+}
+
+static int
+tag_command (void *ctx, unused (int argc), unused (char *argv[]))
+{
+ void *local;
+ int *add_tags, *remove_tags;
+ int add_tags_count = 0;
+ int remove_tags_count = 0;
+ char *query_string;
+ notmuch_database_t *notmuch = NULL;
+ notmuch_query_t *query;
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+ int ret = 0;
+ int i;
+
+ local = talloc_new (ctx);
+ if (local == NULL) {
+ ret = 1;
+ goto DONE;
+ }
+
+ add_tags = talloc_size (local, argc * sizeof (int));
+ if (add_tags == NULL) {
+ ret = 1;
+ goto DONE;
+ }
+
+ remove_tags = talloc_size (local, argc * sizeof (int));
+ if (remove_tags == NULL) {
+ ret = 1;
+ goto DONE;
+ }
+
+ for (i = 0; i < argc; i++) {
+ if (strcmp (argv[i], "--") == 0) {
+ i++;
+ break;
+ }
+ if (argv[i][0] == '+') {
+ add_tags[add_tags_count++] = i;
+ } else if (argv[i][0] == '-') {
+ remove_tags[remove_tags_count++] = i;
+ } else {
+ break;
+ }
+ }
+
+ if (add_tags_count == 0 && remove_tags_count == 0) {
+ fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
+ ret = 1;
+ goto DONE;
+ }
+
+ if (i == argc) {
+ fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n");
+ ret = 1;
+ goto DONE;
+ }
+
+ notmuch = notmuch_database_open (NULL);
+ if (notmuch == NULL) {
+ ret = 1;
+ goto DONE;
+ }
+
+ query_string = query_string_from_args (local, argc - i, &argv[i]);
+
+ 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_messages_has_more (messages);
+ notmuch_messages_advance (messages))
+ {
+ message = notmuch_messages_get (messages);
+
+ notmuch_message_freeze (message);
+
+ for (i = 0; i < remove_tags_count; i++)
+ notmuch_message_remove_tag (message,
+ argv[remove_tags[i]] + 1);
+
+ for (i = 0; i < add_tags_count; i++)
+ notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
+
+ notmuch_message_thaw (message);
+
+ notmuch_message_destroy (message);
+ }
+
+ notmuch_query_destroy (query);
+
+ DONE:
+ if (notmuch)
+ notmuch_database_close (notmuch);
+
+ talloc_free (local);
+
+ return ret;