Esempio n. 1
0
static int
notmuch_reply_format_default(void *ctx,
			     notmuch_config_t *config,
			     notmuch_query_t *query,
			     notmuch_show_params_t *params,
			     notmuch_bool_t reply_all,
			     unused (sprinter_t *sp))
{
    GMimeMessage *reply;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    mime_node_t *root;
    notmuch_status_t status;

    status = notmuch_query_search_messages_st (query, &messages);
    if (print_status_query ("notmuch reply", query, status))
	return 1;

    for (;
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	message = notmuch_messages_get (messages);

	reply = create_reply_message (ctx, config, message, reply_all);

	/* If reply creation failed, we're out of memory, so don't
	 * bother trying any more messages.
	 */
	if (!reply) {
	    notmuch_message_destroy (message);
	    return 1;
	}

	show_reply_headers (reply);

	g_object_unref (G_OBJECT (reply));
	reply = NULL;

	if (mime_node_open (ctx, message, &(params->crypto), &root) == NOTMUCH_STATUS_SUCCESS) {
	    format_part_reply (root);
	    talloc_free (root);
	}

	notmuch_message_destroy (message);
    }
    return 0;
}
Esempio n. 2
0
static int remove_filename(notmuch_database_t *db, const char *path)
{
	notmuch_status_t st;
	notmuch_message_t *msg = NULL;

	dprint(2, (debugfile, "nm: removing filename '%s'\n", path));

	st = notmuch_database_begin_atomic(db);
	if (st)
		return -1;

	st = notmuch_database_find_message_by_filename(db, path, &msg);
	if (st || !msg)
		return -1;

	st = notmuch_database_remove_message(db, path);
	if (st != NOTMUCH_STATUS_SUCCESS &&
	    st != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
		dprint(1, (debugfile, "nm: failed to remove '%s' [st=%d]\n",
						path, (int) st));

	if (st == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
		notmuch_message_maildir_flags_to_tags(msg);

	notmuch_message_destroy(msg);
	notmuch_database_end_atomic(db);
	return 0;
}
Esempio n. 3
0
/* Return the number of files matching the query, or -1 for an error */
static int
count_files (notmuch_query_t *query)
{
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    notmuch_filenames_t *filenames;
    notmuch_status_t status;
    int count = 0;

    status = notmuch_query_search_messages_st (query, &messages);
    if (print_status_query ("notmuch count", query, status))
        return -1;

    for (;
            notmuch_messages_valid (messages);
            notmuch_messages_move_to_next (messages)) {
        message = notmuch_messages_get (messages);
        filenames = notmuch_message_get_filenames (message);

        for (;
                notmuch_filenames_valid (filenames);
                notmuch_filenames_move_to_next (filenames))
            count++;

        notmuch_filenames_destroy (filenames);
        notmuch_message_destroy (message);
    }

    notmuch_messages_destroy (messages);

    return count;
}
Esempio n. 4
0
/* Non-zero return indicates an error in retrieving the message,
 * or in applying the tags.  Missing messages are reported, but not
 * considered errors.
 */
static int
tag_message (unused (void *ctx),
	     notmuch_database_t *notmuch,
	     const char *message_id,
	     tag_op_list_t *tag_ops,
	     tag_op_flag_t flags)
{
    notmuch_status_t status;
    notmuch_message_t *message = NULL;
    int ret = 0;

    status = notmuch_database_find_message (notmuch, message_id, &message);
    if (status) {
	fprintf (stderr, "Error applying tags to message %s: %s\n",
		 message_id, notmuch_status_to_string (status));
	return 1;
    }
    if (message == NULL) {
	fprintf (stderr, "Warning: cannot apply tags to missing message: %s\n",
		 message_id);
	/* We consider this a non-fatal error. */
	return 0;
    }

    /* In order to detect missing messages, this check/optimization is
     * intentionally done *after* first finding the message. */
    if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))
	ret = tag_op_list_apply (message, tag_ops, flags);

    notmuch_message_destroy (message);

    return ret;
}
Esempio n. 5
0
/* This format is currently tuned for a git send-email --notmuch hook */
static int
notmuch_reply_format_headers_only(void *ctx,
				  notmuch_config_t *config,
				  notmuch_query_t *query,
				  unused (notmuch_show_params_t *params),
				  notmuch_bool_t reply_all,
				  unused (sprinter_t *sp))
{
    GMimeMessage *reply;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    const char *in_reply_to, *orig_references, *references;
    char *reply_headers;

    for (messages = notmuch_query_search_messages (query);
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	message = notmuch_messages_get (messages);

	/* The 0 means we do not want headers in a "pretty" order. */
	reply = g_mime_message_new (0);
	if (reply == NULL) {
	    fprintf (stderr, "Out of memory\n");
	    return 1;
	}

	in_reply_to = talloc_asprintf (ctx, "<%s>",
			     notmuch_message_get_message_id (message));

        g_mime_object_set_header (GMIME_OBJECT (reply),
				  "In-Reply-To", in_reply_to);


	orig_references = notmuch_message_get_header (message, "references");

	/* We print In-Reply-To followed by References because git format-patch treats them
         * specially.  Git does not interpret the other headers specially
	 */
	references = talloc_asprintf (ctx, "%s%s%s",
				      orig_references ? orig_references : "",
				      orig_references ? " " : "",
				      in_reply_to);
	g_mime_object_set_header (GMIME_OBJECT (reply),
				  "References", references);

	(void)add_recipients_from_message (reply, config, message, reply_all);

	reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
	printf ("%s", reply_headers);
	free (reply_headers);

	g_object_unref (G_OBJECT (reply));
	reply = NULL;

	notmuch_message_destroy (message);
    }
    return 0;
}
Esempio n. 6
0
void Message::dispose()
{
    dispose_children(this);
    if(libnotmuch_message) {
        notmuch_message_destroy(libnotmuch_message);
        libnotmuch_message = 0;
    }
}
Esempio n. 7
0
static notmuch_status_t
show_messages (void *ctx,
	       const notmuch_show_format_t *format,
	       sprinter_t *sp,
	       notmuch_messages_t *messages,
	       int indent,
	       notmuch_show_params_t *params)
{
    notmuch_message_t *message;
    notmuch_bool_t match;
    notmuch_bool_t excluded;
    int next_indent;
    notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;

    sp->begin_list (sp);

    for (;
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	sp->begin_list (sp);

	message = notmuch_messages_get (messages);

	match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
	excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);

	next_indent = indent;

	if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
	    status = show_message (ctx, format, sp, message, indent, params);
	    if (status && !res)
		res = status;
	    next_indent = indent + 1;
	} else {
	    sp->null (sp);
	}

	status = show_messages (ctx,
				format, sp,
				notmuch_message_get_replies (message),
				next_indent,
				params);
	if (status && !res)
	    res = status;

	notmuch_message_destroy (message);

	sp->end (sp);
    }

    sp->end (sp);

    return res;
}
Esempio n. 8
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;
    unsigned count;
    notmuch_status_t status;

    status = notmuch_query_count_messages_st (query, &count);
    if (print_status_query ("notmuch reply", query, status))
	return 1;

    if (count != 1) {
	fprintf (stderr, "Error: search term did not match precisely one message.\n");
	return 1;
    }

    status = notmuch_query_search_messages_st (query, &messages);
    if (print_status_query ("notmuch reply", query, status))
	return 1;

    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;
}
Esempio n. 9
0
static int remove_filename(struct nm_ctxdata *data, const char *path)
{
    notmuch_status_t st;
    notmuch_filenames_t *ls;
    notmuch_message_t *msg = NULL;
    notmuch_database_t *db = get_db(data, TRUE);
    int trans;

    dprint(2, (debugfile, "nm: remove filename '%s'\n", path));

    if (!db)
        return -1;
    st = notmuch_database_find_message_by_filename(db, path, &msg);
    if (st || !msg)
        return -1;
    trans = db_trans_begin(data);
    if (trans < 0)
        return -1;

    /*
     * note that unlink() is probably unnecessary here, it's already removed
     * by mh_sync_mailbox_message(), but for sure...
     */
    st = notmuch_database_remove_message(db, path);
    switch (st) {
    case NOTMUCH_STATUS_SUCCESS:
        dprint(2, (debugfile, "nm: remove success, call unlink\n"));
        unlink(path);
        break;
    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
        dprint(2, (debugfile, "nm: remove succes (duplicate), call unlink\n"));
        unlink(path);
        for (ls = notmuch_message_get_filenames(msg);
                ls && notmuch_filenames_valid(ls);
                notmuch_filenames_move_to_next(ls)) {

            path = notmuch_filenames_get(ls);

            dprint(2, (debugfile, "nm: remove duplicate: '%s'\n", path));
            unlink(path);
            notmuch_database_remove_message(db, path);
        }
        break;
    default:
        dprint(1, (debugfile, "nm: failed to remove '%s' [st=%d]\n", path, (int) st));
        break;
    }

    notmuch_message_destroy(msg);
    if (trans)
        db_trans_end(data);
    return 0;
}
Esempio n. 10
0
/*
 * add each top level reply in the thread, and then add each
 * reply to the top level replies
 */
