Annotation For src/main.rs
Logged in as anonymous

Lines of src/main.rs from check-in 42ac4c7a9a that are changed by the sequence of edits moving toward check-in bdac638883:

                         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: 
42ac4c7a9a 2026-10-28    5: use smol_macros::{
42ac4c7a9a 2026-10-28    6: 	LocalExecutor,
42ac4c7a9a 2026-10-28    7: 	main,
42ac4c7a9a 2026-10-28    8: };
42ac4c7a9a 2026-10-28    9: 
                        10: use std::{
                        11: 	os::unix::fs::PermissionsExt,
                        12: 	path::Path,
                        13: };
                        14: 
                        15: use clap::Parser;
                        16: use smol::fs::metadata;
                        17: use stacked_errors::{
                        18: 	Result,
                        19: 	StackableErr,
                        20: 	bail,
                        21: };
                        22: 
                        23: use smtp2tg::mail::MailServer;
                        24: 
                        25: /// SMTP-to-Telegram gateway
                        26: #[derive(Parser, Debug)]
                        27: #[command(name = "smtp2tg")]
                        28: #[command(about = format!("SMTP-to-Telegram gateway v{}, (C) 2024 - 2026", env!("CARGO_PKG_VERSION")), long_about = None)]
                        29: struct Args {
                        30: 	/// Set configuration file location
                        31: 	#[arg(short, long, default_value = "smtp2tg.toml")]
                        32: 	config: String,
                        33: }
                        34: 
42ac4c7a9a 2026-10-28   35: main! {
                        36: /// Main asynchronous entry point for the application. Runs the gateway using
                        37: /// the configuration selected on the command line.
                        38: ///
                        39: /// The configuration file must use owner-only permissions. Once configured,
                        40: /// the SMTP server runs until it stops or encounters an error.
                        41: ///
                        42: /// # Errors
                        43: /// Returns an error if the configuration file is missing, inaccessible,
                        44: /// insecure, malformed, or contains invalid required settings.
                        45: /// Also returns an error if the system hostname cannot be read or is not
                        46: /// valid UTF-8, or if mail server initialisation fails.
                        47: ///
                        48: /// # Panics
                        49: /// Panics if configuring or serving the SMTP server fails.
42ac4c7a9a 2026-10-28   50: 	async fn main (ex: &LocalExecutor<'_>) -> Result<()> {
                        51: 		let args = Args::parse();
                        52: 		let settings = {
                        53: 			// Load configuration defaults and overrides after checking file permissions.
                        54: 			let config_file = Path::new(&args.config);
                        55: 			if !config_file.try_exists().stack()? {
                        56: 				bail!("Configuration file not found: {config_file:?}\n\
                        57: 					Hint: Ensure the file exists and the path is correct.");
                        58: 			};
                        59: 			{
                        60: 				let meta = metadata(config_file).await.stack()?;
                        61: 				if (!0o100600 & meta.permissions().mode()) > 0 {
                        62: 					bail!("Configuration file permissions are insecure {config_file:?}\n\
                        63: 						Current permissions: {:o}\n\
                        64: 						Required: 0600 (owner read/write only).\n\
                        65: 						Fix with: chmod 600 {config_file:?}",
                        66: 						meta.permissions().mode());
                        67: 			}	}
                        68: 			config::Config::builder()
                        69: 				.set_default("api_gateway", "https://api.telegram.org").stack()?
                        70: 				.set_default("fields", vec!["date", "from", "subject"]).stack()?
                        71: 				.set_default("hostname", "smtp.2.tg").stack()?
                        72: 				.set_default("listen_on", "0.0.0.0:1025").stack()?
                        73: 				.set_default("domains", vec!["localhost",
                        74: 					hostname::get().context("Failed to get current hostname")?
                        75: 					.to_str().context("Can't convert hostname to string, bad UTF-8?")?]).stack()?
                        76: 				.add_source(config::File::from(config_file))
                        77: 				.build()
                        78: 				.with_context(|| format!(
                        79: 					"Failed to parse configuration file: {config_file:?}\n\
                        80: 					Check syntax against smtp2tg.toml.example.\n\
                        81: 					Common issues: missing quotes, trailing commas in inline tables, or invalid types."
                        82: 				))?
                        83: 		};
                        84: 
                        85: 		let listen_on = settings.get_string("listen_on").stack()?;
                        86: 		let server_name = settings.get_string("hostname").stack()?;
                        87: 		let core = MailServer::new(settings)?;
                        88: 		let mut server = mailin_embedded::Server::new(core);
                        89: 
                        90: 		// TODO: remove unwraps when mailin-embedded bumps with better error handling
                        91: 		server.with_name(server_name)
                        92: 			.with_ssl(mailin_embedded::SslConfig::None).unwrap()
42ac4c7a9a 2026-10-28   93: 			.with_addr(listen_on).unwrap();
                        94: 		server.serve().unwrap();
                        95: 
                        96: 		Ok(())
42ac4c7a9a 2026-10-28   97: }	}