mirror of
https://github.com/chatmail/core.git
synced 2026-05-13 11:56:30 +03:00
fix: make vCard parsing more robust in case of trailing newlines
Contacts should be added only if there is an END:VCARD detected, not because we found the end of file.
This commit is contained in:
@@ -206,22 +206,21 @@ pub fn parse_vcard(vcard: &str) -> Vec<VcardContact> {
|
|||||||
} else if let Some(rev) = vcard_property(line, "rev") {
|
} else if let Some(rev) = vcard_property(line, "rev") {
|
||||||
datetime.get_or_insert(rev);
|
datetime.get_or_insert(rev);
|
||||||
} else if line.eq_ignore_ascii_case("END:VCARD") {
|
} else if line.eq_ignore_ascii_case("END:VCARD") {
|
||||||
|
let (authname, addr) =
|
||||||
|
sanitize_name_and_addr(display_name.unwrap_or(""), addr.unwrap_or(""));
|
||||||
|
|
||||||
|
contacts.push(VcardContact {
|
||||||
|
authname,
|
||||||
|
addr,
|
||||||
|
key: key.map(|s| s.to_string()),
|
||||||
|
profile_image: photo.map(|s| s.to_string()),
|
||||||
|
timestamp: datetime
|
||||||
|
.context("No timestamp in vcard")
|
||||||
|
.and_then(parse_datetime),
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let (authname, addr) =
|
|
||||||
sanitize_name_and_addr(display_name.unwrap_or(""), addr.unwrap_or(""));
|
|
||||||
|
|
||||||
contacts.push(VcardContact {
|
|
||||||
authname,
|
|
||||||
addr,
|
|
||||||
key: key.map(|s| s.to_string()),
|
|
||||||
profile_image: photo.map(|s| s.to_string()),
|
|
||||||
timestamp: datetime
|
|
||||||
.context("No timestamp in vcard")
|
|
||||||
.and_then(parse_datetime),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
contacts
|
contacts
|
||||||
@@ -540,6 +539,30 @@ END:VCARD",
|
|||||||
assert_eq!(contacts.len(), 1);
|
assert_eq!(contacts.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_vcard_with_trailing_newline() {
|
||||||
|
let contacts = parse_vcard(
|
||||||
|
"BEGIN:VCARD\r
|
||||||
|
VERSION:4.0\r
|
||||||
|
FN:Alice Wonderland\r
|
||||||
|
N:Wonderland;Alice;;;Ms.\r
|
||||||
|
GENDER:W\r
|
||||||
|
EMAIL;TYPE=work:alice@example.com\r
|
||||||
|
KEY;TYPE=PGP;ENCODING=b:[base64-data]\r
|
||||||
|
REV:20240418T184242Z\r
|
||||||
|
END:VCARD\r
|
||||||
|
\r",
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(contacts[0].addr, "alice@example.com".to_string());
|
||||||
|
assert_eq!(contacts[0].authname, "Alice Wonderland".to_string());
|
||||||
|
assert_eq!(contacts[0].key, Some("[base64-data]".to_string()));
|
||||||
|
assert_eq!(contacts[0].profile_image, None);
|
||||||
|
assert_eq!(*contacts[0].timestamp.as_ref().unwrap(), 1713465762);
|
||||||
|
|
||||||
|
assert_eq!(contacts.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_make_and_parse_vcard() {
|
fn test_make_and_parse_vcard() {
|
||||||
let contacts = [
|
let contacts = [
|
||||||
|
|||||||
Reference in New Issue
Block a user