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