Example #1
0
int
notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
{
    notmuch_database_t *notmuch;
    notmuch_query_t *query;
    FILE *output = stdout;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    notmuch_tags_t *tags;
    const char *query_str = "";

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
	return 1;

    char *output_file_name = NULL;
    int opt_index;

    int output_format = DUMP_FORMAT_SUP;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
	  (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
				  { "batch-tag", DUMP_FORMAT_BATCH_TAG },
				  { 0, 0 } } },
	{ NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
Example #2
0
int
notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
{
    notmuch_database_t *notmuch;
    notmuch_bool_t accumulate = FALSE;
    tag_op_flag_t flags = 0;
    tag_op_list_t *tag_ops;

    char *input_file_name = NULL;
    const char *name_for_error = NULL;
    gzFile input = NULL;
    char *line = NULL;
    void *line_ctx = NULL;
    ssize_t line_len;

    int ret = 0;
    int opt_index;
    int input_format = DUMP_FORMAT_AUTO;

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
	return EXIT_FAILURE;

    if (notmuch_config_get_maildir_synchronize_flags (config))
	flags |= TAG_FLAG_MAILDIR_SYNC;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
	  (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
				  { "batch-tag", DUMP_FORMAT_BATCH_TAG },
				  { "sup", DUMP_FORMAT_SUP },
Example #3
0
/* Handle the case of "notmuch" being invoked with no command
 * argument. For now we just call notmuch_setup_command, but we plan
 * to be more clever about this in the future.
 */
static int
notmuch (void *ctx)
{
    notmuch_config_t *config;
    notmuch_bool_t is_new;
    char *db_path;
    struct stat st;

    config = notmuch_config_open (ctx, NULL, &is_new);

    /* If the user has never configured notmuch, then run
     * notmuch_setup_command which will give a nice welcome message,
     * and interactively guide the user through the configuration. */
    if (is_new) {
	notmuch_config_close (config);
	return notmuch_setup_command (ctx, 0, NULL);
    }

    /* Notmuch is already configured, but is there a database? */
    db_path = talloc_asprintf (ctx, "%s/%s",
			       notmuch_config_get_database_path (config),
			       ".notmuch");
    if (stat (db_path, &st)) {
	notmuch_config_close (config);
	if (errno != ENOENT) {
	    fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
		     db_path, strerror (errno));
	    return 1;
	}
	printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
		db_path);
	printf ("You probably want to run \"notmuch new\" now to create that database.\n\n"
		"Note that the first run of \"notmuch new\" can take a very long time\n"
		"and that the resulting database will use roughly the same amount of\n"
		"storage space as the email being indexed.\n\n");
	return 0;
    }

    printf ("Notmuch is configured and appears to have a database. Excellent!\n\n"
	    "At this point you can start exploring the functionality of notmuch by\n"
	    "using commands such as:\n\n"
	    "\tnotmuch search tag:inbox\n\n"
	    "\tnotmuch search to:\"%s\"\n\n"
	    "\tnotmuch search from:\"%s\"\n\n"
	    "\tnotmuch search subject:\"my favorite things\"\n\n"
	    "See \"notmuch help search\" for more details.\n\n"
	    "You can also use \"notmuch show\" with any of the thread IDs resulting\n"
	    "from a search. Finally, you may want to explore using a more sophisticated\n"
	    "interface to notmuch such as the emacs interface implemented in notmuch.el\n"
	    "or any other interface described at http://notmuchmail.org\n\n"
	    "And don't forget to run \"notmuch new\" whenever new mail arrives.\n\n"
	    "Have fun, and may your inbox never have much mail.\n\n",
	    notmuch_config_get_user_name (config),
	    notmuch_config_get_user_primary_email (config));

    notmuch_config_close (config);

    return 0;
}
Example #4
0
static int
notmuch_config_command_get (void *ctx, char *item)
{
    notmuch_config_t *config;

    config = notmuch_config_open (ctx, NULL, NULL);
    if (config == NULL)
	return 1;

    if (strcmp(item, "database.path") == 0) {
	printf ("%s\n", notmuch_config_get_database_path (config));
    } else if (strcmp(item, "user.name") == 0) {
	printf ("%s\n", notmuch_config_get_user_name (config));
    } else if (strcmp(item, "user.primary_email") == 0) {
	printf ("%s\n", notmuch_config_get_user_primary_email (config));
    } else if (strcmp(item, "user.other_email") == 0) {
	const char **other_email;
	size_t i, length;
	
	other_email = notmuch_config_get_user_other_email (config, &length);
	for (i = 0; i < length; i++)
	    printf ("%s\n", other_email[i]);
    } else if (strcmp(item, "new.tags") == 0) {
	const char **tags;
	size_t i, length;

	tags = notmuch_config_get_new_tags (config, &length);
	for (i = 0; i < length; i++)
	    printf ("%s\n", tags[i]);
    } else {
	char **value;
	size_t i, length;
	char *group, *key;

	if (_item_split (item, &group, &key))
	    return 1;

	value = g_key_file_get_string_list (config->key_file,
					    group, key,
					    &length, NULL);
	if (value == NULL) {
	    fprintf (stderr, "Unknown configuration item: %s.%s\n",
		     group, key);
	    return 1;
	}

	for (i = 0; i < length; i++)
	    printf ("%s\n", value[i]);

	g_strfreev (value);
    }

    notmuch_config_close (config);

    return 0;
}
Example #5
0
int
notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])
{
    const char *path = notmuch_config_get_database_path (config);
    const char *backup_path = NULL;
    notmuch_status_t ret;
    notmuch_bool_t quiet = FALSE;
    int opt_index;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_STRING, &backup_path, "backup", 0, 0 },
	{ NOTMUCH_OPT_BOOLEAN,  &quiet, "quiet", 'q', 0 },
	{ NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
	{ 0, 0, 0, 0, 0}
    };

    opt_index = parse_arguments (argc, argv, options, 1);
    if (opt_index < 0)
	return EXIT_FAILURE;

    if (notmuch_requested_db_uuid) {
	fprintf (stderr, "Error: --uuid not implemented for compact\n");
	return EXIT_FAILURE;
    }

    notmuch_process_shared_options (argv[0]);

    if (! quiet)
	printf ("Compacting database...\n");
    ret = notmuch_database_compact (path, backup_path,
				    quiet ? NULL : status_update_cb, NULL);
    if (ret) {
	fprintf (stderr, "Compaction failed: %s\n", notmuch_status_to_string (ret));
	return EXIT_FAILURE;
    }

    if (! quiet) {
	if (backup_path)
	    printf ("The old database has been moved to %s.\n", backup_path);

	printf ("Done.\n");
    }

    return EXIT_SUCCESS;
}
Example #6
0
int
notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
{
    notmuch_database_t *notmuch;
    const char *query_str = NULL;
    int ret;

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
	return EXIT_FAILURE;

    char *output_file_name = NULL;
    int opt_index;

    int output_format = DUMP_FORMAT_BATCH_TAG;
    notmuch_bool_t gzip_output = 0;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
	  (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
				  { "batch-tag", DUMP_FORMAT_BATCH_TAG },
				  { 0, 0 } } },
	{ NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
Example #7
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;
    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;
}
Example #8
0
int
notmuch_search_command (void *ctx, int argc, char *argv[])
{
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    notmuch_query_t *query;
    char *query_str;
    char *opt;
    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
    const search_format_t *format = &format_text;
    int i, ret;
    output_t output = OUTPUT_SUMMARY;

    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
	if (strcmp (argv[i], "--") == 0) {
	    i++;
	    break;
	}
        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 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;
	    } else {
		fprintf (stderr, "Invalid value for --format: %s\n", opt);
		return 1;
	    }
	} else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
	    opt = argv[i] + sizeof ("--output=") - 1;
	    if (strcmp (opt, "summary") == 0) {
		output = OUTPUT_SUMMARY;
	    } else if (strcmp (opt, "threads") == 0) {
		output = OUTPUT_THREADS;
	    } else if (strcmp (opt, "messages") == 0) {
		output = OUTPUT_MESSAGES;
	    } else if (strcmp (opt, "files") == 0) {
		output = OUTPUT_FILES;
	    } else if (strcmp (opt, "tags") == 0) {
		output = OUTPUT_TAGS;
	    } 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 (notmuch, argc, argv);
    if (query_str == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return 1;
    }
    if (*query_str == '\0') {
	fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
	return 1;
    }

    query = notmuch_query_create (notmuch, query_str);
    if (query == NULL) {
	fprintf (stderr, "Out of memory\n");
	return 1;
    }

    notmuch_query_set_sort (query, sort);

    switch (output) {
    default:
    case OUTPUT_SUMMARY:
    case OUTPUT_THREADS:
	ret = do_search_threads (format, query, sort, output);
	break;
    case OUTPUT_MESSAGES:
    case OUTPUT_FILES:
	ret = do_search_messages (format, query, output);
	break;
    case OUTPUT_TAGS:
	ret = do_search_tags (notmuch, format, query);
	break;
    }

    notmuch_query_destroy (query);
    notmuch_database_close (notmuch);

    return ret;
}
Example #9
0
int
notmuch_setup_command (notmuch_config_t *config,
		       unused (int argc), unused (char *argv[]))
{
    char *response = NULL;
    size_t response_size = 0;
    const char **old_other_emails;
    size_t old_other_emails_len;
    GPtrArray *other_emails;
    unsigned int i;
    const char **new_tags;
    size_t new_tags_len;
    const char **search_exclude_tags;
    size_t search_exclude_tags_len;

#define prompt(format, ...)					\
    do {							\
	printf (format, ##__VA_ARGS__);				\
	fflush (stdout);					\
	if (getline (&response, &response_size, stdin) < 0) {	\
	    printf ("Exiting.\n");				\
	    exit (EXIT_FAILURE);				\
	}							\
	chomp_newline (response);				\
    } while (0)

    if (notmuch_config_is_new (config))
	welcome_message_pre_setup ();

    prompt ("Your full name [%s]: ", notmuch_config_get_user_name (config));
    if (strlen (response))
	notmuch_config_set_user_name (config, response);

    prompt ("Your primary email address [%s]: ",
	    notmuch_config_get_user_primary_email (config));
    if (strlen (response))
	notmuch_config_set_user_primary_email (config, response);

    other_emails = g_ptr_array_new ();

    old_other_emails = notmuch_config_get_user_other_email (config,
					     &old_other_emails_len);
    for (i = 0; i < old_other_emails_len; i++) {
	prompt ("Additional email address [%s]: ", old_other_emails[i]);
	if (strlen (response))
	    g_ptr_array_add (other_emails, talloc_strdup (config, response));
	else
	    g_ptr_array_add (other_emails, talloc_strdup (config,
							 old_other_emails[i]));
    }

    do {
	prompt ("Additional email address [Press 'Enter' if none]: ");
	if (strlen (response))
	    g_ptr_array_add (other_emails, talloc_strdup (config, response));
    } while (strlen (response));
    if (other_emails->len)
	notmuch_config_set_user_other_email (config,
					     (const char **)
					     other_emails->pdata,
					     other_emails->len);
    g_ptr_array_free (other_emails, TRUE);

    prompt ("Top-level directory of your email archive [%s]: ",
	    notmuch_config_get_database_path (config));
    if (strlen (response)) {
	const char *absolute_path;

	absolute_path = make_path_absolute (config, response);
	notmuch_config_set_database_path (config, absolute_path);
    }

    new_tags = notmuch_config_get_new_tags (config, &new_tags_len);

    printf ("Tags to apply to all new messages (separated by spaces) [");
    print_tag_list (new_tags, new_tags_len);
    prompt ("]: ");

    if (strlen (response)) {
	GPtrArray *tags = parse_tag_list (config, response);

	notmuch_config_set_new_tags (config, (const char **) tags->pdata,
				     tags->len);

	g_ptr_array_free (tags, TRUE);
    }


    search_exclude_tags = notmuch_config_get_search_exclude_tags (config, &search_exclude_tags_len);

    printf ("Tags to exclude when searching messages (separated by spaces) [");
    print_tag_list (search_exclude_tags, search_exclude_tags_len);
    prompt ("]: ");

    if (strlen (response)) {
	GPtrArray *tags = parse_tag_list (config, response);

	notmuch_config_set_search_exclude_tags (config,
						(const char **) tags->pdata,
						tags->len);

	g_ptr_array_free (tags, TRUE);
    }

    if (notmuch_config_save (config))
	return EXIT_FAILURE;

    if (notmuch_config_is_new (config))
	welcome_message_post_setup ();

    return EXIT_SUCCESS;
}
Example #10
0
int
main (int argc, char **argv)
{

    void *ctx = talloc_new (NULL);

    char *config_path  = NULL;
    notmuch_config_t *config;
    notmuch_database_t *notmuch;

    int num_messages = 500;
    int max_tags = 10;
    // leave room for UTF-8 encoding.
    int tag_len = NOTMUCH_TAG_MAX / 6;
    // NOTMUCH_MESSAGE_ID_MAX is not exported, so we make a
    // conservative guess.
    int message_id_len = (NOTMUCH_TAG_MAX - 20) / 6;

    int seed = 734569;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_STRING, &config_path, "config-path", 'c', 0 },
	{ NOTMUCH_OPT_INT, &num_messages, "num-messages", 'n', 0 },
	{ NOTMUCH_OPT_INT, &max_tags, "max-tags", 'm', 0 },
	{ NOTMUCH_OPT_INT, &message_id_len, "message-id-len", 'M', 0 },
	{ NOTMUCH_OPT_INT, &tag_len, "tag-len", 't', 0 },
	{ NOTMUCH_OPT_INT, &seed, "seed", 's', 0 },
	{ 0, 0, 0, 0, 0 }
    };

    int opt_index = parse_arguments (argc, argv, options, 1);

    if (opt_index < 0)
	exit (1);

    if (message_id_len < 1) {
	fprintf (stderr, "message id's must be least length 1\n");
	exit (1);
    }

    if (config_path == NULL) {
	fprintf (stderr, "configuration path must be specified");
	exit (1);
    }

    config = notmuch_config_open (ctx, config_path, FALSE);
    if (config == NULL)
	return 1;

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
	return 1;

    srandom (seed);

    int count;
    for (count = 0; count < num_messages; count++) {
	int j;
	/* explicitly allow zero tags */
	int num_tags = random () % (max_tags + 1);
	/* message ids should be non-empty */
	int this_mid_len = (random () % message_id_len) + 1;
	const char **tag_list;
	char *mid;
	notmuch_status_t status;

	do {
	    mid = random_utf8_string (ctx, this_mid_len);

	    tag_list = talloc_realloc (ctx, NULL, const char *, num_tags + 1);

	    for (j = 0; j < num_tags; j++) {
		int this_tag_len = random () % tag_len + 1;

		tag_list[j] = random_utf8_string (ctx, this_tag_len);
	    }

	    tag_list[j] = NULL;

	    status = notmuch_database_add_stub_message (notmuch, mid, tag_list);
	} while (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID);

	if (status != NOTMUCH_STATUS_SUCCESS) {
	    fprintf (stderr, "error %d adding message", status);
	    exit (status);
	}
    }

    notmuch_database_destroy (notmuch);

    talloc_free (ctx);

    return 0;
}
Example #11
0
int
notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
{
    notmuch_status_t status, close_status;
    notmuch_database_t *notmuch;
    struct sigaction action;
    const char *db_path;
    const char **new_tags;
    size_t new_tags_length;
    tag_op_list_t *tag_ops;
    char *query_string = NULL;
    const char *folder = NULL;
    notmuch_bool_t create_folder = FALSE;
    notmuch_bool_t keep = FALSE;
    notmuch_bool_t no_hooks = FALSE;
    notmuch_bool_t synchronize_flags;
    const char *maildir;
    char *newpath;
    int opt_index;
    unsigned int i;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },
	{ NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
	{ NOTMUCH_OPT_BOOLEAN, &keep, "keep", 0, 0 },
	{ NOTMUCH_OPT_BOOLEAN,  &no_hooks, "no-hooks", 'n', 0 },
	{ NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
	{ NOTMUCH_OPT_END, 0, 0, 0, 0 }
    };

    opt_index = parse_arguments (argc, argv, options, 1);
    if (opt_index < 0)
	return EXIT_FAILURE;

    notmuch_process_shared_options (argv[0]);

    db_path = notmuch_config_get_database_path (config);
    new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);

    tag_ops = tag_op_list_create (config);
    if (tag_ops == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return EXIT_FAILURE;
    }
    for (i = 0; i < new_tags_length; i++) {
	const char *error_msg;

	error_msg = illegal_tag (new_tags[i], FALSE);
	if (error_msg) {
	    fprintf (stderr, "Error: tag '%s' in new.tags: %s\n",
		     new_tags[i],  error_msg);
	    return EXIT_FAILURE;
	}

	if (tag_op_list_append (tag_ops, new_tags[i], FALSE))
	    return EXIT_FAILURE;
    }

    if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
				&query_string, tag_ops))
	return EXIT_FAILURE;

    if (*query_string != '\0') {
	fprintf (stderr, "Error: unexpected query string: %s\n", query_string);
	return EXIT_FAILURE;
    }

    if (folder == NULL) {
	maildir = db_path;
    } else {
	if (! is_valid_folder_name (folder)) {
	    fprintf (stderr, "Error: invalid folder name: '%s'\n", folder);
	    return EXIT_FAILURE;
	}
	maildir = talloc_asprintf (config, "%s/%s", db_path, folder);
	if (! maildir) {
	    fprintf (stderr, "Out of memory\n");
	    return EXIT_FAILURE;
	}
	if (create_folder && ! maildir_create_folder (config, maildir))
	    return EXIT_FAILURE;
    }

    /* Set up our handler for SIGINT. We do not set SA_RESTART so that copying
     * from standard input may be interrupted. */
    memset (&action, 0, sizeof (struct sigaction));
    action.sa_handler = handle_sigint;
    sigemptyset (&action.sa_mask);
    action.sa_flags = 0;
    sigaction (SIGINT, &action, NULL);

    /* Write the message to the Maildir new directory. */
    newpath = maildir_write_new (config, STDIN_FILENO, maildir);
    if (! newpath) {
	return EXIT_FAILURE;
    }

    status = notmuch_database_open (notmuch_config_get_database_path (config),
				    NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch);
    if (status)
	return keep ? NOTMUCH_STATUS_SUCCESS : status_to_exit (status);

    notmuch_exit_if_unmatched_db_uuid (notmuch);


    /* Index the message. */
    status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep);

    /* Commit changes. */
    close_status = notmuch_database_destroy (notmuch);
    if (close_status) {
	/* Hold on to the first error, if any. */
	if (! status)
	    status = close_status;
	fprintf (stderr, "%s: failed to commit database changes: %s\n",
		 keep ? "Warning" : "Error",
		 notmuch_status_to_string (close_status));
    }

    if (status) {
	if (keep) {
	    status = NOTMUCH_STATUS_SUCCESS;
	} else {
	    /* If maildir flag sync failed, this might fail. */
	    if (unlink (newpath)) {
		fprintf (stderr, "Warning: failed to remove '%s' from maildir "
			 "after errors: %s. Please run 'notmuch new' to fix.\n",
			 newpath, strerror (errno));
	    }
	}
    }

    if (! no_hooks && status == NOTMUCH_STATUS_SUCCESS) {
	/* Ignore hook failures. */
	notmuch_run_hook (db_path, "post-insert");
    }

    return status_to_exit (status);
}
Example #12
0
int
notmuch_setup_command (unused (void *ctx),
		       unused (int argc), unused (char *argv[]))
{
    char *response = NULL;
    size_t response_size = 0;
    notmuch_config_t *config;
    const char **old_other_emails;
    size_t old_other_emails_len;
    GPtrArray *other_emails;
    unsigned int i;
    int is_new;
    const char **new_tags;
    size_t new_tags_len;

#define prompt(format, ...)					\
    do {							\
	printf (format, ##__VA_ARGS__);				\
	fflush (stdout);					\
	if (getline (&response, &response_size, stdin) < 0) {	\
	    printf ("Exiting.\n");				\
	    exit (1);						\
	}							\
	chomp_newline (response);				\
    } while (0)

    config = notmuch_config_open (ctx, NULL, &is_new);

    if (is_new)
	welcome_message_pre_setup ();

    prompt ("Your full name [%s]: ", notmuch_config_get_user_name (config));
    if (strlen (response))
	notmuch_config_set_user_name (config, response);

    prompt ("Your primary email address [%s]: ",
	    notmuch_config_get_user_primary_email (config));
    if (strlen (response))
	notmuch_config_set_user_primary_email (config, response);

    other_emails = g_ptr_array_new ();

    old_other_emails = notmuch_config_get_user_other_email (config,
					     &old_other_emails_len);
    for (i = 0; i < old_other_emails_len; i++) {
	prompt ("Additional email address [%s]: ", old_other_emails[i]);
	if (strlen (response))
	    g_ptr_array_add (other_emails, talloc_strdup (ctx, response));
	else
	    g_ptr_array_add (other_emails, talloc_strdup (ctx,
							 old_other_emails[i]));
    }

    do {
	prompt ("Additional email address [Press 'Enter' if none]: ");
	if (strlen (response))
	    g_ptr_array_add (other_emails, talloc_strdup (ctx, response));
    } while (strlen (response));
    if (other_emails->len)
	notmuch_config_set_user_other_email (config,
					     (const char **)
					     other_emails->pdata,
					     other_emails->len);
    g_ptr_array_free (other_emails, TRUE);

    prompt ("Top-level directory of your email archive [%s]: ",
	    notmuch_config_get_database_path (config));
    if (strlen (response)) {
	const char *absolute_path;

	absolute_path = make_path_absolute (ctx, response);
	notmuch_config_set_database_path (config, absolute_path);
    }

    new_tags = notmuch_config_get_new_tags (config, &new_tags_len);

    printf ("Tags to apply to all new messages (separated by spaces) [");

    for (i = 0; i < new_tags_len; i++) {
	if (i != 0)
	    printf (" ");
	printf ("%s", new_tags[i]);
    }

    prompt ("]: ");

    if (strlen (response)) {
	GPtrArray *tags = g_ptr_array_new ();
	char *tag = response;
	char *space;

	while (tag && *tag) {
	    space = strchr (tag, ' ');
	    if (space)
		g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));
	    else
		g_ptr_array_add (tags, talloc_strdup (ctx, tag));
	    tag = space;
	    while (tag && *tag == ' ')
		tag++;
	}

	notmuch_config_set_new_tags (config, (const char **) tags->pdata,
				     tags->len);

	g_ptr_array_free (tags, TRUE);
    }

    if (! notmuch_config_save (config)) {
	if (is_new)
	  welcome_message_post_setup ();
	return 0;
    } else {
	return 1;
    }
}
Example #13
0
int
notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
{
    int *add_tags, *remove_tags;
    int add_tags_count = 0;
    int remove_tags_count = 0;
    char *query_string;
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    notmuch_query_t *query;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    struct sigaction action;
    int i;

    /* Setup our handler for SIGINT */
    memset (&action, 0, sizeof (struct sigaction));
    action.sa_handler = handle_sigint;
    sigemptyset (&action.sa_mask);
    action.sa_flags = SA_RESTART;
    sigaction (SIGINT, &action, NULL);

    add_tags = talloc_size (ctx, argc * sizeof (int));
    if (add_tags == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return 1;
    }

    remove_tags = talloc_size (ctx, argc * sizeof (int));
    if (remove_tags == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return 1;
    }

    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");
	return 1;
    }

    query_string = query_string_from_args (ctx, argc - i, &argv[i]);

    if (*query_string == '\0') {
	fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
	return 1;
    }

    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_WRITE);
    if (notmuch == NULL)
	return 1;

    query = notmuch_query_create (notmuch, query_string);
    if (query == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return 1;
    }

    /* tagging is not interested in any special sort order */
    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);

    for (messages = notmuch_query_search_messages (query);
	 notmuch_messages_valid (messages) && !interrupted;
	 notmuch_messages_move_to_next (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);
    notmuch_database_close (notmuch);

    return interrupted;
}
Example #14
0
/* Open the named notmuch configuration file. If the filename is NULL,
 * the value of the environment variable $NOTMUCH_CONFIG will be used.
 * If $NOTMUCH_CONFIG is unset, the default configuration file
 * ($HOME/.notmuch-config) will be used.
 *
 * If any error occurs, (out of memory, or a permission-denied error,
 * etc.), this function will print a message to stderr and return
 * NULL.
 *
 * FILE NOT FOUND: When the specified configuration file (whether from
 * 'filename' or the $NOTMUCH_CONFIG environment variable) does not
 * exist, the behavior of this function depends on the 'is_new_ret'
 * variable.
 *
 *	If is_new_ret is NULL, then a "file not found" message will be
 *	printed to stderr and NULL will be returned.

 *	If is_new_ret is non-NULL then a default configuration will be
 *	returned and *is_new_ret will be set to 1 on return so that
 *	the caller can recognize this case.
 *
 * 	These default configuration settings are determined as
 * 	follows:
 *
 *		database_path:		$HOME/mail
 *
 *		user_name:		From /etc/passwd
 *
 *		user_primary_mail: 	$EMAIL variable if set, otherwise
 *					constructed from the username and
 *					hostname of the current machine.
 *
 *		user_other_email:	Not set.
 *
 *	The default configuration also contains comments to guide the
 *	user in editing the file directly.
 */
