Пример #1
0
int
cmd_tag_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;
    log_debug("tagging message %s", notmuch_message_get_message_id(msg));

    if (!msg) {
        tcl_result_printf(interp, "no active message");
        return TCL_ERROR;
    }

    for (int i = 1; i < argc; i++) {
        const char *tspec = argv[i];
        notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
        switch (*tspec) {
            case '+':
                log_debug("adding tag %s", tspec + 1);
                if (!ctx->dry_run) {
                    status = notmuch_message_add_tag(msg, tspec+1);
                }
                break;
            case '-':
                log_debug("removing tag %s", tspec + 1);
                if (!ctx->dry_run) {
                    status = notmuch_message_remove_tag(msg, tspec+1);
                }
                break;
            default:
                tcl_result_printf(interp, "invalid tag spec %s", tspec);
                return TCL_ERROR;
                break;
        }
        if (status != NOTMUCH_STATUS_SUCCESS) {
            tcl_result_printf(interp, "failed to apply tag %s", tspec);
            return TCL_ERROR;
        }
    }
    notmuch_message_tags_to_maildir_flags(msg);

    return TCL_OK;
}
Пример #2
0
static int update_tags(notmuch_message_t *msg, const char *tags)
{
    char *tag = NULL, *end = NULL, *p;
    char *buf = safe_strdup(tags);

    if (!buf)
        return -1;

    notmuch_message_freeze(msg);

    for (p = buf; p && *p; p++) {
        if (!tag && isspace(*p))
            continue;
        if (!tag)
            tag = p;		/* begin of the tag */
        if (*p == ',' || *p == ' ')
            end = p;		/* terminate the tag */
        else if (*(p + 1) == '\0')
            end = p + 1;		/* end of optstr */
        if (!tag || !end)
            continue;
        if (tag >= end)
            break;

        *end = '\0';

        if (*tag == '-') {
            dprint(1, (debugfile, "nm: remove tag: '%s'\n", tag + 1));
            notmuch_message_remove_tag(msg, tag + 1);
        } else {
            dprint(1, (debugfile, "nm: add tag: '%s'\n", *tag == '+' ? tag + 1 : tag));
            notmuch_message_add_tag(msg, *tag == '+' ? tag + 1 : tag);
        }
        end = tag = NULL;
    }

    notmuch_message_thaw(msg);
    FREE(&buf);
    return 0;
}
Пример #3
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;
}
Пример #4
0
notmuch_status_t
tag_op_list_apply (notmuch_message_t *message,
		   tag_op_list_t *list,
		   tag_op_flag_t flags)
{
    size_t i;
    notmuch_status_t status = 0;
    tag_operation_t *tag_ops = list->ops;

    if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
	return NOTMUCH_STATUS_SUCCESS;

    status = notmuch_message_freeze (message);
    if (status) {
	message_error (message, status, "freezing message");
	return status;
    }

    if (flags & TAG_FLAG_REMOVE_ALL) {
	status = notmuch_message_remove_all_tags (message);
	if (status) {
	    message_error (message, status, "removing all tags");
	    return status;
	}
    }

    for (i = 0; i < list->count; i++) {
	if (tag_ops[i].remove) {
	    status = notmuch_message_remove_tag (message, tag_ops[i].tag);
	    if (status) {
		message_error (message, status, "removing tag %s", tag_ops[i].tag);
		return status;
	    }
	} else {
	    status = notmuch_message_add_tag (message, tag_ops[i].tag);
	    if (status) {
		message_error (message, status, "adding tag %s", tag_ops[i].tag);
		return status;
	    }

	}
    }

    status = notmuch_message_thaw (message);
    if (status) {
	message_error (message, status, "thawing message");
	return status;
    }


    if (flags & TAG_FLAG_MAILDIR_SYNC) {
	status = notmuch_message_tags_to_maildir_flags (message);
	if (status) {
	    message_error (message, status, "synching tags to maildir");
	    return status;
	}
    }

    return NOTMUCH_STATUS_SUCCESS;

}