Files
himalaya/src/jmap/email/copy.rs
T
2026-05-20 02:36:43 +02:00

80 lines
2.6 KiB
Rust

// This file is part of Himalaya, a CLI to manage emails.
//
// Copyright (C) 2022-2026 soywod <pimalaya.org@posteo.net>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::collections::BTreeMap;
use anyhow::{bail, Result};
use clap::Parser;
use io_jmap::rfc8621::email::EmailCopy;
use pimalaya_cli::printer::{Message, Printer};
use crate::jmap::{client::JmapClient, error::format_set_error};
/// Copy JMAP emails from another account (Email/copy).
#[derive(Debug, Parser)]
pub struct JmapEmailCopyCommand {
/// Email ID(s) to copy.
#[arg(value_name = "ID", required = true)]
pub ids: Vec<String>,
/// Source account ID to copy from.
#[arg(long, value_name = "ACCOUNT-ID")]
pub from_account: String,
/// Destination mailbox ID(s) to place copies in.
#[arg(long, value_name = "MAILBOX-ID", required = false)]
pub mailbox_id: Vec<String>,
}
impl JmapEmailCopyCommand {
pub fn execute(self, printer: &mut impl Printer, mut client: JmapClient) -> Result<()> {
let mailbox_ids: BTreeMap<String, bool> =
self.mailbox_id.into_iter().map(|m| (m, true)).collect();
let emails: BTreeMap<String, EmailCopy> = self
.ids
.into_iter()
.map(|id| {
(
id.clone(),
EmailCopy {
id,
mailbox_ids: mailbox_ids.clone(),
keywords: None,
received_at: None,
},
)
})
.collect();
let output = client.email_copy(self.from_account.clone(), emails)?;
if !output.not_created.is_empty() {
let mut msg = String::from("Copy JMAP email(s) error");
for (id, err) in output.not_created {
msg.push_str(&format!("\n `{id}`"));
msg.push_str(&format_set_error(&err));
}
bail!(msg)
}
printer.out(Message::new("Email(s) successfully copied"))
}
}