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