Annotation For src/main.rs
Logged in as anonymous

Origin for each line in src/main.rs from check-in 9fecd57a99:

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