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        arcade: //! SMTP-to-Telegram gateway main library.
78b11b0319 2026-08-01        arcade: //!
78b11b0319 2026-08-01        arcade: //! This module provides the core functionality for receiving emails via SMTP
78b11b0319 2026-08-01        arcade: //! and forwarding them to Telegram chats.
78b11b0319 2026-08-01        arcade: //!
78b11b0319 2026-08-01        arcade: //! As we are not actually exporting this lib there would be no local Error's
78b11b0319 2026-08-01        arcade: //! for now, everything will be just .stack()?'ed and propagated like in real
78b11b0319 2026-08-01        arcade: //! bin The lib here is just to separate all tests from main code into tests/
78b11b0319 2026-08-01        arcade: 
f4660639ca 2026-07-31        arcade: pub mod mail;
f4660639ca 2026-07-31        arcade: mod telegram;
f4660639ca 2026-07-31        arcade: pub mod utils;
f4660639ca 2026-07-31        arcade: 
f4660639ca 2026-07-31        arcade: use crate::mail::MailServer;
f4660639ca 2026-07-31        arcade: 
f4660639ca 2026-07-31        arcade: use std::{
f4660639ca 2026-07-31        arcade: 	io::Cursor,
f4660639ca 2026-07-31        arcade: 	os::unix::fs::PermissionsExt,
f4660639ca 2026-07-31        arcade: 	path::Path,
f4660639ca 2026-07-31        arcade: };
f4660639ca 2026-07-31        arcade: 
78b11b0319 2026-08-01        arcade: use clap::Parser;
f4660639ca 2026-07-31        arcade: use smol::{
f4660639ca 2026-07-31        arcade: 	fs::metadata,
f4660639ca 2026-07-31        arcade: };
f4660639ca 2026-07-31        arcade: use stacked_errors::{
f4660639ca 2026-07-31        arcade: 	Result,
f4660639ca 2026-07-31        arcade: 	StackableErr,
f4660639ca 2026-07-31        arcade: 	bail,
f4660639ca 2026-07-31        arcade: };
f4660639ca 2026-07-31        arcade: 
78b11b0319 2026-08-01        arcade: /// SMTP-to-Telegram gateway
78b11b0319 2026-08-01        arcade: #[derive(Parser, Debug)]
78b11b0319 2026-08-01        arcade: #[command(name = "smtp2tg")]
78b11b0319 2026-08-01        arcade: #[command(about = format!("SMTP-to-Telegram gateway v{}, (C) 2024 - 2026", env!("CARGO_PKG_VERSION")), long_about = None)]
78b11b0319 2026-08-01        arcade: struct Args {
78b11b0319 2026-08-01        arcade: 	/// Set configuration file location
78b11b0319 2026-08-01        arcade: 	#[arg(short, long, default_value = "smtp2tg.toml")]
78b11b0319 2026-08-01        arcade: 	config: String,
78b11b0319 2026-08-01        arcade: }
78b11b0319 2026-08-01        arcade: 
42867194d6 2026-09-13        arcade: /// Main asynchronous entry point for the application. Runs the gateway using
42867194d6 2026-09-13        arcade: /// the configuration selected on the command line.
78b11b0319 2026-08-01        arcade: ///
42867194d6 2026-09-13        arcade: /// The configuration file must use owner-only permissions. Once configured,
42867194d6 2026-09-13        arcade: /// the SMTP server runs until it stops or encounters an error.
78b11b0319 2026-08-01        arcade: ///
78b11b0319 2026-08-01        arcade: /// # Errors
42867194d6 2026-09-13        arcade: /// Returns an error if the configuration file is missing, inaccessible,
42867194d6 2026-09-13        arcade: /// insecure, malformed, or contains invalid required settings.
42867194d6 2026-09-13        arcade: ///
42867194d6 2026-09-13        arcade: /// # Panics
42867194d6 2026-09-13        arcade: /// Panics if configuring or serving the SMTP server fails.
f4660639ca 2026-07-31        arcade: pub async fn async_main () -> Result<()> {
78b11b0319 2026-08-01        arcade: 	let args = Args::parse();
78b11b0319 2026-08-01        arcade: 	let config_file = Path::new(&args.config);
42867194d6 2026-09-13        arcade: 	if !config_file.try_exists().stack()? {
42867194d6 2026-09-13        arcade: 		bail!("Configuration file not found: {config_file:?}\n\
42867194d6 2026-09-13        arcade: 			Hint: Ensure the file exists and the path is correct.");
f4660639ca 2026-07-31        arcade: 	};
f4660639ca 2026-07-31        arcade: 	{
f4660639ca 2026-07-31        arcade: 		let meta = metadata(config_file).await.stack()?;
f4660639ca 2026-07-31        arcade: 		if (!0o100600 & meta.permissions().mode()) > 0 {
42867194d6 2026-09-13        arcade: 			bail!("Configuration file permissions are insecure {config_file:?}\n\
42867194d6 2026-09-13        arcade: 				Current permissions: {:o}\n\
42867194d6 2026-09-13        arcade: 				Required: 0600 (owner read/write only).\n\
42867194d6 2026-09-13        arcade: 				Fix with: chmod 600 {config_file:?}",
42867194d6 2026-09-13        arcade: 				meta.permissions().mode());
42867194d6 2026-09-13        arcade: 	}	}
f4660639ca 2026-07-31        arcade: 	let settings: config::Config = config::Config::builder()
f4660639ca 2026-07-31        arcade: 		.set_default("api_gateway", "https://api.telegram.org").stack()?
f4660639ca 2026-07-31        arcade: 		.set_default("fields", vec!["date", "from", "subject"]).stack()?
f4660639ca 2026-07-31        arcade: 		.set_default("hostname", "smtp.2.tg").stack()?
f4660639ca 2026-07-31        arcade: 		.set_default("listen_on", "0.0.0.0:1025").stack()?
f4660639ca 2026-07-31        arcade: 		.set_default("domains", vec!["localhost",
78b11b0319 2026-08-01        arcade: 			hostname::get().context("Failed to get current hostname")?
78b11b0319 2026-08-01        arcade: 			.to_str().context("Can't convert hostname to string, bad UTF-8?")?]).stack()?
f4660639ca 2026-07-31        arcade: 		.add_source(config::File::from(config_file))
f4660639ca 2026-07-31        arcade: 		.build()
42867194d6 2026-09-13        arcade: 		.with_context(|| format!(
42867194d6 2026-09-13        arcade: 			"Failed to parse configuration file: {config_file:?}\n\
42867194d6 2026-09-13        arcade: 			Check syntax against smtp2tg.toml.example.\n\
42867194d6 2026-09-13        arcade: 			Common issues: missing quotes, trailing commas in inline tables, or invalid types."
42867194d6 2026-09-13        arcade: 		))?;
f4660639ca 2026-07-31        arcade: 
f4660639ca 2026-07-31        arcade: 	let listen_on = settings.get_string("listen_on").stack()?;
f4660639ca 2026-07-31        arcade: 	let server_name = settings.get_string("hostname").stack()?;
f4660639ca 2026-07-31        arcade: 	let core = MailServer::new(settings)?;
f4660639ca 2026-07-31        arcade: 	let mut server = mailin_embedded::Server::new(core);
f4660639ca 2026-07-31        arcade: 
78b11b0319 2026-08-01        arcade: 	// TODO: remove unwraps when mailin-embedded bumps with better error handling
f4660639ca 2026-07-31        arcade: 	server.with_name(server_name)
f4660639ca 2026-07-31        arcade: 		.with_ssl(mailin_embedded::SslConfig::None).unwrap()
f4660639ca 2026-07-31        arcade: 		.with_addr(listen_on).unwrap();
f4660639ca 2026-07-31        arcade: 	server.serve().unwrap();
f4660639ca 2026-07-31        arcade: 
f4660639ca 2026-07-31        arcade: 	Ok(())
f4660639ca 2026-07-31        arcade: }