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