static void append_thread(CONTEXT *ctx, notmuch_thread_t *thread)
{
	notmuch_messages_t *msgs;

	for (msgs = notmuch_thread_get_toplevel_messages(thread);
	     notmuch_messages_valid(msgs);
	     notmuch_messages_move_to_next(msgs)) {

		notmuch_message_t *m = notmuch_messages_get(msgs);
		append_message(ctx, m);
		append_replies(ctx, m);
		notmuch_message_destroy(m);
	}
}
Esempio n. 11
0
static void
show_messages (void *ctx,
	       const notmuch_show_format_t *format,
	       notmuch_messages_t *messages,
	       int indent,
	       notmuch_show_params_t *params)
{
    notmuch_message_t *message;
    notmuch_bool_t match;
    int first_set = 1;
    int next_indent;

    fputs (format->message_set_start, stdout);

    for (;
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	if (!first_set)
	    fputs (format->message_set_sep, stdout);
	first_set = 0;

	fputs (format->message_set_start, stdout);

	message = notmuch_messages_get (messages);

	match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);

	next_indent = indent;

	if (match || params->entire_thread) {
	    show_message (ctx, format, message, indent, params);
	    next_indent = indent + 1;

	    fputs (format->message_set_sep, stdout);
	}

	show_messages (ctx,
		       format,
		       notmuch_message_get_replies (message),
		       next_indent,
		       params);

	notmuch_message_destroy (message);

	fputs (format->message_set_end, stdout);
    }

    fputs (format->message_set_end, stdout);
}
Esempio n. 12
0
/*
 * add all the replies to a given messages into the display.
 * Careful, this calls itself recursively to make sure we get
 * everything.
 */
static void append_replies(CONTEXT *ctx, notmuch_message_t *top)
{
	notmuch_messages_t *msgs;

	for (msgs = notmuch_message_get_replies(top);
	     notmuch_messages_valid(msgs);
	     notmuch_messages_move_to_next(msgs)) {

		notmuch_message_t *m = notmuch_messages_get(msgs);
		append_message(ctx, m);
		/* recurse through all the replies to this message too */
		append_replies(ctx, m);
		notmuch_message_destroy(m);
	}
}
Esempio n. 13
0
/* Tag messages matching 'query_string' according to 'tag_ops'
 */
