File: cmd-setactive.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (104 lines) | stat: -rw-r--r-- 2,655 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

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

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

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

bool cmd_setactive(struct client_command_context *cmd)
{
	struct client *client = cmd->client;
	struct sieve_storage *storage = client->storage;
	const char *scriptname;
	struct sieve_script *script;
	int ret;

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

	/* Activate, or .. */
	if ( *scriptname != '\0' ) {
		string_t *errors = NULL;
		bool warnings = FALSE;
		bool success = TRUE;

		script = sieve_storage_script_init(storage, scriptname);
		if ( script == NULL ) {
			client_send_storage_error(client, storage);
			return TRUE;
		}

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

				/* 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 */
				if ( (sbin=sieve_compile_script
					(script, ehandler, cpflags, NULL)) == NULL ) {
					success = FALSE;
				} else {
					sieve_close(&sbin);
				}

				warnings = ( sieve_get_warnings(ehandler) > 0 );
				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_storage_script_activate(script, (time_t)-1);
			if ( ret < 0 ) {
				client_send_storage_error(client, storage);
			} else {
				if ( warnings ) {
					client_send_okresp(client, "WARNINGS", str_c(errors));
				} else {
					client_send_ok(client, ( ret > 0 ?
						"Setactive completed." :
						"Script is already active." ));
				}
			}
		} else {
			client_send_no(client, str_c(errors));
		}

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

	/* ... deactivate */
	} else {
		ret = sieve_storage_deactivate(storage, (time_t)-1);

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

	return TRUE;
}