summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/plugins/vacation/ext-vacation-common.c
blob: 97be3a5ead2370e6f44f4b149d39190a6e29f21a (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
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"

#include "sieve-common.h"
#include "sieve-error.h"
#include "sieve-settings.h"
#include "sieve-extensions.h"

#include "ext-vacation-common.h"

bool ext_vacation_load
(const struct sieve_extension *ext, void **context)
{
	struct sieve_instance *svinst = ext->svinst;
	struct ext_vacation_config *config;
	sieve_number_t min_period, max_period, default_period;
	bool use_original_recipient, dont_check_recipient, send_from_recipient,
		to_header_ignore_envelope;
	unsigned long long max_subject_codepoints;
	const char *default_subject, *default_subject_template;

	if ( *context != NULL ) {
		ext_vacation_unload(ext);
	}

	if ( !sieve_setting_get_duration_value
		(svinst, "sieve_vacation_min_period", &min_period) ) {
		min_period = EXT_VACATION_DEFAULT_MIN_PERIOD;
	}

	if ( !sieve_setting_get_duration_value
		(svinst, "sieve_vacation_max_period", &max_period) ) {
		max_period = EXT_VACATION_DEFAULT_MAX_PERIOD;
	}

	if ( !sieve_setting_get_duration_value
		(svinst, "sieve_vacation_default_period", &default_period) ) {
		default_period = EXT_VACATION_DEFAULT_PERIOD;
	}

	if ( max_period > 0
		&& (min_period > max_period || default_period < min_period
			|| default_period > max_period) ) {
		min_period = EXT_VACATION_DEFAULT_MIN_PERIOD;
		max_period = EXT_VACATION_DEFAULT_MAX_PERIOD;
		default_period = EXT_VACATION_DEFAULT_PERIOD;

		e_warning(svinst->event, "vacation extension: "
			  "invalid settings: violated "
			  "sieve_vacation_min_period < "
			  "sieve_vacation_default_period < "
			  "sieve_vacation_max_period");
	}

	default_subject = sieve_setting_get(
		svinst, "sieve_vacation_default_subject");
	default_subject_template = sieve_setting_get(
		svinst, "sieve_vacation_default_subject_template");

	if ( !sieve_setting_get_uint_value
		(svinst, "sieve_vacation_max_subject_codepoints", &max_subject_codepoints) ) {
		max_subject_codepoints = EXT_VACATION_DEFAULT_MAX_SUBJECT_CODEPOINTS;
	}

	if ( !sieve_setting_get_bool_value
		(svinst, "sieve_vacation_use_original_recipient", &use_original_recipient) ) {
		use_original_recipient = FALSE;
	}

	if ( !sieve_setting_get_bool_value
		(svinst, "sieve_vacation_dont_check_recipient", &dont_check_recipient) ) {
		dont_check_recipient = FALSE;
	}

	if ( !sieve_setting_get_bool_value
		(svinst, "sieve_vacation_send_from_recipient", &send_from_recipient) ) {
		send_from_recipient = FALSE;
	}

	if ( !sieve_setting_get_bool_value(svinst,
		"sieve_vacation_to_header_ignore_envelope",
		&to_header_ignore_envelope) ) {
		to_header_ignore_envelope = FALSE;
	}

	config = i_new(struct ext_vacation_config, 1);
	config->min_period = min_period;
	config->max_period = max_period;
	config->default_period = default_period;
	config->max_subject_codepoints = max_subject_codepoints;
	config->default_subject = i_strdup_empty(default_subject);
	config->default_subject_template = i_strdup_empty(default_subject_template);
	config->use_original_recipient = use_original_recipient;
	config->dont_check_recipient = dont_check_recipient;
	config->send_from_recipient = send_from_recipient;
	config->to_header_ignore_envelope = to_header_ignore_envelope;

	*context = (void *) config;

	return TRUE;
}

void ext_vacation_unload
(const struct sieve_extension *ext)
{
	struct ext_vacation_config *config =
		(struct ext_vacation_config *) ext->context;

	i_free(config->default_subject);
	i_free(config->default_subject_template);
	i_free(config);
}