static int
tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
	   tag_op_list_t *tag_ops, tag_op_flag_t flags)
{
    notmuch_query_t *query;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    int ret = NOTMUCH_STATUS_SUCCESS;

    if (! (flags & TAG_FLAG_REMOVE_ALL)) {
	/* Optimize the query so it excludes messages that already
	 * have the specified set of tags. */
	query_string = _optimize_tag_query (ctx, query_string, tag_ops);
	if (query_string == NULL) {
	    fprintf (stderr, "Out of memory.\n");
	    return 1;
	}
	flags |= TAG_FLAG_PRE_OPTIMIZED;
    }

    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);
	ret = tag_op_list_apply (message, tag_ops, flags);
	notmuch_message_destroy (message);
	if (ret != NOTMUCH_STATUS_SUCCESS)
	    break;
    }

    notmuch_query_destroy (query);

    return ret || interrupted;
}
Esempio n. 14
0
static int
do_search_messages (const search_format_t *format,
		    notmuch_query_t *query,
		    output_t output)
{
    notmuch_message_t *message;
    notmuch_messages_t *messages;
    int first_message = 1;

    messages = notmuch_query_search_messages (query);
    if (messages == NULL)
	return 1;

    fputs (format->results_start, stdout);

    for (;
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	message = notmuch_messages_get (messages);

	if (! first_message)
	    fputs (format->item_sep, stdout);

	if (output == OUTPUT_FILES) {
	    format->item_id (message, "",
			     notmuch_message_get_filename (message));
	} else { /* output == OUTPUT_MESSAGES */
	    format->item_id (message, "id:",
			     notmuch_message_get_message_id (message));
	}

	first_message = 0;

	notmuch_message_destroy (message);
    }

    notmuch_messages_destroy (messages);

    fputs (format->results_end, stdout);

    return 0;
}
Esempio n. 15
0
static void read_mesgs_query(CONTEXT *ctx, notmuch_query_t *q, int dedup)
{
    struct nm_ctxdata *data = get_ctxdata(ctx);
    int limit;
    notmuch_messages_t *msgs;

    if (!data)
        return;

    limit = get_limit(data);

    for (msgs = notmuch_query_search_messages(q);
            notmuch_messages_valid(msgs) &&
            (limit == 0 || ctx->msgcount < limit);
            notmuch_messages_move_to_next(msgs)) {

        notmuch_message_t *m = notmuch_messages_get(msgs);
        append_message(ctx, q, m, dedup);
        notmuch_message_destroy(m);
    }
}
Esempio n. 16
0
/* Add the specified message file to the notmuch database, applying tags.
 * The file is renamed to encode notmuch tags as maildir flags. */
static void
add_file_to_database (notmuch_database_t *notmuch, const char *path,
		      tag_op_list_t *tag_ops)
{
    notmuch_message_t *message;
    notmuch_status_t status;

    status = notmuch_database_add_message (notmuch, path, &message);
    switch (status) {
    case NOTMUCH_STATUS_SUCCESS:
    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
	break;
    default:
    case NOTMUCH_STATUS_FILE_NOT_EMAIL:
    case NOTMUCH_STATUS_READ_ONLY_DATABASE:
    case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
    case NOTMUCH_STATUS_OUT_OF_MEMORY:
    case NOTMUCH_STATUS_FILE_ERROR:
    case NOTMUCH_STATUS_NULL_POINTER:
    case NOTMUCH_STATUS_TAG_TOO_LONG:
    case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
    case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
    case NOTMUCH_STATUS_LAST_STATUS:
	fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",
		 path, notmuch_status_to_string (status));
	return;
    }

    if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
	/* Don't change tags of an existing message. */
	status = notmuch_message_tags_to_maildir_flags (message);
	if (status != NOTMUCH_STATUS_SUCCESS)
	    fprintf (stderr, "Error: failed to sync tags to maildir flags\n");
    } else {
	tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC);
    }

    notmuch_message_destroy (message);
}
Esempio n. 17
0
notmuch_tags_t *
notmuch_messages_collect_tags (notmuch_messages_t *messages)
{
    notmuch_string_list_t *tags;
    notmuch_tags_t *msg_tags;
    notmuch_message_t *msg;
    GHashTable *htable;
    GList *keys, *l;
    const char *tag;

    tags = _notmuch_string_list_create (messages);
    if (tags == NULL) return NULL;

    htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);

    while ((msg = notmuch_messages_get (messages))) {
	msg_tags = notmuch_message_get_tags (msg);
	while ((tag = notmuch_tags_get (msg_tags))) {
	    g_hash_table_insert (htable, xstrdup (tag), NULL);
	    notmuch_tags_move_to_next (msg_tags);
	}
	notmuch_tags_destroy (msg_tags);
	notmuch_message_destroy (msg);
	notmuch_messages_move_to_next (messages);
    }

    keys = g_hash_table_get_keys (htable);
    for (l = keys; l; l = l->next) {
	_notmuch_string_list_append (tags, (char *)l->data);
    }

    g_list_free (keys);
    g_hash_table_destroy (htable);

    _notmuch_string_list_sort (tags);
    return _notmuch_tags_create (messages, tags);
}
Esempio n. 18
0
static int add_filename(notmuch_database_t *db, const char *path, HEADER *h)
{
	int rc = -1;
	notmuch_status_t st;
	notmuch_message_t *msg;

	dprint(2, (debugfile, "nm: adding filename '%s'\n", path));

	st = notmuch_database_begin_atomic(db);
	if (st)
		return -1;

	st = notmuch_database_add_message(db, path, &msg);
	switch (st) {
	case NOTMUCH_STATUS_SUCCESS:
		if (h)
			update_tags(msg, nm_header_get_tags(h));
		break;
	case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
		notmuch_message_maildir_flags_to_tags(msg);
		break;
	default:
		dprint(1, (debugfile, "nm: failed to add '%s' [st=%d]\n",
					path, (int) st));
		goto done;
	}

	st = notmuch_database_end_atomic(db);
	if (st)
	    goto done;

	rc = 0;
done:
	if (msg)
	    notmuch_message_destroy(msg);
	return rc;
}
Esempio n. 19
0
static int
database_dump_file (notmuch_database_t *notmuch, gzFile output,
		    const char *query_str, int output_format)
{
    notmuch_query_t *query;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    notmuch_tags_t *tags;

    if (! query_str)
	query_str = "";

    query = notmuch_query_create (notmuch, query_str);
    if (query == NULL) {
	fprintf (stderr, "Out of memory\n");
	return EXIT_FAILURE;
    }
    /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
     * first results quickly at the expense of total time.
     */
    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);

    char *buffer = NULL;
    size_t buffer_size = 0;

    for (messages = notmuch_query_search_messages (query);
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages)) {
	int first = 1;
	const char *message_id;

	message = notmuch_messages_get (messages);
	message_id = notmuch_message_get_message_id (message);

	if (output_format == DUMP_FORMAT_BATCH_TAG &&
	    strchr (message_id, '\n')) {
	    /* This will produce a line break in the output, which
	     * would be difficult to handle in tools.  However, it's
	     * also impossible to produce an email containing a line
	     * break in a message ID because of unfolding, so we can
	     * safely disallow it. */
	    fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
	    notmuch_message_destroy (message);
	    continue;
	}

	if (output_format == DUMP_FORMAT_SUP) {
	    gzprintf (output, "%s (", message_id);
	}

	for (tags = notmuch_message_get_tags (message);
	     notmuch_tags_valid (tags);
	     notmuch_tags_move_to_next (tags)) {
	    const char *tag_str = notmuch_tags_get (tags);

	    if (! first)
		gzputs (output, " ");

	    first = 0;

	    if (output_format == DUMP_FORMAT_SUP) {
		gzputs (output, tag_str);
	    } else {
		if (hex_encode (notmuch, tag_str,
				&buffer, &buffer_size) != HEX_SUCCESS) {
		    fprintf (stderr, "Error: failed to hex-encode tag %s\n",
			     tag_str);
		    return EXIT_FAILURE;
		}
		gzprintf (output, "+%s", buffer);
	    }
	}

	if (output_format == DUMP_FORMAT_SUP) {
	    gzputs (output, ")\n");
	} else {
	    if (make_boolean_term (notmuch, "id", message_id,
				   &buffer, &buffer_size)) {
		    fprintf (stderr, "Error quoting message id %s: %s\n",
			     message_id, strerror (errno));
		    return EXIT_FAILURE;
	    }
	    gzprintf (output, " -- %s\n", buffer);
	}

	notmuch_message_destroy (message);
    }

    notmuch_query_destroy (query);

    return EXIT_SUCCESS;
}
Esempio n. 20
0
/*
 * Add the specified message file to the notmuch database, applying
 * tags in tag_ops. If synchronize_flags is TRUE, the tags are
 * synchronized to maildir flags (which may result in message file
 * rename).
 *
 * Return NOTMUCH_STATUS_SUCCESS on success, errors otherwise. If keep
 * is TRUE, errors in tag changes and flag syncing are ignored and
 * success status is returned; otherwise such errors cause the message
 * to be removed from the database. Failure to add the message to the
 * database results in error status regardless of keep.
 */
