Use UID as maildir filename

This commit is contained in:
FliegendeWurst 2021-03-27 19:17:10 +01:00 committed by Arne Keller
parent 749f1c3aed
commit dd6e75d472
3 changed files with 14 additions and 16 deletions

2
Cargo.lock generated
View File

@ -233,8 +233,6 @@ dependencies = [
[[package]] [[package]]
name = "maildir" name = "maildir"
version = "0.5.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6adcf4693a3c725b9c69ccf24f8ed9c6d3e7168c1a45632570d65529adc13b5e"
dependencies = [ dependencies = [
"gethostname", "gethostname",
"mailparse", "mailparse",

View File

@ -3,11 +3,12 @@ name = "inboxid"
version = "0.1.0" version = "0.1.0"
authors = ["Arne Keller <arne.keller@posteo.de>"] authors = ["Arne Keller <arne.keller@posteo.de>"]
edition = "2018" edition = "2018"
license = "GPL-3.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
imap = { version = "2.4.1", default-features = false } imap = { version = "2.4.1", default-features = false }
itertools = "0.10.0" itertools = "0.10.0"
maildir = "0.5.0" maildir = { path = "../maildir" }
rustls-connector = "0.13.1" rustls-connector = "0.13.1"

View File

@ -36,6 +36,7 @@ fn fetch_inbox_top(
// to do anything useful with the e-mails, we need to log in // to do anything useful with the e-mails, we need to log in
println!("logging in.."); println!("logging in..");
let mut imap_session = client.login(&user, &password).map_err(|e| e.0)?; let mut imap_session = client.login(&user, &password).map_err(|e| e.0)?;
println!("getting capabilities..");
let caps = imap_session.capabilities()?; let caps = imap_session.capabilities()?;
println!("capabilities: {}", caps.iter().map(|x| format!("{:?}", x)).join(" ")); println!("capabilities: {}", caps.iter().map(|x| format!("{:?}", x)).join(" "));
@ -54,8 +55,8 @@ fn fetch_inbox_top(
|x| { |x| {
let mut fields = x.splitn(2, ','); let mut fields = x.splitn(2, ',');
// TODO(2038): check if mailservers still just return the mailbox creation time in seconds // TODO(2038): check if mailservers still just return the mailbox creation time in seconds
let uid_validity = fields.next().map(|x| x.parse::<u32>().ok()).unwrap_or_default().unwrap_or(0); let uid_validity = fields.next().map(|x| x.trim().parse::<u32>().ok()).unwrap_or_default().unwrap_or(0);
let uid_last = fields.next().map(|x| x.parse::<u32>().ok()).unwrap_or_default().unwrap_or(0); let uid_last = fields.next().map(|x| x.trim().parse::<u32>().ok()).unwrap_or_default().unwrap_or(0);
(uid_validity, uid_last) (uid_validity, uid_last)
} }
).unwrap_or((0, 0)); ).unwrap_or((0, 0));
@ -63,9 +64,8 @@ fn fetch_inbox_top(
if uid_validity != prev_uid_validity { if uid_validity != prev_uid_validity {
fetch_range = "1:*".to_owned(); fetch_range = "1:*".to_owned();
// TODO: somehow remove invalidated messages // TODO: somehow remove invalidated messages
// (is the maildir standard really this annoying?)
} else if uid_next != prev_uid + 1 { } else if uid_next != prev_uid + 1 {
fetch_range = format!("{}:*", prev_uid + 1) fetch_range = format!("{}:*", prev_uid + 1);
} else { } else {
println!("no new mail."); println!("no new mail.");
imap_session.logout()?; imap_session.logout()?;
@ -76,15 +76,14 @@ fn fetch_inbox_top(
let messages = imap_session.uid_fetch(&fetch_range, "RFC822")?; let messages = imap_session.uid_fetch(&fetch_range, "RFC822")?;
let mut largest_uid = prev_uid; let mut largest_uid = prev_uid;
for mail in messages.iter() { for mail in messages.iter() {
largest_uid = cmp::max(largest_uid, mail.uid.unwrap()); let uid = mail.uid.unwrap();
println!("mail {:?}", mail.uid); largest_uid = cmp::max(largest_uid, uid);
//let headers = mail.header().expect("message did not have headers!"); println!("mail {:?}", uid);
let body = mail.body().unwrap_or_default(); let id = format!("{}_{}", uid_validity, uid);
let mut data = Vec::new(); if !maildir.exists(&id).unwrap_or(false) {
//data.extend(headers); let mail_data = mail.body().unwrap_or_default();
//data.push(b'\n'); maildir.store_new_with_id(&id, mail_data)?;
data.extend(body); }
maildir.store_new(&data)?;
} }
let uid = cmp::max(uid_next - 1, largest_uid); let uid = cmp::max(uid_next - 1, largest_uid);
maildir.save_file(".uid", &format!("{},{}", uid_validity, uid))?; maildir.save_file(".uid", &format!("{},{}", uid_validity, uid))?;