Example #1
0
static const char *
sieve_attribute_iter_next_script(struct sieve_mailbox_attribute_iter *siter)
{
	struct mail_user *user = siter->iter.box->storage->user;
	struct sieve_mail_user *suser = SIEVE_USER_CONTEXT(user);
	struct sieve_storage *svstorage = suser->sieve_storage;
	const char *scriptname;
	bool active;
	int ret;

	if (siter->sieve_list == NULL)
		return NULL;

	/* Iterate through all scripts in sieve_dir */
	while ((scriptname = sieve_storage_list_next(siter->sieve_list, &active))
		!= NULL) {
		if (active)
			siter->have_active = TRUE;
		str_truncate(siter->name, strlen(MAILBOX_ATTRIBUTE_PREFIX_SIEVE_FILES));
		str_append(siter->name, scriptname);
		return str_c(siter->name);
	}
	if (sieve_storage_list_deinit(&siter->sieve_list) < 0) {
		mail_storage_set_critical(siter->iter.box->storage,
			"Failed to iterate sieve scripts: %s",
			sieve_storage_get_last_error(svstorage, NULL));
		siter->failed = TRUE;
		return NULL;
	}

	/* Check whether active script is a proper symlink or a regular file */
	if ((ret=sieve_storage_is_singular(svstorage)) < 0) {
		mail_storage_set_critical(siter->iter.box->storage,
			"Failed to iterate sieve scripts: %s",
			sieve_storage_get_last_error(svstorage, NULL));
		return NULL;
	}

	/* Regular file */
	if (ret > 0)
		return MAILBOX_ATTRIBUTE_SIEVE_DEFAULT;

	/* Symlink or none active */
	return siter->have_active ? MAILBOX_ATTRIBUTE_SIEVE_DEFAULT : NULL;
}
bool cmd_listscripts(struct client_command_context *cmd)
{
    struct client *client = cmd->client;
    struct sieve_storage_list_context *ctx;
    const char *scriptname;
    bool active;
    string_t *str;

    /* no arguments */
    if ( !client_read_no_args(cmd) )
        return FALSE;

    if ( (ctx = sieve_storage_list_init(client->storage))
            == NULL ) {
        client_send_storage_error(client, client->storage);
        return TRUE;
    }

    /* FIXME: This will be quite slow for large script lists. Implement
     * some buffering to fix this. Wont truely be an issue with managesieve
     * though.
     */
    while ((scriptname = sieve_storage_list_next(ctx, &active)) != NULL) {
        T_BEGIN {
            str = t_str_new(128);

            managesieve_quote_append_string(str, scriptname, FALSE);

            if ( active )
                str_append(str, " ACTIVE");

            client_send_line(client, str_c(str));
        } T_END;
    }

    if ( sieve_storage_list_deinit(&ctx) < 0 ) {
        client_send_storage_error(client, client->storage);
        return TRUE;
    }

    client_send_ok(client, "Listscripts completed.");
    return TRUE;
}