doveadm-mail.h revision 42681892b206d13cb87a5f526d2bf4ff3f2f4af7
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen#ifndef DOVEADM_MAIL_H
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen#define DOVEADM_MAIL_H
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen#include <stdio.h>
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen#include "doveadm.h"
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen#include "doveadm-util.h"
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen#include "module-context.h"
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen#include "mail-error.h"
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen#include "mail-storage-service.h"
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenstruct mailbox;
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenstruct mailbox_list;
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenstruct mail_storage;
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenstruct mail_user;
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenstruct doveadm_mail_cmd_context;
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenstruct doveadm_mail_cmd_vfuncs {
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen /* Parse one getopt() parameter. This is called for each parameter. */
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen bool (*parse_arg)(struct doveadm_mail_cmd_context *ctx, int c);
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen /* Usually not needed. The preinit() is called just after parsing all
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen parameters, but before any userdb lookups are done. This allows the
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen preinit() to alter the userdb lookup behavior (especially
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen service_flags). */
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen void (*preinit)(struct doveadm_mail_cmd_context *ctx);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen /* Initialize the command. Most importantly if the function prints
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen anything, this should initialize the headers. It shouldn't however
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen do any actual work. The init() is called also when doveadm is
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen performing the work via doveadm-server, which could be running
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen remotely with completely different Dovecot configuration. */
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen void (*init)(struct doveadm_mail_cmd_context *ctx,
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen const char *const args[]);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen /* Usually not needed. When iterating through multiple users, use this
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen function to get the next username. Overriding this is usually done
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen only when there's a known username filter, such as the expire
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen plugin. */
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen int (*get_next_user)(struct doveadm_mail_cmd_context *ctx,
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen const char **username_r);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen /* Usually not needed. This is called between
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen mail_storage_service_lookup() and mail_storage_service_next() for
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen each user. */
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen int (*prerun)(struct doveadm_mail_cmd_context *ctx,
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen struct mail_storage_service_user *service_user,
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen const char **error_r);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen /* This is the main function which performs all the work for the
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen command. This is called once per each user. */
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen int (*run)(struct doveadm_mail_cmd_context *ctx,
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen struct mail_user *mail_user);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen /* Deinitialize the command. Called once at the end. */
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen void (*deinit)(struct doveadm_mail_cmd_context *ctx);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen};
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenstruct doveadm_mail_cmd_module_register {
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen unsigned int id;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen};
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenunion doveadm_mail_cmd_module_context {
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen struct doveadm_mail_cmd_vfuncs super;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen struct doveadm_mail_cmd_module_register *reg;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen};
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenstruct doveadm_mail_cmd_context {
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen pool_t pool;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen const struct doveadm_mail_cmd *cmd;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen const char *const *args;
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi /* args including -options */
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen const char *const *full_args;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen /* connection via doveadm-server */
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen struct client_connection *conn;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen const char *getopt_args;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen const struct doveadm_settings *set;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen enum mail_storage_service_flags service_flags;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen struct mail_storage_service_ctx *storage_service;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen struct mail_storage_service_input storage_service_input;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen /* search args aren't set for all mail commands */
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen struct mail_search_args *search_args;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen struct ip_addr cur_client_ip;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen const char *cur_username;
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen struct mail_storage_service_user *cur_service_user;
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen struct mail_user *cur_mail_user;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen struct doveadm_mail_cmd_vfuncs v;
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen ARRAY(union doveadm_mail_cmd_module_context *) module_contexts;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen /* if non-zero, exit with this code */
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen int exit_code;
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen /* This command is being called by a remote doveadm client. */
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen unsigned int proxying:1;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* We're handling only a single user */
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen unsigned int iterate_single_user:1;
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen /* We're going through all users (not set for wildcard usernames) */
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen unsigned int iterate_all_users:1;
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen};
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainenstruct doveadm_mail_cmd {
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen struct doveadm_mail_cmd_context *(*alloc)(void);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen const char *name;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen const char *usage_args;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen};
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo SirainenARRAY_DEFINE_TYPE(doveadm_mail_cmd, struct doveadm_mail_cmd);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainenextern ARRAY_TYPE(doveadm_mail_cmd) doveadm_mail_cmds;
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainenextern void (*hook_doveadm_mail_init)(struct doveadm_mail_cmd_context *ctx);
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainenextern struct doveadm_mail_cmd_module_register doveadm_mail_cmd_module_register;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainenextern char doveadm_mail_cmd_hide;
431c0aba57fba2fd6eb398acd6ef4e73b5dc1b86Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenbool doveadm_mail_try_run(const char *cmd_name, int argc, char *argv[]);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenvoid doveadm_mail_register_cmd(const struct doveadm_mail_cmd *cmd);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenconst struct doveadm_mail_cmd *doveadm_mail_cmd_find(const char *cmd_name);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenvoid doveadm_mail_usage(string_t *out);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenvoid doveadm_mail_help(const struct doveadm_mail_cmd *cmd) ATTR_NORETURN;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenvoid doveadm_mail_help_name(const char *cmd_name) ATTR_NORETURN;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenvoid doveadm_mail_try_help_name(const char *cmd_name);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainenbool doveadm_mail_has_subcommands(const char *cmd_name);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenvoid doveadm_mail_init(void);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenvoid doveadm_mail_deinit(void);
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainenconst struct doveadm_mail_cmd *
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainendoveadm_mail_cmd_find_from_argv(const char *cmd_name, int *argc,
2686ef87921233609d1d8ec8dee2883facc2c5ebTimo Sirainen const char *const **argv);
struct doveadm_mail_cmd_context *
doveadm_mail_cmd_init(const struct doveadm_mail_cmd *cmd,
const struct doveadm_settings *set);
int doveadm_mail_single_user(struct doveadm_mail_cmd_context *ctx,
const struct mail_storage_service_input *input,
const char **error_r);
int doveadm_mail_server_user(struct doveadm_mail_cmd_context *ctx,
const struct mail_storage_service_input *input,
const char **error_r);
void doveadm_mail_server_flush(void);
struct mailbox *
doveadm_mailbox_find(struct mail_user *user, const char *mailbox);
int doveadm_mailbox_find_and_sync(struct mail_user *user, const char *mailbox,
struct mailbox **box_r);
struct mail_search_args *
doveadm_mail_build_search_args(const char *const args[]);
void doveadm_mailbox_args_check(const char *const args[]);
struct mail_search_args *
doveadm_mail_mailbox_search_args_build(const char *const args[]);
void expunge_search_args_check(struct mail_search_args *args, const char *cmd);
struct doveadm_mail_cmd_context *
doveadm_mail_cmd_alloc_size(size_t size);
#define doveadm_mail_cmd_alloc(type) \
(type *)doveadm_mail_cmd_alloc_size(sizeof(type))
void doveadm_mail_failed_error(struct doveadm_mail_cmd_context *ctx,
enum mail_error error);
void doveadm_mail_failed_storage(struct doveadm_mail_cmd_context *ctx,
struct mail_storage *storage);
void doveadm_mail_failed_mailbox(struct doveadm_mail_cmd_context *ctx,
struct mailbox *box);
void doveadm_mail_failed_list(struct doveadm_mail_cmd_context *ctx,
struct mailbox_list *list);
extern struct doveadm_mail_cmd cmd_expunge;
extern struct doveadm_mail_cmd cmd_search;
extern struct doveadm_mail_cmd cmd_fetch;
extern struct doveadm_mail_cmd cmd_flags_add;
extern struct doveadm_mail_cmd cmd_flags_remove;
extern struct doveadm_mail_cmd cmd_flags_replace;
extern struct doveadm_mail_cmd cmd_import;
extern struct doveadm_mail_cmd cmd_index;
extern struct doveadm_mail_cmd cmd_altmove;
extern struct doveadm_mail_cmd cmd_copy;
extern struct doveadm_mail_cmd cmd_deduplicate;
extern struct doveadm_mail_cmd cmd_move;
extern struct doveadm_mail_cmd cmd_mailbox_list;
extern struct doveadm_mail_cmd cmd_mailbox_create;
extern struct doveadm_mail_cmd cmd_mailbox_delete;
extern struct doveadm_mail_cmd cmd_mailbox_rename;
extern struct doveadm_mail_cmd cmd_mailbox_subscribe;
extern struct doveadm_mail_cmd cmd_mailbox_unsubscribe;
extern struct doveadm_mail_cmd cmd_mailbox_status;
extern struct doveadm_mail_cmd cmd_mailbox_metadata_set;
extern struct doveadm_mail_cmd cmd_mailbox_metadata_unset;
extern struct doveadm_mail_cmd cmd_mailbox_metadata_get;
extern struct doveadm_mail_cmd cmd_mailbox_metadata_list;
extern struct doveadm_mail_cmd cmd_batch;
#endif