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