use crate::{AppConfig, AppError, Result};
pub(crate) fn run(config: &AppConfig, command: &str, args: &[String]) -> Result<()> {
if config.app.environment == "production" {
return Err(AppError::Configuration("The development inbox is unavailable in production. Use a delivering mail transport; inspect local messages with APP_ENVIRONMENT=development.".into()));
}
let inbox = capy_mail::DevelopmentInbox::new(&config.mail.capture_directory);
let display = |value: &str| -> String {
value
.chars()
.flat_map(|c| {
if c.is_control() && c != '\n' && c != '\t' {
c.escape_default().collect::<Vec<_>>()
} else {
vec![c]
}
})
.collect()
};
match (command, args) {
("mail:list", []) => {
for mail in inbox.list().map_err(inbox_error)? {
println!(
"{} {} {} {}",
mail.id(),
mail.timestamp_ms(),
display(
&mail
.recipients()
.iter()
.map(|m| m.address())
.collect::<Vec<_>>()
.join(", ")
),
display(mail.subject())
);
}
}
("mail:show", [id]) => {
let mail = inbox.show(id).map_err(inbox_error)?;
println!(
"ID: {}\nTimestamp (Unix ms): {}\nFrom: {}\nTo: {}\nSubject: {}",
mail.id(),
mail.timestamp_ms(),
display(&mail.sender().address()),
display(
&mail
.recipients()
.iter()
.map(|m| m.address())
.collect::<Vec<_>>()
.join(", ")
),
display(mail.subject())
);
for (name, value) in mail.headers() {
println!("{}: {}", display(name), display(value));
}
println!(
"\nText:\n{}\n\nHTML source:\n{}",
display(mail.text().unwrap_or("")),
display(mail.html().unwrap_or(""))
);
}
("mail:clear", []) => println!(
"Cleared {} captured messages.",
inbox.clear().map_err(inbox_error)?
),
_ => {
return Err(AppError::Configuration(
"Use capy mail list, capy mail show <id>, or capy mail clear.".into(),
))
}
}
Ok(())
}
fn inbox_error(error: capy_mail::MailError) -> AppError {
match error {
capy_mail::MailError::CaptureFull => AppError::Configuration("The development inbox exceeds its 100-message/64-MiB limit. Inspect and clear it with capy mail clear; do not use capture for production delivery.".into()),
_ => AppError::Configuration("Cannot access the development inbox. Check MAIL_CAPTURE_DIRECTORY, the message UUID and file integrity. Use a private writable directory (0700 on Unix) with no symlinked inbox or message files; messages contain private development data.".into()),
}
}