sync: also set flags

This commit is contained in:
FliegendeWurst 2021-03-29 15:19:32 +02:00 committed by Arne Keller
parent 15819138f9
commit 9658862e25
2 changed files with 38 additions and 4 deletions

View File

@ -1,12 +1,20 @@
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<()> {
let args = env::args().collect_vec();
if args.len() > 1 {
show_listing(&args[1])
} else {
show_listing("INBOX")
}
}
fn show_listing(mailbox: &str) -> Result<()> {
let maildir = get_maildir(mailbox)?;

View File

@ -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)])?;
}
}