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