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