static notmuch_status_t
add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,
	  notmuch_bool_t synchronize_flags, notmuch_bool_t keep)
{
    notmuch_message_t *message;
    notmuch_status_t status;

    status = notmuch_database_add_message (notmuch, path, &message);
    if (status == NOTMUCH_STATUS_SUCCESS) {
	status = tag_op_list_apply (message, tag_ops, 0);
	if (status) {
	    fprintf (stderr, "%s: failed to apply tags to file '%s': %s\n",
		     keep ? "Warning" : "Error",
		     path, notmuch_status_to_string (status));
	    goto DONE;
	}
    } else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
	status = NOTMUCH_STATUS_SUCCESS;
    } else if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) {
	fprintf (stderr, "Error: delivery of non-mail file: '%s'\n", path);
	goto FAIL;
    } else {
	fprintf (stderr, "Error: failed to add '%s' to notmuch database: %s\n",
		 path, notmuch_status_to_string (status));
	goto FAIL;
    }

    if (synchronize_flags) {
	status = notmuch_message_tags_to_maildir_flags (message);
	if (status != NOTMUCH_STATUS_SUCCESS)
	    fprintf (stderr, "%s: failed to sync tags to maildir flags for '%s': %s\n",
		     keep ? "Warning" : "Error",
		     path, notmuch_status_to_string (status));

	/*
	 * Note: Unfortunately a failed maildir flag sync might
	 * already have renamed the file, in which case the cleanup
	 * path may fail.
	 */
    }

  DONE:
    notmuch_message_destroy (message);

    if (status) {
	if (keep) {
	    status = NOTMUCH_STATUS_SUCCESS;
	} else {
	    notmuch_status_t cleanup_status;

	    cleanup_status = notmuch_database_remove_message (notmuch, path);
	    if (cleanup_status != NOTMUCH_STATUS_SUCCESS &&
		cleanup_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
		fprintf (stderr, "Warning: failed to remove '%s' from database "
			 "after errors: %s. Please run 'notmuch new' to fix.\n",
			 path, notmuch_status_to_string (cleanup_status));
	    }
	}
    }

  FAIL:
    return status;
}
Esempio n. 21
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;
}
Esempio n. 22
0
static int
tag_message (notmuch_database_t *notmuch, const char *message_id,
	     char *file_tags, notmuch_bool_t remove_all,
	     notmuch_bool_t synchronize_flags)
{
    notmuch_status_t status;
    notmuch_tags_t *db_tags;
    char *db_tags_str;
    notmuch_message_t *message = NULL;
    const char *tag;
    char *next;
    int ret = 0;

    status = notmuch_database_find_message (notmuch, message_id, &message);
    if (status || message == NULL) {
	fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
		 message ? "" : "missing ", message_id);
	if (status)
	    fprintf (stderr, "%s\n", notmuch_status_to_string(status));
	return 1;
    }

    /* In order to detect missing messages, this check/optimization is
     * intentionally done *after* first finding the message. */
    if (!remove_all && (file_tags == NULL || *file_tags == '\0'))
	goto DONE;

    db_tags_str = NULL;
    for (db_tags = notmuch_message_get_tags (message);
	 notmuch_tags_valid (db_tags);
	 notmuch_tags_move_to_next (db_tags)) {
	tag = notmuch_tags_get (db_tags);

	if (db_tags_str)
	    db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
	else
	    db_tags_str = talloc_strdup (message, tag);
    }

    if (((file_tags == NULL || *file_tags == '\0') &&
	 (db_tags_str == NULL || *db_tags_str == '\0')) ||
	(file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
	goto DONE;

    notmuch_message_freeze (message);

    if (remove_all)
	notmuch_message_remove_all_tags (message);

    next = file_tags;
    while (next) {
	tag = strsep (&next, " ");
	if (*tag == '\0')
	    continue;
	status = notmuch_message_add_tag (message, tag);
	if (status) {
	    fprintf (stderr, "Error applying tag %s to message %s:\n",
		     tag, message_id);
	    fprintf (stderr, "%s\n", notmuch_status_to_string (status));
	    ret = 1;
	}
    }

    notmuch_message_thaw (message);

    if (synchronize_flags)
	notmuch_message_tags_to_maildir_flags (message);

DONE:
    if (message)
	notmuch_message_destroy (message);

    return ret;
}
Esempio n. 23
0
int
cmd_move_message(ClientData data, Tcl_Interp *interp, int argc, const char *argv[]) {
    filter_context_t *ctx = FILTER_CONTEXT(data);
    notmuch_message_t *msg = ctx->current_message;
    notmuch_status_t nmrc;

    if (argc != 2) {
        tcl_result_printf(interp, "wrong # of args: got %d, expected move folder", argc);
        return TCL_ERROR;
    }
    
    const char *folder = argv[1];
    if (folder[0] == '/') {
        tcl_result_printf(interp, "invalid folder '%s'", folder);
        return TCL_ERROR;
    }

    log_debug("moving message %s to folder %s",
              notmuch_message_get_message_id(msg), folder);
    char *folder_path = g_strdup_printf("%s/%s",
                                        notmuch_database_get_path(ctx->database),
                                        folder);
    int status = TCL_ERROR;

    notmuch_filenames_t *fns = notmuch_message_get_filenames(msg);
    int nmoved = 0;
    while (notmuch_filenames_valid(fns)) {
        const char *fn = notmuch_filenames_get(fns);
        char *new_fn = NULL;
        if (g_str_has_prefix(fn, folder_path)) {
            log_debug("file %s already in folder %s", fn, folder);
        } else if (ctx->dry_run) {
            log_debug("moving %s to folder %s", fn, folder);
        } else {
            log_debug("moving %s to folder %s", fn, folder);
            int rc = maildir_deliver_link(fn, folder_path, &new_fn);
            if (rc) {
                tcl_result_printf(interp, "delivery error: %s", strerror(errno));
                goto done;
            }
            log_debug("delivered as %s", new_fn);
            nmrc = notmuch_database_add_message(ctx->database, new_fn, NULL);
            switch (nmrc) {
                case NOTMUCH_STATUS_SUCCESS:
                    log_warning("%s: message was not in database", new_fn);
                case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
                    log_debug("added new file to database");
                    break;
                default:
                    tcl_result_printf(interp, "error adding %s to database: %s",
                                      new_fn,
                                      notmuch_status_to_string(nmrc));
                    goto done;
            }
            nmrc = notmuch_database_remove_message(ctx->database, fn);
            switch (nmrc) {
                case NOTMUCH_STATUS_SUCCESS:
                    log_warning("%s: file was only file for message", fn);
                case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
                    log_debug("removed file %s from database", fn);
                    break;
                default:
                    tcl_result_printf(interp, "error removing %s from database: %s",
                                      fn, notmuch_status_to_string(nmrc));
                    goto done;
            }
            if (unlink(fn)) {
                tcl_result_printf(interp, "error unlinking %s: %s", fn, strerror(errno));
                goto done;
            }
            nmoved += 1;
        }
        notmuch_filenames_move_to_next(fns);
    }
    notmuch_filenames_destroy(fns);
    fns = NULL;

    if (nmoved == 0) {
        Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
        status = TCL_OK;
        goto done;
    }

    log_debug("syncing maildir flags");
    
    const char *mid = notmuch_message_get_message_id(msg);
    nmrc = notmuch_database_find_message(ctx->database, mid, &msg);
    if (nmrc != NOTMUCH_STATUS_SUCCESS) {
        tcl_result_printf(interp, "cannot re-find message %s", mid);
        goto done;
    }
    notmuch_message_destroy(ctx->current_message);
    ctx->current_message = msg;
    nmrc = notmuch_message_tags_to_maildir_flags(msg);
    if (nmrc != NOTMUCH_STATUS_SUCCESS) {
        tcl_result_printf(interp, "error syncing tags back to flags");
        goto done;
    }

    Tcl_SetObjResult(interp, Tcl_NewIntObj(nmoved));
    status = TCL_OK;

done:
    if (fns) {
        notmuch_filenames_destroy(fns);
    }
    g_free(folder_path);
    return status;
}
Esempio n. 24
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;
}
Esempio n. 25
0
/* Examine 'path' recursively as follows:
 *
 *   o Ask the filesystem for the mtime of 'path' (fs_mtime)
 *   o Ask the database for its timestamp of 'path' (db_mtime)
 *
 *   o Ask the filesystem for files and directories within 'path'
 *     (via scandir and stored in fs_entries)
 *
 *   o Pass 1: For each directory in fs_entries, recursively call into
 *     this same function.
 *
 *   o Compare fs_mtime to db_mtime. If they are equivalent, terminate
 *     the algorithm at this point, (this directory has not been
 *     updated in the filesystem since the last database scan of PASS
 *     2).
 *
 *   o Ask the database for files and directories within 'path'
 *     (db_files and db_subdirs)
 *
 *   o Pass 2: Walk fs_entries simultaneously with db_files and
 *     db_subdirs. Look for one of three interesting cases:
 *
 *	   1. Regular file in fs_entries and not in db_files
 *            This is a new file to add_message into the database.
 *
 *         2. Filename in db_files not in fs_entries.
 *            This is a file that has been removed from the mail store.
 *
 *         3. Directory in db_subdirs not in fs_entries
 *            This is a directory that has been removed from the mail store.
 *
 *     Note that the addition of a directory is not interesting here,
 *     since that will have been taken care of in pass 1. Also, we
 *     don't immediately act on file/directory removal since we must
 *     ensure that in the case of a rename that the new filename is
 *     added before the old filename is removed, (so that no
 *     information is lost from the database).
 *
 *   o Tell the database to update its time of 'path' to 'fs_mtime'
 *     if fs_mtime isn't the current wall-clock time.
 */
