~ubuntu-branches/ubuntu/utopic/dovecot/utopic-proposed

« back to all changes in this revision

Viewing changes to src/doveadm/doveadm-mail-batch.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-08 09:35:49 UTC
  • mfrom: (4.1.35 sid)
  • Revision ID: package-import@ubuntu.com-20140108093549-i72o93pux8p0dlaf
Tags: 1:2.2.9-1ubuntu1
* Merge from Debian unstable, remaining changes:
  + Add mail-stack-delivery package:
    - Update d/rules
    - d/control: convert existing dovecot-postfix package to a dummy
      package and add new mail-stack-delivery package.
    - Update maintainer scripts.
    - Rename d/dovecot-postfix.* to debian/mail-stack-delivery.*
    - d/mail-stack-delivery.preinst: Move previously installed backups and
      config files to a new package namespace.
    - d/mail-stack-delivery.prerm: Added to handle downgrades.
  + Use Snakeoil SSL certificates by default:
    - d/control: Depend on ssl-cert.
    - d/dovecot-core.postinst: Relax grep for SSL_* a bit.
  + Add autopkgtest to debian/tests/*.
  + Add ufw integration:
    - d/dovecot-core.ufw.profile: new ufw profile.
    - d/rules: install profile in dovecot-core.
    - d/control: dovecot-core - suggest ufw.
  + d/dovecot-core.dirs: Added usr/share/doc/dovecot-core
  + Add apport hook:
    - d/rules, d/source_dovecot.py
  + Add upstart job:
    - d/rules, d/dovecot-core.dovecot.upstart, d/control,
      d/dovecot-core.dirs, dovecot-imapd.{postrm, postinst, prerm},
      d/dovecot-pop3d.{postinst, postrm, prerm}.
      d/mail-stack-deliver.postinst: Convert init script to upstart.
  + Use the autotools-dev dh addon to update config.guess/config.sub for
    arm64.
* Dropped changes, included in Debian:
  - Update Dovecot name to reflect distribution in login greeting.
  - Update Drac plugin for >= 2.0.0 support.
* d/control: Drop dovecot-postfix package as its no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2012 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "array.h"
 
5
#include "doveadm-mail.h"
 
6
 
 
7
#include <unistd.h>
 
8
 
 
9
struct batch_cmd_context {
 
10
        struct doveadm_mail_cmd_context ctx;
 
11
        ARRAY(struct doveadm_mail_cmd_context *) commands;
 
12
};
 
13
 
 
14
static int cmd_batch_prerun(struct doveadm_mail_cmd_context *_ctx,
 
15
                            struct mail_storage_service_user *service_user,
 
16
                            const char **error_r)
 
17
{
 
18
        struct batch_cmd_context *ctx = (struct batch_cmd_context *)_ctx;
 
19
        struct doveadm_mail_cmd_context *const *cmdp;
 
20
        int ret = 0;
 
21
 
 
22
        array_foreach(&ctx->commands, cmdp) {
 
23
                if ((*cmdp)->v.prerun != NULL &&
 
24
                    (*cmdp)->v.prerun(*cmdp, service_user, error_r) < 0) {
 
25
                        ret = -1;
 
26
                        break;
 
27
                }
 
28
        }
 
29
        return ret;
 
30
}
 
31
 
 
32
static int cmd_batch_run(struct doveadm_mail_cmd_context *_ctx,
 
33
                         struct mail_user *user)
 
34
{
 
35
        struct batch_cmd_context *ctx = (struct batch_cmd_context *)_ctx;
 
36
        struct doveadm_mail_cmd_context *const *cmdp;
 
37
        int ret = 0;
 
38
 
 
39
        array_foreach(&ctx->commands, cmdp) {
 
40
                (*cmdp)->cur_mail_user = user;
 
41
                if ((*cmdp)->v.run(*cmdp, user) < 0) {
 
42
                        i_assert((*cmdp)->exit_code != 0);
 
43
                        _ctx->exit_code = (*cmdp)->exit_code;
 
44
                        ret = -1;
 
45
                        break;
 
46
                }
 
47
                (*cmdp)->cur_mail_user = NULL;
 
48
        }
 
49
        return ret;
 
50
}
 
51
 
 
52
static void
 
53
cmd_batch_add(struct batch_cmd_context *batchctx,
 
54
              int argc, const char *const *argv)
 
55
{
 
56
        struct doveadm_mail_cmd_context *subctx;
 
57
        const struct doveadm_mail_cmd *cmd;
 
58
        const char *getopt_args;
 
59
        int c;
 
60
 
 
61
        cmd = doveadm_mail_cmd_find_from_argv(argv[0], &argc, &argv);
 
62
        if (cmd == NULL) {
 
63
                i_fatal_status(EX_USAGE, "doveadm batch: Unknown subcommand %s",
 
64
                               argv[1]);
 
65
        }
 
66
 
 
67
        subctx = doveadm_mail_cmd_init(cmd, doveadm_settings);
 
68
        subctx->full_args = argv + 1;
 
69
        subctx->service_flags |= batchctx->ctx.service_flags;
 
70
 
 
71
        optind = 1;
 
72
        getopt_args = subctx->getopt_args != NULL ? subctx->getopt_args : "";
 
73
        while ((c = getopt(argc, (void *)argv, getopt_args)) > 0) {
 
74
                if (subctx->v.parse_arg == NULL ||
 
75
                    !subctx->v.parse_arg(subctx, c))
 
76
                        doveadm_mail_help(cmd);
 
77
        }
 
78
        argv += optind;
 
79
        if (argv[0] != NULL && cmd->usage_args == NULL) {
 
80
                i_fatal_status(EX_USAGE, "doveadm %s: Unknown parameter: %s",
 
81
                               cmd->name, argv[0]);
 
82
        }
 
83
        subctx->args = argv;
 
84
        if (subctx->v.preinit != NULL)
 
85
                subctx->v.preinit(subctx);
 
86
        array_append(&batchctx->commands, &subctx, 1);
 
87
}
 
88
 
 
89
static void
 
90
cmd_batch_preinit(struct doveadm_mail_cmd_context *_ctx)
 
91
{
 
92
        const char *const *args = _ctx->args;
 
93
        struct batch_cmd_context *ctx = (struct batch_cmd_context *)_ctx;
 
94
        ARRAY_TYPE(const_string) sep_args;
 
95
        const char *sep = args[0];
 
96
        unsigned int i, start;
 
97
        int argc;
 
98
        const char *const *argv;
 
99
 
 
100
        if (sep == NULL || args[1] == NULL)
 
101
                doveadm_mail_help_name("batch");
 
102
        args++;
 
103
 
 
104
        p_array_init(&ctx->commands, _ctx->pool, 8);
 
105
        p_array_init(&sep_args, _ctx->pool, 16);
 
106
        for (i = start = 0;; i++) {
 
107
                if (args[i] != NULL && strcmp(args[i], sep) != 0) {
 
108
                        array_append(&sep_args, &args[i], 1);
 
109
                        continue;
 
110
                }
 
111
                if (i > start) {
 
112
                        (void)array_append_space(&sep_args);
 
113
                        argc = i - start;
 
114
                        argv = array_idx(&sep_args, start);
 
115
                        cmd_batch_add(ctx, argc, argv);
 
116
                        start = i+1;
 
117
                }
 
118
                if (args[i] == NULL)
 
119
                        break;
 
120
        }
 
121
        (void)array_append_space(&sep_args);
 
122
}
 
123
 
 
124
static void
 
125
cmd_batch_init(struct doveadm_mail_cmd_context *_ctx,
 
126
               const char *const args[] ATTR_UNUSED)
 
127
{
 
128
        struct batch_cmd_context *ctx = (struct batch_cmd_context *)_ctx;
 
129
        struct doveadm_mail_cmd_context *const *cmdp;
 
130
        struct batch_cmd_context *subctx;
 
131
 
 
132
        array_foreach(&ctx->commands, cmdp) {
 
133
                subctx = (struct batch_cmd_context *)*cmdp;
 
134
                subctx->ctx.storage_service = _ctx->storage_service;
 
135
                if (subctx->ctx.v.init != NULL)
 
136
                        subctx->ctx.v.init(&subctx->ctx, subctx->ctx.args);
 
137
        }
 
138
}
 
139
 
 
140
static void cmd_batch_deinit(struct doveadm_mail_cmd_context *_ctx)
 
141
{
 
142
        struct batch_cmd_context *ctx = (struct batch_cmd_context *)_ctx;
 
143
        struct doveadm_mail_cmd_context *const *cmdp;
 
144
 
 
145
        array_foreach(&ctx->commands, cmdp) {
 
146
                if ((*cmdp)->v.deinit != NULL)
 
147
                        (*cmdp)->v.deinit(*cmdp);
 
148
        }
 
149
}
 
150
 
 
151
static struct doveadm_mail_cmd_context *cmd_batch_alloc(void)
 
152
{
 
153
        struct batch_cmd_context *ctx;
 
154
 
 
155
        ctx = doveadm_mail_cmd_alloc(struct batch_cmd_context);
 
156
        ctx->ctx.getopt_args = "+"; /* disable processing -args in the middle */
 
157
        ctx->ctx.v.preinit = cmd_batch_preinit;
 
158
        ctx->ctx.v.init = cmd_batch_init;
 
159
        ctx->ctx.v.prerun = cmd_batch_prerun;
 
160
        ctx->ctx.v.run = cmd_batch_run;
 
161
        ctx->ctx.v.deinit = cmd_batch_deinit;
 
162
        return &ctx->ctx;
 
163
}
 
164
 
 
165
struct doveadm_mail_cmd cmd_batch = {
 
166
        cmd_batch_alloc, "batch", "<sep> <cmd1> [<sep> <cmd2> [..]]"
 
167
};