Annotation For src/main.rs
Logged in as anonymous

Lines of src/main.rs from check-in c996f5c871 that are changed by the sequence of edits moving toward check-in 3c858ed7c4:

                         1: //! Simple SMTP-to-Telegram gateway. Can parse email and send them as telegram
                         2: //! messages to specified chats, generally you specify which email address is
                         3: //! available in configuration, everything else is sent to default address.
                         4: 
c996f5c871 2026-01-12    5: mod mail;
c996f5c871 2026-01-12    6: mod telegram;
c996f5c871 2026-01-12    7: mod utils;
c996f5c871 2026-01-12    8: 
c996f5c871 2026-01-12    9: #[cfg(test)]
c996f5c871 2026-01-12   10: mod tests;
c996f5c871 2026-01-12   11: 
c996f5c871 2026-01-12   12: use crate::mail::MailServer;
c996f5c871 2026-01-12   13: 
                        14: use async_compat::Compat;
c996f5c871 2026-01-12   15: use just_getopt::{
c996f5c871 2026-01-12   16: 	OptFlags,
c996f5c871 2026-01-12   17: 	OptSpecs,
c996f5c871 2026-01-12   18: 	OptValue,
c996f5c871 2026-01-12   19: };
c996f5c871 2026-01-12   20: use smol::{
c996f5c871 2026-01-12   21: 	fs::metadata,
c996f5c871 2026-01-12   22: };
c996f5c871 2026-01-12   23: use stacked_errors::{
c996f5c871 2026-01-12   24: 	Result,
c996f5c871 2026-01-12   25: 	StackableErr,
c996f5c871 2026-01-12   26: 	bail,
c996f5c871 2026-01-12   27: };
                        28: 
c996f5c871 2026-01-12   29: use std::{
c996f5c871 2026-01-12   30: 	io::Cursor,
c996f5c871 2026-01-12   31: 	os::unix::fs::PermissionsExt,
c996f5c871 2026-01-12   32: 	path::Path,
c996f5c871 2026-01-12   33: };
c996f5c871 2026-01-12   34: 
                        35: fn main () -> Result<()> {
                        36: 	smol::block_on(Compat::new(async {
c996f5c871 2026-01-12   37: 		async_main().await.unwrap()
                        38: 	}));
c996f5c871 2026-01-12   39: 
c996f5c871 2026-01-12   40: 	Ok(())
c996f5c871 2026-01-12   41: }
c996f5c871 2026-01-12   42: 
c996f5c871 2026-01-12   43: /// Actual main function running async with Error propagation support
c996f5c871 2026-01-12   44: async fn async_main () -> Result<()> {
c996f5c871 2026-01-12   45: 	let specs = OptSpecs::new()
c996f5c871 2026-01-12   46: 		.option("help", "h", OptValue::None)
c996f5c871 2026-01-12   47: 		.option("help", "help", OptValue::None)
c996f5c871 2026-01-12   48: 		.option("config", "c", OptValue::Required)
c996f5c871 2026-01-12   49: 		.option("config", "config", OptValue::Required)
c996f5c871 2026-01-12   50: 		.flag(OptFlags::OptionsEverywhere);
c996f5c871 2026-01-12   51: 	let mut args = std::env::args();
c996f5c871 2026-01-12   52: 	args.next();
c996f5c871 2026-01-12   53: 	let parsed = specs.getopt(args);
c996f5c871 2026-01-12   54: 	for u in &parsed.unknown {
c996f5c871 2026-01-12   55: 		println!("Unknown option: {u}");
c996f5c871 2026-01-12   56: 	}
c996f5c871 2026-01-12   57: 	if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
c996f5c871 2026-01-12   58: 		println!("SMTP2TG v{}, (C) 2024 - 2026\n\n\
c996f5c871 2026-01-12   59: 			\t-h|--help\tDisplay this help\n\
c996f5c871 2026-01-12   60: 			\t-c|--config …\tSet configuration file location.",
c996f5c871 2026-01-12   61: 			env!("CARGO_PKG_VERSION"));
c996f5c871 2026-01-12   62: 		return Ok(());
c996f5c871 2026-01-12   63: 	};
c996f5c871 2026-01-12   64: 	let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
c996f5c871 2026-01-12   65: 		&path[..]
c996f5c871 2026-01-12   66: 	} else {
c996f5c871 2026-01-12   67: 		"smtp2tg.toml"
c996f5c871 2026-01-12   68: 	});
c996f5c871 2026-01-12   69: 	if !config_file.exists() {
c996f5c871 2026-01-12   70: 		bail!("can't read configuration from {config_file:?}");
c996f5c871 2026-01-12   71: 	};
c996f5c871 2026-01-12   72: 	{
c996f5c871 2026-01-12   73: 		let meta = metadata(config_file).await.stack()?;
c996f5c871 2026-01-12   74: 		if (!0o100600 & meta.permissions().mode()) > 0 {
c996f5c871 2026-01-12   75: 			bail!("other users can read or write config file {config_file:?}\n\
c996f5c871 2026-01-12   76: 				File permissions: {:o}", meta.permissions().mode());
c996f5c871 2026-01-12   77: 		}
c996f5c871 2026-01-12   78: 	}
c996f5c871 2026-01-12   79: 	let settings: config::Config = config::Config::builder()
c996f5c871 2026-01-12   80: 		.set_default("api_gateway", "https://api.telegram.org").stack()?
c996f5c871 2026-01-12   81: 		.set_default("fields", vec!["date", "from", "subject"]).stack()?
c996f5c871 2026-01-12   82: 		.set_default("hostname", "smtp.2.tg").stack()?
c996f5c871 2026-01-12   83: 		.set_default("listen_on", "0.0.0.0:1025").stack()?
c996f5c871 2026-01-12   84: 		.set_default("unknown", "relay").stack()?
c996f5c871 2026-01-12   85: 		.set_default("domains", vec!["localhost", hostname::get().stack()?.to_str().expect("Failed to get current hostname")]).stack()?
c996f5c871 2026-01-12   86: 		.add_source(config::File::from(config_file))
c996f5c871 2026-01-12   87: 		.build()
c996f5c871 2026-01-12   88: 		.with_context(|| format!("[{config_file:?}] there was an error reading config\n\
c996f5c871 2026-01-12   89: 			\tplease consult \"smtp2tg.toml.example\" for details"))?;
c996f5c871 2026-01-12   90: 
c996f5c871 2026-01-12   91: 	let listen_on = settings.get_string("listen_on").stack()?;
c996f5c871 2026-01-12   92: 	let server_name = settings.get_string("hostname").stack()?;
c996f5c871 2026-01-12   93: 	let core = MailServer::new(settings)?;
c996f5c871 2026-01-12   94: 	let mut server = mailin_embedded::Server::new(core);
c996f5c871 2026-01-12   95: 
c996f5c871 2026-01-12   96: 	server.with_name(server_name)
c996f5c871 2026-01-12   97: 		.with_ssl(mailin_embedded::SslConfig::None).unwrap()
c996f5c871 2026-01-12   98: 		.with_addr(listen_on).unwrap();
c996f5c871 2026-01-12   99: 	server.serve().unwrap();
                       100: 
                       101: 	Ok(())
                       102: }