Annotation For src/lib.rs
Logged in as anonymous

Origin for each line in src/lib.rs from check-in e678fcdc39:

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