1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/* Copyright (c) 2002-2010 Dovecot Sieve authors, see the included COPYING file
*/
#include "lib.h"
#include "mail-storage.h"
#include "mail-namespace.h"
#include "sieve-common.h"
#include "sieve-commands.h"
#include "sieve-code.h"
#include "sieve-actions.h"
#include "sieve-result.h"
#include "sieve-generator.h"
#include "ext-mailbox-common.h"
/*
* Tagged argument
*/
static bool tag_mailbox_create_validate
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
struct sieve_command *cmd);
static bool tag_mailbox_create_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
struct sieve_command *context);
const struct sieve_argument_def mailbox_create_tag = {
"create",
NULL,
tag_mailbox_create_validate,
NULL, NULL,
tag_mailbox_create_generate
};
/*
* Side effect
*/
static void seff_mailbox_create_print
(const struct sieve_side_effect *seffect, const struct sieve_action *action,
const struct sieve_result_print_env *rpenv, bool *keep);
static bool seff_mailbox_create_pre_execute
(const struct sieve_side_effect *seffect, const struct sieve_action *action,
const struct sieve_action_exec_env *aenv, void **se_context,
void *tr_context);;
const struct sieve_side_effect_def mailbox_create_side_effect = {
SIEVE_OBJECT("create", &mailbox_create_operand, 0),
&act_store,
NULL, NULL, NULL,
seff_mailbox_create_print,
seff_mailbox_create_pre_execute,
NULL, NULL, NULL
};
/*
* Operand
*/
static const struct sieve_extension_objects ext_side_effects =
SIEVE_EXT_DEFINE_SIDE_EFFECT(mailbox_create_side_effect);
const struct sieve_operand_def mailbox_create_operand = {
"create operand",
&mailbox_extension,
0,
&sieve_side_effect_operand_class,
&ext_side_effects
};
/*
* Tag validation
*/
static bool tag_mailbox_create_validate
(struct sieve_validator *valdtr ATTR_UNUSED,
struct sieve_ast_argument **arg, struct sieve_command *cmd ATTR_UNUSED)
{
*arg = sieve_ast_argument_next(*arg);
return TRUE;
}
/*
* Code generation
*/
static bool tag_mailbox_create_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
struct sieve_command *context ATTR_UNUSED)
{
if ( sieve_ast_argument_type(arg) != SAAT_TAG ) {
return FALSE;
}
sieve_opr_side_effect_emit
(cgenv->sbin, arg->argument->ext, &mailbox_create_side_effect);
return TRUE;
}
/*
* Side effect implementation
*/
static void seff_mailbox_create_print
(const struct sieve_side_effect *seffect ATTR_UNUSED,
const struct sieve_action *action ATTR_UNUSED,
const struct sieve_result_print_env *rpenv, bool *keep ATTR_UNUSED)
{
sieve_result_seffect_printf(rpenv, "create mailbox if it does not exist");
}
static bool seff_mailbox_create_pre_execute
(const struct sieve_side_effect *seffect ATTR_UNUSED,
const struct sieve_action *action ATTR_UNUSED,
const struct sieve_action_exec_env *aenv ATTR_UNUSED,
void **se_context ATTR_UNUSED, void *tr_context ATTR_UNUSED)
{
struct act_store_transaction *trans =
(struct act_store_transaction *) tr_context;
struct mail_storage **storage = &(aenv->exec_status->last_storage);
enum mailbox_open_flags open_flags =
MAILBOX_OPEN_FAST | MAILBOX_OPEN_KEEP_RECENT |
MAILBOX_OPEN_SAVEONLY | MAILBOX_OPEN_POST_SESSION;
struct mailbox *box = NULL;
/* Check whether creation is necessary */
if ( trans->box != NULL || trans->redundant || trans->disabled )
return TRUE;
/* Check availability of namespace and folder name */
if ( trans->namespace == NULL || trans->folder == NULL )
return FALSE;
/* Check whether creation has a chance of working */
if ( trans->error_code != MAIL_ERROR_NONE
&& trans->error_code != MAIL_ERROR_NOTFOUND )
return FALSE;
*storage = trans->namespace->storage;
/* Create mailbox */
if ( mail_storage_mailbox_create(*storage, trans->folder, FALSE) < 0 ) {
box = NULL;
} else {
/* Subscribe to it if necessary */
if ( aenv->scriptenv->mailbox_autosubscribe ) {
(void)mailbox_list_set_subscribed
(trans->namespace->list, trans->folder, TRUE);
}
/* Open it */
box = mailbox_open(storage, trans->folder, NULL, open_flags);
if ( box != NULL && mailbox_sync(box, 0, 0, NULL) < 0 ) {
mailbox_close(&box);
box = NULL;
}
}
/* Fetch error */
if ( box == NULL )
sieve_act_store_get_storage_error(aenv, trans);
trans->box = box;
return ( box != NULL );
}
|