Annotation For src/lib.rs
Logged in as anonymous

Origin for each line in src/lib.rs from check-in 713c1e7fb2:

713c1e7fb2 2026-07-31    1: pub mod mail;
3c858ed7c4 2026-07-31    2: mod telegram;
3c858ed7c4 2026-07-31    3: pub mod utils;
3c858ed7c4 2026-07-31    4: 
713c1e7fb2 2026-07-31    5: // As we are not actually exporting this lib there would be no local Error's for
713c1e7fb2 2026-07-31    6: // now, everything will be just .stack()?'ed and propagated like in real bin
713c1e7fb2 2026-07-31    7: // The lib here is just to separate all tests from main code into tests/
713c1e7fb2 2026-07-31    8: 
3c858ed7c4 2026-07-31    9: use crate::mail::MailServer;
713c1e7fb2 2026-07-31   10: 
713c1e7fb2 2026-07-31   11: use std::{
713c1e7fb2 2026-07-31   12: 	io::Cursor,
713c1e7fb2 2026-07-31   13: 	os::unix::fs::PermissionsExt,
713c1e7fb2 2026-07-31   14: 	path::Path,
713c1e7fb2 2026-07-31   15: };
3c858ed7c4 2026-07-31   16: 
3c858ed7c4 2026-07-31   17: use just_getopt::{
3c858ed7c4 2026-07-31   18: 	OptFlags,
3c858ed7c4 2026-07-31   19: 	OptSpecs,
3c858ed7c4 2026-07-31   20: 	OptValue,
3c858ed7c4 2026-07-31   21: };
3c858ed7c4 2026-07-31   22: use smol::{
3c858ed7c4 2026-07-31   23: 	fs::metadata,
3c858ed7c4 2026-07-31   24: };
3c858ed7c4 2026-07-31   25: use stacked_errors::{
3c858ed7c4 2026-07-31   26: 	Result,
3c858ed7c4 2026-07-31   27: 	StackableErr,
3c858ed7c4 2026-07-31   28: 	bail,
3c858ed7c4 2026-07-31   29: };
3c858ed7c4 2026-07-31   30: 
3c858ed7c4 2026-07-31   31: /// Actual main function running async with Error propagation support
3c858ed7c4 2026-07-31   32: pub async fn async_main () -> Result<()> {
3c858ed7c4 2026-07-31   33: 	let specs = OptSpecs::new()
3c858ed7c4 2026-07-31   34: 		.option("help", "h", OptValue::None)
3c858ed7c4 2026-07-31   35: 		.option("help", "help", OptValue::None)
3c858ed7c4 2026-07-31   36: 		.option("config", "c", OptValue::Required)
3c858ed7c4 2026-07-31   37: 		.option("config", "config", OptValue::Required)
3c858ed7c4 2026-07-31   38: 		.flag(OptFlags::OptionsEverywhere);
3c858ed7c4 2026-07-31   39: 	let mut args = std::env::args();
3c858ed7c4 2026-07-31   40: 	args.next();
3c858ed7c4 2026-07-31   41: 	let parsed = specs.getopt(args);
3c858ed7c4 2026-07-31   42: 	for u in &parsed.unknown {
3c858ed7c4 2026-07-31   43: 		println!("Unknown option: {u}");
3c858ed7c4 2026-07-31   44: 	}
3c858ed7c4 2026-07-31   45: 	if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
3c858ed7c4 2026-07-31   46: 		println!("SMTP2TG v{}, (C) 2024 - 2026\n\n\
3c858ed7c4 2026-07-31   47: 			\t-h|--help\tDisplay this help\n\
3c858ed7c4 2026-07-31   48: 			\t-c|--config …\tSet configuration file location.",
3c858ed7c4 2026-07-31   49: 			env!("CARGO_PKG_VERSION"));
3c858ed7c4 2026-07-31   50: 		return Ok(());
3c858ed7c4 2026-07-31   51: 	};
3c858ed7c4 2026-07-31   52: 	let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
3c858ed7c4 2026-07-31   53: 		&path[..]
3c858ed7c4 2026-07-31   54: 	} else {
3c858ed7c4 2026-07-31   55: 		"smtp2tg.toml"
3c858ed7c4 2026-07-31   56: 	});
3c858ed7c4 2026-07-31   57: 	if !config_file.exists() {
3c858ed7c4 2026-07-31   58: 		bail!("can't read configuration from {config_file:?}");
3c858ed7c4 2026-07-31   59: 	};
3c858ed7c4 2026-07-31   60: 	{
3c858ed7c4 2026-07-31   61: 		let meta = metadata(config_file).await.stack()?;
3c858ed7c4 2026-07-31   62: 		if (!0o100600 & meta.permissions().mode()) > 0 {
3c858ed7c4 2026-07-31   63: 			bail!("other users can read or write config file {config_file:?}\n\
3c858ed7c4 2026-07-31   64: 				File permissions: {:o}", meta.permissions().mode());
3c858ed7c4 2026-07-31   65: 		}
3c858ed7c4 2026-07-31   66: 	}
3c858ed7c4 2026-07-31   67: 	let settings: config::Config = config::Config::builder()
3c858ed7c4 2026-07-31   68: 		.set_default("api_gateway", "https://api.telegram.org").stack()?
3c858ed7c4 2026-07-31   69: 		.set_default("fields", vec!["date", "from", "subject"]).stack()?
3c858ed7c4 2026-07-31   70: 		.set_default("hostname", "smtp.2.tg").stack()?
3c858ed7c4 2026-07-31   71: 		.set_default("listen_on", "0.0.0.0:1025").stack()?
3c858ed7c4 2026-07-31   72: 		.set_default("unknown", "relay").stack()?
713c1e7fb2 2026-07-31   73: 		.set_default("domains", vec!["localhost",
713c1e7fb2 2026-07-31   74: 			hostname::get().expect("Failed to get current hostname")
713c1e7fb2 2026-07-31   75: 			.to_str().expect("Can't convert hostname to string, bad UTF-8?")]).stack()?
3c858ed7c4 2026-07-31   76: 		.add_source(config::File::from(config_file))
3c858ed7c4 2026-07-31   77: 		.build()
3c858ed7c4 2026-07-31   78: 		.with_context(|| format!("[{config_file:?}] there was an error reading config\n\
3c858ed7c4 2026-07-31   79: 			\tplease consult \"smtp2tg.toml.example\" for details"))?;
3c858ed7c4 2026-07-31   80: 
3c858ed7c4 2026-07-31   81: 	let listen_on = settings.get_string("listen_on").stack()?;
3c858ed7c4 2026-07-31   82: 	let server_name = settings.get_string("hostname").stack()?;
3c858ed7c4 2026-07-31   83: 	let core = MailServer::new(settings)?;
3c858ed7c4 2026-07-31   84: 	let mut server = mailin_embedded::Server::new(core);
3c858ed7c4 2026-07-31   85: 
3c858ed7c4 2026-07-31   86: 	server.with_name(server_name)
3c858ed7c4 2026-07-31   87: 		.with_ssl(mailin_embedded::SslConfig::None).unwrap()
3c858ed7c4 2026-07-31   88: 		.with_addr(listen_on).unwrap();
3c858ed7c4 2026-07-31   89: 	server.serve().unwrap();
3c858ed7c4 2026-07-31   90: 
3c858ed7c4 2026-07-31   91: 	Ok(())
3c858ed7c4 2026-07-31   92: }