/src/pigeonhole/src/lib-sieve/plugins/mime/ext-extracttext.c
Line | Count | Source |
1 | | /* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file |
2 | | */ |
3 | | |
4 | | /* Extension extracttext |
5 | | * --------------------- |
6 | | * |
7 | | * Authors: Stephan Bosch |
8 | | * Specification: RFC 5703, Section 7 |
9 | | * Implementation: full |
10 | | * Status: experimental |
11 | | * |
12 | | */ |
13 | | |
14 | | #include "sieve-common.h" |
15 | | |
16 | | #include "sieve-code.h" |
17 | | #include "sieve-extensions.h" |
18 | | #include "sieve-actions.h" |
19 | | #include "sieve-commands.h" |
20 | | #include "sieve-validator.h" |
21 | | #include "sieve-generator.h" |
22 | | #include "sieve-interpreter.h" |
23 | | #include "sieve-result.h" |
24 | | |
25 | | #include "sieve-ext-variables.h" |
26 | | |
27 | | #include "ext-mime-common.h" |
28 | | |
29 | | /* |
30 | | * Extension |
31 | | */ |
32 | | |
33 | | static int |
34 | | ext_extracttext_load(const struct sieve_extension *ext, void **context_r); |
35 | | static void |
36 | | ext_extracttext_unload(const struct sieve_extension *ext); |
37 | | static bool |
38 | | ext_extracttext_validator_load(const struct sieve_extension *ext, |
39 | | struct sieve_validator *valdtr); |
40 | | |
41 | | const struct sieve_extension_def extracttext_extension = { |
42 | | .name = "extracttext", |
43 | | .load = ext_extracttext_load, |
44 | | .unload = ext_extracttext_unload, |
45 | | .validator_load = ext_extracttext_validator_load, |
46 | | SIEVE_EXT_DEFINE_OPERATION(extracttext_operation), |
47 | | }; |
48 | | |
49 | | static int |
50 | | ext_extracttext_load(const struct sieve_extension *ext, void **context_r) |
51 | 0 | { |
52 | 0 | struct sieve_instance *svinst = ext->svinst; |
53 | 0 | const struct sieve_extension *var_ext; |
54 | 0 | const struct sieve_extension *fep_ext; |
55 | 0 | struct ext_extracttext_context *extctx; |
56 | |
|
57 | 0 | if (sieve_ext_variables_get_extension(ext->svinst, &var_ext) < 0) |
58 | 0 | return -1; |
59 | 0 | if (sieve_extension_register(svinst, &foreverypart_extension, FALSE, |
60 | 0 | &fep_ext) < 0) |
61 | 0 | return -1; |
62 | | |
63 | 0 | extctx = i_new(struct ext_extracttext_context, 1); |
64 | 0 | extctx->var_ext = var_ext; |
65 | 0 | extctx->fep_ext = fep_ext; |
66 | |
|
67 | 0 | *context_r = extctx; |
68 | 0 | return 0; |
69 | 0 | } |
70 | | |
71 | | static void ext_extracttext_unload(const struct sieve_extension *ext) |
72 | 0 | { |
73 | 0 | struct ext_extracttext_context *extctx = ext->context; |
74 | |
|
75 | 0 | i_free(extctx); |
76 | 0 | } |
77 | | |
78 | | /* |
79 | | * Extension validation |
80 | | */ |
81 | | |
82 | | static bool |
83 | | ext_extracttext_validator_validate( |
84 | | const struct sieve_extension *ext, struct sieve_validator *valdtr, |
85 | | void *context, struct sieve_ast_argument *require_arg, bool required); |
86 | | |
87 | | const struct sieve_validator_extension |
88 | | extracttext_validator_extension = { |
89 | | .ext = &extracttext_extension, |
90 | | .validate = ext_extracttext_validator_validate, |
91 | | }; |
92 | | |
93 | | static bool |
94 | | ext_extracttext_validator_load(const struct sieve_extension *ext, |
95 | | struct sieve_validator *valdtr) |
96 | 0 | { |
97 | | /* Register validator extension to check for conflict with eextracttext. |
98 | | */ |
99 | 0 | sieve_validator_extension_register( |
100 | 0 | valdtr, ext, &extracttext_validator_extension, NULL); |
101 | | |
102 | | /* Register new commands */ |
103 | 0 | sieve_validator_register_command(valdtr, ext, &cmd_extracttext); |
104 | |
|
105 | 0 | return TRUE; |
106 | 0 | } |
107 | | |
108 | | static bool |
109 | | ext_extracttext_validator_validate(const struct sieve_extension *ext, |
110 | | struct sieve_validator *valdtr, |
111 | | void *context ATTR_UNUSED, |
112 | | struct sieve_ast_argument *require_arg, |
113 | | bool required ATTR_UNUSED) |
114 | 0 | { |
115 | 0 | struct ext_extracttext_context *extctx = ext->context; |
116 | |
|
117 | 0 | if (extctx->var_ext == NULL || |
118 | 0 | !sieve_ext_variables_is_active(extctx->var_ext, valdtr) ) { |
119 | 0 | sieve_argument_validate_error( |
120 | 0 | valdtr, require_arg, |
121 | 0 | "extracttext extension cannot be used " |
122 | 0 | "without variables extension"); |
123 | 0 | return FALSE; |
124 | 0 | } |
125 | 0 | if (extctx->fep_ext == NULL || |
126 | 0 | !sieve_validator_extension_loaded(valdtr, extctx->fep_ext) ) { |
127 | 0 | sieve_argument_validate_error( |
128 | 0 | valdtr, require_arg, |
129 | 0 | "extracttext extension cannot be used " |
130 | 0 | "without foreverypart extension"); |
131 | 0 | return FALSE; |
132 | 0 | } |
133 | 0 | return TRUE; |
134 | 0 | } |