aboutsummaryrefslogtreecommitdiff
path: root/src/imap/mime_view.rs
diff options
context:
space:
mode:
authorQuentin <quentin@dufour.io>2024-01-08 10:39:26 +0000
committerQuentin <quentin@dufour.io>2024-01-08 10:39:26 +0000
commitd7788e29a8a64550e9b274001ff3fb9a7bf3473b (patch)
treee43a11753472f1917ce4aa6ddba24ae3a513bd50 /src/imap/mime_view.rs
parent152d5b7604337fe19a7aea7fc37b3d4615ca7393 (diff)
parent42a54b2c500294c594f3efdd25db28c18f5ac238 (diff)
downloadaerogramme-d7788e29a8a64550e9b274001ff3fb9a7bf3473b.tar.gz
aerogramme-d7788e29a8a64550e9b274001ff3fb9a7bf3473b.zip
Merge pull request 'Implement search' (#61) from feat/search into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/aerogramme/pulls/61
Diffstat (limited to 'src/imap/mime_view.rs')
-rw-r--r--src/imap/mime_view.rs33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/imap/mime_view.rs b/src/imap/mime_view.rs
index 1f36c47..cf6c751 100644
--- a/src/imap/mime_view.rs
+++ b/src/imap/mime_view.rs
@@ -12,7 +12,7 @@ use eml_codec::{
header, mime, mime::r#type::Deductible, part::composite, part::discrete, part::AnyPart,
};
-use crate::imap::imf_view::message_envelope;
+use crate::imap::imf_view::ImfView;
pub enum BodySection<'a> {
Full(Cow<'a, [u8]>),
@@ -164,8 +164,23 @@ impl<'a> SubsettedSection<'a> {
/// Used for current MIME inspection
///
/// See NodeMime for recursive logic
-struct SelectedMime<'a>(&'a AnyPart<'a>);
+pub struct SelectedMime<'a>(pub &'a AnyPart<'a>);
impl<'a> SelectedMime<'a> {
+ pub fn header_value(&'a self, to_match_ext: &[u8]) -> Option<&'a [u8]> {
+ let to_match = to_match_ext.to_ascii_lowercase();
+
+ self.eml_mime()
+ .kv
+ .iter()
+ .filter_map(|field| match field {
+ header::Field::Good(header::Kv2(k, v)) => Some((k, v)),
+ _ => None,
+ })
+ .find(|(k, _)| k.to_ascii_lowercase() == to_match)
+ .map(|(_, v)| v)
+ .copied()
+ }
+
/// The subsetted fetch section basically tells us the
/// extraction logic to apply on our selected MIME.
/// This function acts as a router for these logic.
@@ -200,6 +215,13 @@ impl<'a> SelectedMime<'a> {
Ok(ExtractedFull(bytes.to_vec().into()))
}
+ fn eml_mime(&self) -> &eml_codec::mime::NaiveMIME<'_> {
+ match &self.0 {
+ AnyPart::Msg(msg) => msg.child.mime(),
+ other => other.mime(),
+ }
+ }
+
/// The [...] HEADER.FIELDS, and HEADER.FIELDS.NOT part
/// specifiers refer to the [RFC-2822] header of the message or of
/// an encapsulated [MIME-IMT] MESSAGE/RFC822 message.
@@ -231,10 +253,7 @@ impl<'a> SelectedMime<'a> {
.collect::<HashSet<_>>();
// Extract MIME headers
- let mime = match &self.0 {
- AnyPart::Msg(msg) => msg.child.mime(),
- other => other.mime(),
- };
+ let mime = self.eml_mime();
// Filter our MIME headers based on the field index
// 1. Keep only the correctly formatted headers
@@ -347,7 +366,7 @@ impl<'a> NodeMsg<'a> {
body: FetchBody {
basic,
specific: SpecificFields::Message {
- envelope: Box::new(message_envelope(&self.1.imf)),
+ envelope: Box::new(ImfView(&self.1.imf).message_envelope()),
body_structure: Box::new(NodeMime(&self.1.child).structure()?),
number_of_lines: nol(self.1.raw_part),
},