From 9658862e25c98fea6471045271c5b1cecfc53d51 Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Mon, 29 Mar 2021 15:19:32 +0200 Subject: [PATCH] sync: also set flags --- src/bin/new.rs | 10 +++++++++- src/bin/sync.rs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/bin/new.rs b/src/bin/new.rs index dadf14f..46c1b89 100644 --- a/src/bin/new.rs +++ b/src/bin/new.rs @@ -1,11 +1,19 @@ +use std::env; + use ascii_table::{Align, AsciiTable, Column}; use chrono::{Local, NaiveDateTime, TimeZone}; +use itertools::Itertools; use mailparse::{MailHeaderMap, dateparse}; use inboxid::*; fn main() -> Result<()> { - show_listing("INBOX") + let args = env::args().collect_vec(); + if args.len() > 1 { + show_listing(&args[1]) + } else { + show_listing("INBOX") + } } fn show_listing(mailbox: &str) -> Result<()> { diff --git a/src/bin/sync.rs b/src/bin/sync.rs index ad0abb9..824bda5 100644 --- a/src/bin/sync.rs +++ b/src/bin/sync.rs @@ -1,3 +1,5 @@ +#![feature(string_remove_matches)] + use std::{collections::HashMap, env}; use imap::types::Flag; @@ -88,7 +90,7 @@ fn sync( let maildir2 = &maildirs[mailbox]; let new_id = gen_id(uid1, uid2); println!("hardlinking: {}/{} -> {}/{}", inbox, local_id, mailbox, new_id); - maildir2.store_new_from_path(&new_id, name)?; + maildir2.store_cur_from_path(&new_id, name)?; save_mail.execute(params![mailbox, store_i64(*full_uid), message_id])?; } else { to_fetch.push(uid2); @@ -108,7 +110,7 @@ fn sync( let id = gen_id(uid_validity, uid); if !maildir.exists(&id) { let mail_data = mail.body().unwrap_or_default(); - maildir.store_new_with_id(&id, mail_data)?; + maildir.store_cur_with_id(&id, mail_data)?; let headers = parse_headers(&mail_data)?.0; let message_id = headers.get_all_values("Message-ID").join(" "); @@ -119,6 +121,30 @@ fn sync( } } } + let maildir = &maildirs[mailbox]; + for message_id in remote_mails.keys() { + let (uid1, uid2, _, ref flags) = remote_mails[message_id]; + let id = gen_id(uid1, uid2); + let _ = maildir.update_flags(&id, |f| { + let mut f = f.to_owned(); + if flags.contains(&Flag::Seen) { + f.push('S'); + } else { + f.remove_matches('S'); + } + if flags.contains(&Flag::Answered) { + f.push('R'); + } else { + f.remove_matches('R'); + } + if flags.contains(&Flag::Flagged) { + f.push('F'); + } else { + f.remove_matches('F'); + } + Maildir::normalize_flags(&f) + }); + } let mails = all_mail.query_map(params![mailbox], |row| Ok((load_i64(row.get::<_, i64>(0)?), row.get::<_, String>(1)?)))? .map(|x| x.unwrap()).collect_vec(); @@ -145,7 +171,7 @@ fn sync( let name = maildir.find_filename(&uid_name).unwrap(); maildirs[".gone"].store_new_from_path(&format!("{}_{}", mailbox, uid_name), name)?; maildir.delete(&uid_name)?; - delete_mail.execute(params![mailbox, uid])?; + delete_mail.execute(params![mailbox, store_i64(uid)])?; } }