1 /* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
2  */
3 
4 #include "lib.h"
5 #include "str.h"
6 
7 #include "sieve.h"
8 #include "sieve-storage.h"
9 
10 #include "managesieve-quote.h"
11 
12 #include "managesieve-common.h"
13 #include "managesieve-commands.h"
14 
cmd_listscripts(struct client_command_context * cmd)15 bool cmd_listscripts(struct client_command_context *cmd)
16 {
17 	struct client *client = cmd->client;
18 	struct sieve_storage_list_context *ctx;
19 	const char *scriptname;
20 	unsigned int script_count = 0;
21 	bool active;
22 	string_t *str;
23 
24 	/* no arguments */
25 	if (!client_read_no_args(cmd))
26 		return FALSE;
27 
28 	if ((ctx = sieve_storage_list_init(client->storage)) == NULL) {
29 		client_command_storage_error(
30 			cmd, "Failed to list scripts");
31 		return TRUE;
32 	}
33 
34 	/* FIXME: This will be quite slow for large script lists. Implement
35 	 * some buffering to fix this. Wont truely be an issue with managesieve
36 	 * though.
37 	 */
38 	while ((scriptname = sieve_storage_list_next(ctx, &active)) != NULL) {
39 		T_BEGIN {
40 			str = t_str_new(128);
41 
42 			managesieve_quote_append_string(str, scriptname, FALSE);
43 
44 			if (active)
45 				str_append(str, " ACTIVE");
46 
47 			client_send_line(client, str_c(str));
48 		} T_END;
49 
50 		script_count++;
51 	}
52 
53 	if (sieve_storage_list_deinit(&ctx) < 0) {
54 		client_command_storage_error(
55 			cmd, "Failed to list scripts");
56 		return TRUE;
57 	}
58 
59 	struct event_passthrough *e =
60 		client_command_create_finish_event(cmd);
61 	e_debug(e->event(), "Listed %u scripts", script_count);
62 
63 	client_send_ok(client, "Listscripts completed.");
64 	return TRUE;
65 }
66