Annotation For src/main.rs
Logged in as anonymous

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

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