Annotation For src/main.rs
Logged in as anonymous

Origin for each line in src/main.rs from check-in 9c12e26fb6:

7620f854a7 2024-05-21    1: use anyhow::Result;
7620f854a7 2024-05-21    2: use async_std::task;
7620f854a7 2024-05-21    3: use samotop::{
7620f854a7 2024-05-21    4: 	mail::{
7620f854a7 2024-05-21    5: 		Builder,
7620f854a7 2024-05-21    6: 		DebugService,
7620f854a7 2024-05-21    7: 		MailDir,
7620f854a7 2024-05-21    8: 		Name
7620f854a7 2024-05-21    9: 	},
51adce1e7e 2024-05-22   10: 	smtp::{
51adce1e7e 2024-05-22   11: 		SmtpParser,
51adce1e7e 2024-05-22   12: 		Prudence,
51adce1e7e 2024-05-22   13: 	},
7620f854a7 2024-05-21   14: };
7620f854a7 2024-05-21   15: use telegram_bot::{
7620f854a7 2024-05-21   16: 	Api,
7620f854a7 2024-05-21   17: 	ParseMode,
7620f854a7 2024-05-21   18: 	SendMessage,
7620f854a7 2024-05-21   19: 	UserId,
7620f854a7 2024-05-21   20: };
7620f854a7 2024-05-21   21: 
7620f854a7 2024-05-21   22: use std::{
7620f854a7 2024-05-21   23: 	borrow::Cow,
61238a3618 2024-05-22   24: 	collections::{
61238a3618 2024-05-22   25: 		HashMap,
61238a3618 2024-05-22   26: 		HashSet,
61238a3618 2024-05-22   27: 	},
7620f854a7 2024-05-21   28: 	io::Read,
7620f854a7 2024-05-21   29: 	path::{
7620f854a7 2024-05-21   30: 		Path,
7620f854a7 2024-05-21   31: 		PathBuf
7620f854a7 2024-05-21   32: 	},
7620f854a7 2024-05-21   33: 	time::Duration,
7620f854a7 2024-05-21   34: 	vec::Vec,
7620f854a7 2024-05-21   35: };
7620f854a7 2024-05-21   36: 
61238a3618 2024-05-22   37: fn address_into_iter<'a>(addr: &'a mail_parser::Address<'a, >) -> impl Iterator<Item = Cow<'a, str>> {
61238a3618 2024-05-22   38: 	addr.clone().into_list().into_iter().map(|a| a.address.unwrap())
61238a3618 2024-05-22   39: }
7620f854a7 2024-05-21   40: 
61238a3618 2024-05-22   41: fn relay_mails(maildir: &Path, core: &TelegramTransport) -> Result<()> {
7620f854a7 2024-05-21   42: 	let new_dir = maildir.join("new");
7620f854a7 2024-05-21   43: 
7620f854a7 2024-05-21   44: 	std::fs::create_dir_all(&new_dir)?;
7620f854a7 2024-05-21   45: 
7620f854a7 2024-05-21   46: 	let files = std::fs::read_dir(new_dir)?;
7620f854a7 2024-05-21   47: 	for file in files {
7620f854a7 2024-05-21   48: 		let file = file?;
7620f854a7 2024-05-21   49: 		let mut buf = Vec::new();
7620f854a7 2024-05-21   50: 		std::fs::File::open(file.path())?.read_to_end(&mut buf)?;
7620f854a7 2024-05-21   51: 
7620f854a7 2024-05-21   52: 		task::block_on(async move {
61238a3618 2024-05-22   53: 			match mail_parser::MessageParser::default().parse(&buf[..]) {
7620f854a7 2024-05-21   54: 				Some(mail) => {
61238a3618 2024-05-22   55: 					let mail = mail.clone();
61238a3618 2024-05-22   56: 
61238a3618 2024-05-22   57: 					// Fetching address lists from fields we know
61238a3618 2024-05-22   58: 					let mut to = HashSet::new();
61238a3618 2024-05-22   59: 					if let Some(addr) = mail.to() {
61238a3618 2024-05-22   60: 						let _ = address_into_iter(addr).map(|x| to.insert(x));
61238a3618 2024-05-22   61: 					};
61238a3618 2024-05-22   62: 					if let Some(addr) = mail.header("X-Samotop-To") {
61238a3618 2024-05-22   63: 						match addr {
61238a3618 2024-05-22   64: 							mail_parser::HeaderValue::Address(addr) => {
61238a3618 2024-05-22   65: 								let _ = address_into_iter(addr).map(|x| to.insert(x));
61238a3618 2024-05-22   66: 							},
61238a3618 2024-05-22   67: 							mail_parser::HeaderValue::Text(text) => {
61238a3618 2024-05-22   68: 								to.insert(text.clone());
61238a3618 2024-05-22   69: 							},
61238a3618 2024-05-22   70: 							_ => {}
61238a3618 2024-05-22   71: 						}
61238a3618 2024-05-22   72: 					};
61238a3618 2024-05-22   73: 
61238a3618 2024-05-22   74: 					// Adding all known addresses to recipient list, for anyone else adding default
61238a3618 2024-05-22   75: 					// Also if list is empty also adding default
61238a3618 2024-05-22   76: 					let mut rcpt: HashSet<UserId> = HashSet::new();
61238a3618 2024-05-22   77: 					for item in to {
61238a3618 2024-05-22   78: 						let item = item.into_owned();
61238a3618 2024-05-22   79: 						if core.recipients.contains_key(&item) {
61238a3618 2024-05-22   80: 							rcpt.insert(core.recipients[&item]);
61238a3618 2024-05-22   81: 						} else {
61238a3618 2024-05-22   82: 							core.debug(format!("Recipient [{}] not found.", &item)).await.unwrap();
61238a3618 2024-05-22   83: 							rcpt.insert(core.default);
61238a3618 2024-05-22   84: 						}
61238a3618 2024-05-22   85: 					};
61238a3618 2024-05-22   86: 					if rcpt.is_empty() {
61238a3618 2024-05-22   87: 						rcpt.insert(core.default);
61238a3618 2024-05-22   88: 						core.debug("No recipient or envelope address.").await.unwrap();
61238a3618 2024-05-22   89: 					};
61238a3618 2024-05-22   90: 
61238a3618 2024-05-22   91: 					// prepating message header
61238a3618 2024-05-22   92: 					let mut reply: Vec<Cow<str>> = vec![];
61238a3618 2024-05-22   93: 					if let Some(subject) = mail.subject() {
667b874fdb 2024-05-22   94: 						reply.push(format!("**Subject:** `{}`", subject).into());
61238a3618 2024-05-22   95: 					} else if let Some(thread) = mail.thread_name() {
667b874fdb 2024-05-22   96: 						reply.push(format!("**Thread:** `{}`", thread).into());
61238a3618 2024-05-22   97: 					}
61238a3618 2024-05-22   98: 					if let Some(from) = mail.from() {
03fe0265ac 2024-05-22   99: 						reply.push(format!("**From:** `{:?}`", address_into_iter(from).collect::<Vec<_>>().join(", ")).into());
61238a3618 2024-05-22  100: 					}
61238a3618 2024-05-22  101: 					if let Some(sender) = mail.sender() {
9c12e26fb6 2024-05-22  102: 						reply.push(format!("**Sender:** `{:?}`", address_into_iter(sender).collect::<Vec<_>>().join(", ")).into());
61238a3618 2024-05-22  103: 					}
61238a3618 2024-05-22  104: 					reply.push("".into());
61238a3618 2024-05-22  105: 					let header_size = reply.join("\n").len() + 1;
61238a3618 2024-05-22  106: 
61238a3618 2024-05-22  107: 					let html_parts = mail.html_body_count();
61238a3618 2024-05-22  108: 					let text_parts = mail.text_body_count();
61238a3618 2024-05-22  109: 					let attachments = mail.attachment_count();
61238a3618 2024-05-22  110: 					if html_parts != text_parts {
61238a3618 2024-05-22  111: 						core.debug(format!("Hm, we have {} HTML parts and {} text parts.", html_parts, text_parts)).await.unwrap();
61238a3618 2024-05-22  112: 					}
61238a3618 2024-05-22  113: 					//let mut html_num = 0;
61238a3618 2024-05-22  114: 					let mut text_num = 0;
61238a3618 2024-05-22  115: 					let mut file_num = 0;
61238a3618 2024-05-22  116: 					// let's display first html or text part as body
61238a3618 2024-05-22  117: 					let mut body = "".into();
7620f854a7 2024-05-21  118: 					/*
61238a3618 2024-05-22  119: 					 * actually I don't wanna parse that html stuff
61238a3618 2024-05-22  120: 					if html_parts > 0 {
61238a3618 2024-05-22  121: 						let text = mail.body_html(0).unwrap();
61238a3618 2024-05-22  122: 						if text.len() < 4096 - header_size {
61238a3618 2024-05-22  123: 							body = text;
61238a3618 2024-05-22  124: 							html_num = 1;
61238a3618 2024-05-22  125: 						}
61238a3618 2024-05-22  126: 					};
61238a3618 2024-05-22  127: 					*/
61238a3618 2024-05-22  128: 					if body == "" && text_parts > 0 {
61238a3618 2024-05-22  129: 						let text = mail.body_text(0).unwrap();
61238a3618 2024-05-22  130: 						if text.len() < 4096 - header_size {
61238a3618 2024-05-22  131: 							body = text;
61238a3618 2024-05-22  132: 							text_num = 1;
61238a3618 2024-05-22  133: 						}
7620f854a7 2024-05-21  134: 					};
667b874fdb 2024-05-22  135: 					reply.push("```".into());
03fe0265ac 2024-05-22  136: 					for line in body.lines() {
03fe0265ac 2024-05-22  137: 						reply.push(line.into());
03fe0265ac 2024-05-22  138: 					}
667b874fdb 2024-05-22  139: 					reply.push("```".into());
61238a3618 2024-05-22  140: 
61238a3618 2024-05-22  141: 					// and let's coillect all other attachment parts
61238a3618 2024-05-22  142: 					let mut files_to_send = vec![];
61238a3618 2024-05-22  143: 					/*
61238a3618 2024-05-22  144: 					 * let's just skip html parts for now, they just duplicate text?
61238a3618 2024-05-22  145: 					while html_num < html_parts {
61238a3618 2024-05-22  146: 						files_to_send.push(mail.html_part(html_num).unwrap());
61238a3618 2024-05-22  147: 						html_num += 1;
61238a3618 2024-05-22  148: 					}
7620f854a7 2024-05-21  149: 					*/
61238a3618 2024-05-22  150: 					while text_num < text_parts {
61238a3618 2024-05-22  151: 						files_to_send.push(mail.text_part(text_num).unwrap());
61238a3618 2024-05-22  152: 						text_num += 1;
61238a3618 2024-05-22  153: 					}
61238a3618 2024-05-22  154: 					while file_num < attachments {
61238a3618 2024-05-22  155: 						files_to_send.push(mail.attachment(file_num).unwrap());
61238a3618 2024-05-22  156: 						file_num += 1;
61238a3618 2024-05-22  157: 					}
61238a3618 2024-05-22  158: 
61238a3618 2024-05-22  159: 					for chat in rcpt {
61238a3618 2024-05-22  160: 						core.send(chat, reply.join("\n")).await.unwrap();
61238a3618 2024-05-22  161: 						for chunk in &files_to_send {
61238a3618 2024-05-22  162: 							let data = chunk.contents().to_vec();
61238a3618 2024-05-22  163: 							let obj = telegram_bot::types::InputFileUpload::with_data(data, "Attachment");
61238a3618 2024-05-22  164: 							core.sendfile(chat, obj).await.unwrap();
61238a3618 2024-05-22  165: 						}
61238a3618 2024-05-22  166: 					}
7620f854a7 2024-05-21  167: 				},
7620f854a7 2024-05-21  168: 				None => { core.debug("None mail.").await.unwrap(); },
7620f854a7 2024-05-21  169: 				//send_to_sendgrid(mail, sendgrid_api_key).await;
7620f854a7 2024-05-21  170: 			};
7620f854a7 2024-05-21  171: 		});
7620f854a7 2024-05-21  172: 
7620f854a7 2024-05-21  173: 		std::fs::remove_file(file.path())?;
7620f854a7 2024-05-21  174: 	}
7620f854a7 2024-05-21  175: 	Ok(())
7620f854a7 2024-05-21  176: }
7620f854a7 2024-05-21  177: 
7620f854a7 2024-05-21  178: fn my_prudence() -> Prudence {
7620f854a7 2024-05-21  179: 	Prudence::default().with_read_timeout(Duration::from_secs(60)).with_banner_delay(Duration::from_secs(1))
7620f854a7 2024-05-21  180: }
7620f854a7 2024-05-21  181: 
61238a3618 2024-05-22  182: pub struct TelegramTransport {
7620f854a7 2024-05-21  183: 	default: UserId,
7620f854a7 2024-05-21  184: 	tg: Api,
7620f854a7 2024-05-21  185: 	recipients: HashMap<String, UserId>,
7620f854a7 2024-05-21  186: }
7620f854a7 2024-05-21  187: 
61238a3618 2024-05-22  188: impl TelegramTransport {
61238a3618 2024-05-22  189: 	pub fn new(settings: &config::Config) -> TelegramTransport {
7620f854a7 2024-05-21  190: 		let api_key = settings.get_string("api_key").unwrap();
7620f854a7 2024-05-21  191: 		let tg = Api::new(api_key);
61238a3618 2024-05-22  192: 		let default_recipient = settings.get_string("default").unwrap();
61238a3618 2024-05-22  193: 		let recipients: HashMap<String, UserId> = settings.get_table("recipients").unwrap().into_iter().map(|(a, b)| (a, UserId::new(b.into_int().unwrap()))).collect();
61238a3618 2024-05-22  194: 		// Barf if no default
7620f854a7 2024-05-21  195: 		let default = recipients[&default_recipient];
7620f854a7 2024-05-21  196: 
61238a3618 2024-05-22  197: 		TelegramTransport {
7620f854a7 2024-05-21  198: 			default,
7620f854a7 2024-05-21  199: 			tg,
7620f854a7 2024-05-21  200: 			recipients,
61238a3618 2024-05-22  201: 		}
7620f854a7 2024-05-21  202: 	}
7620f854a7 2024-05-21  203: 
7620f854a7 2024-05-21  204: 	pub async fn debug<'b, S>(&self, msg: S) -> Result<()>
7620f854a7 2024-05-21  205: 	where S: Into<Cow<'b, str>> {
03fe0265ac 2024-05-22  206: 		task::sleep(Duration::from_secs(5)).await;
7620f854a7 2024-05-21  207: 		self.tg.send(SendMessage::new(self.default, msg)
03fe0265ac 2024-05-22  208: 			.parse_mode(ParseMode::Markdown)).await?;
7620f854a7 2024-05-21  209: 		Ok(())
7620f854a7 2024-05-21  210: 	}
7620f854a7 2024-05-21  211: 
61238a3618 2024-05-22  212: 	pub async fn send<'b, S>(&self, to: UserId, msg: S) -> Result<()>
7620f854a7 2024-05-21  213: 	where S: Into<Cow<'b, str>> {
03fe0265ac 2024-05-22  214: 		task::sleep(Duration::from_secs(5)).await;
61238a3618 2024-05-22  215: 		self.tg.send(SendMessage::new(to, msg)
03fe0265ac 2024-05-22  216: 			.parse_mode(ParseMode::Markdown)).await?;
61238a3618 2024-05-22  217: 		Ok(())
61238a3618 2024-05-22  218: 	}
61238a3618 2024-05-22  219: 
61238a3618 2024-05-22  220: 	pub async fn sendfile<V>(&self, to: UserId, chunk: V) -> Result<()>
61238a3618 2024-05-22  221: 	where V: Into<telegram_bot::InputFile> {
03fe0265ac 2024-05-22  222: 		task::sleep(Duration::from_secs(5)).await;
61238a3618 2024-05-22  223: 		self.tg.send(telegram_bot::SendDocument::new(to, chunk)).await?;
7620f854a7 2024-05-21  224: 		Ok(())
7620f854a7 2024-05-21  225: 	}
7620f854a7 2024-05-21  226: }
7620f854a7 2024-05-21  227: 
7620f854a7 2024-05-21  228: #[async_std::main]
7620f854a7 2024-05-21  229: async fn main() {
7620f854a7 2024-05-21  230: 	let settings: config::Config = config::Config::builder()
7620f854a7 2024-05-21  231: 		.add_source(config::File::with_name("smtp2tg.toml"))
7620f854a7 2024-05-21  232: 		.build().unwrap();
7620f854a7 2024-05-21  233: 
61238a3618 2024-05-22  234: 	let core = TelegramTransport::new(&settings);
7620f854a7 2024-05-21  235: 	let maildir: PathBuf = settings.get_string("maildir").unwrap().into();
7620f854a7 2024-05-21  236: 	let listen_on = settings.get_string("listen_on").unwrap();
7620f854a7 2024-05-21  237: 	let sink = Builder + Name::new("smtp2tg") + DebugService +
51adce1e7e 2024-05-22  238: 		my_prudence() + MailDir::new(maildir.clone()).unwrap();
7620f854a7 2024-05-21  239: 
7620f854a7 2024-05-21  240: 	task::spawn(async move {
7620f854a7 2024-05-21  241: 		loop {
7620f854a7 2024-05-21  242: 			relay_mails(&maildir, &core).unwrap();
61238a3618 2024-05-22  243: 			task::sleep(Duration::from_secs(5)).await;
7620f854a7 2024-05-21  244: 		}
7620f854a7 2024-05-21  245: 	});
7620f854a7 2024-05-21  246: 
7620f854a7 2024-05-21  247: 	match listen_on.as_str() {
51adce1e7e 2024-05-22  248: 		"socket" => {
51adce1e7e 2024-05-22  249: 			let sink = sink + samotop::smtp::Lmtp.with(SmtpParser);
51adce1e7e 2024-05-22  250: 			samotop::server::UnixServer::on("./smtp2tg.sock")
51adce1e7e 2024-05-22  251: 				.serve(sink.build()).await.unwrap();
51adce1e7e 2024-05-22  252: 		},
51adce1e7e 2024-05-22  253: 		_ => {
51adce1e7e 2024-05-22  254: 			let sink = sink + samotop::smtp::Esmtp.with(SmtpParser);
51adce1e7e 2024-05-22  255: 			samotop::server::TcpServer::on(listen_on)
51adce1e7e 2024-05-22  256: 				.serve(sink.build()).await.unwrap();
51adce1e7e 2024-05-22  257: 		},
7620f854a7 2024-05-21  258: 	};
7620f854a7 2024-05-21  259: }