1 /* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
2 */
3
4 #include "lib.h"
5
6 #include "sieve-common.h"
7 #include "sieve-commands.h"
8 #include "sieve-extensions.h"
9 #include "sieve-validator.h"
10 #include "sieve-generator.h"
11
12 /*
13 * Require command
14 *
15 * Syntax
16 * Syntax: require <capabilities: string-list>
17 */
18
19 static bool cmd_require_validate
20 (struct sieve_validator *valdtr, struct sieve_command *cmd);
21
22 const struct sieve_command_def cmd_require = {
23 .identifier = "require",
24 .type = SCT_COMMAND,
25 .positional_args = 1,
26 .subtests = 0,
27 .block_allowed = FALSE,
28 .block_required = FALSE,
29 .validate = cmd_require_validate
30 };
31
32 /*
33 * Validation
34 */
35
cmd_require_validate(struct sieve_validator * valdtr,struct sieve_command * cmd)36 static bool cmd_require_validate
37 (struct sieve_validator *valdtr, struct sieve_command *cmd)
38 {
39 bool result = TRUE;
40 struct sieve_ast_argument *arg;
41 struct sieve_command *prev = sieve_command_prev(cmd);
42
43 /* Check valid command placement */
44 if ( !sieve_command_is_toplevel(cmd) ||
45 ( !sieve_command_is_first(cmd) && prev != NULL &&
46 !sieve_command_is(prev, cmd_require) ) )
47 {
48 sieve_command_validate_error(valdtr, cmd,
49 "require commands can only be placed at top level "
50 "at the beginning of the file");
51 return FALSE;
52 }
53
54 /* Check argument and load specified extension(s) */
55
56 arg = cmd->first_positional;
57 if ( sieve_ast_argument_type(arg) == SAAT_STRING ) {
58 /* Single string */
59 const struct sieve_extension *ext = sieve_validator_extension_load_by_name
60 (valdtr, cmd, arg, sieve_ast_argument_strc(arg));
61
62 if ( ext == NULL ) result = FALSE;
63
64 } else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) {
65 /* String list */
66 struct sieve_ast_argument *stritem = sieve_ast_strlist_first(arg);
67
68 while ( stritem != NULL ) {
69 const struct sieve_extension *ext = sieve_validator_extension_load_by_name
70 (valdtr, cmd, stritem, sieve_ast_strlist_strc(stritem));
71
72 if ( ext == NULL ) result = FALSE;
73
74 stritem = sieve_ast_strlist_next(stritem);
75 }
76 } else {
77 /* Something else */
78 sieve_argument_validate_error(valdtr, arg,
79 "the require command accepts a single string or string list argument, "
80 "but %s was found",
81 sieve_ast_argument_name(arg));
82 return FALSE;
83 }
84
85 return result;
86 }
87