bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen#include "lib.h"
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen#include "str.h"
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen#include "imap-utf7.h"
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen#include "doveadm.h"
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen#include <stdio.h>
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen#include <unistd.h>
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainenstatic void cmd_mailbox_mutf7(int argc, char *argv[])
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen{
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen string_t *str;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen bool from_utf8;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen unsigned int i;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen int c;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen from_utf8 = TRUE;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen while ((c = getopt(argc, argv, "78")) > 0) {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen switch (c) {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen case '7':
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen from_utf8 = FALSE;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen break;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen case '8':
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen from_utf8 = TRUE;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen break;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen default:
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen help(&doveadm_cmd_mailbox_mutf7);
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen }
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen }
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen argv += optind;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen if (argv[0] == NULL)
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen help(&doveadm_cmd_mailbox_mutf7);
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen str = t_str_new(128);
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen for (i = 0; argv[i] != NULL; i++) {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen str_truncate(str, 0);
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen if (from_utf8) {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen if (imap_utf8_to_utf7(argv[i], str) < 0) {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen i_error("Mailbox name not valid UTF-8: %s",
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen argv[i]);
880ac25be5546f3cb31d6f89ab094bb3a1bad4e5Timo Sirainen doveadm_exit_code = EX_DATAERR;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen }
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen } else {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen if (imap_utf7_to_utf8(argv[i], str) < 0) {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen i_error("Mailbox name not valid mUTF-7: %s",
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen argv[i]);
880ac25be5546f3cb31d6f89ab094bb3a1bad4e5Timo Sirainen doveadm_exit_code = EX_DATAERR;
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen }
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen }
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen printf("%s\n", str_c(str));
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen }
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen}
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainenstruct doveadm_cmd doveadm_cmd_mailbox_mutf7 = {
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen cmd_mailbox_mutf7, "mailbox mutf7",
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen "[-7|-8] <name> [...]"
12fdf0f2dd2450c82e60462176b810c9cbbdd4bfTimo Sirainen};