Lines of
src/lib.rs
from check-in 0acb6536ae
that are changed by the sequence of edits moving toward
check-in e678fcdc39:
1: //! SMTP-to-Telegram gateway main library.
2: //!
3: //! This module provides the core functionality for receiving emails via SMTP
4: //! and forwarding them to Telegram chats.
5: //!
6: //! As we are not actually exporting this lib there would be no local Error's
7: //! for now, everything will be just .stack()?'ed and propagated like in real
8: //! bin The lib here is just to separate all tests from main code into tests/
9:
10: pub mod mail;
11: mod telegram;
12: pub mod utils;
13:
14: use crate::mail::MailServer;
15:
16: use std::{
17: io::Cursor,
18: os::unix::fs::PermissionsExt,
19: path::Path,
20: };
21:
22: use clap::Parser;
23: use smol::{
24: fs::metadata,
25: };
26: use stacked_errors::{
27: Result,
28: StackableErr,
29: bail,
30: };
31:
32: /// SMTP-to-Telegram gateway
33: #[derive(Parser, Debug)]
34: #[command(name = "smtp2tg")]
35: #[command(about = format!("SMTP-to-Telegram gateway v{}, (C) 2024 - 2026", env!("CARGO_PKG_VERSION")), long_about = None)]
36: struct Args {
37: /// Set configuration file location
38: #[arg(short, long, default_value = "smtp2tg.toml")]
39: config: String,
40: }
41:
0acb6536ae 2026-09-10 42: /// Main asynchronous entry point for the application.
43: ///
0acb6536ae 2026-09-10 44: /// Parses command-line arguments, loads configuration, and starts the SMTP
0acb6536ae 2026-09-10 45: /// server.
46: ///
47: /// # Errors
0acb6536ae 2026-09-10 48: /// Returns an error if configuration is invalid, files are inaccessible, or
0acb6536ae 2026-09-10 49: /// server fails to start.
50: pub async fn async_main () -> Result<()> {
51: let args = Args::parse();
52: let config_file = Path::new(&args.config);
0acb6536ae 2026-09-10 53: if !config_file.exists() {
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\
0acb6536ae 2026-09-10 79: Common issues: missing quotes, trailing commas, 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: }