/src/pigeonhole/src/lib-sieve/cmd-discard.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "str-sanitize.h" |
5 | | |
6 | | #include "sieve-common.h" |
7 | | #include "sieve-commands.h" |
8 | | #include "sieve-code.h" |
9 | | #include "sieve-dump.h" |
10 | | #include "sieve-actions.h" |
11 | | #include "sieve-validator.h" |
12 | | #include "sieve-generator.h" |
13 | | #include "sieve-interpreter.h" |
14 | | #include "sieve-result.h" |
15 | | |
16 | | /* |
17 | | * Discard command |
18 | | * |
19 | | * Syntax |
20 | | * discard |
21 | | */ |
22 | | |
23 | | static bool |
24 | | cmd_discard_generate(const struct sieve_codegen_env *cgenv, |
25 | | struct sieve_command *ctx ATTR_UNUSED); |
26 | | |
27 | | const struct sieve_command_def cmd_discard = { |
28 | | .identifier = "discard", |
29 | | .type = SCT_COMMAND, |
30 | | .positional_args = 0, |
31 | | .subtests = 0, |
32 | | .block_allowed = FALSE, |
33 | | .block_required = FALSE, |
34 | | .generate = cmd_discard_generate |
35 | | }; |
36 | | |
37 | | /* |
38 | | * Discard operation |
39 | | */ |
40 | | |
41 | | static bool |
42 | | cmd_discard_operation_dump(const struct sieve_dumptime_env *denv, |
43 | | sieve_size_t *address); |
44 | | static int |
45 | | cmd_discard_operation_execute(const struct sieve_runtime_env *renv, |
46 | | sieve_size_t *address); |
47 | | |
48 | | const struct sieve_operation_def cmd_discard_operation = { |
49 | | .mnemonic = "DISCARD", |
50 | | .code = SIEVE_OPERATION_DISCARD, |
51 | | .dump = cmd_discard_operation_dump, |
52 | | .execute = cmd_discard_operation_execute |
53 | | }; |
54 | | |
55 | | /* |
56 | | * Discard actions |
57 | | */ |
58 | | |
59 | | static bool |
60 | | act_discard_equals(const struct sieve_script_env *senv, |
61 | | const struct sieve_action *act1, |
62 | | const struct sieve_action *act2); |
63 | | static int |
64 | | act_discard_check_duplicate(const struct sieve_runtime_env *renv, |
65 | | const struct sieve_action *act, |
66 | | const struct sieve_action *act_other); |
67 | | static void |
68 | | act_discard_print(const struct sieve_action *action, |
69 | | const struct sieve_result_print_env *rpenv, bool *keep); |
70 | | static int |
71 | | act_discard_execute(const struct sieve_action_exec_env *aenv, void *tr_context, |
72 | | bool *keep); |
73 | | |
74 | | const struct sieve_action_def act_discard = { |
75 | | .name = "discard", |
76 | | .equals = act_discard_equals, |
77 | | .check_duplicate = act_discard_check_duplicate, |
78 | | .print = act_discard_print, |
79 | | .execute = act_discard_execute, |
80 | | }; |
81 | | |
82 | | /* |
83 | | * Code generation |
84 | | */ |
85 | | |
86 | | static bool |
87 | | cmd_discard_generate(const struct sieve_codegen_env *cgenv, |
88 | | struct sieve_command *cmd ATTR_UNUSED) |
89 | 0 | { |
90 | 0 | sieve_operation_emit(cgenv->sblock, NULL, &cmd_discard_operation); |
91 | |
|
92 | 0 | return TRUE; |
93 | 0 | } |
94 | | |
95 | | /* |
96 | | * Code dump |
97 | | */ |
98 | | |
99 | | static bool |
100 | | cmd_discard_operation_dump(const struct sieve_dumptime_env *denv, |
101 | | sieve_size_t *address) |
102 | 0 | { |
103 | 0 | sieve_code_dumpf(denv, "DISCARD"); |
104 | 0 | sieve_code_descend(denv); |
105 | |
|
106 | 0 | return (sieve_action_opr_optional_dump(denv, address, NULL) == 0); |
107 | 0 | } |
108 | | |
109 | | /* |
110 | | * Interpretation |
111 | | */ |
112 | | |
113 | | static int |
114 | | cmd_discard_operation_execute(const struct sieve_runtime_env *renv ATTR_UNUSED, |
115 | | sieve_size_t *address ATTR_UNUSED) |
116 | 0 | { |
117 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_ACTIONS, |
118 | 0 | "discard action; cancel implicit keep"); |
119 | |
|
120 | 0 | if (sieve_result_add_action(renv, NULL, "discard", &act_discard, |
121 | 0 | NULL, NULL, 0, FALSE) < 0) |
122 | 0 | return SIEVE_EXEC_FAILURE; |
123 | 0 | return SIEVE_EXEC_OK; |
124 | 0 | } |
125 | | |
126 | | /* |
127 | | * Action implementation |
128 | | */ |
129 | | |
130 | | static bool |
131 | | act_discard_equals(const struct sieve_script_env *senv ATTR_UNUSED, |
132 | | const struct sieve_action *act1 ATTR_UNUSED, |
133 | | const struct sieve_action *act2 ATTR_UNUSED) |
134 | 0 | { |
135 | 0 | return TRUE; |
136 | 0 | } |
137 | | |
138 | | static int |
139 | | act_discard_check_duplicate(const struct sieve_runtime_env *renv ATTR_UNUSED, |
140 | | const struct sieve_action *act ATTR_UNUSED, |
141 | | const struct sieve_action *act_other ATTR_UNUSED) |
142 | 0 | { |
143 | 0 | return 1; |
144 | 0 | } |
145 | | |
146 | | static void |
147 | | act_discard_print(const struct sieve_action *action ATTR_UNUSED, |
148 | | const struct sieve_result_print_env *rpenv, bool *keep) |
149 | 0 | { |
150 | 0 | sieve_result_action_printf(rpenv, "discard"); |
151 | |
|
152 | 0 | *keep = FALSE; |
153 | 0 | } |
154 | | |
155 | | static int |
156 | | act_discard_execute(const struct sieve_action_exec_env *aenv, |
157 | | void *tr_context ATTR_UNUSED, bool *keep) |
158 | 0 | { |
159 | 0 | const struct sieve_execute_env *eenv = aenv->exec_env; |
160 | |
|
161 | 0 | eenv->exec_status->significant_action_executed = TRUE; |
162 | |
|
163 | 0 | struct event_passthrough *e = sieve_action_create_finish_event(aenv); |
164 | |
|
165 | 0 | sieve_result_event_log(aenv, e->event(), |
166 | 0 | "Marked message to be discarded if not explicitly delivered " |
167 | 0 | "(discard action)"); |
168 | 0 | *keep = FALSE; |
169 | |
|
170 | 0 | return SIEVE_EXEC_OK; |
171 | 0 | } |
172 | | |