#[cfg(not(feature = "std"))]
compile_error!("asimov-imap-fetcher requires the 'std' feature");
use asimov_imap_module::{ImapConfiguration, ImapReader};
use asimov_module::SysexitsError::{self, *};
use clap::Parser;
use clientele::StandardOptions;
use dogma::{
Uri,
UriScheme::{Imap, Imaps},
UriValueParser,
};
use know::datatypes::EmailMessageId;
use std::error::Error;
#[derive(Debug, Parser)]
#[command(arg_required_else_help = true)]
struct Options {
#[clap(flatten)]
flags: StandardOptions,
#[arg(value_name = "FORMAT", short = 'o', long)]
output: Option<String>,
#[arg(value_name = "IMAP-MESSAGE-URL", value_parser = UriValueParser::new(&[Imap, Imaps]))]
message_url: Uri<'static>,
}
fn main() -> Result<SysexitsError, Box<dyn Error>> {
asimov_module::dotenv().ok();
let args = asimov_module::args_os()?;
let options = Options::parse_from(args);
if options.flags.version {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
return Ok(EX_OK);
}
if options.flags.license {
print!("{}", include_str!("../../UNLICENSE"));
return Ok(EX_OK);
}
#[cfg(feature = "tracing")]
asimov_module::init_tracing_subscriber(&options.flags).expect("failed to initialize logging");
let config = ImapConfiguration::load()?;
let imap_url = config.resolve_url((&options.message_url).into())?;
let mut server = ImapReader::open(&imap_url)?;
let message_id: EmailMessageId = options
.message_url
.fragment_str()
.expect("message ID should be given in the URL fragment")
.into();
match server.fetch(&message_id)? {
Some(message) => {
match options.output.unwrap_or_default().as_str() {
"jsonld" | "json" => print!("{}", message.headers.jsonld()),
"mime" | _ => {
print!("{}", message.headers.mime());
if let Some(body) = message.body {
println!();
print!("{}", body);
}
},
}
Ok(EX_OK)
},
None => {
eprintln!("message ID <{}> not found", message_id.as_str());
Ok(EX_NOINPUT)
},
}
}