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