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