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