2017-09-07 23:00:08 +03:00
|
|
|
/*
|
|
|
|
* meli - email module
|
|
|
|
*
|
|
|
|
* Copyright 2017 Manos Pitsidianakis
|
|
|
|
*
|
|
|
|
* This file is part of meli.
|
|
|
|
*
|
|
|
|
* meli is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* meli is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-07-01 16:20:59 +03:00
|
|
|
//! Email parsing and composing.
|
|
|
|
//!
|
|
|
|
//! # Parsing bytes into an `Envelope`
|
|
|
|
//!
|
2023-12-17 19:14:00 +02:00
|
|
|
//! An [`Envelope`] represents the information you can get from an email's
|
|
|
|
//! headers and body structure. Addresses in `To`, `From` fields etc are parsed
|
|
|
|
//! into [`Address`] types.
|
2023-07-01 16:20:59 +03:00
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use melib::{Attachment, Envelope};
|
|
|
|
//!
|
|
|
|
//! let raw_mail = r#"From: "some name" <some@example.com>
|
|
|
|
//! To: "me" <myself@example.com>
|
|
|
|
//! Cc:
|
|
|
|
//! Subject: =?utf-8?Q?gratuitously_encoded_subject?=
|
|
|
|
//! Message-ID: <h2g7f.z0gy2pgaen5m@example.com>
|
|
|
|
//! MIME-Version: 1.0
|
|
|
|
//! Content-Type: multipart/mixed; charset="utf-8";
|
|
|
|
//! boundary="bzz_bzz__bzz__"
|
|
|
|
//!
|
|
|
|
//! This is a MIME formatted message with attachments. Use a MIME-compliant client to view it properly.
|
|
|
|
//! --bzz_bzz__bzz__
|
|
|
|
//!
|
|
|
|
//! hello world.
|
|
|
|
//! --bzz_bzz__bzz__
|
|
|
|
//! Content-Type: image/gif; name="test_image.gif"; charset="utf-8"
|
|
|
|
//! Content-Disposition: attachment
|
|
|
|
//! Content-Transfer-Encoding: base64
|
|
|
|
//!
|
|
|
|
//! R0lGODdhKAAXAOfZAAABzAADzQAEzgQFtBEAxAAGxBcAxwALvRcFwAAPwBcLugATuQEUuxoNuxYQ
|
|
|
|
//! sxwOvAYVvBsStSAVtx8YsRUcuhwhth4iuCQsyDAwuDc1vTc3uDg4uT85rkc9ukJBvENCvURGukdF
|
|
|
|
//! wUVKt0hLuUxPvVZSvFlYu1hbt2BZuFxdul5joGhqlnNuf3FvlnBvwXJyt3Jxw3N0oXx1gH12gV99
|
|
|
|
//! z317f3N7spFxwHp5wH99gYB+goF/g25+26tziIOBhWqD3oiBjICAuudkjIN+zHeC2n6Bzc1vh4eF
|
|
|
|
//! iYaBw8F0kImHi4KFxYyHmIWIvI2Lj4uIvYaJyY+IuJGMi5iJl4qKxZSMmIuLxpONnpGPk42NvI2M
|
|
|
|
//! 1LKGl46OvZePm5ORlZiQnJqSnpaUmLyJnJuTn5iVmZyUoJGVyZ2VoZSVw5iXoZmWrO18rJiUyp6W
|
|
|
|
//! opuYnKaVnZ+Xo5yZncaMoaCYpJiaqo+Z2Z2annuf5qGZpa2WoJybpZmayZ2Z0KCZypydrZ6dp6Cd
|
|
|
|
//! oZ6a0aGay5ucy5+eqKGeouWMgp+b0qKbzKCfqdqPnp2ezaGgqqOgpKafqrScpp+gz6ajqKujr62j
|
|
|
|
//! qayksKmmq62lsaiosqqorOyWnaqqtKeqzLGptaurta2rr7Kqtq+ssLOrt6+uuLGusuqhfbWtubCv
|
|
|
|
//! ubKvs7GwurOwtPSazbevu+ali7SxtbiwvOykjLOyvLWytuCmqOankrSzvbazuLmyvrW0vre0uba1
|
|
|
|
//! wLi1ury0wLm2u721wbe3wbq3vMC2vLi4wr+3w7m5w8C4xLi6yry6vsG5xbu7xcC6zMK6xry8xry+
|
|
|
|
//! u8O7x729x8C9wb++yMG+wsO+vMK/w8a+y8e/zMnBzcXH18nL2///////////////////////////
|
|
|
|
//! ////////////////////////////////////////////////////////////////////////////
|
|
|
|
//! /////////////////////////////////////////////////////ywAAAAAKAAXAAAI/gBP4Cjh
|
|
|
|
//! IYMLEh0w4EgBgsMLEyFGFBEB5cOFABgzatS4AVssZAOsLOHCxooVMzCyoNmzaBOkJlS0VEDyZMjG
|
|
|
|
//! mxk3XOMF60CDBgsoPABK9KcDCRImPCiQYAECAgQCRMU4VSrGCjFarBgUSJCgQ10FBTrkNRCfPnz4
|
|
|
|
//! dA3UNa1btnDZqgU7Ntqzu3ej2X2mFy9eaHuhNRtMGJrhwYYN930G2K7eaNIY34U2mfJkwpgzI9Yr
|
|
|
|
//! GBqwR2KSvAlMOXHnw5pTNzPdLNoWIWtU9XjGjDEYS8LAlFm1SrVvzIKj5TH0KpORSZOryPgCZgqL
|
|
|
|
//! Ob+jG0YVRBErUrOiiGJ8KxgtYsh27xWL/tswnTtEbsiRVYdJNMHk4yOGhswGjR88UKjQ9Ey+/8TL
|
|
|
|
//! XKKGGn7Akph/8XX2WDTTcAYfguVt9hhrEPqmzIOJ3VUheb48WJiHG6amC4i+WVJKKCimqGIoYxyj
|
|
|
|
//! WWK8kKjaJ9bA18sxvXjYhourmbbMMrjI+OIn1QymDCVXANGFK4S1gQw0PxozzC+33FLLKUJq9gk1
|
|
|
|
//! gyWDhyNwrMLkYGUEM4wvuLRiCiieXIJJJVlmJskcZ9TZRht1lnFGGmTMkMoonVQSSSOFAGJHHI0w
|
|
|
|
//! ouiijDaaCCGQRgrpH3q4QYYXWDihxBE+7KCDDjnUIEVAADs=
|
|
|
|
//! --bzz_bzz__bzz__--"#;
|
|
|
|
//!
|
|
|
|
//! let envelope = Envelope::from_bytes(raw_mail.as_bytes(), None).expect("Could not parse mail");
|
|
|
|
//! assert_eq!(envelope.subject().as_ref(), "gratuitously encoded subject");
|
|
|
|
//! assert_eq!(
|
|
|
|
//! envelope.message_id_display().as_ref(),
|
|
|
|
//! "<h2g7f.z0gy2pgaen5m@example.com>"
|
|
|
|
//! );
|
|
|
|
//!
|
|
|
|
//! let body = envelope.body_bytes(raw_mail.as_bytes());
|
|
|
|
//! assert_eq!(body.content_type().to_string().as_str(), "multipart/mixed");
|
|
|
|
//!
|
|
|
|
//! let body_text = body.text();
|
|
|
|
//! assert_eq!(body_text.as_str(), "hello world.");
|
|
|
|
//!
|
|
|
|
//! let subattachments: Vec<Attachment> = body.attachments();
|
|
|
|
//! assert_eq!(subattachments.len(), 3);
|
|
|
|
//! assert_eq!(
|
|
|
|
//! subattachments[2].content_type().name().unwrap(),
|
|
|
|
//! "test_image.gif"
|
|
|
|
//! );
|
|
|
|
//! ```
|
2020-09-24 16:51:51 +03:00
|
|
|
|
2020-09-09 14:24:30 +03:00
|
|
|
pub mod address;
|
|
|
|
pub mod attachment_types;
|
2018-07-22 15:44:44 +03:00
|
|
|
pub mod attachments;
|
2020-09-09 14:24:30 +03:00
|
|
|
pub mod compose;
|
|
|
|
pub mod headers;
|
|
|
|
pub mod list_management;
|
|
|
|
pub mod mailto;
|
2018-07-27 21:37:56 +03:00
|
|
|
pub mod parser;
|
2020-10-05 18:43:08 +03:00
|
|
|
pub mod pgp;
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2023-08-24 11:32:21 +03:00
|
|
|
use std::{borrow::Cow, ops::Deref};
|
2023-04-30 19:39:41 +03:00
|
|
|
|
2020-09-09 14:24:30 +03:00
|
|
|
pub use address::{Address, MessageID, References, StrBuild, StrBuilder};
|
|
|
|
pub use attachments::{Attachment, AttachmentBuilder};
|
|
|
|
pub use compose::{attachment_from_file, Draft};
|
2020-08-25 12:25:26 +03:00
|
|
|
pub use headers::*;
|
2023-07-22 20:46:23 +03:00
|
|
|
#[cfg(feature = "imap")]
|
2023-08-17 18:36:13 +03:00
|
|
|
use imap_codec::imap_types::{
|
2023-06-05 19:37:03 +03:00
|
|
|
core::{AString, Atom, NonEmptyVec},
|
2023-08-17 18:36:13 +03:00
|
|
|
fetch::{MacroOrMessageDataItemNames, MessageDataItemName, Section},
|
2023-06-05 19:37:03 +03:00
|
|
|
flag::Flag as ImapCodecFlag,
|
|
|
|
};
|
2023-11-24 12:58:21 +02:00
|
|
|
use indexmap::IndexSet;
|
2020-01-08 17:04:44 +02:00
|
|
|
use smallvec::SmallVec;
|
2023-04-30 19:39:41 +03:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
error::{Error, Result},
|
|
|
|
parser::BytesExt,
|
|
|
|
thread::ThreadNodeHash,
|
2023-06-18 22:28:40 +03:00
|
|
|
TagHash, UnixTimestamp,
|
2023-04-30 19:39:41 +03:00
|
|
|
};
|
2017-09-01 15:24:32 +03:00
|
|
|
|
2023-07-22 20:46:23 +03:00
|
|
|
#[cfg(feature = "imap")]
|
2023-07-13 16:47:11 +03:00
|
|
|
// [ref:TODO]: (#222) Make this `const` as soon as it is possible.
|
2023-07-04 21:40:51 +03:00
|
|
|
pub(crate) fn common_attributes() -> MacroOrMessageDataItemNames<'static> {
|
|
|
|
MacroOrMessageDataItemNames::MessageDataItemNames(vec![
|
|
|
|
MessageDataItemName::Uid,
|
|
|
|
MessageDataItemName::Flags,
|
|
|
|
MessageDataItemName::Envelope,
|
|
|
|
MessageDataItemName::BodyExt {
|
2023-06-05 19:37:03 +03:00
|
|
|
section: Some(Section::HeaderFields(
|
|
|
|
None,
|
2023-07-04 21:40:51 +03:00
|
|
|
NonEmptyVec::from(AString::from(Atom::unvalidated("REFERENCES"))),
|
2023-06-05 19:37:03 +03:00
|
|
|
)),
|
|
|
|
partial: None,
|
|
|
|
peek: true,
|
|
|
|
},
|
2023-07-04 21:40:51 +03:00
|
|
|
MessageDataItemName::BodyStructure,
|
2023-06-05 19:37:03 +03:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2017-09-16 19:15:51 +03:00
|
|
|
bitflags! {
|
2023-08-25 08:17:05 +03:00
|
|
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
2017-09-16 19:15:51 +03:00
|
|
|
pub struct Flag: u8 {
|
2018-08-23 15:36:52 +03:00
|
|
|
const PASSED = 0b0000_0001;
|
|
|
|
const REPLIED = 0b0000_0010;
|
|
|
|
const SEEN = 0b0000_0100;
|
|
|
|
const TRASHED = 0b0000_1000;
|
|
|
|
const DRAFT = 0b0001_0000;
|
|
|
|
const FLAGGED = 0b0010_0000;
|
2017-09-16 19:15:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 20:24:00 +02:00
|
|
|
impl PartialEq<&str> for Flag {
|
|
|
|
fn eq(&self, other: &&str) -> bool {
|
2023-07-01 16:34:06 +03:00
|
|
|
(other.eq_ignore_ascii_case("passed") && self.contains(Self::PASSED))
|
|
|
|
|| (other.eq_ignore_ascii_case("replied") && self.contains(Self::REPLIED))
|
|
|
|
|| (other.eq_ignore_ascii_case("seen") && self.contains(Self::SEEN))
|
|
|
|
|| (other.eq_ignore_ascii_case("read") && self.contains(Self::SEEN))
|
|
|
|
|| (other.eq_ignore_ascii_case("junk") && self.contains(Self::TRASHED))
|
|
|
|
|| (other.eq_ignore_ascii_case("trash") && self.contains(Self::TRASHED))
|
|
|
|
|| (other.eq_ignore_ascii_case("trashed") && self.contains(Self::TRASHED))
|
|
|
|
|| (other.eq_ignore_ascii_case("draft") && self.contains(Self::DRAFT))
|
|
|
|
|| (other.eq_ignore_ascii_case("flagged") && self.contains(Self::FLAGGED))
|
2020-03-01 20:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:20:05 +02:00
|
|
|
macro_rules! flag_impl {
|
|
|
|
(fn $name:ident, $val:expr) => {
|
|
|
|
pub const fn $name(&self) -> bool {
|
|
|
|
self.contains($val)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Flag {
|
2023-07-01 16:34:06 +03:00
|
|
|
flag_impl!(fn is_passed, Self::PASSED);
|
|
|
|
flag_impl!(fn is_replied, Self::REPLIED);
|
|
|
|
flag_impl!(fn is_seen, Self::SEEN);
|
|
|
|
flag_impl!(fn is_trashed, Self::TRASHED);
|
|
|
|
flag_impl!(fn is_draft, Self::DRAFT);
|
|
|
|
flag_impl!(fn is_flagged, Self::FLAGGED);
|
2021-01-10 13:20:05 +02:00
|
|
|
|
2023-07-22 20:46:23 +03:00
|
|
|
#[cfg(feature = "imap")]
|
2023-06-05 19:37:03 +03:00
|
|
|
pub(crate) fn derive_imap_codec_flags(&self) -> Vec<ImapCodecFlag> {
|
|
|
|
let mut flags = Vec::new();
|
|
|
|
|
|
|
|
if self.is_passed() {
|
|
|
|
// This is from http://cr.yp.to/proto/maildir.html and not meaningful in IMAP.
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_replied() {
|
|
|
|
flags.push(ImapCodecFlag::Answered);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_seen() {
|
|
|
|
flags.push(ImapCodecFlag::Seen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_trashed() {
|
|
|
|
flags.push(ImapCodecFlag::Deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_draft() {
|
|
|
|
flags.push(ImapCodecFlag::Draft);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_flagged() {
|
|
|
|
flags.push(ImapCodecFlag::Flagged);
|
|
|
|
}
|
|
|
|
|
|
|
|
flags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 19:39:41 +03:00
|
|
|
///`Mail` holds both the envelope info of an email in its `envelope` field and
|
|
|
|
/// the raw bytes that describe the email in `bytes`. Its body as an
|
|
|
|
/// `melib::email::Attachment` can be parsed on demand
|
|
|
|
/// with the `melib::email::Mail::body` method.
|
2023-12-09 18:47:16 +02:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2020-09-09 14:24:30 +03:00
|
|
|
pub struct Mail {
|
|
|
|
pub envelope: Envelope,
|
|
|
|
pub bytes: Vec<u8>,
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:24:30 +03:00
|
|
|
impl Deref for Mail {
|
2018-08-18 17:50:31 +03:00
|
|
|
type Target = Envelope;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Envelope {
|
|
|
|
&self.envelope
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:24:30 +03:00
|
|
|
impl Mail {
|
2020-09-09 22:51:55 +03:00
|
|
|
pub fn new(bytes: Vec<u8>, flags: Option<Flag>) -> Result<Self> {
|
2023-07-01 16:34:06 +03:00
|
|
|
Ok(Self {
|
2020-09-09 22:51:55 +03:00
|
|
|
envelope: Envelope::from_bytes(&bytes, flags)?,
|
2020-09-09 14:24:30 +03:00
|
|
|
bytes,
|
2018-08-18 23:19:39 +03:00
|
|
|
})
|
2018-08-18 17:50:31 +03:00
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
|
2018-08-18 17:50:31 +03:00
|
|
|
pub fn envelope(&self) -> &Envelope {
|
|
|
|
&self.envelope
|
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
|
|
|
pub fn bytes(&self) -> &[u8] {
|
|
|
|
&self.bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn body(&self) -> Attachment {
|
|
|
|
self.envelope.body_bytes(&self.bytes)
|
2018-08-11 18:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 21:34:32 +02:00
|
|
|
crate::declare_u64_hash!(EnvelopeHash);
|
2018-09-05 16:08:11 +03:00
|
|
|
|
2023-04-30 19:39:41 +03:00
|
|
|
/// `Envelope` represents all the header and structure data of an email we need
|
|
|
|
/// to know.
|
2017-10-01 17:31:20 +03:00
|
|
|
///
|
2020-09-09 14:24:30 +03:00
|
|
|
/// Attachments (the email's body) is parsed on demand with `body` method.
|
2017-10-01 17:31:20 +03:00
|
|
|
///
|
2023-04-30 19:39:41 +03:00
|
|
|
///To access the email attachments, you need to parse them from the raw email
|
|
|
|
/// bytes into an `Attachment` object.
|
2023-12-09 18:47:16 +02:00
|
|
|
#[derive(Clone, Deserialize, Serialize)]
|
2017-09-16 15:05:28 +03:00
|
|
|
pub struct Envelope {
|
2023-05-30 13:04:03 +03:00
|
|
|
// ----- IMAP4rev1 -----
|
2020-09-09 14:24:30 +03:00
|
|
|
pub date: String,
|
2023-05-30 13:04:03 +03:00
|
|
|
pub subject: Option<String>,
|
2020-09-09 14:24:30 +03:00
|
|
|
pub from: SmallVec<[Address; 1]>,
|
2023-05-30 13:04:03 +03:00
|
|
|
// pub sender
|
|
|
|
// pub reply_to
|
2020-09-09 14:24:30 +03:00
|
|
|
pub to: SmallVec<[Address; 1]>,
|
|
|
|
pub cc: SmallVec<[Address; 1]>,
|
|
|
|
pub bcc: Vec<Address>,
|
|
|
|
pub in_reply_to: Option<MessageID>,
|
2023-05-30 13:04:03 +03:00
|
|
|
pub message_id: MessageID,
|
|
|
|
// ----- Other -----
|
|
|
|
pub hash: EnvelopeHash,
|
|
|
|
pub timestamp: UnixTimestamp,
|
2019-12-04 19:42:31 +02:00
|
|
|
pub references: Option<References>,
|
2020-09-09 14:24:30 +03:00
|
|
|
pub other_headers: HeaderMap,
|
|
|
|
pub thread: ThreadNodeHash,
|
|
|
|
pub flags: Flag,
|
|
|
|
pub has_attachments: bool,
|
2023-11-24 12:58:21 +02:00
|
|
|
pub tags: IndexSet<TagHash>,
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
|
2023-08-11 13:16:47 +03:00
|
|
|
impl std::fmt::Debug for Envelope {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2020-08-25 12:25:26 +03:00
|
|
|
f.debug_struct("Envelope")
|
|
|
|
.field("Subject", &self.subject())
|
|
|
|
.field("Date", &self.date)
|
|
|
|
.field("From", &self.from)
|
|
|
|
.field("To", &self.to)
|
|
|
|
.field("Message-ID", &self.message_id_display())
|
|
|
|
.field("In-Reply-To", &self.in_reply_to_display())
|
|
|
|
.field("References", &self.references)
|
2020-10-05 18:43:08 +03:00
|
|
|
.field("Flags", &self.flags)
|
2020-08-25 12:25:26 +03:00
|
|
|
.field("Hash", &self.hash)
|
|
|
|
.finish()
|
2018-09-22 16:56:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 20:24:00 +02:00
|
|
|
impl Default for Envelope {
|
|
|
|
fn default() -> Self {
|
2023-07-01 16:34:06 +03:00
|
|
|
Self::new(EnvelopeHash::default())
|
2020-03-01 20:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 15:05:28 +03:00
|
|
|
impl Envelope {
|
2018-09-05 16:08:11 +03:00
|
|
|
pub fn new(hash: EnvelopeHash) -> Self {
|
2023-07-01 16:34:06 +03:00
|
|
|
Self {
|
2020-09-09 14:24:30 +03:00
|
|
|
hash,
|
2018-08-12 16:55:45 +03:00
|
|
|
date: String::new(),
|
2020-09-09 14:24:30 +03:00
|
|
|
timestamp: 0,
|
2020-07-26 16:08:22 +03:00
|
|
|
from: SmallVec::new(),
|
|
|
|
to: SmallVec::new(),
|
|
|
|
cc: SmallVec::new(),
|
2018-09-04 01:49:29 +03:00
|
|
|
bcc: Vec::new(),
|
2017-10-01 17:31:20 +03:00
|
|
|
subject: None,
|
2018-09-17 07:50:03 +03:00
|
|
|
message_id: MessageID::default(),
|
2017-10-01 17:31:20 +03:00
|
|
|
in_reply_to: None,
|
|
|
|
references: None,
|
2020-08-25 12:25:26 +03:00
|
|
|
other_headers: Default::default(),
|
2020-01-18 02:00:39 +02:00
|
|
|
thread: ThreadNodeHash::null(),
|
2019-05-26 18:44:59 +03:00
|
|
|
has_attachments: false,
|
2017-10-01 17:31:20 +03:00
|
|
|
flags: Flag::default(),
|
2023-11-24 12:58:21 +02:00
|
|
|
tags: IndexSet::new(),
|
2017-10-01 17:31:20 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-14 19:49:16 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_hash(&mut self, new_hash: EnvelopeHash) -> &mut Self {
|
2018-10-14 19:49:16 +03:00
|
|
|
self.hash = new_hash;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-10-14 19:49:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-01 16:34:06 +03:00
|
|
|
pub fn from_bytes(bytes: &[u8], flags: Option<Flag>) -> Result<Self> {
|
2023-07-03 10:57:42 +03:00
|
|
|
let mut e = Self::new(EnvelopeHash::from_bytes(bytes.trim()));
|
2018-08-18 17:50:31 +03:00
|
|
|
let res = e.populate_headers(bytes).ok();
|
|
|
|
if res.is_some() {
|
2019-06-28 19:34:40 +03:00
|
|
|
if let Some(f) = flags {
|
|
|
|
e.flags = f;
|
|
|
|
}
|
2018-08-18 23:19:39 +03:00
|
|
|
return Ok(e);
|
2018-08-18 17:50:31 +03:00
|
|
|
}
|
2022-12-08 22:20:05 +02:00
|
|
|
Err(Error::new("Couldn't parse mail."))
|
2018-08-18 17:50:31 +03:00
|
|
|
}
|
2020-06-30 19:36:02 +03:00
|
|
|
|
2018-09-05 16:08:11 +03:00
|
|
|
pub fn hash(&self) -> EnvelopeHash {
|
2018-08-12 16:55:45 +03:00
|
|
|
self.hash
|
2018-08-06 11:05:09 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2020-06-06 12:24:39 +03:00
|
|
|
pub fn populate_headers(&mut self, mut bytes: &[u8]) -> Result<()> {
|
|
|
|
if bytes.starts_with(b"From ") {
|
|
|
|
/* Attempt to recover if message includes the mbox From label as first line */
|
|
|
|
if let Some(offset) = bytes.find(b"\n") {
|
|
|
|
bytes = &bytes[offset + 1..];
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 19:38:20 +03:00
|
|
|
let (headers, body) = match parser::mail(bytes) {
|
2018-08-18 17:50:31 +03:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
2019-05-01 19:20:33 +03:00
|
|
|
debug!("error in parsing mail\n{:?}\n", e);
|
2018-08-18 17:50:31 +03:00
|
|
|
let error_msg = String::from("Mail cannot be shown because of errors.");
|
2022-12-08 22:20:05 +02:00
|
|
|
return Err(Error::new(error_msg));
|
2018-08-18 17:50:31 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
for (name, value) in headers {
|
2023-08-24 11:32:21 +03:00
|
|
|
match name {
|
|
|
|
HeaderName::TO => {
|
|
|
|
let parse_result = parser::address::rfc2822address_list(value);
|
|
|
|
if let Ok((_, value)) = parse_result {
|
|
|
|
self.set_to(value);
|
|
|
|
};
|
2018-09-08 00:38:58 +03:00
|
|
|
}
|
2023-08-24 11:32:21 +03:00
|
|
|
HeaderName::CC => {
|
|
|
|
let parse_result = parser::address::rfc2822address_list(value);
|
|
|
|
if let Ok((_, value)) = parse_result {
|
|
|
|
self.set_cc(value);
|
|
|
|
};
|
2018-08-23 15:36:52 +03:00
|
|
|
}
|
2023-08-24 11:32:21 +03:00
|
|
|
HeaderName::BCC => {
|
|
|
|
let parse_result = parser::address::rfc2822address_list(value);
|
|
|
|
if let Ok((_, value)) = parse_result {
|
|
|
|
self.set_bcc(value.to_vec());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
HeaderName::FROM => {
|
|
|
|
let parse_result = parser::address::rfc2822address_list(value);
|
|
|
|
if let Ok((_, value)) = parse_result {
|
|
|
|
self.set_from(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HeaderName::SUBJECT => {
|
|
|
|
let parse_result = parser::encodings::phrase(value.trim(), false);
|
|
|
|
if let Ok((_, value)) = parse_result {
|
|
|
|
self.set_subject(value);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
HeaderName::MESSAGE_ID => {
|
|
|
|
self.set_message_id(value);
|
|
|
|
}
|
|
|
|
HeaderName::REFERENCES => {
|
|
|
|
self.set_references(value);
|
|
|
|
}
|
|
|
|
HeaderName::IN_REPLY_TO => {
|
|
|
|
self.set_in_reply_to(value);
|
|
|
|
}
|
|
|
|
HeaderName::DATE => {
|
|
|
|
let parse_result = parser::encodings::phrase(value, false);
|
|
|
|
if let Ok((_, value)) = parse_result {
|
|
|
|
self.set_date(value.as_slice());
|
|
|
|
} else {
|
|
|
|
self.set_date(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HeaderName::CONTENT_TYPE => match parser::attachments::content_type(value) {
|
2020-06-06 19:38:20 +03:00
|
|
|
Ok((_, (ct, cst, ref params)))
|
2019-05-26 18:44:59 +03:00
|
|
|
if ct.eq_ignore_ascii_case(b"multipart")
|
|
|
|
&& cst.eq_ignore_ascii_case(b"mixed") =>
|
|
|
|
{
|
2019-09-25 22:00:30 +03:00
|
|
|
let mut builder = AttachmentBuilder::default();
|
2019-07-30 21:33:15 +03:00
|
|
|
builder.set_content_type_from_bytes(value);
|
2019-09-25 22:00:30 +03:00
|
|
|
let mut boundary = None;
|
|
|
|
for (n, v) in params {
|
2020-09-15 13:49:41 +03:00
|
|
|
if n.eq_ignore_ascii_case(b"boundary") {
|
2019-09-25 22:00:30 +03:00
|
|
|
boundary = Some(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(boundary) = boundary {
|
|
|
|
self.has_attachments =
|
|
|
|
Attachment::check_if_has_attachments_quick(body, boundary);
|
|
|
|
} else {
|
2023-04-30 19:39:41 +03:00
|
|
|
debug!(
|
|
|
|
"{:?} has no boundary field set in multipart/mixed content-type \
|
|
|
|
field.",
|
|
|
|
&self
|
|
|
|
);
|
2019-09-25 22:00:30 +03:00
|
|
|
}
|
2019-05-26 18:44:59 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2023-08-24 11:32:21 +03:00
|
|
|
},
|
|
|
|
_ => {}
|
2017-10-01 17:31:20 +03:00
|
|
|
}
|
2020-08-25 12:25:26 +03:00
|
|
|
self.other_headers.insert(
|
|
|
|
name,
|
|
|
|
parser::encodings::phrase(value, false)
|
|
|
|
.map(|(_, value)| {
|
|
|
|
String::from_utf8(value)
|
|
|
|
.unwrap_or_else(|err| String::from_utf8_lossy(&err.into_bytes()).into())
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| String::from_utf8_lossy(value).into()),
|
|
|
|
);
|
2018-08-18 17:50:31 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* https://tools.ietf.org/html/rfc5322#section-3.6.4
|
|
|
|
*
|
|
|
|
* if self.message_id.is_none() ...
|
|
|
|
*/
|
2020-09-03 14:44:03 +03:00
|
|
|
if let Some(x) = self.in_reply_to.clone() {
|
2018-08-18 17:50:31 +03:00
|
|
|
self.push_references(x);
|
|
|
|
}
|
2022-09-18 00:09:23 +03:00
|
|
|
if let Ok(d) = parser::dates::rfc5322_date(self.date.as_bytes()) {
|
2018-08-19 12:51:15 +03:00
|
|
|
self.set_datetime(d);
|
2018-08-18 17:50:31 +03:00
|
|
|
}
|
2018-09-17 07:50:03 +03:00
|
|
|
if self.message_id.raw().is_empty() {
|
|
|
|
let hash = self.hash;
|
2022-12-08 20:43:52 +02:00
|
|
|
self.set_message_id(format!("<{:x}>", hash.0).as_bytes());
|
2018-08-05 16:37:53 +03:00
|
|
|
}
|
2018-09-12 15:10:19 +03:00
|
|
|
if self.references.is_some() {
|
|
|
|
if let Some(pos) = self
|
|
|
|
.references
|
|
|
|
.as_ref()
|
|
|
|
.map(|r| &r.refs)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2018-09-17 07:50:03 +03:00
|
|
|
.position(|r| r == &self.message_id)
|
2018-09-12 15:10:19 +03:00
|
|
|
{
|
|
|
|
self.references.as_mut().unwrap().refs.remove(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 09:14:36 +03:00
|
|
|
Ok(())
|
2017-10-01 17:31:20 +03:00
|
|
|
}
|
2018-08-18 17:50:31 +03:00
|
|
|
|
2018-09-05 16:08:11 +03:00
|
|
|
pub fn date(&self) -> UnixTimestamp {
|
2018-07-22 11:14:23 +03:00
|
|
|
self.timestamp
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2018-07-22 11:14:23 +03:00
|
|
|
|
2020-01-06 16:10:36 +02:00
|
|
|
pub fn datetime(&self) -> UnixTimestamp {
|
|
|
|
self.timestamp
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2020-01-06 16:10:36 +02:00
|
|
|
|
2018-07-22 11:14:23 +03:00
|
|
|
pub fn date_as_str(&self) -> &str {
|
|
|
|
&self.date
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2020-07-26 16:08:22 +03:00
|
|
|
pub fn from(&self) -> &[Address] {
|
|
|
|
self.from.as_slice()
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-09-04 01:49:29 +03:00
|
|
|
pub fn field_bcc_to_string(&self) -> String {
|
2020-07-26 16:08:22 +03:00
|
|
|
if self.bcc.is_empty() {
|
|
|
|
self.other_headers
|
|
|
|
.get("Bcc")
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
self.bcc.iter().fold(String::new(), |mut acc, x| {
|
|
|
|
if !acc.is_empty() {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(&x.to_string());
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
}
|
2018-09-04 01:49:29 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-09-04 01:49:29 +03:00
|
|
|
pub fn field_cc_to_string(&self) -> String {
|
2020-07-26 16:08:22 +03:00
|
|
|
if self.cc.is_empty() {
|
2023-05-16 13:17:13 +03:00
|
|
|
self.other_headers.get("Cc").unwrap_or_default().to_string()
|
2020-07-26 16:08:22 +03:00
|
|
|
} else {
|
|
|
|
self.cc.iter().fold(String::new(), |mut acc, x| {
|
|
|
|
if !acc.is_empty() {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(&x.to_string());
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
}
|
2018-09-04 01:49:29 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-08-23 15:36:52 +03:00
|
|
|
pub fn field_from_to_string(&self) -> String {
|
2020-07-26 16:08:22 +03:00
|
|
|
if self.from.is_empty() {
|
|
|
|
self.other_headers
|
|
|
|
.get("From")
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
self.from.iter().fold(String::new(), |mut acc, x| {
|
|
|
|
if !acc.is_empty() {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(&x.to_string());
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
}
|
2018-07-27 18:01:52 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2020-07-26 16:08:22 +03:00
|
|
|
pub fn to(&self) -> &[Address] {
|
|
|
|
self.to.as_slice()
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-10-15 12:27:51 +03:00
|
|
|
pub fn cc(&self) -> &[Address] {
|
|
|
|
self.cc.as_slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bcc(&self) -> &[Address] {
|
|
|
|
self.bcc.as_slice()
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:36:52 +03:00
|
|
|
pub fn field_to_to_string(&self) -> String {
|
2020-07-26 16:08:22 +03:00
|
|
|
if self.to.is_empty() {
|
2023-05-16 13:17:13 +03:00
|
|
|
self.other_headers.get("To").unwrap_or_default().to_string()
|
2020-07-26 16:08:22 +03:00
|
|
|
} else {
|
|
|
|
self.to
|
|
|
|
.iter()
|
|
|
|
.map(|a| format!("{}", a))
|
|
|
|
.fold(String::new(), |mut acc, x| {
|
|
|
|
if !acc.is_empty() {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(&x);
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
}
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2019-10-07 16:42:44 +03:00
|
|
|
pub fn field_references_to_string(&self) -> String {
|
2020-07-26 16:08:22 +03:00
|
|
|
let refs = self.references();
|
|
|
|
if refs.is_empty() {
|
|
|
|
self.other_headers
|
|
|
|
.get("References")
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
refs.iter()
|
|
|
|
.map(|a| a.to_string())
|
|
|
|
.fold(String::new(), |mut acc, x| {
|
|
|
|
if !acc.is_empty() {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(&x);
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
}
|
2019-10-07 16:42:44 +03:00
|
|
|
}
|
2019-09-27 13:18:59 +03:00
|
|
|
|
2018-08-18 17:50:31 +03:00
|
|
|
pub fn body_bytes(&self, bytes: &[u8]) -> Attachment {
|
2019-09-25 22:00:30 +03:00
|
|
|
let builder = AttachmentBuilder::new(bytes);
|
2018-08-18 17:50:31 +03:00
|
|
|
builder.build()
|
|
|
|
}
|
2019-09-27 13:18:59 +03:00
|
|
|
|
2023-08-24 11:32:21 +03:00
|
|
|
pub fn headers<'a>(&self, bytes: &'a [u8]) -> Result<Vec<(HeaderName, &'a str)>> {
|
2020-06-06 19:38:20 +03:00
|
|
|
let ret = parser::headers::headers(bytes)?.1;
|
2019-03-26 15:26:09 +02:00
|
|
|
let len = ret.len();
|
2019-03-26 19:53:39 +02:00
|
|
|
ret.into_iter()
|
|
|
|
.try_fold(Vec::with_capacity(len), |mut acc, (a, b)| {
|
|
|
|
Ok({
|
2023-08-24 11:32:21 +03:00
|
|
|
acc.push((a, std::str::from_utf8(b)?));
|
2019-03-26 19:53:39 +02:00
|
|
|
acc
|
|
|
|
})
|
|
|
|
})
|
2019-03-26 15:26:09 +02:00
|
|
|
}
|
2019-09-27 13:18:59 +03:00
|
|
|
|
2018-08-04 20:40:20 +03:00
|
|
|
pub fn subject(&self) -> Cow<str> {
|
2018-07-22 11:14:23 +03:00
|
|
|
match self.subject {
|
2019-12-12 00:44:47 +02:00
|
|
|
Some(ref s) => Cow::from(s),
|
2018-08-04 20:40:20 +03:00
|
|
|
_ => Cow::from(String::new()),
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2020-01-15 12:34:40 +02:00
|
|
|
|
2018-10-14 19:49:16 +03:00
|
|
|
pub fn in_reply_to(&self) -> Option<&MessageID> {
|
2020-01-15 12:34:40 +02:00
|
|
|
self.in_reply_to
|
|
|
|
.as_ref()
|
2020-08-25 16:39:12 +03:00
|
|
|
.or_else(|| self.references.as_ref().and_then(|r| r.refs.last()))
|
2018-09-17 07:50:03 +03:00
|
|
|
}
|
2020-01-15 12:34:40 +02:00
|
|
|
|
2018-10-14 19:49:16 +03:00
|
|
|
pub fn in_reply_to_display(&self) -> Option<Cow<str>> {
|
2020-01-15 12:34:40 +02:00
|
|
|
self.in_reply_to
|
|
|
|
.as_ref()
|
|
|
|
.map(|m| String::from_utf8_lossy(m.val()))
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-10-14 19:49:16 +03:00
|
|
|
pub fn in_reply_to_raw(&self) -> Option<Cow<str>> {
|
2020-01-15 12:34:40 +02:00
|
|
|
self.in_reply_to
|
|
|
|
.as_ref()
|
|
|
|
.map(|m| String::from_utf8_lossy(m.raw()))
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-09-12 15:10:19 +03:00
|
|
|
pub fn message_id(&self) -> &MessageID {
|
2018-09-17 07:50:03 +03:00
|
|
|
&self.message_id
|
2018-09-12 15:10:19 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-09-12 15:10:19 +03:00
|
|
|
pub fn message_id_display(&self) -> Cow<str> {
|
2018-09-17 07:50:03 +03:00
|
|
|
String::from_utf8_lossy(self.message_id.val())
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-08-04 20:40:20 +03:00
|
|
|
pub fn message_id_raw(&self) -> Cow<str> {
|
2018-09-17 07:50:03 +03:00
|
|
|
String::from_utf8_lossy(self.message_id.raw())
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_date(&mut self, new_val: &[u8]) -> &mut Self {
|
2020-08-25 21:15:51 +03:00
|
|
|
let new_val = new_val.trim();
|
2018-08-04 20:40:20 +03:00
|
|
|
self.date = String::from_utf8_lossy(new_val).into_owned();
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_bcc(&mut self, new_val: Vec<Address>) -> &mut Self {
|
2018-09-08 00:38:58 +03:00
|
|
|
self.bcc = new_val;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-09-04 01:49:29 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_cc(&mut self, new_val: SmallVec<[Address; 1]>) -> &mut Self {
|
2018-09-08 00:38:58 +03:00
|
|
|
self.cc = new_val;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-09-04 01:49:29 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_from(&mut self, new_val: SmallVec<[Address; 1]>) -> &mut Self {
|
2018-07-27 18:01:52 +03:00
|
|
|
self.from = new_val;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_to(&mut self, new_val: SmallVec<[Address; 1]>) -> &mut Self {
|
2018-07-27 18:01:52 +03:00
|
|
|
self.to = new_val;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_in_reply_to(&mut self, new_val: &[u8]) -> &mut Self {
|
2023-07-13 16:47:11 +03:00
|
|
|
// [ref:FIXME]: msg_id_list
|
2020-08-25 21:15:51 +03:00
|
|
|
let new_val = new_val.trim();
|
2021-01-05 01:06:39 +02:00
|
|
|
if !new_val.is_empty() {
|
|
|
|
let val = match parser::address::msg_id(new_val) {
|
|
|
|
Ok(v) => v.1,
|
|
|
|
Err(_) => {
|
|
|
|
self.in_reply_to = Some(MessageID::new(new_val, new_val));
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.in_reply_to = Some(val);
|
|
|
|
} else {
|
|
|
|
self.in_reply_to = None;
|
|
|
|
}
|
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_subject(&mut self, new_val: Vec<u8>) -> &mut Self {
|
2019-12-12 00:44:47 +02:00
|
|
|
let mut new_val = String::from_utf8(new_val)
|
|
|
|
.unwrap_or_else(|err| String::from_utf8_lossy(&err.into_bytes()).into());
|
2019-12-07 01:36:05 +02:00
|
|
|
while new_val
|
2019-12-12 00:44:47 +02:00
|
|
|
.chars()
|
2019-12-07 01:36:05 +02:00
|
|
|
.last()
|
2020-08-25 16:39:12 +03:00
|
|
|
.map(char::is_control)
|
2019-12-07 01:36:05 +02:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
new_val.pop();
|
|
|
|
}
|
|
|
|
|
2018-07-22 11:14:23 +03:00
|
|
|
self.subject = Some(new_val);
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_message_id(&mut self, new_val: &[u8]) -> &mut Self {
|
2020-08-25 21:15:51 +03:00
|
|
|
let new_val = new_val.trim();
|
2020-09-03 14:44:03 +03:00
|
|
|
match parser::address::msg_id(new_val) {
|
|
|
|
Ok((_, val)) => {
|
|
|
|
self.message_id = val;
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2019-12-13 00:01:59 +02:00
|
|
|
Err(_) => {
|
|
|
|
self.message_id = MessageID::new(new_val, new_val);
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2020-09-03 14:44:03 +03:00
|
|
|
pub fn push_references(&mut self, new_ref: MessageID) {
|
2018-07-22 11:14:23 +03:00
|
|
|
match self.references {
|
|
|
|
Some(ref mut s) => {
|
|
|
|
if s.refs.contains(&new_ref) {
|
|
|
|
if s.refs[s.refs.len() - 1] != new_ref {
|
|
|
|
if let Some(index) = s.refs.iter().position(|x| *x == new_ref) {
|
|
|
|
s.refs.remove(index);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
2017-09-16 13:32:56 +03:00
|
|
|
} else {
|
2018-07-22 11:14:23 +03:00
|
|
|
return;
|
2017-09-16 13:32:56 +03:00
|
|
|
}
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2018-07-22 11:14:23 +03:00
|
|
|
s.refs.push(new_ref);
|
|
|
|
}
|
|
|
|
None => {
|
2018-09-07 09:44:37 +03:00
|
|
|
let v = vec![new_ref];
|
2018-07-22 11:14:23 +03:00
|
|
|
self.references = Some(References {
|
2018-08-04 20:40:20 +03:00
|
|
|
raw: "".into(),
|
2018-07-22 11:14:23 +03:00
|
|
|
refs: v,
|
|
|
|
});
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_references(&mut self, new_val: &[u8]) -> &mut Self {
|
2020-08-25 21:15:51 +03:00
|
|
|
let new_val = new_val.trim();
|
2021-01-05 01:06:39 +02:00
|
|
|
if !new_val.is_empty() {
|
|
|
|
self.references = None;
|
|
|
|
{
|
|
|
|
let parse_result = parser::address::msg_id_list(new_val);
|
|
|
|
if let Ok((_, value)) = parse_result {
|
|
|
|
for v in value {
|
|
|
|
self.push_references(v);
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2021-01-05 01:06:39 +02:00
|
|
|
match self.references {
|
|
|
|
Some(ref mut s) => {
|
|
|
|
s.raw = new_val.into();
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.references = Some(References {
|
|
|
|
raw: new_val.into(),
|
|
|
|
refs: Vec::new(),
|
|
|
|
});
|
|
|
|
}
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2018-07-20 12:44:04 +03:00
|
|
|
}
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 15:44:44 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2020-07-26 16:08:22 +03:00
|
|
|
pub fn references(&self) -> SmallVec<[&MessageID; 8]> {
|
2018-07-22 11:14:23 +03:00
|
|
|
match self.references {
|
2020-07-26 16:08:22 +03:00
|
|
|
Some(ref s) => s.refs.iter().fold(SmallVec::new(), |mut acc, x| {
|
|
|
|
acc.push(x);
|
|
|
|
acc
|
|
|
|
}),
|
|
|
|
None => SmallVec::new(),
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 19:42:31 +02:00
|
|
|
|
2020-08-25 12:25:26 +03:00
|
|
|
pub fn other_headers(&self) -> &HeaderMap {
|
2019-06-18 21:38:24 +03:00
|
|
|
&self.other_headers
|
|
|
|
}
|
2019-09-08 10:27:10 +03:00
|
|
|
|
2020-08-25 12:25:26 +03:00
|
|
|
pub fn other_headers_mut(&mut self) -> &mut HeaderMap {
|
2019-09-08 10:27:10 +03:00
|
|
|
&mut self.other_headers
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:00:39 +02:00
|
|
|
pub fn thread(&self) -> ThreadNodeHash {
|
2018-07-22 11:14:23 +03:00
|
|
|
self.thread
|
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_thread(&mut self, new_val: ThreadNodeHash) -> &mut Self {
|
2018-07-22 11:14:23 +03:00
|
|
|
self.thread = new_val;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_datetime(&mut self, new_val: UnixTimestamp) -> &mut Self {
|
2020-01-06 16:10:36 +02:00
|
|
|
self.timestamp = new_val;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-22 11:14:23 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_flag(&mut self, f: Flag, value: bool) -> &mut Self {
|
2019-11-15 20:28:03 +02:00
|
|
|
self.flags.set(f, value);
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2018-07-27 21:37:56 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_flags(&mut self, f: Flag) -> &mut Self {
|
2019-07-11 11:44:27 +03:00
|
|
|
self.flags = f;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2019-07-11 11:44:27 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-07-22 11:14:23 +03:00
|
|
|
pub fn flags(&self) -> Flag {
|
|
|
|
self.flags
|
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_seen(&mut self) -> &mut Self {
|
|
|
|
self.set_flag(Flag::SEEN, true);
|
|
|
|
self
|
2018-08-06 11:05:09 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_unseen(&mut self) -> &mut Self {
|
|
|
|
self.set_flag(Flag::SEEN, false);
|
|
|
|
self
|
2019-06-23 11:42:48 +03:00
|
|
|
}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2018-07-22 11:14:23 +03:00
|
|
|
pub fn is_seen(&self) -> bool {
|
2018-08-12 16:55:45 +03:00
|
|
|
self.flags.contains(Flag::SEEN)
|
2017-09-16 19:15:51 +03:00
|
|
|
}
|
2019-11-18 13:00:43 +02:00
|
|
|
|
2021-01-05 01:06:39 +02:00
|
|
|
pub fn set_has_attachments(&mut self, new_val: bool) -> &mut Self {
|
2019-11-18 13:00:43 +02:00
|
|
|
self.has_attachments = new_val;
|
2021-01-05 01:06:39 +02:00
|
|
|
self
|
2019-11-18 13:00:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 18:44:59 +03:00
|
|
|
pub fn has_attachments(&self) -> bool {
|
|
|
|
self.has_attachments
|
|
|
|
}
|
2019-11-30 17:37:00 +02:00
|
|
|
|
2023-11-24 12:58:21 +02:00
|
|
|
pub fn tags(&self) -> &IndexSet<TagHash> {
|
2022-12-08 21:34:32 +02:00
|
|
|
&self.tags
|
2019-11-30 17:37:00 +02:00
|
|
|
}
|
|
|
|
|
2023-11-24 12:58:21 +02:00
|
|
|
pub fn tags_mut(&mut self) -> &mut IndexSet<TagHash> {
|
2022-12-08 21:34:32 +02:00
|
|
|
&mut self.tags
|
2019-11-30 17:37:00 +02:00
|
|
|
}
|
2018-07-20 12:44:04 +03:00
|
|
|
}
|
2017-09-01 15:24:32 +03:00
|
|
|
|
2017-09-16 14:14:08 +03:00
|
|
|
impl Eq for Envelope {}
|
2020-09-09 14:24:30 +03:00
|
|
|
|
2017-09-16 14:14:08 +03:00
|
|
|
impl Ord for Envelope {
|
2023-07-01 16:34:06 +03:00
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
2018-07-20 12:44:04 +03:00
|
|
|
self.datetime().cmp(&other.datetime())
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 20:28:03 +02:00
|
|
|
|
2017-09-16 14:14:08 +03:00
|
|
|
impl PartialOrd for Envelope {
|
2023-07-01 16:34:06 +03:00
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
2017-09-01 15:24:32 +03:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 14:14:08 +03:00
|
|
|
impl PartialEq for Envelope {
|
2023-07-01 16:34:06 +03:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2018-09-17 07:50:03 +03:00
|
|
|
self.hash == other.hash
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|