bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#include "imap-common.h"
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#include "base64.h"
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#include "str.h"
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#include "imap-commands.h"
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#include "imap-state.h"
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenbool cmd_x_state(struct client_command_context *cmd)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen{
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* FIXME: state importing can cause unnecessarily large memory usage
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen by specifying an old modseq, because the EXPUNGE/FETCH replies
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen aren't currently sent asynchronously. so this command is disabled
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen for now. */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#if 0
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const struct imap_arg *args;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char *str, *error;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_t *state, *state_encoded;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen int ret;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (!client_read_args(cmd, 0, 0, &args))
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return FALSE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen state = buffer_create_dynamic(cmd->pool, 256);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (imap_arg_get_astring(&args[0], &str)) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (cmd->client->mailbox != NULL) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_tagline(cmd,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen "BAD Can't be used in SELECTED state");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return TRUE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen }
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (base64_decode(str, strlen(str), NULL, state) < 0)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ret = 0;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen else {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ret = imap_state_import_external(cmd->client,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen state->data, state->used, &error);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen }
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (ret < 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_tagline(cmd, t_strdup_printf(
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen "NO Failed to restore state: %s", error));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen } else if (ret == 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_tagline(cmd, t_strdup_printf(
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen "BAD Broken state: %s", error));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen } else {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_tagline(cmd, "OK State imported.");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen }
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return TRUE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen } else if (args[0].type == IMAP_ARG_EOL) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (!imap_state_export_external(cmd->client, state, &error)) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_tagline(cmd, t_strdup_printf(
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen "NO Can't save state: %s", error));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return TRUE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen }
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen state_encoded = buffer_create_dynamic(cmd->pool,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen MAX_BASE64_ENCODED_SIZE(state->used)+10);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen str_append(state_encoded, "* STATE ");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen base64_encode(state->data, state->used, state_encoded);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_line(cmd->client, str_c(state_encoded));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_tagline(cmd, "OK State exported.");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return TRUE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen } else {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return TRUE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen }
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#else
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client_send_command_error(cmd, "Command is disabled for now.");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return TRUE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen#endif
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen}