Annotation For src/lib.rs
Logged in as anonymous

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

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