Be better about handling header errors

This commit is contained in:
Jon Gjengset 2017-05-03 21:41:22 -04:00
parent e7d5cece4d
commit ae72225a09
No known key found for this signature in database
GPG Key ID: D64AC9D67176DC71

View File

@ -204,30 +204,42 @@ fn main() {
let mut subjects = Vec::new(); let mut subjects = Vec::new();
if !uids.is_empty() { if !uids.is_empty() {
let mut finish = |message: &[u8]| { let mut finish = |message: &[u8]| -> bool {
if message.is_empty() { match mailparse::parse_headers(message) {
return; Ok((headers, _)) => {
}
if let Ok((headers, _)) = mailparse::parse_headers(message) {
use mailparse::MailHeaderMap; use mailparse::MailHeaderMap;
if let Ok(Some(subject)) = headers.get_first_value("Subject") { match headers.get_first_value("Subject") {
Ok(Some(subject)) => {
subjects.push(subject); subjects.push(subject);
return true;
}
Ok(None) => {
subjects.push(String::from("<no subject>"));
return true;
}
Err(e) => {
println!("failed to get message subject: {:?}", e);
} }
} }
}
Err(e) => println!("failed to parse headers of message: {:?}", e),
}
false
}; };
let lines = imap_socket let lines = imap_socket
.fetch(&uids.join(","), "RFC822.HEADER") .fetch(&uids.join(","), "RFC822.HEADER")
.unwrap(); .unwrap();
let mut message = Vec::new(); let mut message = Vec::new();
for line in lines { for line in &lines {
if line.starts_with("* ") { if line.starts_with("* ") {
if !message.is_empty() {
finish(&message[..]); finish(&message[..]);
message.clear(); message.clear();
}
continue; continue;
} }
message.extend(line.into_bytes()); message.extend(line.as_bytes());
} }
finish(&message[..]); finish(&message[..]);
} }