Annotation For src/lib.rs
Logged in as anonymous

Lines of src/lib.rs from check-in 512369f93e that are changed by the sequence of edits moving toward check-in 158c9cffc6:

                         1: //! SMTP-to-Telegram gateway main library.
                         2: //!
                         3: //! This module provides the core functionality for receiving emails via SMTP
                         4: //! and forwarding them to Telegram chats.
                         5: //!
                         6: //! As we are not actually exporting this lib there would be no local Error's
                         7: //! for now, everything will be just .stack()?'ed and propagated like in real
                         8: //! bin The lib here is just to separate all tests from main code into tests/
                         9: 
                        10: pub mod mail;
                        11: mod telegram;
                        12: pub mod utils;
                        13: 
                        14: use crate::mail::MailServer;
                        15: 
                        16: use std::{
                        17: 	io::Cursor,
                        18: 	os::unix::fs::PermissionsExt,
                        19: 	path::Path,
                        20: };
                        21: 
                        22: use clap::Parser;
                        23: use smol::{
                        24: 	fs::metadata,
                        25: };
                        26: use stacked_errors::{
                        27: 	Result,
                        28: 	StackableErr,
                        29: 	bail,
                        30: };
                        31: 
                        32: /// SMTP-to-Telegram gateway
                        33: #[derive(Parser, Debug)]
                        34: #[command(name = "smtp2tg")]
                        35: #[command(about = format!("SMTP-to-Telegram gateway v{}, (C) 2024 - 2026", env!("CARGO_PKG_VERSION")), long_about = None)]
                        36: struct Args {
                        37: 	/// Set configuration file location
                        38: 	#[arg(short, long, default_value = "smtp2tg.toml")]
                        39: 	config: String,
                        40: }
                        41: 
                        42: /// Main asynchronous entry point for the application.
                        43: ///
                        44: /// Parses command-line arguments, loads configuration, and starts the SMTP
                        45: /// server.
                        46: ///
                        47: /// # Errors
                        48: /// Returns an error if configuration is invalid, files are inaccessible, or
                        49: /// server fails to start.
                        50: pub async fn async_main () -> Result<()> {
                        51: 	let args = Args::parse();
                        52: 	let config_file = Path::new(&args.config);
                        53: 	if !config_file.exists() {
                        54: 		bail!("can't read configuration from {config_file:?}");
                        55: 	};
                        56: 	{
                        57: 		let meta = metadata(config_file).await.stack()?;
                        58: 		if (!0o100600 & meta.permissions().mode()) > 0 {
                        59: 			bail!("other users can read or write config file {config_file:?}\n\
                        60: 				File permissions: {:o}", meta.permissions().mode());
                        61: 		}
                        62: 	}
                        63: 	let settings: config::Config = config::Config::builder()
                        64: 		.set_default("api_gateway", "https://api.telegram.org").stack()?
                        65: 		.set_default("fields", vec!["date", "from", "subject"]).stack()?
                        66: 		.set_default("hostname", "smtp.2.tg").stack()?
                        67: 		.set_default("listen_on", "0.0.0.0:1025").stack()?
512369f93e 2026-08-01   68: 		.set_default("unknown", "relay").stack()?
                        69: 		.set_default("domains", vec!["localhost",
                        70: 			hostname::get().expect("Failed to get current hostname")
                        71: 			.to_str().expect("Can't convert hostname to string, bad UTF-8?")]).stack()?
                        72: 		.add_source(config::File::from(config_file))
                        73: 		.build()
                        74: 		.with_context(|| format!("[{config_file:?}] there was an error reading config\n\
                        75: 			\tplease consult \"smtp2tg.toml.example\" for details"))?;
                        76: 
                        77: 	let listen_on = settings.get_string("listen_on").stack()?;
                        78: 	let server_name = settings.get_string("hostname").stack()?;
                        79: 	let core = MailServer::new(settings)?;
                        80: 	let mut server = mailin_embedded::Server::new(core);
                        81: 
                        82: 	server.with_name(server_name)
                        83: 		.with_ssl(mailin_embedded::SslConfig::None).unwrap()
                        84: 		.with_addr(listen_on).unwrap();
                        85: 	server.serve().unwrap();
                        86: 
                        87: 	Ok(())
                        88: }