mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-16 20:57:53 +08:00
76 lines
2.3 KiB
Rust
76 lines
2.3 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::fmt;
|
|
|
|
use anyhow::{Result, bail};
|
|
use clap::Parser;
|
|
use io_maildir::maildir::Maildir;
|
|
use mail_parser::Message;
|
|
use pimalaya_cli::printer::Printer;
|
|
use serde::Serialize;
|
|
|
|
use crate::maildir::{
|
|
arg::{MaildirPathFlag, MessageIdArg},
|
|
client::MaildirClient,
|
|
};
|
|
|
|
/// Get Maildir message to the given mailbox.
|
|
///
|
|
/// This command copies message(s) identified by the given sequence
|
|
/// set from the source mailbox to the destination mailbox.
|
|
#[derive(Debug, Parser)]
|
|
pub struct MaildirMessageGetCommand {
|
|
#[command(flatten)]
|
|
pub maildir: MaildirPathFlag,
|
|
#[command(flatten)]
|
|
pub id: MessageIdArg,
|
|
}
|
|
|
|
impl MaildirMessageGetCommand {
|
|
pub fn execute(self, printer: &mut impl Printer, client: MaildirClient) -> Result<()> {
|
|
let maildir = match Maildir::try_from(self.maildir.inner.clone()) {
|
|
Ok(maildir) => maildir,
|
|
Err(_) => Maildir::try_from(client.root.join(&self.maildir.inner))?,
|
|
};
|
|
|
|
let msg = client.get(maildir, &self.id.inner)?;
|
|
|
|
let path = msg.path().to_owned();
|
|
|
|
let Some(parsed) = msg.headers() else {
|
|
bail!("Invalid MIME message at {}", path.display());
|
|
};
|
|
|
|
printer.out(MessageView(parsed))
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(transparent)]
|
|
pub struct MessageView<'a>(Message<'a>);
|
|
|
|
impl fmt::Display for MessageView<'_> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
for header in self.0.headers() {
|
|
writeln!(f, "{}: {:?}", header.name(), header.value())?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|