Annotation For src/main.rs
Logged in as anonymous

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

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: 
a044f68fa7 2025-08-23   14: use async_std::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: };
7620f854a7 2024-05-21   25: 
7620f854a7 2024-05-21   26: use std::{
d96b1b4710 2025-06-11   27: 	io::Cursor,
65b2967a92 2025-01-25   28: 	os::unix::fs::PermissionsExt,
65b2967a92 2025-01-25   29: 	path::Path,
f5ed284f8c 2025-06-21   30: };
65b2967a92 2025-01-25   31: 
65b2967a92 2025-01-25   32: #[async_std::main]
65b2967a92 2025-01-25   33: async fn main () -> Result<()> {
65b2967a92 2025-01-25   34: 	let specs = OptSpecs::new()
6887d3b7f9 2025-06-05   35: 		.option("help", "h", OptValue::None)
6887d3b7f9 2025-06-05   36: 		.option("help", "help", OptValue::None)
6887d3b7f9 2025-06-05   37: 		.option("config", "c", OptValue::Required)
6887d3b7f9 2025-06-05   38: 		.option("config", "config", OptValue::Required)
e66352b9cc 2025-01-21   39: 		.flag(OptFlags::OptionsEverywhere);
e66352b9cc 2025-01-21   40: 	let mut args = std::env::args();
e66352b9cc 2025-01-21   41: 	args.next();
e66352b9cc 2025-01-21   42: 	let parsed = specs.getopt(args);
e66352b9cc 2025-01-21   43: 	for u in &parsed.unknown {
c808d500e6 2025-03-09   44: 		println!("Unknown option: {u}");
e66352b9cc 2025-01-21   45: 	}
e66352b9cc 2025-01-21   46: 	if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
e66352b9cc 2025-01-21   47: 		println!("SMTP2TG v{}, (C) 2024 - 2025\n\n\
e66352b9cc 2025-01-21   48: 			\t-h|--help\tDisplay this help\n\
f5ed284f8c 2025-06-21   49: 			\t-c|--config …\tSet configuration file location.",
e66352b9cc 2025-01-21   50: 			env!("CARGO_PKG_VERSION"));
e66352b9cc 2025-01-21   51: 		return Ok(());
e66352b9cc 2025-01-21   52: 	};
e66352b9cc 2025-01-21   53: 	let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
e66352b9cc 2025-01-21   54: 		&path[..]
e66352b9cc 2025-01-21   55: 	} else {
e66352b9cc 2025-01-21   56: 		"smtp2tg.toml"
e66352b9cc 2025-01-21   57: 	});
e66352b9cc 2025-01-21   58: 	if !config_file.exists() {
f5ed284f8c 2025-06-21   59: 		bail!("can't read configuration from {config_file:?}");
e66352b9cc 2025-01-21   60: 	};
cfe321bd6f 2025-01-23   61: 	{
a044f68fa7 2025-08-23   62: 		let meta = metadata(config_file).await.stack()?;
cfe321bd6f 2025-01-23   63: 		if (!0o100600 & meta.permissions().mode()) > 0 {
f5ed284f8c 2025-06-21   64: 			bail!("other users can read or write config file {config_file:?}\n\
c808d500e6 2025-03-09   65: 				File permissions: {:o}", meta.permissions().mode());
cfe321bd6f 2025-01-23   66: 		}
cfe321bd6f 2025-01-23   67: 	}
e66352b9cc 2025-01-21   68: 	let settings: config::Config = config::Config::builder()
a044f68fa7 2025-08-23   69: 		.set_default("fields", vec!["date", "from", "subject"]).stack()?
a044f68fa7 2025-08-23   70: 		.set_default("hostname", "smtp.2.tg").stack()?
a044f68fa7 2025-08-23   71: 		.set_default("listen_on", "0.0.0.0:1025").stack()?
a044f68fa7 2025-08-23   72: 		.set_default("unknown", "relay").stack()?
a044f68fa7 2025-08-23   73: 		.set_default("domains", vec!["localhost", hostname::get().stack()?.to_str().expect("Failed to get current hostname")]).stack()?
e66352b9cc 2025-01-21   74: 		.add_source(config::File::from(config_file))
e66352b9cc 2025-01-21   75: 		.build()
f5ed284f8c 2025-06-21   76: 		.with_context(|| format!("[{config_file:?}] there was an error reading config\n\
f5ed284f8c 2025-06-21   77: 			\tplease consult \"smtp2tg.toml.example\" for details"))?;
e66352b9cc 2025-01-21   78: 
a044f68fa7 2025-08-23   79: 	let listen_on = settings.get_string("listen_on").stack()?;
a044f68fa7 2025-08-23   80: 	let server_name = settings.get_string("hostname").stack()?;
f5ed284f8c 2025-06-21   81: 	let core = MailServer::new(settings)?;
1db9dbe390 2024-11-28   82: 	let mut server = mailin_embedded::Server::new(core);
1db9dbe390 2024-11-28   83: 
1db9dbe390 2024-11-28   84: 	server.with_name(server_name)
1db9dbe390 2024-11-28   85: 		.with_ssl(mailin_embedded::SslConfig::None).unwrap()
1db9dbe390 2024-11-28   86: 		.with_addr(listen_on).unwrap();
1db9dbe390 2024-11-28   87: 	server.serve().unwrap();
1db9dbe390 2024-11-28   88: 
1db9dbe390 2024-11-28   89: 	Ok(())
7620f854a7 2024-05-21   90: }