2017-09-07 20:00:08 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
2018-08-06 19:20:34 +00:00
|
|
|
/*!
|
|
|
|
* Email parsing, handling, sending etc.
|
|
|
|
*/
|
2019-06-18 18:38:24 +00:00
|
|
|
use fnv::FnvHashMap;
|
2018-08-29 11:11:59 +00:00
|
|
|
mod compose;
|
|
|
|
pub use self::compose::*;
|
|
|
|
|
2019-11-18 18:37:48 +00:00
|
|
|
pub mod list_management;
|
2019-06-18 18:58:55 +00:00
|
|
|
mod mailto;
|
|
|
|
pub use mailto::*;
|
2018-08-08 07:41:25 +00:00
|
|
|
mod attachment_types;
|
2018-07-22 12:44:44 +00:00
|
|
|
pub mod attachments;
|
2019-04-04 11:21:52 +00:00
|
|
|
pub use crate::attachments::*;
|
2019-09-20 07:38:29 +00:00
|
|
|
mod address;
|
2018-07-27 18:37:56 +00:00
|
|
|
pub mod parser;
|
2019-06-18 18:13:58 +00:00
|
|
|
use crate::parser::BytesExt;
|
2019-09-20 07:38:29 +00:00
|
|
|
pub use address::*;
|
2019-09-25 20:12:38 +00:00
|
|
|
pub mod signatures;
|
2018-07-22 11:05:12 +00:00
|
|
|
|
2019-05-26 12:54:45 +00:00
|
|
|
use crate::backends::BackendOp;
|
2020-01-06 14:10:36 +00:00
|
|
|
use crate::datetime::UnixTimestamp;
|
2019-04-04 11:24:05 +00:00
|
|
|
use crate::error::{MeliError, Result};
|
2020-01-18 00:00:39 +00:00
|
|
|
use crate::thread::ThreadNodeHash;
|
2017-09-07 20:00:08 +00:00
|
|
|
|
2020-01-08 15:04:44 +00:00
|
|
|
use smallvec::SmallVec;
|
2018-08-07 12:01:15 +00:00
|
|
|
use std::borrow::Cow;
|
2017-09-01 12:24:32 +00:00
|
|
|
use std::cmp::Ordering;
|
2018-08-07 12:01:15 +00:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2017-09-01 12:24:32 +00:00
|
|
|
use std::fmt;
|
2018-08-07 12:01:15 +00:00
|
|
|
use std::hash::Hasher;
|
2017-09-01 12:24:32 +00:00
|
|
|
use std::option::Option;
|
2018-09-03 22:49:29 +00:00
|
|
|
use std::str;
|
2018-07-27 18:37:56 +00:00
|
|
|
use std::string::String;
|
2017-09-01 12:24:32 +00:00
|
|
|
|
2017-09-16 16:15:51 +00:00
|
|
|
bitflags! {
|
2018-08-13 06:25:48 +00:00
|
|
|
#[derive(Default, Serialize, Deserialize)]
|
2017-09-16 16:15:51 +00:00
|
|
|
pub struct Flag: u8 {
|
2018-08-23 12:36:52 +00: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 16:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:00:21 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2018-08-18 14:50:31 +00:00
|
|
|
pub struct EnvelopeWrapper {
|
|
|
|
envelope: Envelope,
|
|
|
|
buffer: Vec<u8>,
|
2018-08-11 15:00:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 14:50:31 +00:00
|
|
|
use std::ops::Deref;
|
2018-08-11 15:00:21 +00:00
|
|
|
|
2018-08-18 14:50:31 +00:00
|
|
|
impl Deref for EnvelopeWrapper {
|
|
|
|
type Target = Envelope;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Envelope {
|
|
|
|
&self.envelope
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnvelopeWrapper {
|
2018-08-18 20:19:39 +00:00
|
|
|
pub fn new(buffer: Vec<u8>) -> Result<Self> {
|
|
|
|
Ok(EnvelopeWrapper {
|
2019-06-28 16:34:40 +00:00
|
|
|
envelope: Envelope::from_bytes(&buffer, None)?,
|
2018-08-18 14:50:31 +00:00
|
|
|
buffer,
|
2018-08-18 20:19:39 +00:00
|
|
|
})
|
2018-08-18 14:50:31 +00:00
|
|
|
}
|
2018-08-11 15:00:21 +00:00
|
|
|
|
2018-08-18 14:50:31 +00:00
|
|
|
pub fn update(&mut self, new_buffer: Vec<u8>) {
|
2018-08-18 20:19:39 +00:00
|
|
|
// TODO: Propagate error.
|
|
|
|
if let Ok(e) = EnvelopeWrapper::new(new_buffer) {
|
|
|
|
*self = e;
|
|
|
|
}
|
2018-08-18 14:50:31 +00:00
|
|
|
}
|
2018-08-11 15:00:21 +00:00
|
|
|
|
2018-08-18 14:50:31 +00:00
|
|
|
pub fn envelope(&self) -> &Envelope {
|
|
|
|
&self.envelope
|
|
|
|
}
|
|
|
|
pub fn buffer(&self) -> &[u8] {
|
|
|
|
&self.buffer
|
2018-08-11 15:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:08:11 +00:00
|
|
|
pub type EnvelopeHash = u64;
|
|
|
|
|
2017-10-01 14:31:20 +00:00
|
|
|
/// `Envelope` represents all the data of an email we need to know.
|
|
|
|
///
|
2018-07-20 09:44:04 +00:00
|
|
|
/// Attachments (the email's body) is parsed on demand with `body`.
|
2017-10-01 14:31:20 +00:00
|
|
|
///
|
|
|
|
/// Access to the underlying email object in the account's backend (for example the file or the
|
|
|
|
/// entry in an IMAP server) is given through `operation_token`. For more information see
|
|
|
|
/// `BackendOp`.
|
2018-09-22 13:56:50 +00:00
|
|
|
#[derive(Clone, Default, Serialize, Deserialize)]
|
2017-09-16 12:05:28 +00:00
|
|
|
pub struct Envelope {
|
2017-09-01 12:24:32 +00:00
|
|
|
date: String,
|
2018-07-27 15:01:52 +00:00
|
|
|
from: Vec<Address>,
|
|
|
|
to: Vec<Address>,
|
2018-09-03 22:49:29 +00:00
|
|
|
cc: Vec<Address>,
|
|
|
|
bcc: Vec<Address>,
|
2019-12-11 22:44:47 +00:00
|
|
|
subject: Option<String>,
|
2018-09-17 04:50:03 +00:00
|
|
|
message_id: MessageID,
|
2017-09-01 12:24:32 +00:00
|
|
|
in_reply_to: Option<MessageID>,
|
2019-12-04 17:42:31 +00:00
|
|
|
pub references: Option<References>,
|
2019-06-18 18:38:24 +00:00
|
|
|
other_headers: FnvHashMap<String, String>,
|
2017-09-01 12:24:32 +00:00
|
|
|
|
2018-09-05 13:08:11 +00:00
|
|
|
timestamp: UnixTimestamp,
|
2020-01-18 00:00:39 +00:00
|
|
|
thread: ThreadNodeHash,
|
2017-09-14 15:08:14 +00:00
|
|
|
|
2018-09-05 13:08:11 +00:00
|
|
|
hash: EnvelopeHash,
|
2017-09-16 16:15:51 +00:00
|
|
|
|
|
|
|
flags: Flag,
|
2019-05-26 15:44:59 +00:00
|
|
|
has_attachments: bool,
|
2020-01-08 15:04:44 +00:00
|
|
|
labels: SmallVec<[u64; 8]>,
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 13:56:50 +00:00
|
|
|
impl fmt::Debug for Envelope {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-09-08 07:27:10 +00:00
|
|
|
write!(f, "Envelope {{\n\tsubject: {}\n\tdate: {},\n\tfrom:{:#?},\n\tto {:#?},\n\tmessage_id: {},\n\tin_reply_to: {:?}\n\treferences: {:#?},\n\thash: {}\n}}",
|
|
|
|
self.subject(),
|
2018-09-22 13:56:50 +00:00
|
|
|
self.date,
|
|
|
|
self.from,
|
|
|
|
self.to,
|
|
|
|
self.message_id_display(),
|
2019-09-08 07:27:10 +00:00
|
|
|
self.in_reply_to_display(),
|
2018-09-22 13:56:50 +00:00
|
|
|
self.references,
|
|
|
|
self.hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 12:05:28 +00:00
|
|
|
impl Envelope {
|
2018-09-05 13:08:11 +00:00
|
|
|
pub fn new(hash: EnvelopeHash) -> Self {
|
2017-10-01 14:31:20 +00:00
|
|
|
Envelope {
|
2018-08-12 13:55:45 +00:00
|
|
|
date: String::new(),
|
2018-07-27 15:01:52 +00:00
|
|
|
from: Vec::new(),
|
|
|
|
to: Vec::new(),
|
2018-09-03 22:49:29 +00:00
|
|
|
cc: Vec::new(),
|
|
|
|
bcc: Vec::new(),
|
2017-10-01 14:31:20 +00:00
|
|
|
subject: None,
|
2018-09-17 04:50:03 +00:00
|
|
|
message_id: MessageID::default(),
|
2017-10-01 14:31:20 +00:00
|
|
|
in_reply_to: None,
|
|
|
|
references: None,
|
2019-06-18 18:38:24 +00:00
|
|
|
other_headers: FnvHashMap::default(),
|
2017-10-01 14:31:20 +00:00
|
|
|
|
|
|
|
timestamp: 0,
|
|
|
|
|
2020-01-18 00:00:39 +00:00
|
|
|
thread: ThreadNodeHash::null(),
|
2017-10-01 14:31:20 +00:00
|
|
|
|
2018-08-12 13:55:45 +00:00
|
|
|
hash,
|
2019-05-26 15:44:59 +00:00
|
|
|
has_attachments: false,
|
2017-10-01 14:31:20 +00:00
|
|
|
flags: Flag::default(),
|
2020-01-08 15:04:44 +00:00
|
|
|
labels: SmallVec::new(),
|
2017-10-01 14:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-14 16:49:16 +00:00
|
|
|
|
|
|
|
pub fn set_hash(&mut self, new_hash: EnvelopeHash) {
|
|
|
|
self.hash = new_hash;
|
|
|
|
}
|
|
|
|
|
2019-06-28 16:34:40 +00:00
|
|
|
pub fn from_bytes(bytes: &[u8], flags: Option<Flag>) -> Result<Envelope> {
|
2018-08-18 14:50:31 +00:00
|
|
|
let mut h = DefaultHasher::new();
|
|
|
|
h.write(bytes);
|
|
|
|
let mut e = Envelope::new(h.finish());
|
|
|
|
let res = e.populate_headers(bytes).ok();
|
|
|
|
if res.is_some() {
|
2019-06-28 16:34:40 +00:00
|
|
|
if let Some(f) = flags {
|
|
|
|
e.flags = f;
|
|
|
|
}
|
2018-08-18 20:19:39 +00:00
|
|
|
return Ok(e);
|
2018-08-18 14:50:31 +00:00
|
|
|
}
|
2018-08-18 20:19:39 +00:00
|
|
|
Err(MeliError::new("Couldn't parse mail."))
|
2018-08-18 14:50:31 +00:00
|
|
|
}
|
2019-09-09 08:54:47 +00:00
|
|
|
pub fn from_token(mut operation: Box<dyn BackendOp>, hash: EnvelopeHash) -> Option<Envelope> {
|
2018-08-12 13:55:45 +00:00
|
|
|
let mut e = Envelope::new(hash);
|
2017-10-01 14:31:20 +00:00
|
|
|
e.flags = operation.fetch_flags();
|
2018-08-18 14:50:31 +00:00
|
|
|
if let Ok(bytes) = operation.as_bytes() {
|
|
|
|
let res = e.populate_headers(bytes).ok();
|
|
|
|
if res.is_some() {
|
|
|
|
return Some(e);
|
|
|
|
}
|
2018-08-12 13:55:45 +00:00
|
|
|
}
|
2018-08-18 14:50:31 +00:00
|
|
|
None
|
2017-10-01 14:31:20 +00:00
|
|
|
}
|
2018-09-05 13:08:11 +00:00
|
|
|
pub fn hash(&self) -> EnvelopeHash {
|
2018-08-12 13:55:45 +00:00
|
|
|
self.hash
|
2018-08-06 08:05:09 +00:00
|
|
|
}
|
2018-08-18 14:50:31 +00:00
|
|
|
pub fn populate_headers(&mut self, bytes: &[u8]) -> Result<()> {
|
2019-05-26 15:44:59 +00:00
|
|
|
let (headers, body) = match parser::mail(bytes).to_full_result() {
|
2018-08-18 14:50:31 +00:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
2019-05-01 16:20:33 +00:00
|
|
|
debug!("error in parsing mail\n{:?}\n", e);
|
2018-08-18 14:50:31 +00:00
|
|
|
let error_msg = String::from("Mail cannot be shown because of errors.");
|
|
|
|
return Err(MeliError::new(error_msg));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mut in_reply_to = None;
|
2017-10-01 14:31:20 +00:00
|
|
|
|
2018-08-18 14:50:31 +00:00
|
|
|
for (name, value) in headers {
|
|
|
|
if value.len() == 1 && value.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if name.eq_ignore_ascii_case(b"to") {
|
|
|
|
let parse_result = parser::rfc2822address_list(value);
|
2018-09-07 21:38:58 +00:00
|
|
|
if parse_result.is_done() {
|
|
|
|
let value = parse_result.to_full_result().unwrap();
|
|
|
|
self.set_to(value);
|
2018-08-18 14:50:31 +00:00
|
|
|
};
|
2018-09-03 22:49:29 +00:00
|
|
|
} else if name.eq_ignore_ascii_case(b"cc") {
|
|
|
|
let parse_result = parser::rfc2822address_list(value);
|
2018-09-07 21:38:58 +00:00
|
|
|
if parse_result.is_done() {
|
|
|
|
let value = parse_result.to_full_result().unwrap();
|
|
|
|
self.set_cc(value);
|
2018-09-03 22:49:29 +00:00
|
|
|
};
|
|
|
|
} else if name.eq_ignore_ascii_case(b"bcc") {
|
|
|
|
let parse_result = parser::rfc2822address_list(value);
|
2018-09-07 21:38:58 +00:00
|
|
|
if parse_result.is_done() {
|
|
|
|
let value = parse_result.to_full_result().unwrap();
|
|
|
|
self.set_bcc(value);
|
2018-09-03 22:49:29 +00:00
|
|
|
};
|
2018-08-18 14:50:31 +00:00
|
|
|
} else if name.eq_ignore_ascii_case(b"from") {
|
|
|
|
let parse_result = parser::rfc2822address_list(value);
|
2018-09-07 21:38:58 +00:00
|
|
|
if parse_result.is_done() {
|
|
|
|
let value = parse_result.to_full_result().unwrap();
|
|
|
|
self.set_from(value);
|
|
|
|
}
|
2018-08-18 14:50:31 +00:00
|
|
|
} else if name.eq_ignore_ascii_case(b"subject") {
|
|
|
|
let parse_result = parser::phrase(value.trim());
|
2018-09-07 21:38:58 +00:00
|
|
|
if parse_result.is_done() {
|
|
|
|
let value = parse_result.to_full_result().unwrap();
|
|
|
|
self.set_subject(value);
|
2018-08-18 14:50:31 +00:00
|
|
|
};
|
|
|
|
} else if name.eq_ignore_ascii_case(b"message-id") {
|
|
|
|
self.set_message_id(value);
|
|
|
|
} else if name.eq_ignore_ascii_case(b"references") {
|
|
|
|
{
|
|
|
|
let parse_result = parser::references(value);
|
|
|
|
if parse_result.is_done() {
|
|
|
|
for v in parse_result.to_full_result().unwrap() {
|
|
|
|
self.push_references(v);
|
2017-10-01 14:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 14:50:31 +00:00
|
|
|
self.set_references(value);
|
|
|
|
} else if name.eq_ignore_ascii_case(b"in-reply-to") {
|
|
|
|
self.set_in_reply_to(value);
|
|
|
|
in_reply_to = Some(value);
|
|
|
|
} else if name.eq_ignore_ascii_case(b"date") {
|
2018-08-23 12:36:52 +00:00
|
|
|
let parse_result = parser::phrase(value);
|
|
|
|
if parse_result.is_done() {
|
|
|
|
let value = parse_result.to_full_result().unwrap();
|
|
|
|
self.set_date(value.as_slice());
|
|
|
|
} else {
|
|
|
|
self.set_date(value);
|
|
|
|
}
|
2019-05-26 15:44:59 +00:00
|
|
|
} else if name.eq_ignore_ascii_case(b"content-type") {
|
|
|
|
match parser::content_type(value).to_full_result() {
|
2019-09-25 19:00:30 +00:00
|
|
|
Ok((ct, cst, ref params))
|
2019-05-26 15:44:59 +00:00
|
|
|
if ct.eq_ignore_ascii_case(b"multipart")
|
|
|
|
&& cst.eq_ignore_ascii_case(b"mixed") =>
|
|
|
|
{
|
2019-09-25 19:00:30 +00:00
|
|
|
let mut builder = AttachmentBuilder::default();
|
2019-07-30 18:33:15 +00:00
|
|
|
builder.set_content_type_from_bytes(value);
|
2019-09-25 19:00:30 +00:00
|
|
|
let mut boundary = None;
|
|
|
|
for (n, v) in params {
|
|
|
|
if n == b"boundary" {
|
|
|
|
boundary = Some(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(boundary) = boundary {
|
|
|
|
self.has_attachments =
|
|
|
|
Attachment::check_if_has_attachments_quick(body, boundary);
|
|
|
|
} else {
|
|
|
|
debug!("{:?} has no boundary field set in multipart/mixed content-type field.", &self);
|
|
|
|
}
|
2019-05-26 15:44:59 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-06-18 18:38:24 +00:00
|
|
|
} else {
|
|
|
|
self.other_headers.insert(
|
2019-12-12 08:58:47 +00:00
|
|
|
String::from_utf8(name.to_vec())
|
|
|
|
.unwrap_or_else(|err| String::from_utf8_lossy(&err.into_bytes()).into()),
|
|
|
|
parser::phrase(value)
|
|
|
|
.to_full_result()
|
|
|
|
.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()),
|
2019-06-18 18:38:24 +00:00
|
|
|
);
|
2017-10-01 14:31:20 +00:00
|
|
|
}
|
2018-08-18 14:50:31 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* https://tools.ietf.org/html/rfc5322#section-3.6.4
|
|
|
|
*
|
|
|
|
* if self.message_id.is_none() ...
|
|
|
|
*/
|
|
|
|
if let Some(ref mut x) = in_reply_to {
|
|
|
|
self.push_references(x);
|
|
|
|
}
|
2019-12-11 22:44:47 +00:00
|
|
|
if let Ok(d) = parser::date(&self.date.as_bytes()) {
|
2018-08-19 09:51:15 +00:00
|
|
|
self.set_datetime(d);
|
2018-08-18 14:50:31 +00:00
|
|
|
}
|
2018-09-17 04:50:03 +00:00
|
|
|
if self.message_id.raw().is_empty() {
|
|
|
|
let hash = self.hash;
|
|
|
|
self.set_message_id(format!("<{:x}>", hash).as_bytes());
|
2018-08-05 13:37:53 +00:00
|
|
|
}
|
2018-09-12 12:10:19 +00:00
|
|
|
if self.references.is_some() {
|
|
|
|
if let Some(pos) = self
|
|
|
|
.references
|
|
|
|
.as_ref()
|
|
|
|
.map(|r| &r.refs)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2018-09-17 04:50:03 +00:00
|
|
|
.position(|r| r == &self.message_id)
|
2018-09-12 12:10:19 +00:00
|
|
|
{
|
|
|
|
self.references.as_mut().unwrap().refs.remove(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 06:14:36 +00:00
|
|
|
Ok(())
|
2017-10-01 14:31:20 +00:00
|
|
|
}
|
2018-08-18 14:50:31 +00:00
|
|
|
|
2018-09-05 13:08:11 +00:00
|
|
|
pub fn date(&self) -> UnixTimestamp {
|
2018-07-22 08:14:23 +00:00
|
|
|
self.timestamp
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2018-07-22 08:14:23 +00:00
|
|
|
|
2020-01-06 14:10:36 +00:00
|
|
|
pub fn datetime(&self) -> UnixTimestamp {
|
|
|
|
self.timestamp
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2020-01-06 14:10:36 +00:00
|
|
|
|
2018-07-22 08:14:23 +00:00
|
|
|
pub fn date_as_str(&self) -> &str {
|
|
|
|
&self.date
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2018-07-27 15:01:52 +00:00
|
|
|
pub fn from(&self) -> &Vec<Address> {
|
|
|
|
&self.from
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2018-09-03 22:49:29 +00:00
|
|
|
pub fn field_bcc_to_string(&self) -> String {
|
|
|
|
let _strings: Vec<String> = self.bcc.iter().map(|a| format!("{}", a)).collect();
|
|
|
|
_strings.join(", ")
|
|
|
|
}
|
|
|
|
pub fn field_cc_to_string(&self) -> String {
|
|
|
|
let _strings: Vec<String> = self.cc.iter().map(|a| format!("{}", a)).collect();
|
|
|
|
_strings.join(", ")
|
|
|
|
}
|
2018-08-23 12:36:52 +00:00
|
|
|
pub fn field_from_to_string(&self) -> String {
|
2018-09-07 21:38:58 +00:00
|
|
|
let _strings: Vec<String> = self.from().iter().map(|a| format!("{}", a)).collect();
|
2018-07-27 15:01:52 +00:00
|
|
|
_strings.join(", ")
|
|
|
|
}
|
|
|
|
pub fn to(&self) -> &Vec<Address> {
|
|
|
|
&self.to
|
2018-07-27 18:37:56 +00:00
|
|
|
}
|
2018-08-23 12:36:52 +00:00
|
|
|
pub fn field_to_to_string(&self) -> String {
|
2018-07-27 15:01:52 +00:00
|
|
|
let _strings: Vec<String> = self.to.iter().map(|a| format!("{}", a)).collect();
|
|
|
|
_strings.join(", ")
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2019-10-07 13:42:44 +00:00
|
|
|
pub fn field_references_to_string(&self) -> String {
|
|
|
|
let _strings: Vec<String> = self.references().iter().map(|a| a.to_string()).collect();
|
|
|
|
_strings.join(", ")
|
|
|
|
}
|
2019-09-27 10:18:59 +00:00
|
|
|
|
|
|
|
/// Requests bytes from backend and thus can fail
|
|
|
|
pub fn bytes(&self, mut operation: Box<dyn BackendOp>) -> Result<Vec<u8>> {
|
|
|
|
operation.as_bytes().map(|v| v.to_vec())
|
2018-08-05 09:44:31 +00:00
|
|
|
}
|
2019-09-27 10:18:59 +00:00
|
|
|
|
2018-08-18 14:50:31 +00:00
|
|
|
pub fn body_bytes(&self, bytes: &[u8]) -> Attachment {
|
2019-09-25 19:00:30 +00:00
|
|
|
let builder = AttachmentBuilder::new(bytes);
|
2018-08-18 14:50:31 +00:00
|
|
|
builder.build()
|
|
|
|
}
|
2019-09-27 10:18:59 +00:00
|
|
|
|
|
|
|
/// Requests bytes from backend and thus can fail
|
2019-03-26 13:26:09 +00:00
|
|
|
pub fn headers<'a>(&self, bytes: &'a [u8]) -> Result<Vec<(&'a str, &'a str)>> {
|
|
|
|
let ret = parser::headers(bytes).to_full_result()?;
|
|
|
|
let len = ret.len();
|
2019-03-26 17:53:39 +00:00
|
|
|
ret.into_iter()
|
|
|
|
.try_fold(Vec::with_capacity(len), |mut acc, (a, b)| {
|
|
|
|
Ok({
|
|
|
|
acc.push((std::str::from_utf8(a)?, std::str::from_utf8(b)?));
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
})
|
2019-03-26 13:26:09 +00:00
|
|
|
}
|
2019-09-27 10:18:59 +00:00
|
|
|
|
|
|
|
/// Requests bytes from backend and thus can fail
|
|
|
|
pub fn body(&self, mut operation: Box<dyn BackendOp>) -> Result<Attachment> {
|
2019-05-01 16:20:33 +00:00
|
|
|
debug!("searching body for {:?}", self.message_id_display());
|
2019-09-27 10:18:59 +00:00
|
|
|
let file = operation.as_bytes()?;
|
|
|
|
Ok(self.body_bytes(file))
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2019-09-27 10:18:59 +00:00
|
|
|
|
2018-08-04 17:40:20 +00:00
|
|
|
pub fn subject(&self) -> Cow<str> {
|
2018-07-22 08:14:23 +00:00
|
|
|
match self.subject {
|
2019-12-11 22:44:47 +00:00
|
|
|
Some(ref s) => Cow::from(s),
|
2018-08-04 17:40:20 +00:00
|
|
|
_ => Cow::from(String::new()),
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2020-01-15 10:34:40 +00:00
|
|
|
|
2018-10-14 16:49:16 +00:00
|
|
|
pub fn in_reply_to(&self) -> Option<&MessageID> {
|
2020-01-15 10:34:40 +00:00
|
|
|
self.in_reply_to
|
|
|
|
.as_ref()
|
|
|
|
.or(self.references.as_ref().and_then(|r| r.refs.last()))
|
2018-09-17 04:50:03 +00:00
|
|
|
}
|
2020-01-15 10:34:40 +00:00
|
|
|
|
2018-10-14 16:49:16 +00:00
|
|
|
pub fn in_reply_to_display(&self) -> Option<Cow<str>> {
|
2020-01-15 10:34:40 +00:00
|
|
|
self.in_reply_to
|
|
|
|
.as_ref()
|
|
|
|
.map(|m| String::from_utf8_lossy(m.val()))
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2018-10-14 16:49:16 +00:00
|
|
|
pub fn in_reply_to_raw(&self) -> Option<Cow<str>> {
|
2020-01-15 10:34:40 +00:00
|
|
|
self.in_reply_to
|
|
|
|
.as_ref()
|
|
|
|
.map(|m| String::from_utf8_lossy(m.raw()))
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2018-09-12 12:10:19 +00:00
|
|
|
pub fn message_id(&self) -> &MessageID {
|
2018-09-17 04:50:03 +00:00
|
|
|
&self.message_id
|
2018-09-12 12:10:19 +00:00
|
|
|
}
|
|
|
|
pub fn message_id_display(&self) -> Cow<str> {
|
2018-09-17 04:50:03 +00:00
|
|
|
String::from_utf8_lossy(self.message_id.val())
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2018-08-04 17:40:20 +00:00
|
|
|
pub fn message_id_raw(&self) -> Cow<str> {
|
2018-09-17 04:50:03 +00:00
|
|
|
String::from_utf8_lossy(self.message_id.raw())
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_date(&mut self, new_val: &[u8]) {
|
2018-08-04 17:40:20 +00:00
|
|
|
self.date = String::from_utf8_lossy(new_val).into_owned();
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_bcc(&mut self, new_val: Vec<Address>) {
|
2018-09-07 21:38:58 +00:00
|
|
|
self.bcc = new_val;
|
2018-09-03 22:49:29 +00:00
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_cc(&mut self, new_val: Vec<Address>) {
|
2018-09-07 21:38:58 +00:00
|
|
|
self.cc = new_val;
|
2018-09-03 22:49:29 +00:00
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_from(&mut self, new_val: Vec<Address>) {
|
2018-07-27 15:01:52 +00:00
|
|
|
self.from = new_val;
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_to(&mut self, new_val: Vec<Address>) {
|
2018-07-27 15:01:52 +00:00
|
|
|
self.to = new_val;
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_in_reply_to(&mut self, new_val: &[u8]) {
|
2018-08-04 17:40:20 +00:00
|
|
|
let slice = match parser::message_id(new_val).to_full_result() {
|
2018-07-22 08:14:23 +00:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => {
|
|
|
|
self.in_reply_to = None;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.in_reply_to = Some(MessageID::new(new_val, slice));
|
|
|
|
}
|
2019-12-11 22:44:47 +00:00
|
|
|
pub fn set_subject(&mut self, new_val: Vec<u8>) {
|
|
|
|
let mut new_val = String::from_utf8(new_val)
|
|
|
|
.unwrap_or_else(|err| String::from_utf8_lossy(&err.into_bytes()).into());
|
2019-12-06 23:36:05 +00:00
|
|
|
while new_val
|
2019-12-11 22:44:47 +00:00
|
|
|
.chars()
|
2019-12-06 23:36:05 +00:00
|
|
|
.last()
|
2019-12-11 22:44:47 +00:00
|
|
|
.map(|c| char::is_control(c))
|
2019-12-06 23:36:05 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
new_val.pop();
|
|
|
|
}
|
|
|
|
|
2018-07-22 08:14:23 +00:00
|
|
|
self.subject = Some(new_val);
|
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_message_id(&mut self, new_val: &[u8]) {
|
2019-12-12 22:01:59 +00:00
|
|
|
match parser::message_id(new_val).to_full_result() {
|
|
|
|
Ok(slice) => {
|
|
|
|
self.message_id = MessageID::new(new_val, slice);
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2019-12-12 22:01:59 +00:00
|
|
|
Err(_) => {
|
|
|
|
self.message_id = MessageID::new(new_val, new_val);
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn push_references(&mut self, new_val: &[u8]) {
|
2018-08-04 17:40:20 +00:00
|
|
|
let slice = match parser::message_id(new_val).to_full_result() {
|
2018-07-22 08:14:23 +00:00
|
|
|
Ok(v) => v,
|
2019-09-08 07:27:10 +00:00
|
|
|
Err(e) => {
|
|
|
|
debug!(e);
|
2018-07-22 08:14:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let new_ref = MessageID::new(new_val, slice);
|
|
|
|
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 10:32:56 +00:00
|
|
|
} else {
|
2018-07-22 08:14:23 +00:00
|
|
|
return;
|
2017-09-16 10:32:56 +00:00
|
|
|
}
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
2018-07-22 08:14:23 +00:00
|
|
|
s.refs.push(new_ref);
|
|
|
|
}
|
|
|
|
None => {
|
2018-09-07 06:44:37 +00:00
|
|
|
let v = vec![new_ref];
|
2018-07-22 08:14:23 +00:00
|
|
|
self.references = Some(References {
|
2018-08-04 17:40:20 +00:00
|
|
|
raw: "".into(),
|
2018-07-22 08:14:23 +00:00
|
|
|
refs: v,
|
|
|
|
});
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
pub fn set_references(&mut self, new_val: &[u8]) {
|
2018-07-22 08:14:23 +00:00
|
|
|
match self.references {
|
|
|
|
Some(ref mut s) => {
|
2018-08-04 17:40:20 +00:00
|
|
|
s.raw = new_val.into();
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.references = Some(References {
|
2018-08-04 17:40:20 +00:00
|
|
|
raw: new_val.into(),
|
2018-09-07 06:44:37 +00:00
|
|
|
refs: Vec::new(),
|
2018-07-22 08:14:23 +00:00
|
|
|
});
|
|
|
|
}
|
2018-07-20 09:44:04 +00:00
|
|
|
}
|
2018-07-22 12:44:44 +00:00
|
|
|
}
|
2018-07-22 08:14:23 +00:00
|
|
|
pub fn references(&self) -> Vec<&MessageID> {
|
|
|
|
match self.references {
|
2018-08-07 12:01:15 +00:00
|
|
|
Some(ref s) => s
|
|
|
|
.refs
|
2018-07-22 08:14:23 +00:00
|
|
|
.iter()
|
|
|
|
.fold(Vec::with_capacity(s.refs.len()), |mut acc, x| {
|
|
|
|
acc.push(x);
|
|
|
|
acc
|
|
|
|
}),
|
|
|
|
None => Vec::new(),
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 17:42:31 +00:00
|
|
|
|
2019-06-18 18:38:24 +00:00
|
|
|
pub fn other_headers(&self) -> &FnvHashMap<String, String> {
|
|
|
|
&self.other_headers
|
|
|
|
}
|
2019-09-08 07:27:10 +00:00
|
|
|
|
|
|
|
pub fn other_headers_mut(&mut self) -> &mut FnvHashMap<String, String> {
|
|
|
|
&mut self.other_headers
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:00:39 +00:00
|
|
|
pub fn thread(&self) -> ThreadNodeHash {
|
2018-07-22 08:14:23 +00:00
|
|
|
self.thread
|
|
|
|
}
|
2020-01-18 00:00:39 +00:00
|
|
|
pub fn set_thread(&mut self, new_val: ThreadNodeHash) {
|
2018-07-22 08:14:23 +00:00
|
|
|
self.thread = new_val;
|
|
|
|
}
|
2020-01-06 14:10:36 +00:00
|
|
|
pub fn set_datetime(&mut self, new_val: UnixTimestamp) {
|
|
|
|
self.timestamp = new_val;
|
2018-07-22 08:14:23 +00:00
|
|
|
}
|
2019-11-15 18:28:03 +00:00
|
|
|
pub fn set_flag(
|
|
|
|
&mut self,
|
|
|
|
f: Flag,
|
|
|
|
value: bool,
|
|
|
|
mut operation: Box<dyn BackendOp>,
|
|
|
|
) -> Result<()> {
|
|
|
|
self.flags.set(f, value);
|
|
|
|
operation.set_flag(self, f, value)
|
2018-07-27 18:37:56 +00:00
|
|
|
}
|
2019-07-11 08:44:27 +00:00
|
|
|
pub fn set_flags(&mut self, f: Flag) {
|
|
|
|
self.flags = f;
|
|
|
|
}
|
2018-07-22 08:14:23 +00:00
|
|
|
pub fn flags(&self) -> Flag {
|
|
|
|
self.flags
|
|
|
|
}
|
2019-09-09 08:54:47 +00:00
|
|
|
pub fn set_seen(&mut self, operation: Box<dyn BackendOp>) -> Result<()> {
|
2019-06-24 20:57:35 +00:00
|
|
|
if !self.flags.contains(Flag::SEEN) {
|
2019-11-15 18:28:03 +00:00
|
|
|
self.set_flag(Flag::SEEN, true, operation)
|
2019-06-24 20:57:35 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-08-06 08:05:09 +00:00
|
|
|
}
|
2019-09-09 08:54:47 +00:00
|
|
|
pub fn set_unseen(&mut self, operation: Box<dyn BackendOp>) -> Result<()> {
|
2019-06-24 20:57:35 +00:00
|
|
|
if self.flags.contains(Flag::SEEN) {
|
2019-11-15 18:28:03 +00:00
|
|
|
self.set_flag(Flag::SEEN, false, operation)
|
2019-06-24 20:57:35 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-06-23 08:42:48 +00:00
|
|
|
}
|
2018-07-22 08:14:23 +00:00
|
|
|
pub fn is_seen(&self) -> bool {
|
2018-08-12 13:55:45 +00:00
|
|
|
self.flags.contains(Flag::SEEN)
|
2017-09-16 16:15:51 +00:00
|
|
|
}
|
2019-11-18 11:00:43 +00:00
|
|
|
|
|
|
|
pub fn set_has_attachments(&mut self, new_val: bool) {
|
|
|
|
self.has_attachments = new_val;
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:44:59 +00:00
|
|
|
pub fn has_attachments(&self) -> bool {
|
|
|
|
self.has_attachments
|
|
|
|
}
|
2019-11-30 15:37:00 +00:00
|
|
|
|
2020-01-08 15:04:44 +00:00
|
|
|
pub fn labels(&self) -> &SmallVec<[u64; 8]> {
|
2019-11-30 15:37:00 +00:00
|
|
|
&self.labels
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:04:44 +00:00
|
|
|
pub fn labels_mut(&mut self) -> &mut SmallVec<[u64; 8]> {
|
2019-11-30 15:37:00 +00:00
|
|
|
&mut self.labels
|
|
|
|
}
|
2018-07-20 09:44:04 +00:00
|
|
|
}
|
2017-09-01 12:24:32 +00:00
|
|
|
|
2017-09-16 11:14:08 +00:00
|
|
|
impl Eq for Envelope {}
|
|
|
|
impl Ord for Envelope {
|
|
|
|
fn cmp(&self, other: &Envelope) -> Ordering {
|
2018-07-20 09:44:04 +00:00
|
|
|
self.datetime().cmp(&other.datetime())
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 18:28:03 +00:00
|
|
|
|
2017-09-16 11:14:08 +00:00
|
|
|
impl PartialOrd for Envelope {
|
|
|
|
fn partial_cmp(&self, other: &Envelope) -> Option<Ordering> {
|
2017-09-01 12:24:32 +00:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 11:14:08 +00:00
|
|
|
impl PartialEq for Envelope {
|
|
|
|
fn eq(&self, other: &Envelope) -> bool {
|
2018-09-17 04:50:03 +00:00
|
|
|
self.hash == other.hash
|
2017-09-01 12:24:32 +00:00
|
|
|
}
|
|
|
|
}
|