Lines of
src/main.rs
from check-in 0f47e23e21
that are changed by the sequence of edits moving toward
check-in c996f5c871:
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: mod mail;
6: mod telegram;
7: mod utils;
8:
9: use crate::mail::MailServer;
10:
11: use async_compat::Compat;
12: use just_getopt::{
13: OptFlags,
14: OptSpecs,
15: OptValue,
16: };
17: use smol::{
18: fs::metadata,
19: };
20: use stacked_errors::{
21: Result,
22: StackableErr,
23: bail,
24: };
25:
26: use std::{
27: io::Cursor,
28: os::unix::fs::PermissionsExt,
29: path::Path,
30: };
31:
32: fn main () -> Result<()> {
33: smol::block_on(Compat::new(async {
34: async_main().await.unwrap()
35: }));
36:
37: Ok(())
38: }
39:
40: /// Actual main function running async with Error propagation support
41: async fn async_main () -> Result<()> {
42: let specs = OptSpecs::new()
43: .option("help", "h", OptValue::None)
44: .option("help", "help", OptValue::None)
45: .option("config", "c", OptValue::Required)
46: .option("config", "config", OptValue::Required)
47: .flag(OptFlags::OptionsEverywhere);
48: let mut args = std::env::args();
49: args.next();
50: let parsed = specs.getopt(args);
51: for u in &parsed.unknown {
52: println!("Unknown option: {u}");
53: }
54: if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
0f47e23e21 2026-01-12 55: println!("SMTP2TG v{}, (C) 2024 - 2025\n\n\
56: \t-h|--help\tDisplay this help\n\
57: \t-c|--config …\tSet configuration file location.",
58: env!("CARGO_PKG_VERSION"));
59: return Ok(());
60: };
61: let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
62: &path[..]
63: } else {
64: "smtp2tg.toml"
65: });
66: if !config_file.exists() {
67: bail!("can't read configuration from {config_file:?}");
68: };
69: {
70: let meta = metadata(config_file).await.stack()?;
71: if (!0o100600 & meta.permissions().mode()) > 0 {
72: bail!("other users can read or write config file {config_file:?}\n\
73: File permissions: {:o}", meta.permissions().mode());
74: }
75: }
76: let settings: config::Config = config::Config::builder()
77: .set_default("api_gateway", "https://api.telegram.org").stack()?
78: .set_default("fields", vec!["date", "from", "subject"]).stack()?
79: .set_default("hostname", "smtp.2.tg").stack()?
80: .set_default("listen_on", "0.0.0.0:1025").stack()?
81: .set_default("unknown", "relay").stack()?
82: .set_default("domains", vec!["localhost", hostname::get().stack()?.to_str().expect("Failed to get current hostname")]).stack()?
83: .add_source(config::File::from(config_file))
84: .build()
85: .with_context(|| format!("[{config_file:?}] there was an error reading config\n\
86: \tplease consult \"smtp2tg.toml.example\" for details"))?;
87:
88: let listen_on = settings.get_string("listen_on").stack()?;
89: let server_name = settings.get_string("hostname").stack()?;
90: let core = MailServer::new(settings)?;
91: let mut server = mailin_embedded::Server::new(core);
92:
93: server.with_name(server_name)
94: .with_ssl(mailin_embedded::SslConfig::None).unwrap()
95: .with_addr(listen_on).unwrap();
96: server.serve().unwrap();
97:
98: Ok(())
99: }