summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/managesieve/cmd-setactive.c
blob: 07844c5e3d4013c4b155311d47d8d09ecaa1578a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"

#include "sieve.h"
#include "sieve-script.h"
#include "sieve-storage.h"

#include "managesieve-common.h"
#include "managesieve-commands.h"

static void
cmd_setactive_activate(struct client_command_context *cmd,
		       const char *scriptname)
{
	struct client *client = cmd->client;
	struct sieve_storage *storage = client->storage;
	struct sieve_script *script;
	string_t *errors = NULL;
	const char *errormsg = NULL;
	unsigned int warning_count = 0, error_count = 0;
	bool success = TRUE;
	int ret;

	event_add_str(cmd->event, "script_name", scriptname);

	script = sieve_storage_open_script(storage, scriptname, NULL);
	if (script == NULL) {
		client_command_storage_error(
			cmd, "Failed to open script `%s' for activation",
			scriptname);
		return;
	}

	if (sieve_script_is_active(script) <= 0) T_BEGIN {
		/* Script is first being activated; compile it again without the
		   UPLOAD flag. */
		struct sieve_error_handler *ehandler;
		enum sieve_compile_flags cpflags =
			SIEVE_COMPILE_FLAG_NOGLOBAL |
			SIEVE_COMPILE_FLAG_ACTIVATED;
		struct sieve_binary *sbin;
		enum sieve_error error;

		/* Prepare error handler */
		errors = str_new(default_pool, 1024);
		ehandler = sieve_strbuf_ehandler_create(
			client->svinst, errors, TRUE,
			client->set->managesieve_max_compile_errors);

		/* Compile */
		sbin = sieve_compile_script(script, ehandler, cpflags, &error);
		if (sbin == NULL) {
			if (error != SIEVE_ERROR_NOT_VALID) {
				errormsg = sieve_script_get_last_error(
					script, &error);
				if (error == SIEVE_ERROR_NONE)
					errormsg = NULL;
			}
			success = FALSE;
		} else {
			sieve_close(&sbin);
		}

		warning_count = sieve_get_warnings(ehandler);
		error_count = sieve_get_errors(ehandler);
		sieve_error_handler_unref(&ehandler);
	} T_END;

	/* Activate only when script is valid (or already active) */
	if (success) {
		/* Refresh activation no matter what; this can also
		   resolve some erroneous situations. */
		ret = sieve_script_activate(script, (time_t)-1);
		if (ret < 0) {
			client_command_storage_error(
				cmd, "Failed to activate script `%s'",
				scriptname);
		} else {
			struct event_passthrough *e =
				client_command_create_finish_event(cmd)->
				add_int("compile_warnings", warning_count);
			e_debug(e->event(), "Activated script `%s' "
				" (%u warnings%s)",
				scriptname, warning_count,
				(ret == 0 ? ", redundant" : ""));

			if (warning_count > 0) {
				client_send_okresp(
					client, "WARNINGS",
					str_c(errors));
			} else {
				client_send_ok(client,
					(ret > 0 ?
					 "Setactive completed." :
					 "Script is already active."));
			}
		}
	} else if (errormsg == NULL) {
		struct event_passthrough *e =
			client_command_create_finish_event(cmd)->
			add_str("error", "Compilation failed")->
			add_int("compile_errors", error_count)->
			add_int("compile_warnings", warning_count);
		e_debug(e->event(), "Failed to activate script `%s': "
			"Compilation failed (%u errors, %u warnings)",
			scriptname, error_count, warning_count);

		client_send_no(client, str_c(errors));
	} else {
		struct event_passthrough *e =
			client_command_create_finish_event(cmd)->
			add_str("error", errormsg);
		e_debug(e->event(), "Failed to activate script `%s': %s",
			scriptname, errormsg);

		client_send_no(client, errormsg);
	}

	if (errors != NULL)
		str_free(&errors);
	sieve_script_unref(&script);
}

static void
cmd_setactive_deactivate(struct client_command_context *cmd)
{
	struct client *client = cmd->client;
	struct sieve_storage *storage = client->storage;
	int ret;

	ret = sieve_storage_deactivate(storage, (time_t)-1);
	if (ret < 0) {
		client_command_storage_error(
			cmd, "Failed to deactivate script");
		return;
	}

	struct event_passthrough *e =
		client_command_create_finish_event(cmd);
	e_debug(e->event(), "Deactivated script");

	client_send_ok(client, (ret > 0 ?
				"Active script is now deactivated." :
				"No scripts currently active."));
}

bool cmd_setactive(struct client_command_context *cmd)
{
	const char *scriptname;

	/* <scriptname> */
	if (!client_read_string_args(cmd, TRUE, 1, &scriptname))
		return FALSE;

	/* Activate, or deactivate */
	if (*scriptname != '\0')
		cmd_setactive_activate(cmd, scriptname);
	else
		cmd_setactive_deactivate(cmd);

	return TRUE;
}