static notmuch_status_t
add_files_recursive (notmuch_database_t *notmuch,
		     const char *path,
		     add_files_state_t *state)
{
    DIR *dir = NULL;
    struct dirent *entry = NULL;
    char *next = NULL;
    time_t fs_mtime, db_mtime;
    notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;
    notmuch_message_t *message = NULL;
    struct dirent **fs_entries = NULL;
    int i, num_fs_entries;
    notmuch_directory_t *directory;
    notmuch_filenames_t *db_files = NULL;
    notmuch_filenames_t *db_subdirs = NULL;
    time_t stat_time;
    struct stat st;
    notmuch_bool_t is_maildir, new_directory;
    const char **tag;

    if (stat (path, &st)) {
	fprintf (stderr, "Error reading directory %s: %s\n",
		 path, strerror (errno));
	return NOTMUCH_STATUS_FILE_ERROR;
    }
    stat_time = time (NULL);

    /* This is not an error since we may have recursed based on a
     * symlink to a regular file, not a directory, and we don't know
     * that until this stat. */
    if (! S_ISDIR (st.st_mode))
	return NOTMUCH_STATUS_SUCCESS;

    fs_mtime = st.st_mtime;

    directory = notmuch_database_get_directory (notmuch, path);
    db_mtime = notmuch_directory_get_mtime (directory);

    new_directory = db_mtime ? FALSE : TRUE;

    /* XXX This is a temporary workaround.  If we don't update the
     * database mtime until after processing messages in this
     * directory, then a 0 mtime is *not* sufficient to indicate that
     * this directory has no messages or subdirs in the database (for
     * example, if an earlier run skipped the mtime update because
     * fs_mtime == stat_time, or was interrupted before updating the
     * mtime at the end).  To address this, we record a (bogus)
     * non-zero value before processing any child messages so that a
     * later run won't mistake this for a new directory (and, for
     * example, fail to detect removed files and subdirs).
     *
     * A better solution would be for notmuch_database_get_directory
     * to indicate if it really created a new directory or not, either
     * by a new out-argument, or by recording this information and
     * providing an accessor.
     */
    if (new_directory)
	notmuch_directory_set_mtime (directory, -1);

    /* If the database knows about this directory, then we sort based
     * on strcmp to match the database sorting. Otherwise, we can do
     * inode-based sorting for faster filesystem operation. */
    num_fs_entries = scandir (path, &fs_entries, 0,
			      new_directory ?
			      dirent_sort_inode : dirent_sort_strcmp_name);

    if (num_fs_entries == -1) {
	fprintf (stderr, "Error opening directory %s: %s\n",
		 path, strerror (errno));
	ret = NOTMUCH_STATUS_FILE_ERROR;
	goto DONE;
    }

    /* Pass 1: Recurse into all sub-directories. */
    is_maildir = _entries_resemble_maildir (fs_entries, num_fs_entries);

    for (i = 0; i < num_fs_entries; i++) {
	if (interrupted)
	    break;

	entry = fs_entries[i];

	/* We only want to descend into directories.
	 * But symlinks can be to directories too, of course.
	 *
	 * And if the filesystem doesn't tell us the file type in the
	 * scandir results, then it might be a directory (and if not,
	 * then we'll stat and return immediately in the next level of
	 * recursion). */
	if (entry->d_type != DT_DIR &&
	    entry->d_type != DT_LNK &&
	    entry->d_type != DT_UNKNOWN)
	{
	    continue;
	}

	/* Ignore special directories to avoid infinite recursion.
	 * Also ignore the .notmuch directory and any "tmp" directory
	 * that appears within a maildir.
	 */
	/* XXX: Eventually we'll want more sophistication to let the
	 * user specify files to be ignored. */
	if (strcmp (entry->d_name, ".") == 0 ||
	    strcmp (entry->d_name, "..") == 0 ||
	    (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
	    strcmp (entry->d_name, ".notmuch") ==0)
	{
	    continue;
	}

	next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
	status = add_files_recursive (notmuch, next, state);
	if (status && ret == NOTMUCH_STATUS_SUCCESS)
	    ret = status;
	talloc_free (next);
	next = NULL;
    }

    /* If the directory's modification time in the filesystem is the
     * same as what we recorded in the database the last time we
     * scanned it, then we can skip the second pass entirely.
     *
     * We test for strict equality here to avoid a bug that can happen
     * if the system clock jumps backward, (preventing new mail from
     * being discovered until the clock catches up and the directory
     * is modified again).
     */
    if (fs_mtime == db_mtime)
	goto DONE;

    /* new_directory means a directory that the database has never
     * seen before. In that case, we can simply leave db_files and
     * db_subdirs NULL. */
    if (!new_directory) {
	db_files = notmuch_directory_get_child_files (directory);
	db_subdirs = notmuch_directory_get_child_directories (directory);
    }

    /* Pass 2: Scan for new files, removed files, and removed directories. */
    for (i = 0; i < num_fs_entries; i++)
    {
	if (interrupted)
	    break;

        entry = fs_entries[i];

	/* Check if we've walked past any names in db_files or
	 * db_subdirs. If so, these have been deleted. */
	while (notmuch_filenames_valid (db_files) &&
	       strcmp (notmuch_filenames_get (db_files), entry->d_name) < 0)
	{
	    char *absolute = talloc_asprintf (state->removed_files,
					      "%s/%s", path,
					      notmuch_filenames_get (db_files));

	    _filename_list_add (state->removed_files, absolute);

	    notmuch_filenames_move_to_next (db_files);
	}

	while (notmuch_filenames_valid (db_subdirs) &&
	       strcmp (notmuch_filenames_get (db_subdirs), entry->d_name) <= 0)
	{
	    const char *filename = notmuch_filenames_get (db_subdirs);

	    if (strcmp (filename, entry->d_name) < 0)
	    {
		char *absolute = talloc_asprintf (state->removed_directories,
						  "%s/%s", path, filename);

		_filename_list_add (state->removed_directories, absolute);
	    }

	    notmuch_filenames_move_to_next (db_subdirs);
	}

	/* If we're looking at a symlink, we only want to add it if it
	 * links to a regular file, (and not to a directory, say).
	 *
	 * Similarly, if the file is of unknown type (due to filesystem
	 * limitations), then we also need to look closer.
	 *
	 * In either case, a stat does the trick.
	 */
	if (entry->d_type == DT_LNK || entry->d_type == DT_UNKNOWN) {
	    int err;

	    next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
	    err = stat (next, &st);
	    talloc_free (next);
	    next = NULL;

	    /* Don't emit an error for a link pointing nowhere, since
	     * the directory-traversal pass will have already done
	     * that. */
	    if (err)
		continue;

	    if (! S_ISREG (st.st_mode))
		continue;
	} else if (entry->d_type != DT_REG) {
	    continue;
	}

	/* Don't add a file that we've added before. */
	if (notmuch_filenames_valid (db_files) &&
	    strcmp (notmuch_filenames_get (db_files), entry->d_name) == 0)
	{
	    notmuch_filenames_move_to_next (db_files);
	    continue;
	}

	/* We're now looking at a regular file that doesn't yet exist
	 * in the database, so add it. */
	next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);

	state->processed_files++;

	if (state->verbose) {
	    if (state->output_is_a_tty)
		printf("\r\033[K");

	    printf ("%i/%i: %s",
		    state->processed_files,
		    state->total_files,
		    next);

	    putchar((state->output_is_a_tty) ? '\r' : '\n');
	    fflush (stdout);
	}

	status = notmuch_database_add_message (notmuch, next, &message);
	switch (status) {
	/* success */
	case NOTMUCH_STATUS_SUCCESS:
	    state->added_messages++;
	    notmuch_message_freeze (message);
	    for (tag=state->new_tags; *tag != NULL; tag++)
	        notmuch_message_add_tag (message, *tag);
	    if (state->synchronize_flags == TRUE)
		notmuch_message_maildir_flags_to_tags (message);
	    notmuch_message_thaw (message);
	    break;
	/* Non-fatal issues (go on to next file) */
	case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
	    /* Defer sync of maildir flags until after old filenames
	     * are removed in the case of a rename. */
	    if (state->synchronize_flags == TRUE)
		_filename_list_add (state->message_ids_to_sync,
				    notmuch_message_get_message_id (message));
	    break;
	case NOTMUCH_STATUS_FILE_NOT_EMAIL:
	    fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
		     next);
	    break;
	/* Fatal issues. Don't process anymore. */
	case NOTMUCH_STATUS_READ_ONLY_DATABASE:
	case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
	case NOTMUCH_STATUS_OUT_OF_MEMORY:
	    fprintf (stderr, "Error: %s. Halting processing.\n",
		     notmuch_status_to_string (status));
	    ret = status;
	    goto DONE;
	default:
	case NOTMUCH_STATUS_FILE_ERROR:
	case NOTMUCH_STATUS_NULL_POINTER:
	case NOTMUCH_STATUS_TAG_TOO_LONG:
	case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
	case NOTMUCH_STATUS_LAST_STATUS:
	    INTERNAL_ERROR ("add_message returned unexpected value: %d",  status);
	    goto DONE;
	}

	if (message) {
	    notmuch_message_destroy (message);
	    message = NULL;
	}

	if (do_print_progress) {
	    do_print_progress = 0;
	    generic_print_progress ("Processed", "files", state->tv_start,
				    state->processed_files, state->total_files);
	}

	talloc_free (next);
	next = NULL;
    }

    if (interrupted)
	goto DONE;

    /* Now that we've walked the whole filesystem list, anything left
     * over in the database lists has been deleted. */
    while (notmuch_filenames_valid (db_files))
    {
	char *absolute = talloc_asprintf (state->removed_files,
					  "%s/%s", path,
					  notmuch_filenames_get (db_files));

	_filename_list_add (state->removed_files, absolute);

	notmuch_filenames_move_to_next (db_files);
    }

    while (notmuch_filenames_valid (db_subdirs))
    {
	char *absolute = talloc_asprintf (state->removed_directories,
					  "%s/%s", path,
					  notmuch_filenames_get (db_subdirs));

	_filename_list_add (state->removed_directories, absolute);

	notmuch_filenames_move_to_next (db_subdirs);
    }

    /* If the directory's mtime is the same as the wall-clock time
     * when we stat'ed the directory, we skip updating the mtime in
     * the database because a message could be delivered later in this
     * same second.  This may lead to unnecessary re-scans, but it
     * avoids overlooking messages. */
    if (! interrupted && fs_mtime != stat_time) {
	status = notmuch_directory_set_mtime (directory, fs_mtime);
	if (status && ret == NOTMUCH_STATUS_SUCCESS)
	    ret = status;
    }

  DONE:
    if (next)
	talloc_free (next);
    if (entry)
	free (entry);
    if (dir)
	closedir (dir);
    if (fs_entries)
	free (fs_entries);
    if (db_subdirs)
	notmuch_filenames_destroy (db_subdirs);
    if (db_files)
	notmuch_filenames_destroy (db_files);
    if (directory)
	notmuch_directory_destroy (directory);

    return ret;
}
int main (int argc, char** argv) {
  GFile              *db_dir, *db_file;
  notmuch_status_t    status;
  notmuch_database_t *db   = NULL;
  notmuch_query_t *query = NULL;
  notmuch_messages_t *messages = NULL;
  notmuch_message_t *message = NULL;
  GMainLoop          *loop = NULL;

  const char *query_string = "date:2014-02-01..";

  if (argc != 2)
  {
    g_warning ("Usage: %s EVOLUTION_MAILDIR", argv[0]);
    return 1;
  }

  db_dir = g_file_new_for_path (argv[1]);
  db_file = g_file_get_child (db_dir, ".notmuch");

  if (!g_file_query_exists (db_dir, NULL))
  {
    g_object_unref (db_dir);
    g_object_unref (db_file);
    g_error ("directory %s does not exists");
    return 2;
  }

  if (!g_file_query_exists (db_file, NULL))
    status = notmuch_database_create (argv[1], &db);
  else
    status = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);

  if (status)
  {
    g_error ("Could not open database: %d", status);
    g_object_unref (db_dir);
    g_object_unref (db_file);
    notmuch_database_destroy (db);
    return 3;
  }


  scan_directory (db, db_dir);
  //loop = g_main_loop_new (NULL, FALSE);
  //g_main_loop_run (loop);

  query = notmuch_query_create (db, query_string);

  if (!query) {
    g_error ("Could not create query from string = \"%s\"", query_string);
    notmuch_database_destroy (db);
    g_object_unref (db_file);
    g_object_unref (db_dir);
    return 4;
  }

  g_message ("Query results -\n\n");

  for (messages = notmuch_query_search_messages (query);
         notmuch_messages_valid (messages);
         notmuch_messages_move_to_next (messages)) {

    message = notmuch_messages_get (messages);

    g_message ("Message file: %s", notmuch_message_get_filename (message));
    g_message ("Message ID: %s", notmuch_message_get_message_id (message));
    g_message ("Message Sender: %s", notmuch_message_get_header (message, "from"));
    g_message ("Message Recipients: %s", notmuch_message_get_header (message, "to"));
    g_message ("Message Subject: %s", notmuch_message_get_header (message, "subject"));
    g_message ("Message date: %s\n", notmuch_message_get_header (message, "date"));

    notmuch_message_destroy (message);
  }

  notmuch_query_destroy (query);

  notmuch_database_destroy (db);
  g_object_unref (db_file);
  g_object_unref (db_dir);
  return 0;
}
Esempio n. 27
0
static int
do_search_messages (search_context_t *ctx)
{
    notmuch_message_t *message;
    notmuch_messages_t *messages;
    notmuch_filenames_t *filenames;
    sprinter_t *format = ctx->format;
    int i;
    notmuch_status_t status;

    if (ctx->offset < 0) {
	unsigned count;
	notmuch_status_t status;
	status = notmuch_query_count_messages_st (ctx->query, &count);
	if (print_status_query ("notmuch search", ctx->query, status))
	    return 1;

	ctx->offset += count;
	if (ctx->offset < 0)
	    ctx->offset = 0;
    }

    status = notmuch_query_search_messages_st (ctx->query, &messages);
    if (print_status_query ("notmuch search", ctx->query, status))
	return 1;

    format->begin_list (format);

    for (i = 0;
	 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
	 notmuch_messages_move_to_next (messages), i++)
    {
	if (i < ctx->offset)
	    continue;

	message = notmuch_messages_get (messages);

	if (ctx->output == OUTPUT_FILES) {
	    int j;
	    filenames = notmuch_message_get_filenames (message);

	    for (j = 1;
		 notmuch_filenames_valid (filenames);
		 notmuch_filenames_move_to_next (filenames), j++)
	    {
		if (ctx->dupe < 0 || ctx->dupe == j) {
		    format->string (format, notmuch_filenames_get (filenames));
		    format->separator (format);
		}
	    }
	    
	    notmuch_filenames_destroy( filenames );

	} else if (ctx->output == OUTPUT_MESSAGES) {
            /* special case 1 for speed */
            if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
                format->set_prefix (format, "id");
                format->string (format,
                                notmuch_message_get_message_id (message));
                format->separator (format);
            }
	} else {
	    if (ctx->output & OUTPUT_SENDER) {
		const char *addrs;

		addrs = notmuch_message_get_header (message, "from");
		process_address_header (ctx, addrs);
	    }

	    if (ctx->output & OUTPUT_RECIPIENTS) {
		const char *hdrs[] = { "to", "cc", "bcc" };
		const char *addrs;
		size_t j;

		for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
		    addrs = notmuch_message_get_header (message, hdrs[j]);
		    process_address_header (ctx, addrs);
		}
	    }
	}

	notmuch_message_destroy (message);
    }

    if (ctx->addresses &&
	(ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS))
	g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);

    notmuch_messages_destroy (messages);

    format->end (format);

    return 0;
}
Esempio n. 28
0
static notmuch_status_t
show_messages (void *ctx,
               const notmuch_show_format_t *format,
               notmuch_messages_t *messages,
               int indent,
               notmuch_show_params_t *params)
{
    notmuch_message_t *message;
    notmuch_bool_t match;
    notmuch_bool_t excluded;
    int first_set = 1;
    int next_indent;
    notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;

    if (format->message_set_start)
        fputs (format->message_set_start, stdout);

    for (;
            notmuch_messages_valid (messages);
            notmuch_messages_move_to_next (messages))
    {
        if (!first_set && format->message_set_sep)
            fputs (format->message_set_sep, stdout);
        first_set = 0;

        if (format->message_set_start)
            fputs (format->message_set_start, stdout);

        message = notmuch_messages_get (messages);

        match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
        excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);

        next_indent = indent;

        if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
            status = show_message (ctx, format, message, indent, params);
            if (status && !res)
                res = status;
            next_indent = indent + 1;

            if (!status && format->message_set_sep)
                fputs (format->message_set_sep, stdout);
        }

        status = show_messages (ctx,
                                format,
                                notmuch_message_get_replies (message),
                                next_indent,
                                params);
        if (status && !res)
            res = status;

        notmuch_message_destroy (message);

        if (format->message_set_end)
            fputs (format->message_set_end, stdout);
    }

    if (format->message_set_end)
        fputs (format->message_set_end, stdout);

    return res;
}
Esempio n. 29
0
static int
notmuch_reply_format_default(void *ctx,
			     notmuch_config_t *config,
			     notmuch_query_t *query,
			     notmuch_show_params_t *params)
{
    GMimeMessage *reply;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    const char *subject, *from_addr = NULL;
    const char *in_reply_to, *orig_references, *references;
    const notmuch_show_format_t *format = &format_reply;

    for (messages = notmuch_query_search_messages (query);
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	message = notmuch_messages_get (messages);

	/* The 1 means we want headers in a "pretty" order. */
	reply = g_mime_message_new (1);
	if (reply == NULL) {
	    fprintf (stderr, "Out of memory\n");
	    return 1;
	}

	subject = notmuch_message_get_header (message, "subject");
	if (subject) {
	    if (strncasecmp (subject, "Re:", 3))
		subject = talloc_asprintf (ctx, "Re: %s", subject);
	    g_mime_message_set_subject (reply, subject);
	}

	from_addr = add_recipients_from_message (reply, config, message);

	if (from_addr == NULL)
	    from_addr = guess_from_received_header (config, message);

	if (from_addr == NULL)
	    from_addr = notmuch_config_get_user_primary_email (config);

	from_addr = talloc_asprintf (ctx, "%s <%s>",
				     notmuch_config_get_user_name (config),
				     from_addr);
	g_mime_object_set_header (GMIME_OBJECT (reply),
				  "From", from_addr);

	in_reply_to = talloc_asprintf (ctx, "<%s>",
			     notmuch_message_get_message_id (message));

	g_mime_object_set_header (GMIME_OBJECT (reply),
				  "In-Reply-To", in_reply_to);

	orig_references = notmuch_message_get_header (message, "references");
	references = talloc_asprintf (ctx, "%s%s%s",
				      orig_references ? orig_references : "",
				      orig_references ? " " : "",
				      in_reply_to);
	g_mime_object_set_header (GMIME_OBJECT (reply),
				  "References", references);

	show_reply_headers (reply);

	g_object_unref (G_OBJECT (reply));
	reply = NULL;

	printf ("On %s, %s wrote:\n",
		notmuch_message_get_header (message, "date"),
		notmuch_message_get_header (message, "from"));

	show_message_body (notmuch_message_get_filename (message),
			   format, params);

	notmuch_message_destroy (message);
    }
    return 0;
}
Esempio n. 30
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;
}