use clap::Parser;
use color_eyre::Result;
use email::{backend::feature::BackendFeatureSource, config::Config};
use pimalaya_tui::{
himalaya::backend::BackendBuilder,
terminal::{cli::printer::Printer, config::TomlConfig as _},
};
use std::{
io::{self, BufRead, IsTerminal},
sync::Arc,
};
use tracing::info;
use crate::{account::arg::name::AccountNameFlag, config::TomlConfig, message::arg::MessageRawArg};
#[derive(Debug, Parser)]
pub struct MessageSendCommand {
#[command(flatten)]
pub message: MessageRawArg,
#[command(flatten)]
pub account: AccountNameFlag,
}
impl MessageSendCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing send message command");
let (toml_account_config, account_config) = config
.clone()
.into_account_configs(self.account.name.as_deref(), |c: &Config, name| {
c.account(name).ok()
})?;
let backend = BackendBuilder::new(
Arc::new(toml_account_config),
Arc::new(account_config),
|builder| {
builder
.without_features()
.with_add_message(BackendFeatureSource::Context)
.with_send_message(BackendFeatureSource::Context)
},
)
.build()
.await?;
let msg = if io::stdin().is_terminal() {
self.message.raw()
} else {
io::stdin()
.lock()
.lines()
.map_while(Result::ok)
.collect::<Vec<_>>()
.join("\r\n")
};
backend.send_message_then_save_copy(msg.as_bytes()).await?;
printer.out("Message successfully sent!")
}
}