From: Ruben Beltran del Rio Date: Mon, 27 Jan 2025 15:55:41 +0000 (+0100) Subject: Add compatibility middleware X-Git-Tag: 1.1.0~20 X-Git-Url: https://git.r.bdr.sh/rbdr/olden-mail/commitdiff_plain/573aaf2a8ccdb6c8c917b2d88a39c9c8103f64ef?ds=inline Add compatibility middleware --- diff --git a/src/main.rs b/src/main.rs index 930f7e5..832b6e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,6 +71,7 @@ use std::process::exit; use std::sync::mpsc; mod configuration; +mod middleware; mod proxy; use configuration::Configuration; diff --git a/src/middleware/find_mailboxes_compatibility.rs b/src/middleware/find_mailboxes_compatibility.rs new file mode 100644 index 0000000..f1a450b --- /dev/null +++ b/src/middleware/find_mailboxes_compatibility.rs @@ -0,0 +1,14 @@ +use log::debug; + +/// MailDrop can't find folders to sync because it sends FIND MAILBOXES /* +/// which is not understood by modern servers. It instead replaces it with +/// a LIST command. +pub fn middleware(input: &[u8]) -> Vec { + let command = String::from_utf8_lossy(input); + if let Some(tag) = command.split("FIND MAILBOXES /*").next() { + let replacement = format!("{} LIST \"/INBOX\" \"*\"\r\n", tag.trim()); + debug!("### {replacement}"); + return replacement.into_bytes(); + } + return input.to_vec(); +} diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs new file mode 100644 index 0000000..a1070f4 --- /dev/null +++ b/src/middleware/mod.rs @@ -0,0 +1,7 @@ +mod find_mailboxes_compatibility; + +use find_mailboxes_compatibility::middleware as find_mailboxes_compatibility_middleware; + +type Middleware = fn(&[u8]) -> Vec; + +pub const MIDDLEWARE: [Middleware; 1] = [find_mailboxes_compatibility_middleware]; diff --git a/src/proxy.rs b/src/proxy.rs index 2551015..2798df3 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -42,6 +42,7 @@ use std::thread::{sleep, spawn, JoinHandle}; use std::time::Duration; use crate::configuration::Proxy; +use crate::middleware::MIDDLEWARE; /// A proxy server that listens for plaintext connections and forwards them /// via TLS. @@ -226,7 +227,13 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc) { } }; - let debug_str = String::from_utf8_lossy(&buffer[..bytes_read]) + let mut command = buffer[..bytes_read].to_vec(); + + for middleware in MIDDLEWARE { + command = middleware(&command); + } + + let debug_str = String::from_utf8_lossy(&command) .replace('\n', "\\n") .replace('\r', "\\r") .replace('\t', "\\t");