Annotation For src/main.rs
Logged in as anonymous

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

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