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        arcade: //! Simple SMTP-to-Telegram gateway. Can parse email and send them as telegram
1db9dbe390 2024-11-28        arcade: //! messages to specified chats, generally you specify which email address is
1db9dbe390 2024-11-28        arcade: //! available in configuration, everything else is sent to default address.
1db9dbe390 2024-11-28        arcade: 
f5ed284f8c 2025-06-21        arcade: mod mail;
f5ed284f8c 2025-06-21        arcade: mod telegram;
f5ed284f8c 2025-06-21        arcade: mod utils;
f5ed284f8c 2025-06-21        arcade: 
f5ed284f8c 2025-06-21        arcade: #[cfg(test)]
f5ed284f8c 2025-06-21        arcade: mod tests;
f5ed284f8c 2025-06-21        arcade: 
f5ed284f8c 2025-06-21        arcade: use crate::mail::MailServer;
f5ed284f8c 2025-06-21        arcade: 
072229b5bf 2026-01-01        arcade: use smol::fs::metadata;
e66352b9cc 2025-01-21        arcade: use just_getopt::{
e66352b9cc 2025-01-21        arcade: 	OptFlags,
e66352b9cc 2025-01-21        arcade: 	OptSpecs,
6887d3b7f9 2025-06-05        arcade: 	OptValue,
a044f68fa7 2025-08-23        arcade: };
a044f68fa7 2025-08-23        arcade: use stacked_errors::{
a044f68fa7 2025-08-23        arcade: 	Result,
a044f68fa7 2025-08-23        arcade: 	StackableErr,
a044f68fa7 2025-08-23        arcade: 	bail,
f5ed284f8c 2025-06-21        arcade: };
f5ed284f8c 2025-06-21        arcade: 
f5ed284f8c 2025-06-21        arcade: use std::{
d96b1b4710 2025-06-11        arcade: 	io::Cursor,
d96b1b4710 2025-06-11        arcade: 	os::unix::fs::PermissionsExt,
d96b1b4710 2025-06-11        arcade: 	path::Path,
f5ed284f8c 2025-06-21        arcade: };
f5ed284f8c 2025-06-21        arcade: 
072229b5bf 2026-01-01        arcade: fn main () -> Result<()> {
072229b5bf 2026-01-01        arcade: 	smol::block_on(async {
072229b5bf 2026-01-01        arcade: 		let specs = OptSpecs::new()
072229b5bf 2026-01-01        arcade: 			.option("help", "h", OptValue::None)
072229b5bf 2026-01-01        arcade: 			.option("help", "help", OptValue::None)
072229b5bf 2026-01-01        arcade: 			.option("config", "c", OptValue::Required)
072229b5bf 2026-01-01        arcade: 			.option("config", "config", OptValue::Required)
072229b5bf 2026-01-01        arcade: 			.flag(OptFlags::OptionsEverywhere);
072229b5bf 2026-01-01        arcade: 		let mut args = std::env::args();
072229b5bf 2026-01-01        arcade: 		args.next();
072229b5bf 2026-01-01        arcade: 		let parsed = specs.getopt(args);
072229b5bf 2026-01-01        arcade: 		for u in &parsed.unknown {
072229b5bf 2026-01-01        arcade: 			println!("Unknown option: {u}");
072229b5bf 2026-01-01        arcade: 		}
072229b5bf 2026-01-01        arcade: 		if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
072229b5bf 2026-01-01        arcade: 			println!("SMTP2TG v{}, (C) 2024 - 2025\n\n\
072229b5bf 2026-01-01        arcade: 				\t-h|--help\tDisplay this help\n\
072229b5bf 2026-01-01        arcade: 				\t-c|--config …\tSet configuration file location.",
072229b5bf 2026-01-01        arcade: 				env!("CARGO_PKG_VERSION"));
072229b5bf 2026-01-01        arcade: 			return Ok(());
072229b5bf 2026-01-01        arcade: 		};
072229b5bf 2026-01-01        arcade: 		let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
072229b5bf 2026-01-01        arcade: 			&path[..]
072229b5bf 2026-01-01        arcade: 		} else {
072229b5bf 2026-01-01        arcade: 			"smtp2tg.toml"
072229b5bf 2026-01-01        arcade: 		});
072229b5bf 2026-01-01        arcade: 		if !config_file.exists() {
072229b5bf 2026-01-01        arcade: 			bail!("can't read configuration from {config_file:?}");
072229b5bf 2026-01-01        arcade: 		};
072229b5bf 2026-01-01        arcade: 		{
072229b5bf 2026-01-01        arcade: 			let meta = metadata(config_file).await.stack()?;
072229b5bf 2026-01-01        arcade: 			if (!0o100600 & meta.permissions().mode()) > 0 {
072229b5bf 2026-01-01        arcade: 				bail!("other users can read or write config file {config_file:?}\n\
072229b5bf 2026-01-01        arcade: 					File permissions: {:o}", meta.permissions().mode());
072229b5bf 2026-01-01        arcade: 			}
072229b5bf 2026-01-01        arcade: 		}
072229b5bf 2026-01-01        arcade: 		let settings: config::Config = config::Config::builder()
072229b5bf 2026-01-01        arcade: 			.set_default("fields", vec!["date", "from", "subject"]).stack()?
072229b5bf 2026-01-01        arcade: 			.set_default("hostname", "smtp.2.tg").stack()?
072229b5bf 2026-01-01        arcade: 			.set_default("listen_on", "0.0.0.0:1025").stack()?
072229b5bf 2026-01-01        arcade: 			.set_default("unknown", "relay").stack()?
072229b5bf 2026-01-01        arcade: 			.set_default("domains", vec!["localhost", hostname::get().stack()?.to_str().expect("Failed to get current hostname")]).stack()?
072229b5bf 2026-01-01        arcade: 			.add_source(config::File::from(config_file))
072229b5bf 2026-01-01        arcade: 			.build()
072229b5bf 2026-01-01        arcade: 			.with_context(|| format!("[{config_file:?}] there was an error reading config\n\
072229b5bf 2026-01-01        arcade: 				\tplease consult \"smtp2tg.toml.example\" for details"))?;
072229b5bf 2026-01-01        arcade: 
072229b5bf 2026-01-01        arcade: 		let listen_on = settings.get_string("listen_on").stack()?;
072229b5bf 2026-01-01        arcade: 		let server_name = settings.get_string("hostname").stack()?;
072229b5bf 2026-01-01        arcade: 		let core = MailServer::new(settings)?;
072229b5bf 2026-01-01        arcade: 		let mut server = mailin_embedded::Server::new(core);
072229b5bf 2026-01-01        arcade: 
072229b5bf 2026-01-01        arcade: 		server.with_name(server_name)
072229b5bf 2026-01-01        arcade: 			.with_ssl(mailin_embedded::SslConfig::None).unwrap()
072229b5bf 2026-01-01        arcade: 			.with_addr(listen_on).unwrap();
072229b5bf 2026-01-01        arcade: 		server.serve().unwrap();
072229b5bf 2026-01-01        arcade: 
072229b5bf 2026-01-01        arcade: 		Ok(())
072229b5bf 2026-01-01        arcade: 	})
7620f854a7 2024-05-21        arcade: }