Annotation For src/main.rs
Logged in as anonymous

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

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