notmuch_config_t *
notmuch_config_open (void *ctx,
		     const char *filename,
		     notmuch_bool_t *is_new_ret)
{
    GError *error = NULL;
    int is_new = 0;
    size_t tmp;
    char *notmuch_config_env = NULL;
    int file_had_database_group;
    int file_had_new_group;
    int file_had_user_group;
    int file_had_maildir_group;
    int file_had_search_group;

    if (is_new_ret)
	*is_new_ret = 0;

    notmuch_config_t *config = talloc (ctx, notmuch_config_t);
    if (config == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return NULL;
    }
    
    talloc_set_destructor (config, notmuch_config_destructor);

    if (filename) {
	config->filename = talloc_strdup (config, filename);
    } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
	config->filename = talloc_strdup (config, notmuch_config_env);
    } else {
	config->filename = talloc_asprintf (config, "%s/.notmuch-config",
					    getenv ("HOME"));
    }

    config->key_file = g_key_file_new ();

    config->database_path = NULL;
    config->user_name = NULL;
    config->user_primary_email = NULL;
    config->user_other_email = NULL;
    config->user_other_email_length = 0;
    config->new_tags = NULL;
    config->new_tags_length = 0;
    config->new_ignore = NULL;
    config->new_ignore_length = 0;
    config->maildir_synchronize_flags = TRUE;
    config->search_exclude_tags = NULL;
    config->search_exclude_tags_length = 0;

    if (! g_key_file_load_from_file (config->key_file,
				     config->filename,
				     G_KEY_FILE_KEEP_COMMENTS,
				     &error))
    {
	/* If the caller passed a non-NULL value for is_new_ret, then
	 * the caller is prepared for a default configuration file in
	 * the case of FILE NOT FOUND. Otherwise, any read failure is
	 * an error.
	 */
	if (is_new_ret &&
	    error->domain == G_FILE_ERROR &&
	    error->code == G_FILE_ERROR_NOENT)
	{
	    g_error_free (error);
	    is_new = 1;
	}
	else
	{
	    fprintf (stderr, "Error reading configuration file %s: %s\n",
		     config->filename, error->message);
	    talloc_free (config);
	    g_error_free (error);
	    return NULL;
	}
    }

    /* Whenever we know of configuration sections that don't appear in
     * the configuration file, we add some comments to help the user
     * understand what can be done.
     *
     * It would be convenient to just add those comments now, but
     * apparently g_key_file will clear any comments when keys are
     * added later that create the groups. So we have to check for the
     * groups now, but add the comments only after setting all of our
     * values.
     */
    file_had_database_group = g_key_file_has_group (config->key_file,
						    "database");
    file_had_new_group = g_key_file_has_group (config->key_file, "new");
    file_had_user_group = g_key_file_has_group (config->key_file, "user");
    file_had_maildir_group = g_key_file_has_group (config->key_file, "maildir");
    file_had_search_group = g_key_file_has_group (config->key_file, "search");


    if (notmuch_config_get_database_path (config) == NULL) {
	char *path = talloc_asprintf (config, "%s/mail",
				      getenv ("HOME"));
	notmuch_config_set_database_path (config, path);
	talloc_free (path);
    }

    if (notmuch_config_get_user_name (config) == NULL) {
	char *name = get_name_from_passwd_file (config);
	notmuch_config_set_user_name (config, name);
	talloc_free (name);
    }

    if (notmuch_config_get_user_primary_email (config) == NULL) {
	char *email = getenv ("EMAIL");
	if (email) {
	    notmuch_config_set_user_primary_email (config, email);
	} else {
	    char hostname[256];
	    struct hostent *hostent;
	    const char *domainname;

	    char *username = get_username_from_passwd_file (config);

	    gethostname (hostname, 256);
	    hostname[255] = '\0';

	    hostent = gethostbyname (hostname);
	    if (hostent && (domainname = strchr (hostent->h_name, '.')))
		domainname += 1;
	    else
		domainname = "(none)";

	    email = talloc_asprintf (config, "%s@%s.%s",
				     username, hostname, domainname);

	    notmuch_config_set_user_primary_email (config, email);

	    talloc_free (username);
	    talloc_free (email);
	}
    }

    if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
        const char *tags[] = { "unread", "inbox" };
	notmuch_config_set_new_tags (config, tags, 2);
    }

    if (notmuch_config_get_new_ignore (config, &tmp) == NULL) {
	notmuch_config_set_new_ignore (config, NULL, 0);
    }

    if (notmuch_config_get_search_exclude_tags (config, &tmp) == NULL) {
	if (is_new) {
	    const char *tags[] = { "deleted", "spam" };
	    notmuch_config_set_search_exclude_tags (config, tags, 2);
	} else {
	    notmuch_config_set_search_exclude_tags (config, NULL, 0);
	}
    }

    error = NULL;
    config->maildir_synchronize_flags =
	g_key_file_get_boolean (config->key_file,
				"maildir", "synchronize_flags", &error);
    if (error) {
	notmuch_config_set_maildir_synchronize_flags (config, TRUE);
	g_error_free (error);
    }

    /* Whenever we know of configuration sections that don't appear in
     * the configuration file, we add some comments to help the user
     * understand what can be done. */
    if (is_new)
    {
	g_key_file_set_comment (config->key_file, NULL, NULL,
				toplevel_config_comment, NULL);
    }

    if (! file_had_database_group)
    {
	g_key_file_set_comment (config->key_file, "database", NULL,
				database_config_comment, NULL);
    }

    if (! file_had_new_group)
    {
	g_key_file_set_comment (config->key_file, "new", NULL,
				new_config_comment, NULL);
    }

    if (! file_had_user_group)
    {
	g_key_file_set_comment (config->key_file, "user", NULL,
				user_config_comment, NULL);
    }

    if (! file_had_maildir_group)
    {
	g_key_file_set_comment (config->key_file, "maildir", NULL,
				maildir_config_comment, NULL);
    }

    if (! file_had_search_group) {
	g_key_file_set_comment (config->key_file, "search", NULL,
				search_config_comment, NULL);
    }

    if (is_new_ret)
	*is_new_ret = is_new;

    return config;
}
Example #15
0
int
notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
{
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    notmuch_bool_t synchronize_flags;
    notmuch_bool_t accumulate = FALSE;
    char *input_file_name = NULL;
    FILE *input = stdin;
    char *line = NULL;
    size_t line_size;
    ssize_t line_len;
    regex_t regex;
    int rerr;
    int opt_index;

    config = notmuch_config_open (ctx, NULL, NULL);
    if (config == NULL)
	return 1;

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
	return 1;

    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_POSITION, &input_file_name, 0, 0, 0 },
	{ NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
	{ 0, 0, 0, 0, 0 }
    };

    opt_index = parse_arguments (argc, argv, options, 1);

    if (opt_index < 0) {
	/* diagnostics already printed */
	return 1;
    }

    if (input_file_name) {
	input = fopen (input_file_name, "r");
	if (input == NULL) {
	    fprintf (stderr, "Error opening %s for reading: %s\n",
		     input_file_name, strerror (errno));
	    return 1;
	}
	optind++;
    }

    if (opt_index < argc) {
	fprintf (stderr,
	 "Cannot read dump from more than one file: %s\n",
		 argv[optind]);
	return 1;
    }

    /* Dump output is one line per message. We match a sequence of
     * non-space characters for the message-id, then one or more
     * spaces, then a list of space-separated tags as a sequence of
     * characters within literal '(' and ')'. */
    if ( xregcomp (&regex,
		   "^([^ ]+) \\(([^)]*)\\)$",
		   REG_EXTENDED) )
	INTERNAL_ERROR("compile time constant regex failed.");

    while ((line_len = getline (&line, &line_size, input)) != -1) {
	regmatch_t match[3];
	char *message_id, *file_tags;

	chomp_newline (line);

	rerr = xregexec (&regex, line, 3, match, 0);
	if (rerr == REG_NOMATCH)
	{
	    fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
		     line);
	    continue;
	}

	message_id = xstrndup (line + match[1].rm_so,
			       match[1].rm_eo - match[1].rm_so);
	file_tags = xstrndup (line + match[2].rm_so,
			      match[2].rm_eo - match[2].rm_so);

	tag_message (notmuch, message_id, file_tags, !accumulate,
		     synchronize_flags);

	free (message_id);
	free (file_tags);
    }

    regfree (&regex);

    if (line)
	free (line);

    notmuch_database_destroy (notmuch);
    if (input != stdin)
	fclose (input);

    return 0;
}
Example #16
0
static int
_notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
{
    char *query_str;
    unsigned int i;
    char *status_string = NULL;

    switch (ctx->format_sel) {
    case NOTMUCH_FORMAT_TEXT:
	ctx->format = sprinter_text_create (config, stdout);
	break;
    case NOTMUCH_FORMAT_TEXT0:
	if (ctx->output == OUTPUT_SUMMARY) {
	    fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
	    return EXIT_FAILURE;
	}
	ctx->format = sprinter_text0_create (config, stdout);
	break;
    case NOTMUCH_FORMAT_JSON:
	ctx->format = sprinter_json_create (config, stdout);
	break;
    case NOTMUCH_FORMAT_SEXP:
	ctx->format = sprinter_sexp_create (config, stdout);
	break;
    default:
	/* this should never happen */
	INTERNAL_ERROR("no output format selected");
    }

    notmuch_exit_if_unsupported_format ();

    if (notmuch_database_open_verbose (
	    notmuch_config_get_database_path (config),
	    NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {

	if (status_string) {
	    fputs (status_string, stderr);
	    free (status_string);
	}

	return EXIT_FAILURE;
    }

    notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);

    query_str = query_string_from_args (ctx->notmuch, argc, argv);
    if (query_str == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return EXIT_FAILURE;
    }
    if (*query_str == '\0') {
	fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
	return EXIT_FAILURE;
    }

    ctx->query = notmuch_query_create (ctx->notmuch, query_str);
    if (ctx->query == NULL) {
	fprintf (stderr, "Out of memory\n");
	return EXIT_FAILURE;
    }

    notmuch_query_set_sort (ctx->query, ctx->sort);

    if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
	/* If we are not doing summary output there is nowhere to
	 * print the excluded flag so fall back on including the
	 * excluded messages. */
	fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
	ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
    }

    if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
	const char **search_exclude_tags;
	size_t search_exclude_tags_length;

	search_exclude_tags = notmuch_config_get_search_exclude_tags
	    (config, &search_exclude_tags_length);
	for (i = 0; i < search_exclude_tags_length; i++)
	    notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
	notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
    }

    return 0;
}
Example #17
0
int
notmuch_new_command (void *ctx, int argc, char *argv[])
{
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    add_files_state_t add_files_state;
    double elapsed;
    struct timeval tv_now, tv_start;
    int ret = 0;
    struct stat st;
    const char *db_path;
    char *dot_notmuch_path;
    struct sigaction action;
    _filename_node_t *f;
    int renamed_files, removed_files;
    notmuch_status_t status;
    int i;
    notmuch_bool_t timer_is_active = FALSE;

    add_files_state.verbose = 0;
    add_files_state.output_is_a_tty = isatty (fileno (stdout));

    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
	if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
	    add_files_state.verbose = 1;
	} else {
	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
	    return 1;
	}
    }
    config = notmuch_config_open (ctx, NULL, NULL);
    if (config == NULL)
	return 1;

    add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
    add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
    add_files_state.message_ids_to_sync = _filename_list_create (ctx);
    db_path = notmuch_config_get_database_path (config);

    dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");

    if (stat (dot_notmuch_path, &st)) {
	int count;

	count = 0;
	count_files (db_path, &count);
	if (interrupted)
	    return 1;

	printf ("Found %d total files (that's not much mail).\n", count);
	notmuch = notmuch_database_create (db_path);
	add_files_state.total_files = count;
    } else {
	notmuch = notmuch_database_open (db_path,
					 NOTMUCH_DATABASE_MODE_READ_WRITE);
	if (notmuch == NULL)
	    return 1;

	if (notmuch_database_needs_upgrade (notmuch)) {
	    printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");
	    gettimeofday (&add_files_state.tv_start, NULL);
	    notmuch_database_upgrade (notmuch, upgrade_print_progress,
				      &add_files_state);
	    printf ("Your notmuch database has now been upgraded to database format version %u.\n",
		    notmuch_database_get_version (notmuch));
	}

	add_files_state.total_files = 0;
    }

    if (notmuch == NULL)
	return 1;

    /* Setup our handler for SIGINT. We do this after having
     * potentially done a database upgrade we this interrupt handler
     * won't support. */
    memset (&action, 0, sizeof (struct sigaction));
    action.sa_handler = handle_sigint;
    sigemptyset (&action.sa_mask);
    action.sa_flags = SA_RESTART;
    sigaction (SIGINT, &action, NULL);

    talloc_free (dot_notmuch_path);
    dot_notmuch_path = NULL;

    add_files_state.processed_files = 0;
    add_files_state.added_messages = 0;
    gettimeofday (&add_files_state.tv_start, NULL);

    add_files_state.removed_files = _filename_list_create (ctx);
    add_files_state.removed_directories = _filename_list_create (ctx);

    if (! debugger_is_active () && add_files_state.output_is_a_tty
	&& ! add_files_state.verbose) {
	setup_progress_printing_timer ();
	timer_is_active = TRUE;
    }

    ret = add_files (notmuch, db_path, &add_files_state);

    removed_files = 0;
    renamed_files = 0;
    gettimeofday (&tv_start, NULL);
    for (f = add_files_state.removed_files->head; f; f = f->next) {
	status = notmuch_database_remove_message (notmuch, f->filename);
	if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
	    renamed_files++;
	else
	    removed_files++;
	if (do_print_progress) {
	    do_print_progress = 0;
	    generic_print_progress ("Cleaned up", "messages",
		tv_start, removed_files + renamed_files,
		add_files_state.removed_files->count);
	}
    }

    gettimeofday (&tv_start, NULL);
    for (f = add_files_state.removed_directories->head, i = 0; f; f = f->next, i++) {
	_remove_directory (ctx, notmuch, f->filename,
			   &renamed_files, &removed_files);
	if (do_print_progress) {
	    do_print_progress = 0;
	    generic_print_progress ("Cleaned up", "directories",
		tv_start, i,
		add_files_state.removed_directories->count);
	}
    }

    talloc_free (add_files_state.removed_files);
    talloc_free (add_files_state.removed_directories);

    /* Now that removals are done (hence the database is aware of all
     * renames), we can synchronize maildir_flags to tags for all
     * messages that had new filenames appear on this run. */
    gettimeofday (&tv_start, NULL);
    if (add_files_state.synchronize_flags) {
	_filename_node_t *node;
	notmuch_message_t *message;
	for (node = add_files_state.message_ids_to_sync->head, i = 0;
	     node;
	     node = node->next, i++)
	{
	    message = notmuch_database_find_message (notmuch, node->filename);
	    notmuch_message_maildir_flags_to_tags (message);
	    notmuch_message_destroy (message);
	    if (do_print_progress) {
		do_print_progress = 0;
		generic_print_progress (
		    "Synchronized tags for", "messages",
		    tv_start, i, add_files_state.message_ids_to_sync->count);
	    }
	}
    }

    talloc_free (add_files_state.message_ids_to_sync);
    add_files_state.message_ids_to_sync = NULL;

    if (timer_is_active)
	stop_progress_printing_timer ();

    gettimeofday (&tv_now, NULL);
    elapsed = notmuch_time_elapsed (add_files_state.tv_start,
				    tv_now);

    if (add_files_state.processed_files) {
	printf ("Processed %d %s in ", add_files_state.processed_files,
		add_files_state.processed_files == 1 ?
		"file" : "total files");
	notmuch_time_print_formatted_seconds (elapsed);
	if (elapsed > 1) {
	    printf (" (%d files/sec.).\033[K\n",
		    (int) (add_files_state.processed_files / elapsed));
	} else {
	    printf (".\033[K\n");
	}
    }

    if (add_files_state.added_messages) {
	printf ("Added %d new %s to the database.",
		add_files_state.added_messages,
		add_files_state.added_messages == 1 ?
		"message" : "messages");
    } else {
	printf ("No new mail.");
    }

    if (removed_files) {
	printf (" Removed %d %s.",
		removed_files,
		removed_files == 1 ? "message" : "messages");
    }

    if (renamed_files) {
	printf (" Detected %d file %s.",
		renamed_files,
		renamed_files == 1 ? "rename" : "renames");
    }

    printf ("\n");

    if (ret) {
	printf ("\nNote: At least one error was encountered: %s\n",
		notmuch_status_to_string (ret));
    }

    notmuch_database_close (notmuch);

    return ret || interrupted;
}
Example #18
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;
}
Example #19
0
int
notmuch_reply_command (void *ctx, int argc, char *argv[])
{
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    notmuch_query_t *query;
    char *opt, *query_string;
    int i, ret = 0;
    int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params);
    notmuch_show_params_t params;

    reply_format_func = notmuch_reply_format_default;
    params.part = -1;
    params.cryptoctx = NULL;

    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, "default") == 0) {
		reply_format_func = notmuch_reply_format_default;
	    } else if (strcmp (opt, "headers-only") == 0) {
		reply_format_func = notmuch_reply_format_headers_only;
	    } else {
		fprintf (stderr, "Invalid value for --format: %s\n", opt);
		return 1;
	    }
	} else if ((STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
	    if (params.cryptoctx == NULL) {
		GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
		if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
		    fprintf (stderr, "Failed to construct gpg context.\n");
		else
		    g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
		g_object_unref (session);
		session = NULL;
	    }
	} 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 (*query_string == '\0') {
	fprintf (stderr, "Error: notmuch reply 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 (reply_format_func (ctx, config, query, &params) != 0)
	return 1;

    notmuch_query_destroy (query);
    notmuch_database_close (notmuch);

    if (params.cryptoctx)
	g_object_unref(params.cryptoctx);

    return ret;
}
Example #20
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) {
		GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
		if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
		    fprintf (stderr, "Failed to construct gpg context.\n");
		else
		    g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
		g_object_unref (session);
		session = NULL;
	    }
	    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, &params);
    else
	return do_show (ctx, query, format, &params);

    notmuch_query_destroy (query);
    notmuch_database_close (notmuch);

    if (params.cryptoctx)
	g_object_unref(params.cryptoctx);

    return 0;
}
Example #21
0
int
notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
{
    tag_op_list_t *tag_ops = NULL;
    char *query_string = NULL;
    notmuch_database_t *notmuch;
    struct sigaction action;
    tag_op_flag_t tag_flags = TAG_FLAG_NONE;
    notmuch_bool_t batch = FALSE;
    notmuch_bool_t remove_all = FALSE;
    FILE *input = stdin;
    char *input_file_name = NULL;
    int opt_index;
    int ret = 0;

    /* Setup our handler for SIGINT */
    memset (&action, 0, sizeof (struct sigaction));
    action.sa_handler = handle_sigint;
    sigemptyset (&action.sa_mask);
    action.sa_flags = SA_RESTART;
    sigaction (SIGINT, &action, NULL);

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
	{ NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
	{ NOTMUCH_OPT_BOOLEAN, &remove_all, "remove-all", 0, 0 },
	{ 0, 0, 0, 0, 0 }
    };

    opt_index = parse_arguments (argc, argv, options, 1);
    if (opt_index < 0)
	return 1;

    if (input_file_name) {
	batch = TRUE;
	input = fopen (input_file_name, "r");
	if (input == NULL) {
	    fprintf (stderr, "Error opening %s for reading: %s\n",
		     input_file_name, strerror (errno));
	    return 1;
	}
    }

    if (batch) {
	if (opt_index != argc) {
	    fprintf (stderr, "Can't specify both cmdline and stdin!\n");
	    return 1;
	}
	if (remove_all) {
	    fprintf (stderr, "Can't specify both --remove-all and --batch\n");
	    return 1;
	}
    } else {
	tag_ops = tag_op_list_create (config);
	if (tag_ops == NULL) {
	    fprintf (stderr, "Out of memory.\n");
	    return 1;
	}

	if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
				    &query_string, tag_ops))
	    return 1;

	if (tag_op_list_size (tag_ops) == 0 && ! remove_all) {
	    fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
	    return 1;
	}
    }

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
	return 1;

    if (notmuch_config_get_maildir_synchronize_flags (config))
	tag_flags |= TAG_FLAG_MAILDIR_SYNC;

    if (remove_all)
	tag_flags |= TAG_FLAG_REMOVE_ALL;

    if (batch)
	ret = tag_file (config, notmuch, tag_flags, input);
    else
	ret = tag_query (config, notmuch, query_string, tag_ops, tag_flags);

    notmuch_database_destroy (notmuch);

    if (input != stdin)
	fclose (input);

    return ret || interrupted;
}
Example #22
0
int
notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
{
    notmuch_database_t *notmuch;
    struct sigaction action;
    const char *db_path;
    const char **new_tags;
    size_t new_tags_length;
    tag_op_list_t *tag_ops;
    char *query_string = NULL;
    const char *folder = NULL;
    notmuch_bool_t create_folder = FALSE;
    const char *maildir;
    int opt_index;
    unsigned int i;
    notmuch_bool_t ret;

    notmuch_opt_desc_t options[] = {
	{ NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },
	{ NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
	{ NOTMUCH_OPT_END, 0, 0, 0, 0 }
    };

    opt_index = parse_arguments (argc, argv, options, 1);

    if (opt_index < 0) {
	/* diagnostics already printed */
	return 1;
    }

    db_path = notmuch_config_get_database_path (config);
    new_tags = notmuch_config_get_new_tags (config, &new_tags_length);

    tag_ops = tag_op_list_create (config);
    if (tag_ops == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return 1;
    }
    for (i = 0; i < new_tags_length; i++) {
	if (tag_op_list_append (tag_ops, new_tags[i], FALSE))
	    return 1;
    }

    if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
				&query_string, tag_ops))
	return 1;

    if (*query_string != '\0') {
	fprintf (stderr, "Error: unexpected query string: %s\n", query_string);
	return 1;
    }

    if (folder == NULL) {
	maildir = db_path;
    } else {
	if (! check_folder_name (folder)) {
	    fprintf (stderr, "Error: bad folder name: %s\n", folder);
	    return 1;
	}
	maildir = talloc_asprintf (config, "%s/%s", db_path, folder);
	if (! maildir) {
	    fprintf (stderr, "Out of memory\n");
	    return 1;
	}
	if (create_folder && ! maildir_create_folder (config, maildir)) {
	    fprintf (stderr, "Error: creating maildir %s: %s\n",
		     maildir, strerror (errno));
	    return 1;
	}
    }

    /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying
     * from standard input may be interrupted. */
    memset (&action, 0, sizeof (struct sigaction));
    action.sa_handler = handle_sigint;
    sigemptyset (&action.sa_mask);
    action.sa_flags = 0;
    sigaction (SIGINT, &action, NULL);

    if (notmuch_database_open (notmuch_config_get_database_path (config),
			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
	return 1;

    ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops);

    notmuch_database_destroy (notmuch);

    return (ret) ? 0 : 1;
}