Remove aheader module dependency on mailparse

This commit is contained in:
link2xt
2023-01-10 12:41:17 +00:00
parent 68cd8dbc3e
commit 5ae6c25394
2 changed files with 23 additions and 32 deletions

View File

@@ -4,11 +4,9 @@
use anyhow::{bail, Context as _, Error, Result};
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::{fmt, str};
use crate::contact::addr_cmp;
use crate::headerdef::{HeaderDef, HeaderDefMap};
use crate::key::{DcKey, SignedPublicKey};
/// Possible values for encryption preference
@@ -36,7 +34,7 @@ impl fmt::Display for EncryptPreference {
}
}
impl str::FromStr for EncryptPreference {
impl FromStr for EncryptPreference {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
@@ -69,29 +67,6 @@ impl Aheader {
prefer_encrypt,
}
}
/// Tries to parse Autocrypt header.
///
/// If there is none, returns None. If the header is present but cannot be parsed, returns an
/// error.
pub fn from_headers(
wanted_from: &str,
headers: &[mailparse::MailHeader<'_>],
) -> Result<Option<Self>> {
if let Some(value) = headers.get_header_value(HeaderDef::Autocrypt) {
let header = Self::from_str(&value)?;
if !addr_cmp(&header.addr, wanted_from) {
bail!(
"Autocrypt header address {:?} is not {:?}",
header.addr,
wanted_from
);
}
Ok(Some(header))
} else {
Ok(None)
}
}
}
impl fmt::Display for Aheader {
@@ -118,7 +93,7 @@ impl fmt::Display for Aheader {
}
}
impl str::FromStr for Aheader {
impl FromStr for Aheader {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {

View File

@@ -1,6 +1,7 @@
//! End-to-end decryption support.
use std::collections::HashSet;
use std::str::FromStr;
use anyhow::Result;
use mailparse::ParsedMail;
@@ -13,7 +14,6 @@ use crate::context::Context;
use crate::headerdef::{HeaderDef, HeaderDefMap};
use crate::key::{DcKey, Fingerprint, SignedPublicKey, SignedSecretKey};
use crate::keyring::Keyring;
use crate::log::LogExt;
use crate::peerstate::Peerstate;
use crate::pgp;
@@ -72,9 +72,25 @@ pub(crate) async fn prepare_decryption(
});
}
let autocrypt_header = Aheader::from_headers(from, &mail.headers)
.ok_or_log_msg(context, "Failed to parse Autocrypt header")
.flatten();
let autocrypt_header =
if let Some(autocrypt_header_value) = mail.headers.get_header_value(HeaderDef::Autocrypt) {
match Aheader::from_str(&autocrypt_header_value) {
Ok(header) if addr_cmp(&header.addr, from) => Some(header),
Ok(header) => {
warn!(
context,
"Autocrypt header address {:?} is not {:?}.", header.addr, from
);
None
}
Err(err) => {
warn!(context, "Failed to parse Autocrypt header: {:#}.", err);
None
}
}
} else {
None
};
let dkim_results = handle_authres(context, mail, from, message_time).await?;