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:
78b11b0319 2026-08-01 42: /// Main asynchronous entry point for the application.
78b11b0319 2026-08-01 43: ///
78b11b0319 2026-08-01 44: /// Parses command-line arguments, loads configuration, and starts the SMTP
78b11b0319 2026-08-01 45: /// server.
78b11b0319 2026-08-01 46: ///
78b11b0319 2026-08-01 47: /// # Errors
78b11b0319 2026-08-01 48: /// Returns an error if configuration is invalid, files are inaccessible, or
78b11b0319 2026-08-01 49: /// server fails to start.
f4660639ca 2026-07-31 50: pub async fn async_main () -> Result<()> {
78b11b0319 2026-08-01 51: let args = Args::parse();
78b11b0319 2026-08-01 52: let config_file = Path::new(&args.config);
f4660639ca 2026-07-31 53: if !config_file.exists() {
f4660639ca 2026-07-31 54: bail!("can't read configuration from {config_file:?}");
f4660639ca 2026-07-31 55: };
f4660639ca 2026-07-31 56: {
f4660639ca 2026-07-31 57: let meta = metadata(config_file).await.stack()?;
f4660639ca 2026-07-31 58: if (!0o100600 & meta.permissions().mode()) > 0 {
f4660639ca 2026-07-31 59: bail!("other users can read or write config file {config_file:?}\n\
f4660639ca 2026-07-31 60: File permissions: {:o}", meta.permissions().mode());
f4660639ca 2026-07-31 61: }
f4660639ca 2026-07-31 62: }
f4660639ca 2026-07-31 63: let settings: config::Config = config::Config::builder()
f4660639ca 2026-07-31 64: .set_default("api_gateway", "https://api.telegram.org").stack()?
f4660639ca 2026-07-31 65: .set_default("fields", vec!["date", "from", "subject"]).stack()?
f4660639ca 2026-07-31 66: .set_default("hostname", "smtp.2.tg").stack()?
f4660639ca 2026-07-31 67: .set_default("listen_on", "0.0.0.0:1025").stack()?
f4660639ca 2026-07-31 68: .set_default("domains", vec!["localhost",
78b11b0319 2026-08-01 69: hostname::get().context("Failed to get current hostname")?
78b11b0319 2026-08-01 70: .to_str().context("Can't convert hostname to string, bad UTF-8?")?]).stack()?
f4660639ca 2026-07-31 71: .add_source(config::File::from(config_file))
f4660639ca 2026-07-31 72: .build()
f4660639ca 2026-07-31 73: .with_context(|| format!("[{config_file:?}] there was an error reading config\n\
f4660639ca 2026-07-31 74: \tplease consult \"smtp2tg.toml.example\" for details"))?;
f4660639ca 2026-07-31 75:
f4660639ca 2026-07-31 76: let listen_on = settings.get_string("listen_on").stack()?;
f4660639ca 2026-07-31 77: let server_name = settings.get_string("hostname").stack()?;
f4660639ca 2026-07-31 78: let core = MailServer::new(settings)?;
f4660639ca 2026-07-31 79: let mut server = mailin_embedded::Server::new(core);
f4660639ca 2026-07-31 80:
78b11b0319 2026-08-01 81: // TODO: remove unwraps when mailin-embedded bumps with better error handling
f4660639ca 2026-07-31 82: server.with_name(server_name)
f4660639ca 2026-07-31 83: .with_ssl(mailin_embedded::SslConfig::None).unwrap()
f4660639ca 2026-07-31 84: .with_addr(listen_on).unwrap();
f4660639ca 2026-07-31 85: server.serve().unwrap();
f4660639ca 2026-07-31 86:
f4660639ca 2026-07-31 87: Ok(())
f4660639ca 2026-07-31 88: }