/* 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); return show_message (ctx, format, message, 0, params) != NOTMUCH_STATUS_SUCCESS; }
static int print_count (notmuch_database_t *notmuch, const char *query_str, const char **exclude_tags, size_t exclude_tags_length, int output) { notmuch_query_t *query; size_t i; query = notmuch_query_create (notmuch, query_str); if (query == NULL) { fprintf (stderr, "Out of memory\n"); return 1; } for (i = 0; i < exclude_tags_length; i++) notmuch_query_add_tag_exclude (query, exclude_tags[i]); switch (output) { case OUTPUT_MESSAGES: printf ("%u\n", notmuch_query_count_messages (query)); break; case OUTPUT_THREADS: printf ("%u\n", notmuch_query_count_threads (query)); break; } notmuch_query_destroy (query); return 0; }
/* 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); (void) fwrite (buf, size, 1, stdout); } fclose (file); } else { show_message (ctx, format, message, 0, params); } return 0; }
static int notmuch_reply_format_sprinter(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params, notmuch_bool_t reply_all, sprinter_t *sp) { GMimeMessage *reply; notmuch_messages_t *messages; notmuch_message_t *message; mime_node_t *node; 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 (mime_node_open (ctx, message, &(params->crypto), &node) != NOTMUCH_STATUS_SUCCESS) return 1; reply = create_reply_message (ctx, config, message, reply_all); if (!reply) return 1; sp->begin_map (sp); /* The headers of the reply message we've created */ sp->map_key (sp, "reply-headers"); format_headers_sprinter (sp, reply, TRUE); g_object_unref (G_OBJECT (reply)); reply = NULL; /* Start the original */ sp->map_key (sp, "original"); format_part_sprinter (ctx, sp, node, TRUE, TRUE, FALSE); /* End */ sp->end (sp); notmuch_message_destroy (message); return 0; }
static void nm_progress_update(CONTEXT *ctx, notmuch_query_t *q) { struct nm_ctxdata *data = get_ctxdata(ctx); if (ctx->quiet || data->noprogress) return; if (!data->progress_ready && q) { static char msg[STRING]; snprintf(msg, sizeof(msg), _("Reading messages...")); mutt_progress_init(&data->progress, msg, M_PROGRESS_MSG, ReadInc, notmuch_query_count_messages(q)); data->progress_ready = 1; } if (data->progress_ready) mutt_progress_update(&data->progress, ctx->msgcount + data->ignmsgcount - data->oldmsgcount, -1); }
int notmuch_count_command (void *ctx, int argc, char *argv[]) { notmuch_config_t *config; notmuch_database_t *notmuch; notmuch_query_t *query; char *query_str; int i; notmuch_bool_t output_messages = TRUE; 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], "--output=") == 0) { const char *opt = argv[i] + sizeof ("--output=") - 1; if (strcmp (opt, "threads") == 0) { output_messages = FALSE; } else if (strcmp (opt, "messages") == 0) { output_messages = TRUE; } else { fprintf (stderr, "Invalid value for --output: %s\n", opt); return 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; notmuch = notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) return 1; query_str = query_string_from_args (ctx, argc, argv); if (query_str == NULL) { fprintf (stderr, "Out of memory.\n"); return 1; } if (*query_str == '\0') { query_str = talloc_strdup (ctx, ""); } query = notmuch_query_create (notmuch, query_str); if (query == NULL) { fprintf (stderr, "Out of memory\n"); return 1; } if (output_messages) printf ("%u\n", notmuch_query_count_messages (query)); else printf ("%u\n", notmuch_query_count_threads (query)); notmuch_query_destroy (query); notmuch_database_close (notmuch); return 0; }
int notmuch_count_command (void *ctx, int argc, char *argv[]) { notmuch_config_t *config; notmuch_database_t *notmuch; notmuch_query_t *query; char *query_str; int i; #if 0 char *opt, *end; int i, first = 0, max_threads = -1; notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST; #endif for (i = 0; i < argc && argv[i][0] == '-'; i++) { if (strcmp (argv[i], "--") == 0) { i++; break; } #if 0 if (STRNCMP_LITERAL (argv[i], "--first=") == 0) { opt = argv[i] + sizeof ("--first=") - 1; first = strtoul (opt, &end, 10); if (*opt == '\0' || *end != '\0') { fprintf (stderr, "Invalid value for --first: %s\n", opt); return 1; } } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) { opt = argv[i] + sizeof ("--max-threads=") - 1; max_threads = strtoul (opt, &end, 10); if (*opt == '\0' || *end != '\0') { fprintf (stderr, "Invalid value for --max-threads: %s\n", opt); return 1; } } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) { opt = argv[i] + sizeof ("--sort=") - 1; if (strcmp (opt, "oldest-first") == 0) { sort = NOTMUCH_SORT_OLDEST_FIRST; } else if (strcmp (opt, "newest-first") == 0) { sort = NOTMUCH_SORT_NEWEST_FIRST; } else { fprintf (stderr, "Invalid value for --sort: %s\n", opt); return 1; } } else #endif { 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; notmuch = notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) return 1; query_str = query_string_from_args (ctx, argc, argv); if (query_str == NULL) { fprintf (stderr, "Out of memory.\n"); return 1; } if (*query_str == '\0') { fprintf (stderr, "Error: notmuch count requires at least one count term.\n"); return 1; } query = notmuch_query_create (notmuch, query_str); if (query == NULL) { fprintf (stderr, "Out of memory\n"); return 1; } printf ("%u\n", notmuch_query_count_messages(query)); notmuch_query_destroy (query); notmuch_database_close (notmuch); return 0; }
static int do_search_messages (const search_format_t *format, notmuch_query_t *query, output_t output, int offset, int limit) { notmuch_message_t *message; notmuch_messages_t *messages; notmuch_filenames_t *filenames; int first_message = 1; int i; if (offset < 0) { offset += notmuch_query_count_messages (query); if (offset < 0) offset = 0; } messages = notmuch_query_search_messages (query); if (messages == NULL) return 1; fputs (format->results_start, stdout); for (i = 0; notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit); notmuch_messages_move_to_next (messages), i++) { if (i < offset) continue; message = notmuch_messages_get (messages); if (output == OUTPUT_FILES) { filenames = notmuch_message_get_filenames (message); for (; notmuch_filenames_valid (filenames); notmuch_filenames_move_to_next (filenames)) { if (! first_message) fputs (format->item_sep, stdout); format->item_id (message, "", notmuch_filenames_get (filenames)); first_message = 0; } notmuch_filenames_destroy( filenames ); } else { /* output == OUTPUT_MESSAGES */ if (! first_message) fputs (format->item_sep, stdout); format->item_id (message, "id:", notmuch_message_get_message_id (message)); first_message = 0; } notmuch_message_destroy (message); } notmuch_messages_destroy (messages); if (first_message) fputs (format->results_null, stdout); else fputs (format->results_end, stdout); return 0; }