Annotation For src/main.rs
Logged in as anonymous

Origin for each line in src/main.rs from check-in 072229b5bf:

1db9dbe390 2024-11-28    1: //! Simple SMTP-to-Telegram gateway. Can parse email and send them as telegram
1db9dbe390 2024-11-28    2: //! messages to specified chats, generally you specify which email address is
1db9dbe390 2024-11-28    3: //! available in configuration, everything else is sent to default address.
1db9dbe390 2024-11-28    4: 
f5ed284f8c 2025-06-21    5: mod mail;
f5ed284f8c 2025-06-21    6: mod telegram;
f5ed284f8c 2025-06-21    7: mod utils;
f5ed284f8c 2025-06-21    8: 
f5ed284f8c 2025-06-21    9: #[cfg(test)]
f5ed284f8c 2025-06-21   10: mod tests;
f5ed284f8c 2025-06-21   11: 
f5ed284f8c 2025-06-21   12: use crate::mail::MailServer;
f5ed284f8c 2025-06-21   13: 
072229b5bf 2026-01-01   14: use smol::fs::metadata;
e66352b9cc 2025-01-21   15: use just_getopt::{
e66352b9cc 2025-01-21   16: 	OptFlags,
e66352b9cc 2025-01-21   17: 	OptSpecs,
6887d3b7f9 2025-06-05   18: 	OptValue,
a044f68fa7 2025-08-23   19: };
a044f68fa7 2025-08-23   20: use stacked_errors::{
a044f68fa7 2025-08-23   21: 	Result,
a044f68fa7 2025-08-23   22: 	StackableErr,
a044f68fa7 2025-08-23   23: 	bail,
f5ed284f8c 2025-06-21   24: };
f5ed284f8c 2025-06-21   25: 
f5ed284f8c 2025-06-21   26: use std::{
d96b1b4710 2025-06-11   27: 	io::Cursor,
d96b1b4710 2025-06-11   28: 	os::unix::fs::PermissionsExt,
d96b1b4710 2025-06-11   29: 	path::Path,
f5ed284f8c 2025-06-21   30: };
f5ed284f8c 2025-06-21   31: 
072229b5bf 2026-01-01   32: fn main () -> Result<()> {
072229b5bf 2026-01-01   33: 	smol::block_on(async {
072229b5bf 2026-01-01   34: 		let specs = OptSpecs::new()
072229b5bf 2026-01-01   35: 			.option("help", "h", OptValue::None)
072229b5bf 2026-01-01   36: 			.option("help", "help", OptValue::None)
072229b5bf 2026-01-01   37: 			.option("config", "c", OptValue::Required)
072229b5bf 2026-01-01   38: 			.option("config", "config", OptValue::Required)
072229b5bf 2026-01-01   39: 			.flag(OptFlags::OptionsEverywhere);
072229b5bf 2026-01-01   40: 		let mut args = std::env::args();
072229b5bf 2026-01-01   41: 		args.next();
072229b5bf 2026-01-01   42: 		let parsed = specs.getopt(args);
072229b5bf 2026-01-01   43: 		for u in &parsed.unknown {
072229b5bf 2026-01-01   44: 			println!("Unknown option: {u}");
072229b5bf 2026-01-01   45: 		}
072229b5bf 2026-01-01   46: 		if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
072229b5bf 2026-01-01   47: 			println!("SMTP2TG v{}, (C) 2024 - 2025\n\n\
072229b5bf 2026-01-01   48: 				\t-h|--help\tDisplay this help\n\
072229b5bf 2026-01-01   49: 				\t-c|--config …\tSet configuration file location.",
072229b5bf 2026-01-01   50: 				env!("CARGO_PKG_VERSION"));
072229b5bf 2026-01-01   51: 			return Ok(());
072229b5bf 2026-01-01   52: 		};
072229b5bf 2026-01-01   53: 		let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
072229b5bf 2026-01-01   54: 			&path[..]
072229b5bf 2026-01-01   55: 		} else {
072229b5bf 2026-01-01   56: 			"smtp2tg.toml"
072229b5bf 2026-01-01   57: 		});
072229b5bf 2026-01-01   58: 		if !config_file.exists() {
072229b5bf 2026-01-01   59: 			bail!("can't read configuration from {config_file:?}");
072229b5bf 2026-01-01   60: 		};
072229b5bf 2026-01-01   61: 		{
072229b5bf 2026-01-01   62: 			let meta = metadata(config_file).await.stack()?;
072229b5bf 2026-01-01   63: 			if (!0o100600 & meta.permissions().mode()) > 0 {
072229b5bf 2026-01-01   64: 				bail!("other users can read or write config file {config_file:?}\n\
072229b5bf 2026-01-01   65: 					File permissions: {:o}", meta.permissions().mode());
072229b5bf 2026-01-01   66: 			}
072229b5bf 2026-01-01   67: 		}
072229b5bf 2026-01-01   68: 		let settings: config::Config = config::Config::builder()
072229b5bf 2026-01-01   69: 			.set_default("fields", vec!["date", "from", "subject"]).stack()?
072229b5bf 2026-01-01   70: 			.set_default("hostname", "smtp.2.tg").stack()?
072229b5bf 2026-01-01   71: 			.set_default("listen_on", "0.0.0.0:1025").stack()?
072229b5bf 2026-01-01   72: 			.set_default("unknown", "relay").stack()?
072229b5bf 2026-01-01   73: 			.set_default("domains", vec!["localhost", hostname::get().stack()?.to_str().expect("Failed to get current hostname")]).stack()?
072229b5bf 2026-01-01   74: 			.add_source(config::File::from(config_file))
072229b5bf 2026-01-01   75: 			.build()
072229b5bf 2026-01-01   76: 			.with_context(|| format!("[{config_file:?}] there was an error reading config\n\
072229b5bf 2026-01-01   77: 				\tplease consult \"smtp2tg.toml.example\" for details"))?;
072229b5bf 2026-01-01   78: 
072229b5bf 2026-01-01   79: 		let listen_on = settings.get_string("listen_on").stack()?;
072229b5bf 2026-01-01   80: 		let server_name = settings.get_string("hostname").stack()?;
072229b5bf 2026-01-01   81: 		let core = MailServer::new(settings)?;
072229b5bf 2026-01-01   82: 		let mut server = mailin_embedded::Server::new(core);
072229b5bf 2026-01-01   83: 
072229b5bf 2026-01-01   84: 		server.with_name(server_name)
072229b5bf 2026-01-01   85: 			.with_ssl(mailin_embedded::SslConfig::None).unwrap()
072229b5bf 2026-01-01   86: 			.with_addr(listen_on).unwrap();
072229b5bf 2026-01-01   87: 		server.serve().unwrap();
072229b5bf 2026-01-01   88: 
072229b5bf 2026-01-01   89: 		Ok(())
072229b5bf 2026-01-01   90: 	})
7620f854a7 2024-05-21   91: }