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