Lines of
src/main.rs
from check-in 2158b44929
that are changed by the sequence of edits moving toward
check-in f5ed284f8c:
1: //! Simple SMTP-to-Telegram gateway. Can parse email and send them as telegram
2: //! messages to specified chats, generally you specify which email address is
3: //! available in configuration, everything else is sent to default address.
4:
2158b44929 2025-06-12 5: use anyhow::Result;
6: use async_std::{
7: fs::metadata,
2158b44929 2025-06-12 8: io::Error,
2158b44929 2025-06-12 9: task,
10: };
11: use just_getopt::{
12: OptFlags,
13: OptSpecs,
14: OptValue,
15: };
2158b44929 2025-06-12 16: use lazy_static::lazy_static;
2158b44929 2025-06-12 17: use mailin_embedded::{
2158b44929 2025-06-12 18: Response,
2158b44929 2025-06-12 19: response::*,
2158b44929 2025-06-12 20: };
2158b44929 2025-06-12 21: use regex::{
2158b44929 2025-06-12 22: Regex,
2158b44929 2025-06-12 23: escape,
2158b44929 2025-06-12 24: };
2158b44929 2025-06-12 25: use tgbot::{
2158b44929 2025-06-12 26: api::Client,
2158b44929 2025-06-12 27: types::{
2158b44929 2025-06-12 28: ChatPeerId,
2158b44929 2025-06-12 29: InputFile,
2158b44929 2025-06-12 30: InputFileReader,
2158b44929 2025-06-12 31: InputMediaDocument,
2158b44929 2025-06-12 32: MediaGroup,
2158b44929 2025-06-12 33: MediaGroupItem,
2158b44929 2025-06-12 34: Message,
2158b44929 2025-06-12 35: ParseMode::MarkdownV2,
2158b44929 2025-06-12 36: SendDocument,
2158b44929 2025-06-12 37: SendMediaGroup,
2158b44929 2025-06-12 38: SendMessage,
2158b44929 2025-06-12 39: },
2158b44929 2025-06-12 40: };
2158b44929 2025-06-12 41: use thiserror::Error;
42:
43: use std::{
2158b44929 2025-06-12 44: borrow::Cow,
2158b44929 2025-06-12 45: collections::{
2158b44929 2025-06-12 46: HashMap,
2158b44929 2025-06-12 47: HashSet,
2158b44929 2025-06-12 48: },
49: io::Cursor,
50: os::unix::fs::PermissionsExt,
51: path::Path,
2158b44929 2025-06-12 52: vec::Vec,
53: };
2158b44929 2025-06-12 54:
2158b44929 2025-06-12 55: #[derive(Error, Debug)]
2158b44929 2025-06-12 56: pub enum MyError {
2158b44929 2025-06-12 57: #[error("Failed to parse mail")]
2158b44929 2025-06-12 58: BadMail,
2158b44929 2025-06-12 59: #[error("Missing default address in recipient table")]
2158b44929 2025-06-12 60: NoDefault,
2158b44929 2025-06-12 61: #[error("No headers found")]
2158b44929 2025-06-12 62: NoHeaders,
2158b44929 2025-06-12 63: #[error("No recipient addresses")]
2158b44929 2025-06-12 64: NoRecipient,
2158b44929 2025-06-12 65: #[error("Failed to extract text from message")]
2158b44929 2025-06-12 66: NoText,
2158b44929 2025-06-12 67: #[error(transparent)]
2158b44929 2025-06-12 68: RequestError(#[from] tgbot::api::ExecuteError),
2158b44929 2025-06-12 69: #[error(transparent)]
2158b44929 2025-06-12 70: TryFromIntError(#[from] std::num::TryFromIntError),
2158b44929 2025-06-12 71: #[error(transparent)]
2158b44929 2025-06-12 72: InputMediaError(#[from] tgbot::types::InputMediaError),
2158b44929 2025-06-12 73: #[error(transparent)]
2158b44929 2025-06-12 74: MediaGroupError(#[from] tgbot::types::MediaGroupError),
2158b44929 2025-06-12 75: }
2158b44929 2025-06-12 76:
2158b44929 2025-06-12 77: /// `SomeHeaders` object to store data through SMTP session
2158b44929 2025-06-12 78: #[derive(Clone, Debug)]
2158b44929 2025-06-12 79: struct SomeHeaders {
2158b44929 2025-06-12 80: from: String,
2158b44929 2025-06-12 81: to: Vec<String>,
2158b44929 2025-06-12 82: }
2158b44929 2025-06-12 83:
2158b44929 2025-06-12 84: struct Attachment {
2158b44929 2025-06-12 85: data: Cursor<Vec<u8>>,
2158b44929 2025-06-12 86: name: String,
2158b44929 2025-06-12 87: }
2158b44929 2025-06-12 88:
2158b44929 2025-06-12 89: /// `TelegramTransport` Central object with TG api and configuration
2158b44929 2025-06-12 90: #[derive(Clone)]
2158b44929 2025-06-12 91: struct TelegramTransport {
2158b44929 2025-06-12 92: data: Vec<u8>,
2158b44929 2025-06-12 93: headers: Option<SomeHeaders>,
2158b44929 2025-06-12 94: recipients: HashMap<String, ChatPeerId>,
2158b44929 2025-06-12 95: relay: bool,
2158b44929 2025-06-12 96: tg: Client,
2158b44929 2025-06-12 97: fields: HashSet<String>,
2158b44929 2025-06-12 98: address: Regex,
2158b44929 2025-06-12 99: }
2158b44929 2025-06-12 100:
2158b44929 2025-06-12 101: lazy_static! {
2158b44929 2025-06-12 102: static ref RE_SPECIAL: Regex = Regex::new(r"([\-_*\[\]()~`>#+|{}\.!])").unwrap();
2158b44929 2025-06-12 103: static ref RE_DOMAIN: Regex = Regex::new(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$").unwrap();
2158b44929 2025-06-12 104: }
2158b44929 2025-06-12 105:
2158b44929 2025-06-12 106: /// Encodes special HTML entities to prevent them interfering with Telegram HTML
2158b44929 2025-06-12 107: fn encode (text: &str) -> Cow<'_, str> {
2158b44929 2025-06-12 108: RE_SPECIAL.replace_all(text, "\\$1")
2158b44929 2025-06-12 109: }
2158b44929 2025-06-12 110:
2158b44929 2025-06-12 111: #[cfg(test)]
2158b44929 2025-06-12 112: mod tests {
2158b44929 2025-06-12 113: use crate::encode;
2158b44929 2025-06-12 114:
2158b44929 2025-06-12 115: #[test]
2158b44929 2025-06-12 116: fn check_regex () {
2158b44929 2025-06-12 117: let res = encode("-_*[]()~`>#+|{}.!");
2158b44929 2025-06-12 118: assert_eq!(res, "\\-\\_\\*\\[\\]\\(\\)\\~\\`\\>\\#\\+\\|\\{\\}\\.\\!");
2158b44929 2025-06-12 119: }
2158b44929 2025-06-12 120: }
2158b44929 2025-06-12 121:
2158b44929 2025-06-12 122: impl TelegramTransport {
2158b44929 2025-06-12 123: /// Initialize API and read configuration
2158b44929 2025-06-12 124: fn new(settings: config::Config) -> TelegramTransport {
2158b44929 2025-06-12 125: let tg = Client::new(settings.get_string("api_key")
2158b44929 2025-06-12 126: .expect("[smtp2tg.toml] missing \"api_key\" parameter.\n"))
2158b44929 2025-06-12 127: .expect("Failed to create API.\n");
2158b44929 2025-06-12 128: let recipients: HashMap<String, ChatPeerId> = settings.get_table("recipients")
2158b44929 2025-06-12 129: .expect("[smtp2tg.toml] missing table \"recipients\".\n")
2158b44929 2025-06-12 130: .into_iter().map(|(a, b)| (a, ChatPeerId::from(b.into_int()
2158b44929 2025-06-12 131: .expect("[smtp2tg.toml] \"recipient\" table values should be integers.\n")
2158b44929 2025-06-12 132: ))).collect();
2158b44929 2025-06-12 133: if !recipients.contains_key("_") {
2158b44929 2025-06-12 134: eprintln!("[smtp2tg.toml] \"recipient\" table misses \"default_recipient\".\n");
2158b44929 2025-06-12 135: panic!("no default recipient");
2158b44929 2025-06-12 136: }
2158b44929 2025-06-12 137: let fields = HashSet::<String>::from_iter(settings.get_array("fields")
2158b44929 2025-06-12 138: .expect("[smtp2tg.toml] \"fields\" should be an array")
2158b44929 2025-06-12 139: .iter().map(|x| x.clone().into_string().expect("should be strings")));
2158b44929 2025-06-12 140: let value = settings.get_string("unknown");
2158b44929 2025-06-12 141: let mut domains: HashSet<String> = HashSet::new();
2158b44929 2025-06-12 142: let extra_domains = settings.get_array("domains").unwrap();
2158b44929 2025-06-12 143: for domain in extra_domains {
2158b44929 2025-06-12 144: let domain = domain.to_string().to_lowercase();
2158b44929 2025-06-12 145: if RE_DOMAIN.is_match(&domain) {
2158b44929 2025-06-12 146: domains.insert(domain);
2158b44929 2025-06-12 147: } else {
2158b44929 2025-06-12 148: panic!("[smtp2tg.toml] can't check of domains in \"domains\": {domain}");
2158b44929 2025-06-12 149: }
2158b44929 2025-06-12 150: }
2158b44929 2025-06-12 151: let domains = domains.into_iter().map(|s| escape(&s))
2158b44929 2025-06-12 152: .collect::<Vec<String>>().join("|");
2158b44929 2025-06-12 153: let address = Regex::new(&format!("^(?P<user>[a-z0-9][-a-z0-9])(@({domains}))$")).unwrap();
2158b44929 2025-06-12 154: let relay = match value {
2158b44929 2025-06-12 155: Ok(value) => {
2158b44929 2025-06-12 156: match value.as_str() {
2158b44929 2025-06-12 157: "relay" => true,
2158b44929 2025-06-12 158: "deny" => false,
2158b44929 2025-06-12 159: _ => {
2158b44929 2025-06-12 160: eprintln!("[smtp2tg.toml] \"unknown\" should be either \"relay\" or \"deny\".\n");
2158b44929 2025-06-12 161: panic!("bad setting");
2158b44929 2025-06-12 162: },
2158b44929 2025-06-12 163: }
2158b44929 2025-06-12 164: },
2158b44929 2025-06-12 165: Err(err) => {
2158b44929 2025-06-12 166: eprintln!("[smtp2tg.toml] can't get \"unknown\":\n {err:?}\n");
2158b44929 2025-06-12 167: panic!("bad setting");
2158b44929 2025-06-12 168: },
2158b44929 2025-06-12 169: };
2158b44929 2025-06-12 170:
2158b44929 2025-06-12 171: TelegramTransport {
2158b44929 2025-06-12 172: data: vec!(),
2158b44929 2025-06-12 173: headers: None,
2158b44929 2025-06-12 174: recipients,
2158b44929 2025-06-12 175: relay,
2158b44929 2025-06-12 176: tg,
2158b44929 2025-06-12 177: fields,
2158b44929 2025-06-12 178: address,
2158b44929 2025-06-12 179: }
2158b44929 2025-06-12 180: }
2158b44929 2025-06-12 181:
2158b44929 2025-06-12 182: /// Send message to default user, used for debug/log/info purposes
2158b44929 2025-06-12 183: async fn debug (&self, msg: &str) -> Result<Message, MyError> {
2158b44929 2025-06-12 184: self.send(self.recipients.get("_").ok_or(MyError::NoDefault)?, encode(msg)).await
2158b44929 2025-06-12 185: }
2158b44929 2025-06-12 186:
2158b44929 2025-06-12 187: /// Send message to specified user
2158b44929 2025-06-12 188: async fn send <S> (&self, to: &ChatPeerId, msg: S) -> Result<Message, MyError>
2158b44929 2025-06-12 189: where S: Into<String> {
2158b44929 2025-06-12 190: Ok(self.tg.execute(
2158b44929 2025-06-12 191: SendMessage::new(*to, msg)
2158b44929 2025-06-12 192: .with_parse_mode(MarkdownV2)
2158b44929 2025-06-12 193: ).await?)
2158b44929 2025-06-12 194: }
2158b44929 2025-06-12 195:
2158b44929 2025-06-12 196: /// Returns id for provided email address
2158b44929 2025-06-12 197: fn get_id (&self, name: &str) -> Result<&ChatPeerId, MyError> {
2158b44929 2025-06-12 198: // here we need to store String locally to borrow it after
2158b44929 2025-06-12 199: let mut link = name;
2158b44929 2025-06-12 200: let name: String;
2158b44929 2025-06-12 201: if let Some(caps) = self.address.captures(link) {
2158b44929 2025-06-12 202: name = caps["name"].to_string();
2158b44929 2025-06-12 203: link = &name;
2158b44929 2025-06-12 204: }
2158b44929 2025-06-12 205: match self.recipients.get(link) {
2158b44929 2025-06-12 206: Some(addr) => Ok(addr),
2158b44929 2025-06-12 207: None => {
2158b44929 2025-06-12 208: self.recipients.get("_")
2158b44929 2025-06-12 209: .ok_or(MyError::NoDefault)
2158b44929 2025-06-12 210: }
2158b44929 2025-06-12 211: }
2158b44929 2025-06-12 212: }
2158b44929 2025-06-12 213:
2158b44929 2025-06-12 214: /// Attempt to deliver one message
2158b44929 2025-06-12 215: async fn relay_mail (&self) -> Result<(), MyError> {
2158b44929 2025-06-12 216: if let Some(headers) = &self.headers {
2158b44929 2025-06-12 217: let mail = mail_parser::MessageParser::new().parse(&self.data)
2158b44929 2025-06-12 218: .ok_or(MyError::BadMail)?;
2158b44929 2025-06-12 219:
2158b44929 2025-06-12 220: // Adding all known addresses to recipient list, for anyone else adding default
2158b44929 2025-06-12 221: // Also if list is empty also adding default
2158b44929 2025-06-12 222: let mut rcpt: HashSet<&ChatPeerId> = HashSet::new();
2158b44929 2025-06-12 223: if headers.to.is_empty() {
2158b44929 2025-06-12 224: return Err(MyError::NoRecipient);
2158b44929 2025-06-12 225: }
2158b44929 2025-06-12 226: for item in &headers.to {
2158b44929 2025-06-12 227: rcpt.insert(self.get_id(item)?);
2158b44929 2025-06-12 228: };
2158b44929 2025-06-12 229: if rcpt.is_empty() {
2158b44929 2025-06-12 230: self.debug("No recipient or envelope address.").await?;
2158b44929 2025-06-12 231: rcpt.insert(self.recipients.get("_")
2158b44929 2025-06-12 232: .ok_or(MyError::NoDefault)?);
2158b44929 2025-06-12 233: };
2158b44929 2025-06-12 234:
2158b44929 2025-06-12 235: // prepating message header
2158b44929 2025-06-12 236: let mut reply: Vec<String> = vec![];
2158b44929 2025-06-12 237: if self.fields.contains("subject") {
2158b44929 2025-06-12 238: if let Some(subject) = mail.subject() {
2158b44929 2025-06-12 239: reply.push(format!("__*Subject:*__ `{}`", encode(subject)));
2158b44929 2025-06-12 240: } else if let Some(thread) = mail.thread_name() {
2158b44929 2025-06-12 241: reply.push(format!("__*Thread:*__ `{}`", encode(thread)));
2158b44929 2025-06-12 242: }
2158b44929 2025-06-12 243: }
2158b44929 2025-06-12 244: let mut short_headers: Vec<String> = vec![];
2158b44929 2025-06-12 245: // do we need to replace spaces here?
2158b44929 2025-06-12 246: if self.fields.contains("from") {
2158b44929 2025-06-12 247: short_headers.push(format!("__*From:*__ `{}`", encode(&headers.from)));
2158b44929 2025-06-12 248: }
2158b44929 2025-06-12 249: if self.fields.contains("date") {
2158b44929 2025-06-12 250: if let Some(date) = mail.date() {
2158b44929 2025-06-12 251: short_headers.push(format!("__*Date:*__ `{date}`"));
2158b44929 2025-06-12 252: }
2158b44929 2025-06-12 253: }
2158b44929 2025-06-12 254: reply.push(short_headers.join(" "));
2158b44929 2025-06-12 255: let header_size = reply.join(" ").len() + 1;
2158b44929 2025-06-12 256:
2158b44929 2025-06-12 257: let html_parts = mail.html_body_count();
2158b44929 2025-06-12 258: let text_parts = mail.text_body_count();
2158b44929 2025-06-12 259: let attachments = mail.attachment_count();
2158b44929 2025-06-12 260: if html_parts != text_parts {
2158b44929 2025-06-12 261: self.debug(&format!("Hm, we have {html_parts} HTML parts and {text_parts} text parts.")).await?;
2158b44929 2025-06-12 262: }
2158b44929 2025-06-12 263: //let mut html_num = 0;
2158b44929 2025-06-12 264: let mut text_num = 0;
2158b44929 2025-06-12 265: let mut file_num = 0;
2158b44929 2025-06-12 266: // let's display first html or text part as body
2158b44929 2025-06-12 267: let mut body: Cow<'_, str> = "".into();
2158b44929 2025-06-12 268: /*
2158b44929 2025-06-12 269: * actually I don't wanna parse that html stuff
2158b44929 2025-06-12 270: if html_parts > 0 {
2158b44929 2025-06-12 271: let text = mail.body_html(0).unwrap();
2158b44929 2025-06-12 272: if text.len() < 4096 - header_size {
2158b44929 2025-06-12 273: body = text;
2158b44929 2025-06-12 274: html_num = 1;
2158b44929 2025-06-12 275: }
2158b44929 2025-06-12 276: };
2158b44929 2025-06-12 277: */
2158b44929 2025-06-12 278: if body.is_empty() && text_parts > 0 {
2158b44929 2025-06-12 279: let text = mail.body_text(0)
2158b44929 2025-06-12 280: .ok_or(MyError::NoText)?;
2158b44929 2025-06-12 281: if text.len() < 4096 - header_size {
2158b44929 2025-06-12 282: body = text;
2158b44929 2025-06-12 283: text_num = 1;
2158b44929 2025-06-12 284: }
2158b44929 2025-06-12 285: };
2158b44929 2025-06-12 286: reply.push("```".into());
2158b44929 2025-06-12 287: reply.extend(body.lines().map(|x| x.into()));
2158b44929 2025-06-12 288: reply.push("```".into());
2158b44929 2025-06-12 289:
2158b44929 2025-06-12 290: // and let's collect all other attachment parts
2158b44929 2025-06-12 291: let mut files_to_send = vec![];
2158b44929 2025-06-12 292: /*
2158b44929 2025-06-12 293: * let's just skip html parts for now, they just duplicate text?
2158b44929 2025-06-12 294: while html_num < html_parts {
2158b44929 2025-06-12 295: files_to_send.push(mail.html_part(html_num).unwrap());
2158b44929 2025-06-12 296: html_num += 1;
2158b44929 2025-06-12 297: }
2158b44929 2025-06-12 298: */
2158b44929 2025-06-12 299: while text_num < text_parts {
2158b44929 2025-06-12 300: files_to_send.push(mail.text_part(text_num.try_into()?)
2158b44929 2025-06-12 301: .ok_or(MyError::NoText)?);
2158b44929 2025-06-12 302: text_num += 1;
2158b44929 2025-06-12 303: }
2158b44929 2025-06-12 304: while file_num < attachments {
2158b44929 2025-06-12 305: files_to_send.push(mail.attachment(file_num.try_into()?)
2158b44929 2025-06-12 306: .ok_or(MyError::NoText)?);
2158b44929 2025-06-12 307: file_num += 1;
2158b44929 2025-06-12 308: }
2158b44929 2025-06-12 309:
2158b44929 2025-06-12 310: let msg = reply.join("\n");
2158b44929 2025-06-12 311: for chat in rcpt {
2158b44929 2025-06-12 312: if !files_to_send.is_empty() {
2158b44929 2025-06-12 313: let mut files = vec![];
2158b44929 2025-06-12 314: // let mut first_one = true;
2158b44929 2025-06-12 315: for chunk in &files_to_send {
2158b44929 2025-06-12 316: let data: Vec<u8> = chunk.contents().to_vec();
2158b44929 2025-06-12 317: let mut filename: Option<String> = None;
2158b44929 2025-06-12 318: for header in chunk.headers() {
2158b44929 2025-06-12 319: if header.name() == "Content-Type" {
2158b44929 2025-06-12 320: match header.value() {
2158b44929 2025-06-12 321: mail_parser::HeaderValue::ContentType(contenttype) => {
2158b44929 2025-06-12 322: if let Some(fname) = contenttype.attribute("name") {
2158b44929 2025-06-12 323: filename = Some(fname.to_owned());
2158b44929 2025-06-12 324: }
2158b44929 2025-06-12 325: },
2158b44929 2025-06-12 326: _ => {
2158b44929 2025-06-12 327: self.debug("Attachment has bad ContentType header.").await?;
2158b44929 2025-06-12 328: },
2158b44929 2025-06-12 329: };
2158b44929 2025-06-12 330: };
2158b44929 2025-06-12 331: };
2158b44929 2025-06-12 332: let filename = if let Some(fname) = filename {
2158b44929 2025-06-12 333: fname
2158b44929 2025-06-12 334: } else {
2158b44929 2025-06-12 335: "Attachment.txt".into()
2158b44929 2025-06-12 336: };
2158b44929 2025-06-12 337: files.push(Attachment {
2158b44929 2025-06-12 338: data: Cursor::new(data),
2158b44929 2025-06-12 339: name: filename,
2158b44929 2025-06-12 340: });
2158b44929 2025-06-12 341: }
2158b44929 2025-06-12 342: self.sendgroup(chat, files, &msg).await?;
2158b44929 2025-06-12 343: } else {
2158b44929 2025-06-12 344: self.send(chat, &msg).await?;
2158b44929 2025-06-12 345: }
2158b44929 2025-06-12 346: }
2158b44929 2025-06-12 347: } else {
2158b44929 2025-06-12 348: return Err(MyError::NoHeaders);
2158b44929 2025-06-12 349: }
2158b44929 2025-06-12 350: Ok(())
2158b44929 2025-06-12 351: }
2158b44929 2025-06-12 352:
2158b44929 2025-06-12 353: /// Send media to specified user
2158b44929 2025-06-12 354: pub async fn sendgroup (&self, to: &ChatPeerId, media: Vec<Attachment>, msg: &str) -> Result<(), MyError> {
2158b44929 2025-06-12 355: if media.len() > 1 {
2158b44929 2025-06-12 356: let mut attach = vec![];
2158b44929 2025-06-12 357: let mut pos = media.len();
2158b44929 2025-06-12 358: for file in media {
2158b44929 2025-06-12 359: let mut caption = InputMediaDocument::default();
2158b44929 2025-06-12 360: if pos == 1 {
2158b44929 2025-06-12 361: caption = caption.with_caption(msg)
2158b44929 2025-06-12 362: .with_caption_parse_mode(MarkdownV2);
2158b44929 2025-06-12 363: }
2158b44929 2025-06-12 364: pos -= 1;
2158b44929 2025-06-12 365: attach.push(
2158b44929 2025-06-12 366: MediaGroupItem::for_document(
2158b44929 2025-06-12 367: InputFile::from(
2158b44929 2025-06-12 368: InputFileReader::from(file.data)
2158b44929 2025-06-12 369: .with_file_name(file.name)
2158b44929 2025-06-12 370: ),
2158b44929 2025-06-12 371: caption
2158b44929 2025-06-12 372: )
2158b44929 2025-06-12 373: );
2158b44929 2025-06-12 374: }
2158b44929 2025-06-12 375: self.tg.execute(SendMediaGroup::new(*to, MediaGroup::new(attach)?)).await?;
2158b44929 2025-06-12 376: } else {
2158b44929 2025-06-12 377: self.tg.execute(
2158b44929 2025-06-12 378: SendDocument::new(
2158b44929 2025-06-12 379: *to,
2158b44929 2025-06-12 380: InputFileReader::from(media[0].data.clone())
2158b44929 2025-06-12 381: .with_file_name(media[0].name.clone())
2158b44929 2025-06-12 382: ).with_caption(msg)
2158b44929 2025-06-12 383: .with_caption_parse_mode(MarkdownV2)
2158b44929 2025-06-12 384: ).await?;
2158b44929 2025-06-12 385: }
2158b44929 2025-06-12 386: Ok(())
2158b44929 2025-06-12 387: }
2158b44929 2025-06-12 388: }
2158b44929 2025-06-12 389:
2158b44929 2025-06-12 390: impl mailin_embedded::Handler for TelegramTransport {
2158b44929 2025-06-12 391: /// Just deny login auth
2158b44929 2025-06-12 392: fn auth_login (&mut self, _username: &str, _password: &str) -> Response {
2158b44929 2025-06-12 393: INVALID_CREDENTIALS
2158b44929 2025-06-12 394: }
2158b44929 2025-06-12 395:
2158b44929 2025-06-12 396: /// Just deny plain auth
2158b44929 2025-06-12 397: fn auth_plain (&mut self, _authorization_id: &str, _authentication_id: &str, _password: &str) -> Response {
2158b44929 2025-06-12 398: INVALID_CREDENTIALS
2158b44929 2025-06-12 399: }
2158b44929 2025-06-12 400:
2158b44929 2025-06-12 401: /// Verify whether address is deliverable
2158b44929 2025-06-12 402: fn rcpt (&mut self, to: &str) -> Response {
2158b44929 2025-06-12 403: if self.relay {
2158b44929 2025-06-12 404: OK
2158b44929 2025-06-12 405: } else {
2158b44929 2025-06-12 406: match self.get_id(to) {
2158b44929 2025-06-12 407: Ok(_) => OK,
2158b44929 2025-06-12 408: Err(_) => {
2158b44929 2025-06-12 409: if self.relay {
2158b44929 2025-06-12 410: OK
2158b44929 2025-06-12 411: } else {
2158b44929 2025-06-12 412: NO_MAILBOX
2158b44929 2025-06-12 413: }
2158b44929 2025-06-12 414: }
2158b44929 2025-06-12 415: }
2158b44929 2025-06-12 416: }
2158b44929 2025-06-12 417: }
2158b44929 2025-06-12 418:
2158b44929 2025-06-12 419: /// Save headers we need
2158b44929 2025-06-12 420: fn data_start (&mut self, _domain: &str, from: &str, _is8bit: bool, to: &[String]) -> Response {
2158b44929 2025-06-12 421: self.headers = Some(SomeHeaders{
2158b44929 2025-06-12 422: from: from.to_string(),
2158b44929 2025-06-12 423: to: to.to_vec(),
2158b44929 2025-06-12 424: });
2158b44929 2025-06-12 425: OK
2158b44929 2025-06-12 426: }
2158b44929 2025-06-12 427:
2158b44929 2025-06-12 428: /// Save chunk(?) of data
2158b44929 2025-06-12 429: fn data (&mut self, buf: &[u8]) -> Result<(), Error> {
2158b44929 2025-06-12 430: self.data.append(buf.to_vec().as_mut());
2158b44929 2025-06-12 431: Ok(())
2158b44929 2025-06-12 432: }
2158b44929 2025-06-12 433:
2158b44929 2025-06-12 434: /// Attempt to send email, return temporary error if that fails
2158b44929 2025-06-12 435: fn data_end (&mut self) -> Response {
2158b44929 2025-06-12 436: let mut result = OK;
2158b44929 2025-06-12 437: task::block_on(async {
2158b44929 2025-06-12 438: // relay mail
2158b44929 2025-06-12 439: if let Err(err) = self.relay_mail().await {
2158b44929 2025-06-12 440: result = INTERNAL_ERROR;
2158b44929 2025-06-12 441: // in case that fails - inform default recipient
2158b44929 2025-06-12 442: if let Err(err) = self.debug(&format!("Sending emails failed:\n{err:?}")).await {
2158b44929 2025-06-12 443: // in case that also fails - write some logs and bail
2158b44929 2025-06-12 444: eprintln!("{err:?}");
2158b44929 2025-06-12 445: };
2158b44929 2025-06-12 446: };
2158b44929 2025-06-12 447: });
2158b44929 2025-06-12 448: // clear - just in case
2158b44929 2025-06-12 449: self.data = vec![];
2158b44929 2025-06-12 450: self.headers = None;
2158b44929 2025-06-12 451: result
2158b44929 2025-06-12 452: }
2158b44929 2025-06-12 453: }
454:
455: #[async_std::main]
456: async fn main () -> Result<()> {
457: let specs = OptSpecs::new()
458: .option("help", "h", OptValue::None)
459: .option("help", "help", OptValue::None)
460: .option("config", "c", OptValue::Required)
461: .option("config", "config", OptValue::Required)
462: .flag(OptFlags::OptionsEverywhere);
463: let mut args = std::env::args();
464: args.next();
465: let parsed = specs.getopt(args);
466: for u in &parsed.unknown {
467: println!("Unknown option: {u}");
468: }
469: if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
470: println!("SMTP2TG v{}, (C) 2024 - 2025\n\n\
471: \t-h|--help\tDisplay this help\n\
2158b44929 2025-06-12 472: \t-c|-config …\tSet configuration file location.",
473: env!("CARGO_PKG_VERSION"));
474: return Ok(());
475: };
476: let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
477: &path[..]
478: } else {
479: "smtp2tg.toml"
480: });
481: if !config_file.exists() {
2158b44929 2025-06-12 482: eprintln!("Error: can't read configuration from {config_file:?}");
2158b44929 2025-06-12 483: std::process::exit(1);
484: };
485: {
486: let meta = metadata(config_file).await?;
487: if (!0o100600 & meta.permissions().mode()) > 0 {
2158b44929 2025-06-12 488: eprintln!("Error: other users can read or write config file {config_file:?}\n\
489: File permissions: {:o}", meta.permissions().mode());
2158b44929 2025-06-12 490: std::process::exit(1);
491: }
492: }
493: let settings: config::Config = config::Config::builder()
494: .set_default("fields", vec!["date", "from", "subject"]).unwrap()
495: .set_default("hostname", "smtp.2.tg").unwrap()
496: .set_default("listen_on", "0.0.0.0:1025").unwrap()
497: .set_default("unknown", "relay").unwrap()
498: .set_default("domains", vec!["localhost", hostname::get()?.to_str().expect("Failed to get current hostname")]).unwrap()
499: .add_source(config::File::from(config_file))
500: .build()
2158b44929 2025-06-12 501: .unwrap_or_else(|_| panic!("[{config_file:?}] there was an error reading config\n\
2158b44929 2025-06-12 502: \tplease consult \"smtp2tg.toml.example\" for details"));
503:
504: let listen_on = settings.get_string("listen_on")?;
505: let server_name = settings.get_string("hostname")?;
2158b44929 2025-06-12 506: let core = TelegramTransport::new(settings);
507: let mut server = mailin_embedded::Server::new(core);
508:
509: server.with_name(server_name)
510: .with_ssl(mailin_embedded::SslConfig::None).unwrap()
511: .with_addr(listen_on).unwrap();
512: server.serve().unwrap();
513:
514: Ok(())
515: }