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