File: ext-spamvirustest.c

package info (click to toggle)
dovecot 1%3A2.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 (145 lines) | stat: -rw-r--r-- 3,935 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
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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

/* Extensions spamtest, spamtestplus and virustest
 * -----------------------------------------------
 *
 * Authors: Stephan Bosch
 * Specification: RFC 5235
 * Implementation: full
 * Status: testing
 *
 */

/* Configuration examples:
 *
 * # 1: X-Spam-Score: No, score=-3.2
 *
 * sieve_spamtest_status_header = \
 *   X-Spam-Score: [[:alnum:]]+, score=(-?[[:digit:]]+\.[[:digit:]])
 * sieve_spamtest_max_value = 5.0
 *
 * # 2: X-Spam-Status: Yes
 *
 * sieve_spamtest_status_header = X-Spam-Status
 * sieve_spamtest_status_type = yesno
 * sieve_spamtest_max_value = Yes
 *
 * # 3: X-Spam-Score: sssssss
 * sieve_spamtest_status_header = X-Spam-Score
 * sieve_spamtest_status_type = strlen
 * sieve_spamtest_max_value = 5
 *
 * # 4: X-Spam-Score: status=3.2 required=5.0
 *
 * sieve_spamtest_status_header = \
 *   X-Spam-Score: score=(-?[[:digit:]]+\.[[:digit:]]).*
 * sieve_spamtest_max_header = \
 *   X-Spam-Score: score=-?[[:digit:]]+\.[[:digit:]] required=([[:digit:]]+\.[[:digit:]])
 *
 * # 5: X-Virus-Scan: Found to be clean.
 *
 * sieve_virustest_status_header = \
 *   X-Virus-Scan: Found to be (.+)\.
 * sieve_virustest_status_type = text
 * sieve_virustest_text_value1 = clean
 * sieve_virustest_text_value5 = infected
 */

#include "lib.h"
#include "array.h"

#include "sieve-common.h"

#include "sieve-extensions.h"
#include "sieve-commands.h"

#include "sieve-validator.h"

#include "ext-spamvirustest-common.h"

/*
 * Extensions
 */

/* Spamtest */

static bool ext_spamvirustest_validator_load
(const struct sieve_extension *ext, struct sieve_validator *validator);

const struct sieve_extension_def spamtest_extension = {
	.name = "spamtest",
	.load = ext_spamvirustest_load,
	.unload = ext_spamvirustest_unload,
	.validator_load = ext_spamvirustest_validator_load,
	SIEVE_EXT_DEFINE_OPERATION(spamtest_operation)
};

const struct sieve_extension_def spamtestplus_extension = {
	.name = "spamtestplus",
	.load = ext_spamvirustest_load,
	.unload = ext_spamvirustest_unload,
	.validator_load = ext_spamvirustest_validator_load,
	SIEVE_EXT_DEFINE_OPERATION(spamtest_operation)
};

const struct sieve_extension_def virustest_extension = {
	.name = "virustest",
	.load = ext_spamvirustest_load,
	.unload = ext_spamvirustest_unload,
	.validator_load = ext_spamvirustest_validator_load,
	SIEVE_EXT_DEFINE_OPERATION(virustest_operation)
};

/*
 * Implementation
 */

static bool ext_spamtest_validator_extension_validate
	(const struct sieve_extension *ext, struct sieve_validator *valdtr,
		void *context, struct sieve_ast_argument *require_arg);

const struct sieve_validator_extension spamtest_validator_extension = {
	&spamtest_extension,
	ext_spamtest_validator_extension_validate,
	NULL
};

static bool ext_spamvirustest_validator_load
(const struct sieve_extension *ext, struct sieve_validator *valdtr)
{
	/* Register new test */

	if ( sieve_extension_is(ext, virustest_extension) ) {
		sieve_validator_register_command(valdtr, ext, &virustest_test);
	} else {
		if ( sieve_extension_is(ext, spamtest_extension) ) {
			/* Register validator extension to warn for duplicate */
			sieve_validator_extension_register
				(valdtr, ext, &spamtest_validator_extension, NULL);
		}

		sieve_validator_register_command(valdtr, ext, &spamtest_test);
	}

	return TRUE;
}

static bool ext_spamtest_validator_extension_validate
(const struct sieve_extension *ext, struct sieve_validator *valdtr,
	void *context ATTR_UNUSED, struct sieve_ast_argument *require_arg)
{
	const struct sieve_extension *ext_spamtestplus =
		sieve_extension_get_by_name(ext->svinst, "spamtestplus");

	if ( ext_spamtestplus != NULL &&
		sieve_validator_extension_loaded(valdtr, ext_spamtestplus) ) {
		sieve_argument_validate_warning(valdtr, require_arg,
			"the spamtest and spamtestplus extensions should not be specified "
			"at the same time");
	}

	return TRUE;
}