From dd6e75d47249b4337d6a39e17becc1fba6315e32 Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Sat, 27 Mar 2021 19:17:10 +0100 Subject: [PATCH] Use UID as maildir filename --- Cargo.lock | 2 -- Cargo.toml | 3 ++- src/main.rs | 25 ++++++++++++------------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ab838c..0bdf8eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -233,8 +233,6 @@ dependencies = [ [[package]] name = "maildir" version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6adcf4693a3c725b9c69ccf24f8ed9c6d3e7168c1a45632570d65529adc13b5e" dependencies = [ "gethostname", "mailparse", diff --git a/Cargo.toml b/Cargo.toml index a64b4b8..53bb56f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,11 +3,12 @@ name = "inboxid" version = "0.1.0" authors = ["Arne Keller "] edition = "2018" +license = "GPL-3.0-or-later" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] imap = { version = "2.4.1", default-features = false } itertools = "0.10.0" -maildir = "0.5.0" +maildir = { path = "../maildir" } rustls-connector = "0.13.1" diff --git a/src/main.rs b/src/main.rs index c23d3f9..6c37c5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ fn fetch_inbox_top( // to do anything useful with the e-mails, we need to log in println!("logging in.."); let mut imap_session = client.login(&user, &password).map_err(|e| e.0)?; + println!("getting capabilities.."); let caps = imap_session.capabilities()?; println!("capabilities: {}", caps.iter().map(|x| format!("{:?}", x)).join(" ")); @@ -54,8 +55,8 @@ fn fetch_inbox_top( |x| { let mut fields = x.splitn(2, ','); // TODO(2038): check if mailservers still just return the mailbox creation time in seconds - let uid_validity = fields.next().map(|x| x.parse::().ok()).unwrap_or_default().unwrap_or(0); - let uid_last = fields.next().map(|x| x.parse::().ok()).unwrap_or_default().unwrap_or(0); + let uid_validity = fields.next().map(|x| x.trim().parse::().ok()).unwrap_or_default().unwrap_or(0); + let uid_last = fields.next().map(|x| x.trim().parse::().ok()).unwrap_or_default().unwrap_or(0); (uid_validity, uid_last) } ).unwrap_or((0, 0)); @@ -63,9 +64,8 @@ fn fetch_inbox_top( if uid_validity != prev_uid_validity { fetch_range = "1:*".to_owned(); // TODO: somehow remove invalidated messages - // (is the maildir standard really this annoying?) } else if uid_next != prev_uid + 1 { - fetch_range = format!("{}:*", prev_uid + 1) + fetch_range = format!("{}:*", prev_uid + 1); } else { println!("no new mail."); imap_session.logout()?; @@ -76,15 +76,14 @@ fn fetch_inbox_top( let messages = imap_session.uid_fetch(&fetch_range, "RFC822")?; let mut largest_uid = prev_uid; for mail in messages.iter() { - largest_uid = cmp::max(largest_uid, mail.uid.unwrap()); - println!("mail {:?}", mail.uid); - //let headers = mail.header().expect("message did not have headers!"); - let body = mail.body().unwrap_or_default(); - let mut data = Vec::new(); - //data.extend(headers); - //data.push(b'\n'); - data.extend(body); - maildir.store_new(&data)?; + let uid = mail.uid.unwrap(); + largest_uid = cmp::max(largest_uid, uid); + println!("mail {:?}", uid); + let id = format!("{}_{}", uid_validity, uid); + if !maildir.exists(&id).unwrap_or(false) { + let mail_data = mail.body().unwrap_or_default(); + maildir.store_new_with_id(&id, mail_data)?; + } } let uid = cmp::max(uid_next - 1, largest_uid); maildir.save_file(".uid", &format!("{},{}", uid_validity, uid))?;