Annotation For src/main.rs
Logged in as anonymous

Lines of src/main.rs from check-in c808d500e6 that are changed by the sequence of edits moving toward check-in 6887d3b7f9:

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