WIP attempt to parse List-Id header

This commit is contained in:
link2xt
2023-05-16 15:33:02 +00:00
parent 5bdc3cefb7
commit 80f59b3ff0
2 changed files with 32 additions and 6 deletions

View File

@@ -63,6 +63,10 @@ pub(crate) struct MimeMessage {
/// Whether the From address was repeated in the signed part /// Whether the From address was repeated in the signed part
/// (and we know that the signer intended to send from this address) /// (and we know that the signer intended to send from this address)
pub from_is_signed: bool, pub from_is_signed: bool,
/// List-Id header as defined in <https://datatracker.ietf.org/doc/html/rfc2919>.
pub list_id: Option<SingleInfo>,
pub list_post: Option<String>, pub list_post: Option<String>,
pub chat_disposition_notification_to: Option<SingleInfo>, pub chat_disposition_notification_to: Option<SingleInfo>,
pub decryption_info: DecryptionInfo, pub decryption_info: DecryptionInfo,
@@ -212,6 +216,7 @@ impl MimeMessage {
let mut headers = Default::default(); let mut headers = Default::default();
let mut recipients = Default::default(); let mut recipients = Default::default();
let mut from = Default::default(); let mut from = Default::default();
let mut list_id = Default::default();
let mut list_post = Default::default(); let mut list_post = Default::default();
let mut chat_disposition_notification_to = None; let mut chat_disposition_notification_to = None;
@@ -221,6 +226,7 @@ impl MimeMessage {
&mut headers, &mut headers,
&mut recipients, &mut recipients,
&mut from, &mut from,
&mut list_id,
&mut list_post, &mut list_post,
&mut chat_disposition_notification_to, &mut chat_disposition_notification_to,
&mail.headers, &mail.headers,
@@ -239,6 +245,7 @@ impl MimeMessage {
&mut headers, &mut headers,
&mut recipients, &mut recipients,
&mut from, &mut from,
&mut list_id,
&mut list_post, &mut list_post,
&mut chat_disposition_notification_to, &mut chat_disposition_notification_to,
&part.headers, &part.headers,
@@ -344,6 +351,7 @@ impl MimeMessage {
&mut headers, &mut headers,
&mut recipients, &mut recipients,
&mut signed_from, &mut signed_from,
&mut list_id,
&mut list_post, &mut list_post,
&mut chat_disposition_notification_to, &mut chat_disposition_notification_to,
&mail.headers, &mail.headers,
@@ -386,6 +394,7 @@ impl MimeMessage {
parts: Vec::new(), parts: Vec::new(),
header: headers, header: headers,
recipients, recipients,
list_id,
list_post, list_post,
from, from,
from_is_signed, from_is_signed,
@@ -1375,6 +1384,7 @@ impl MimeMessage {
headers: &mut HashMap<String, String>, headers: &mut HashMap<String, String>,
recipients: &mut Vec<SingleInfo>, recipients: &mut Vec<SingleInfo>,
from: &mut Option<SingleInfo>, from: &mut Option<SingleInfo>,
list_id: &mut Option<SingleInfo>,
list_post: &mut Option<String>, list_post: &mut Option<String>,
chat_disposition_notification_to: &mut Option<SingleInfo>, chat_disposition_notification_to: &mut Option<SingleInfo>,
fields: &[mailparse::MailHeader<'_>], fields: &[mailparse::MailHeader<'_>],
@@ -1392,6 +1402,14 @@ impl MimeMessage {
} }
Err(e) => warn!(context, "Could not read {} address: {}", key, e), Err(e) => warn!(context, "Could not read {} address: {}", key, e),
} }
} else if key == HeaderDef::ListId.get_headername() {
match addrparse_header(field) {
Ok(addrlist) => {
*list_id = addrlist.extract_single_info();
}
Err(e) => warn!(context, "Could not read {} address: {}", key, e),
}
eprintln!("LIST ID parsed as {:?}", *list_id);
} else { } else {
let value = field.get_value(); let value = field.get_value();
headers.insert(key.to_string(), value); headers.insert(key.to_string(), value);

View File

@@ -5,7 +5,7 @@ use std::collections::HashSet;
use std::convert::TryFrom; use std::convert::TryFrom;
use anyhow::{bail, ensure, Context as _, Result}; use anyhow::{bail, ensure, Context as _, Result};
use mailparse::{parse_mail, SingleInfo}; use mailparse::{addrparse, parse_mail, SingleInfo};
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
@@ -1835,15 +1835,23 @@ async fn create_or_lookup_mailinglist(
mime_parser: &MimeMessage, mime_parser: &MimeMessage,
) -> Result<Option<(ChatId, Blocked)>> { ) -> Result<Option<(ChatId, Blocked)>> {
static LIST_ID: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(.+)<(.+)>$").unwrap()); static LIST_ID: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(.+)<(.+)>$").unwrap());
eprintln!("List ID is {list_id_header:?}");
let (mut name, listid) = match LIST_ID.captures(list_id_header) { let (mut name, listid) = match LIST_ID.captures(list_id_header) {
Some(cap) => (cap[1].trim().to_string(), cap[2].trim().to_string()), Some(cap) => (cap[1].trim().to_string(), cap[2].trim().to_string()),
None => ( None => (
"".to_string(), "".to_string(),
list_id_header match addrparse(list_id_header)
.ok()
.and_then(|addrlist| addrlist.extract_single_info())
.and_then(|info| info.display_name)
{
Some(name) => name.clone(),
None => list_id_header
.trim() .trim()
.trim_start_matches('<') .trim_start_matches('<')
.trim_end_matches('>') .trim_end_matches('>')
.to_string(), .to_string(),
},
), ),
}; };