mirror of
https://github.com/chatmail/core.git
synced 2026-05-22 16:26:31 +03:00
Remove aheader module dependency on mailparse
This commit is contained in:
@@ -4,11 +4,9 @@
|
|||||||
|
|
||||||
use anyhow::{bail, Context as _, Error, Result};
|
use anyhow::{bail, Context as _, Error, Result};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use std::fmt;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::{fmt, str};
|
|
||||||
|
|
||||||
use crate::contact::addr_cmp;
|
|
||||||
use crate::headerdef::{HeaderDef, HeaderDefMap};
|
|
||||||
use crate::key::{DcKey, SignedPublicKey};
|
use crate::key::{DcKey, SignedPublicKey};
|
||||||
|
|
||||||
/// Possible values for encryption preference
|
/// 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;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
fn from_str(s: &str) -> Result<Self> {
|
||||||
@@ -69,29 +67,6 @@ impl Aheader {
|
|||||||
prefer_encrypt,
|
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 {
|
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;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
fn from_str(s: &str) -> Result<Self> {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
//! End-to-end decryption support.
|
//! End-to-end decryption support.
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use mailparse::ParsedMail;
|
use mailparse::ParsedMail;
|
||||||
@@ -13,7 +14,6 @@ use crate::context::Context;
|
|||||||
use crate::headerdef::{HeaderDef, HeaderDefMap};
|
use crate::headerdef::{HeaderDef, HeaderDefMap};
|
||||||
use crate::key::{DcKey, Fingerprint, SignedPublicKey, SignedSecretKey};
|
use crate::key::{DcKey, Fingerprint, SignedPublicKey, SignedSecretKey};
|
||||||
use crate::keyring::Keyring;
|
use crate::keyring::Keyring;
|
||||||
use crate::log::LogExt;
|
|
||||||
use crate::peerstate::Peerstate;
|
use crate::peerstate::Peerstate;
|
||||||
use crate::pgp;
|
use crate::pgp;
|
||||||
|
|
||||||
@@ -72,9 +72,25 @@ pub(crate) async fn prepare_decryption(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let autocrypt_header = Aheader::from_headers(from, &mail.headers)
|
let autocrypt_header =
|
||||||
.ok_or_log_msg(context, "Failed to parse Autocrypt header")
|
if let Some(autocrypt_header_value) = mail.headers.get_header_value(HeaderDef::Autocrypt) {
|
||||||
.flatten();
|
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?;
|
let dkim_results = handle_authres(context, mail, from, message_time).await?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user