summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/plugins/copy/ext-copy.c
blob: e7db9dd8c0f2abdfdc76023d6a0f0a54af67e9fa (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

/* Extension copy
 * --------------
 *
 * Authors: Stephan Bosch
 * Specification: RFC 3894
 * Implementation: full
 * Status: testing
 *
 */

#include "sieve-common.h"

#include "sieve-code.h"
#include "sieve-extensions.h"
#include "sieve-actions.h"
#include "sieve-commands.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-result.h"

#include "sieve-ext-copy.h"

/*
 * Forward declarations
 */

static const struct sieve_argument_def copy_tag;
static const struct sieve_operand_def copy_side_effect_operand;

/*
 * Extension
 */

static bool
ext_copy_validator_load(const struct sieve_extension *ext,
			struct sieve_validator *valdtr);

const struct sieve_extension_def copy_extension = {
	.name = "copy",
	.validator_load = ext_copy_validator_load,
	SIEVE_EXT_DEFINE_OPERAND(copy_side_effect_operand),
};

static bool
ext_copy_validator_load(const struct sieve_extension *ext,
			struct sieve_validator *valdtr)
{
	/* Register copy tag with redirect and fileinto commands and we don't
	   care whether these commands are registered or even whether they will
	   be registered at all. The validator handles either situation
	   gracefully.
	 */
	sieve_validator_register_external_tag(valdtr, "redirect", ext,
					      &copy_tag, SIEVE_OPT_SIDE_EFFECT);
	sieve_validator_register_external_tag(valdtr, "fileinto", ext,
					      &copy_tag, SIEVE_OPT_SIDE_EFFECT);
	return TRUE;
}

/*
 * Side effect
 */

static void
seff_copy_print(const struct sieve_side_effect *seffect,
		const struct sieve_action *action,
		const struct sieve_result_print_env *rpenv, bool *keep);
static int
seff_copy_post_execute(const struct sieve_side_effect *seffect ATTR_UNUSED,
		       const struct sieve_action_exec_env *aenv ATTR_UNUSED,
		       void *tr_context ATTR_UNUSED,
		       void *se_tr_context ATTR_UNUSED, bool *keep);

const struct sieve_side_effect_def copy_side_effect = {
	SIEVE_OBJECT("copy", &copy_side_effect_operand, 0),
	.to_action = &act_store,
	.print = seff_copy_print,
	.post_execute = seff_copy_post_execute,
};

/*
 * Tagged argument
 */

static bool
tag_copy_validate(struct sieve_validator *valdtr,
		  struct sieve_ast_argument **arg, struct sieve_command *cmd);
static bool
tag_copy_generate(const struct sieve_codegen_env *cgenv,
		  struct sieve_ast_argument *arg, struct sieve_command *cmd);

static const struct sieve_argument_def copy_tag = {
	.identifier = "copy",
	.validate = tag_copy_validate,
	.generate = tag_copy_generate,
};

/*
 * Operand
 */

static const struct sieve_extension_objects ext_side_effects =
	SIEVE_EXT_DEFINE_SIDE_EFFECT(copy_side_effect);

static const struct sieve_operand_def copy_side_effect_operand = {
	.name = "copy operand",
	.ext_def = &copy_extension,
	.class = &sieve_side_effect_operand_class,
	.interface = &ext_side_effects,
};

/*
 * Tag registration
 */

void sieve_ext_copy_register_tag(struct sieve_validator *valdtr,
				 const struct sieve_extension *copy_ext,
				 const char *command)
{
	if (sieve_validator_extension_loaded(valdtr, copy_ext)) {
		sieve_validator_register_external_tag(
			valdtr, command, copy_ext, &copy_tag,
			SIEVE_OPT_SIDE_EFFECT);
	}
}

/*
 * Tag validation
 */

static bool
tag_copy_validate(struct sieve_validator *valdtr ATTR_UNUSED,
		  struct sieve_ast_argument **arg ATTR_UNUSED,
		  struct sieve_command *cmd ATTR_UNUSED)
{
	*arg = sieve_ast_argument_next(*arg);

	return TRUE;
}

/*
 * Code generation
 */

static bool
tag_copy_generate(const struct sieve_codegen_env *cgenv,
		  struct sieve_ast_argument *arg,
		  struct sieve_command *cmd ATTR_UNUSED)
{
	if (sieve_ast_argument_type(arg) != SAAT_TAG)
		return FALSE;

	sieve_opr_side_effect_emit(cgenv->sblock, sieve_argument_ext(arg),
				   &copy_side_effect);
	return TRUE;
}

/*
 * Side effect implementation
 */

static void
seff_copy_print(const struct sieve_side_effect *seffect ATTR_UNUSED,
		const struct sieve_action *action ATTR_UNUSED,
		const struct sieve_result_print_env *rpenv, bool *keep)
{
	sieve_result_seffect_printf(rpenv, "preserve implicit keep");
	*keep = TRUE;
}

static int
seff_copy_post_execute(const struct sieve_side_effect *seffect ATTR_UNUSED,
		       const struct sieve_action_exec_env *aenv ATTR_UNUSED,
		       void *tr_context ATTR_UNUSED,
		       void *se_tr_context ATTR_UNUSED, bool *keep)
{
	*keep = TRUE;
	return SIEVE_EXEC_OK;
}