Lines of
src/main.rs
from check-in 65b2967a92
that are changed by the sequence of edits moving toward
check-in c808d500e6:
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: 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) => {
65b2967a92 2025-01-25 134: eprintln!("[smtp2tg.toml] can't get \"unknown\":\n {}\n", err);
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
65b2967a92 2025-01-25 150: async fn debug <'a, S> (&self, msg: S) -> Result<Message, MyError>
65b2967a92 2025-01-25 151: where S: Into<&'a str> {
65b2967a92 2025-01-25 152: let msg = msg.into();
153: Ok(self.tg.send_message(*self.recipients.get("_").ok_or(MyError::NoDefault)?, encode(msg)).await?)
154: }
155:
156: /// Send message to specified user
157: async fn send <S> (&self, to: &ChatId, msg: S) -> Result<Message, MyError>
158: where S: Into<String> {
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
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 => {
65b2967a92 2025-01-25 178: self.debug(&*format!("Recipient [{}] not found.", &item)).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") {
65b2967a92 2025-01-25 202: short_headers.push(format!("__*From:*__ `{}`", encode(&headers.from[..])));
203: }
204: if self.fields.contains("date") {
205: if let Some(date) = mail.date() {
65b2967a92 2025-01-25 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 {
65b2967a92 2025-01-25 216: self.debug(&*format!("Hm, we have {} HTML parts and {} text parts.", html_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 = "".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 == "" && 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)
256: .ok_or(MyError::NoText)?);
257: text_num += 1;
258: }
259: while file_num < attachments {
260: files_to_send.push(mail.attachment(file_num)
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![];
269: let mut first_one = true;
270: for chunk in &files_to_send {
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: };
292: let item = teloxide::types::InputMediaDocument::new(
293: teloxide::types::InputFile::memory(data.to_vec())
294: .file_name(filename));
295: let item = if first_one {
296: first_one = false;
297: item.caption(&msg)
298: } else {
299: item
300: };
301: files.push(InputMedia::Document(item));
302: }
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
315: pub async fn sendgroup <M> (&self, to: &ChatId, media: M) -> Result<Vec<Message>, MyError>
316: where M: IntoIterator<Item = InputMedia> {
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
65b2967a92 2025-01-25 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
65b2967a92 2025-01-25 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", OptValueType::None)
390: .option("help", "help", OptValueType::None)
391: .option("config", "c", OptValueType::Required)
392: .option("config", "config", OptValueType::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 {
65b2967a92 2025-01-25 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() {
65b2967a92 2025-01-25 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 {
65b2967a92 2025-01-25 419: eprintln!("Error: other users can read or write config file {:?}\n\
65b2967a92 2025-01-25 420: File permissions: {:o}",
65b2967a92 2025-01-25 421: config_file, meta.permissions().mode());
422: std::process::exit(1);
423: }
424: }
425: let settings: config::Config = config::Config::builder()
426: .set_default("fields", vec!["date", "from", "subject"]).unwrap()
427: .set_default("hostname", "smtp.2.tg").unwrap()
428: .set_default("listen_on", "0.0.0.0:1025").unwrap()
429: .set_default("unknown", "relay").unwrap()
430: .add_source(config::File::from(config_file))
431: .build()
65b2967a92 2025-01-25 432: .expect(&format!("[{:?}] there was an error reading config\n\
65b2967a92 2025-01-25 433: \tplease consult \"smtp2tg.toml.example\" for details",
65b2967a92 2025-01-25 434: config_file)[..]);
435:
436: let listen_on = settings.get_string("listen_on")?;
437: let server_name = settings.get_string("hostname")?;
438: let core = TelegramTransport::new(settings);
439: let mut server = mailin_embedded::Server::new(core);
440:
441: server.with_name(server_name)
442: .with_ssl(mailin_embedded::SslConfig::None).unwrap()
443: .with_addr(listen_on).unwrap();
444: server.serve().unwrap();
445:
446: Ok(())
447: }