Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pigeonhole/src/lib-sieve/plugins/copy/ext-copy.c
Line
Count
Source
1
/* Copyright (c) Pigeonhole authors, see top-level COPYING file */
2
3
/* Extension copy
4
 * --------------
5
 *
6
 * Authors: Stephan Bosch
7
 * Specification: RFC 3894
8
 * Implementation: full
9
 * Status: testing
10
 *
11
 */
12
13
#include "sieve-common.h"
14
15
#include "sieve-code.h"
16
#include "sieve-extensions.h"
17
#include "sieve-actions.h"
18
#include "sieve-commands.h"
19
#include "sieve-validator.h"
20
#include "sieve-generator.h"
21
#include "sieve-interpreter.h"
22
#include "sieve-result.h"
23
24
#include "sieve-ext-copy.h"
25
26
/*
27
 * Forward declarations
28
 */
29
30
static const struct sieve_argument_def copy_tag;
31
static const struct sieve_operand_def copy_side_effect_operand;
32
33
/*
34
 * Extension
35
 */
36
37
static bool
38
ext_copy_validator_load(const struct sieve_extension *ext,
39
      struct sieve_validator *valdtr);
40
41
const struct sieve_extension_def copy_extension = {
42
  .name = "copy",
43
  .validator_load = ext_copy_validator_load,
44
  SIEVE_EXT_DEFINE_OPERAND(copy_side_effect_operand),
45
};
46
47
static bool
48
ext_copy_validator_load(const struct sieve_extension *ext,
49
      struct sieve_validator *valdtr)
50
0
{
51
  /* Register copy tag with redirect and fileinto commands and we don't
52
     care whether these commands are registered or even whether they will
53
     be registered at all. The validator handles either situation
54
     gracefully.
55
   */
56
0
  sieve_validator_register_external_tag(valdtr, "redirect", ext,
57
0
                &copy_tag, SIEVE_OPT_SIDE_EFFECT);
58
0
  sieve_validator_register_external_tag(valdtr, "fileinto", ext,
59
0
                &copy_tag, SIEVE_OPT_SIDE_EFFECT);
60
0
  return TRUE;
61
0
}
62
63
/*
64
 * Side effect
65
 */
66
67
static void
68
seff_copy_print(const struct sieve_side_effect *seffect,
69
    const struct sieve_action *action,
70
    const struct sieve_result_print_env *rpenv, bool *keep);
71
static int
72
seff_copy_post_execute(const struct sieve_side_effect *seffect ATTR_UNUSED,
73
           const struct sieve_action_exec_env *aenv ATTR_UNUSED,
74
           void *tr_context ATTR_UNUSED,
75
           void *se_tr_context ATTR_UNUSED, bool *keep);
76
77
const struct sieve_side_effect_def copy_side_effect = {
78
  SIEVE_OBJECT("copy", &copy_side_effect_operand, 0),
79
  .to_action = &act_store,
80
  .print = seff_copy_print,
81
  .post_execute = seff_copy_post_execute,
82
};
83
84
/*
85
 * Tagged argument
86
 */
87
88
static bool
89
tag_copy_validate(struct sieve_validator *valdtr,
90
      struct sieve_ast_argument **arg, struct sieve_command *cmd);
91
static bool
92
tag_copy_generate(const struct sieve_codegen_env *cgenv,
93
      struct sieve_ast_argument *arg, struct sieve_command *cmd);
94
95
static const struct sieve_argument_def copy_tag = {
96
  .identifier = "copy",
97
  .validate = tag_copy_validate,
98
  .generate = tag_copy_generate,
99
};
100
101
/*
102
 * Operand
103
 */
104
105
static const struct sieve_extension_objects ext_side_effects =
106
  SIEVE_EXT_DEFINE_SIDE_EFFECT(copy_side_effect);
107
108
static const struct sieve_operand_def copy_side_effect_operand = {
109
  .name = "copy operand",
110
  .ext_def = &copy_extension,
111
  .class = &sieve_side_effect_operand_class,
112
  .interface = &ext_side_effects,
113
};
114
115
/*
116
 * Tag registration
117
 */
118
119
void sieve_ext_copy_register_tag(struct sieve_validator *valdtr,
120
         const struct sieve_extension *copy_ext,
121
         const char *command)
122
0
{
123
0
  if (sieve_validator_extension_loaded(valdtr, copy_ext)) {
124
0
    sieve_validator_register_external_tag(
125
0
      valdtr, command, copy_ext, &copy_tag,
126
0
      SIEVE_OPT_SIDE_EFFECT);
127
0
  }
128
0
}
129
130
/*
131
 * Tag validation
132
 */
133
134
static bool
135
tag_copy_validate(struct sieve_validator *valdtr ATTR_UNUSED,
136
      struct sieve_ast_argument **arg ATTR_UNUSED,
137
      struct sieve_command *cmd ATTR_UNUSED)
138
0
{
139
0
  *arg = sieve_ast_argument_next(*arg);
140
141
0
  return TRUE;
142
0
}
143
144
/*
145
 * Code generation
146
 */
147
148
static bool
149
tag_copy_generate(const struct sieve_codegen_env *cgenv,
150
      struct sieve_ast_argument *arg,
151
      struct sieve_command *cmd ATTR_UNUSED)
152
0
{
153
0
  if (sieve_ast_argument_type(arg) != SAAT_TAG)
154
0
    return FALSE;
155
156
0
  sieve_opr_side_effect_emit(cgenv->sblock, sieve_argument_ext(arg),
157
0
           &copy_side_effect);
158
0
  return TRUE;
159
0
}
160
161
/*
162
 * Side effect implementation
163
 */
164
165
static void
166
seff_copy_print(const struct sieve_side_effect *seffect ATTR_UNUSED,
167
    const struct sieve_action *action ATTR_UNUSED,
168
    const struct sieve_result_print_env *rpenv, bool *keep)
169
0
{
170
0
  sieve_result_seffect_printf(rpenv, "preserve implicit keep");
171
0
  *keep = TRUE;
172
0
}
173
174
static int
175
seff_copy_post_execute(const struct sieve_side_effect *seffect ATTR_UNUSED,
176
           const struct sieve_action_exec_env *aenv ATTR_UNUSED,
177
           void *tr_context ATTR_UNUSED,
178
           void *se_tr_context ATTR_UNUSED, bool *keep)
179
0
{
180
0
  *keep = TRUE;
181
0
  return SIEVE_EXEC_OK;
182
0
}