1
/* Copyright (c) 2002-2009 Dovecot Sieve authors, see the included COPYING file
5
#include "mail-storage.h"
6
#include "mail-namespace.h"
8
#include "sieve-common.h"
9
#include "sieve-commands.h"
10
#include "sieve-code.h"
11
#include "sieve-actions.h"
12
#include "sieve-result.h"
13
#include "sieve-generator.h"
15
#include "ext-mailbox-common.h"
21
static bool tag_mailbox_create_validate
22
(struct sieve_validator *validator, struct sieve_ast_argument **arg,
23
struct sieve_command_context *cmd);
24
static bool tag_mailbox_create_generate
25
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
26
struct sieve_command_context *context);
28
const struct sieve_argument mailbox_create_tag = {
31
tag_mailbox_create_validate,
33
tag_mailbox_create_generate
40
static void seff_mailbox_create_print
41
(const struct sieve_side_effect *seffect, const struct sieve_action *action,
42
const struct sieve_result_print_env *rpenv, void *se_context, bool *keep);
43
static bool seff_mailbox_create_pre_execute
44
(const struct sieve_side_effect *seffect, const struct sieve_action *action,
45
const struct sieve_action_exec_env *aenv, void **se_context,
48
const struct sieve_side_effect mailbox_create_side_effect = {
49
SIEVE_OBJECT("create", &mailbox_create_operand, 0),
52
seff_mailbox_create_print,
53
seff_mailbox_create_pre_execute,
61
static const struct sieve_extension_objects ext_side_effects =
62
SIEVE_EXT_DEFINE_SIDE_EFFECT(mailbox_create_side_effect);
64
const struct sieve_operand mailbox_create_operand = {
68
&sieve_side_effect_operand_class,
76
static bool tag_mailbox_create_validate
77
(struct sieve_validator *validator ATTR_UNUSED,
78
struct sieve_ast_argument **arg ATTR_UNUSED,
79
struct sieve_command_context *cmd ATTR_UNUSED)
81
*arg = sieve_ast_argument_next(*arg);
90
static bool tag_mailbox_create_generate
91
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
92
struct sieve_command_context *context ATTR_UNUSED)
94
if ( sieve_ast_argument_type(arg) != SAAT_TAG ) {
98
sieve_opr_side_effect_emit(cgenv->sbin, &mailbox_create_side_effect);
104
* Side effect implementation
107
static void seff_mailbox_create_print
108
(const struct sieve_side_effect *seffect ATTR_UNUSED,
109
const struct sieve_action *action ATTR_UNUSED,
110
const struct sieve_result_print_env *rpenv,
111
void *se_context ATTR_UNUSED, bool *keep ATTR_UNUSED)
113
sieve_result_seffect_printf(rpenv, "create mailbox if it does not exist");
116
static bool seff_mailbox_create_pre_execute
117
(const struct sieve_side_effect *seffect ATTR_UNUSED,
118
const struct sieve_action *action ATTR_UNUSED,
119
const struct sieve_action_exec_env *aenv ATTR_UNUSED,
120
void **se_context ATTR_UNUSED, void *tr_context ATTR_UNUSED)
122
struct act_store_transaction *trans =
123
(struct act_store_transaction *) tr_context;
124
struct mail_storage **storage = &(aenv->exec_status->last_storage);
125
enum mailbox_open_flags open_flags =
126
MAILBOX_OPEN_FAST | MAILBOX_OPEN_KEEP_RECENT |
127
MAILBOX_OPEN_SAVEONLY | MAILBOX_OPEN_POST_SESSION;
128
struct mailbox *box = NULL;
130
/* Check whether creation is necessary */
131
if ( trans->box != NULL || trans->redundant || trans->disabled )
134
/* Check availability of namespace and folder name */
135
if ( trans->namespace == NULL || trans->folder == NULL )
138
/* Check whether creation has a chance of working */
139
if ( trans->error_code != MAIL_ERROR_NONE
140
&& trans->error_code != MAIL_ERROR_NOTFOUND )
143
*storage = trans->namespace->storage;
146
if ( mail_storage_mailbox_create(*storage, trans->folder, FALSE) < 0 ) {
150
/* Subscribe to it if necessary */
151
if ( aenv->scriptenv->mailbox_autosubscribe ) {
152
(void)mailbox_list_set_subscribed
153
(trans->namespace->list, trans->folder, TRUE);
157
box = mailbox_open(storage, trans->folder, NULL, open_flags);
159
if ( box != NULL && mailbox_sync(box, 0, 0, NULL) < 0 ) {
167
sieve_act_store_get_storage_error(aenv, trans);
171
return ( box != NULL );