meli/melib/src/email.rs

812 lines
25 KiB
Rust
Raw Normal View History

/*
* 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.
*/
use fnv::FnvHashMap;
2018-08-29 11:11:59 +00:00
mod compose;
pub use self::compose::*;
mod mailto;
pub use mailto::*;
mod attachment_types;
pub mod attachments;
2019-04-04 11:21:52 +00:00
pub use crate::attachments::*;
2018-07-27 18:37:56 +00:00
pub mod parser;
2019-06-18 18:13:58 +00:00
use crate::parser::BytesExt;
2019-05-26 12:54:45 +00:00
use crate::backends::BackendOp;
2019-04-04 11:24:05 +00:00
use crate::error::{MeliError, Result};
2019-05-26 12:54:45 +00:00
use crate::thread::ThreadHash;
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;
use std::str;
2018-07-27 18:37:56 +00:00
use std::string::String;
2017-09-01 12:24:32 +00:00
use chrono;
use chrono::TimeZone;
#[derive(Clone, Debug, Serialize, Deserialize)]
2018-07-27 15:01:52 +00:00
pub struct GroupAddress {
raw: Vec<u8>,
2018-07-27 15:01:52 +00:00
display_name: StrBuilder,
mailbox_list: Vec<Address>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
2018-07-27 15:01:52 +00:00
pub struct MailboxAddress {
raw: Vec<u8>,
2018-07-27 15:01:52 +00:00
display_name: StrBuilder,
address_spec: StrBuilder,
}
#[derive(Clone, Serialize, Deserialize)]
2018-07-27 15:01:52 +00:00
pub enum Address {
Mailbox(MailboxAddress),
Group(GroupAddress),
}
2019-02-15 07:06:42 +00:00
impl Address {
pub fn get_display_name(&self) -> String {
match self {
Address::Mailbox(m) => m.display_name.display(&m.raw),
Address::Group(g) => g.display_name.display(&g.raw),
}
}
2019-03-14 10:19:25 +00:00
2019-02-15 07:06:42 +00:00
pub fn get_email(&self) -> String {
match self {
Address::Mailbox(m) => m.address_spec.display(&m.raw),
Address::Group(_) => String::new(),
}
}
2019-04-14 14:26:33 +00:00
pub fn get_fqdn(&self) -> Option<String> {
match self {
Address::Mailbox(m) => {
let raw_address = m.address_spec.display_bytes(&m.raw);
let fqdn_pos = raw_address.iter().position(|&b| b == b'@')? + 1;
Some(String::from_utf8_lossy(&raw_address[fqdn_pos..]).into())
}
Address::Group(_) => None,
}
}
2019-02-15 07:06:42 +00:00
}
2018-07-27 15:01:52 +00:00
impl Eq for Address {}
impl PartialEq for Address {
fn eq(&self, other: &Address) -> bool {
match (self, other) {
2018-07-27 18:37:56 +00:00
(Address::Mailbox(_), Address::Group(_)) | (Address::Group(_), Address::Mailbox(_)) => {
2018-07-27 15:01:52 +00:00
false
2018-07-27 18:37:56 +00:00
}
2018-07-27 15:01:52 +00:00
(Address::Mailbox(s), Address::Mailbox(o)) => {
s.address_spec.display_bytes(&s.raw) == o.address_spec.display_bytes(&o.raw)
2018-07-27 18:37:56 +00:00
}
2018-07-27 15:01:52 +00:00
(Address::Group(s), Address::Group(o)) => {
s.display_name.display_bytes(&s.raw) == o.display_name.display_bytes(&o.raw)
2019-03-14 10:19:25 +00:00
&& s.mailbox_list
.iter()
.zip(o.mailbox_list.iter())
.fold(true, |b, (s, o)| b && (s == o))
2018-07-27 18:37:56 +00:00
}
2018-07-27 15:01:52 +00:00
}
}
}
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
2018-07-27 18:37:56 +00:00
Address::Mailbox(m) if m.display_name.length > 0 => write!(
f,
"{} <{}>",
m.display_name.display(&m.raw),
m.address_spec.display(&m.raw)
),
Address::Group(g) => {
let attachment_strings: Vec<String> =
g.mailbox_list.iter().map(|a| format!("{}", a)).collect();
write!(
f,
"{}: {}",
g.display_name.display(&g.raw),
attachment_strings.join(", ")
)
}
Address::Mailbox(m) => write!(f, "{}", m.address_spec.display(&m.raw)),
2018-07-27 15:01:52 +00:00
}
}
}
impl fmt::Debug for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
2017-10-01 14:31:20 +00:00
/// Helper struct to return slices from a struct field on demand.
2018-09-17 04:50:03 +00:00
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
2017-09-01 12:24:32 +00:00
struct StrBuilder {
offset: usize,
length: usize,
}
2017-10-01 14:31:20 +00:00
/// Structs implementing this trait must contain a `StrBuilder` field.
2017-09-01 12:24:32 +00:00
pub trait StrBuild {
2017-10-01 14:31:20 +00:00
/// Create a new `Self` out of a string and a slice
fn new(string: &[u8], slice: &[u8]) -> Self;
2017-10-01 14:31:20 +00:00
/// Get the slice part of the string
fn raw(&self) -> &[u8];
2017-10-01 14:31:20 +00:00
/// Get the entire string as a slice
fn val(&self) -> &[u8];
2017-09-01 12:24:32 +00:00
}
2018-07-27 15:01:52 +00:00
impl StrBuilder {
fn display<'a>(&self, s: &'a [u8]) -> String {
2018-07-27 15:01:52 +00:00
let offset = self.offset;
let length = self.length;
String::from_utf8(s[offset..offset + length].to_vec()).unwrap()
2018-08-23 12:36:52 +00:00
}
2018-08-13 21:13:08 +00:00
fn display_bytes<'a>(&self, b: &'a [u8]) -> &'a [u8] {
2018-08-23 12:36:52 +00:00
&b[self.offset..(self.offset + self.length)]
2018-08-13 21:13:08 +00:00
}
2018-07-27 15:01:52 +00:00
}
2017-10-01 14:31:20 +00:00
/// `MessageID` is accessed through the `StrBuild` trait.
2018-09-17 04:50:03 +00:00
#[derive(Clone, Serialize, Deserialize, Default)]
pub struct MessageID(Vec<u8>, StrBuilder);
2017-09-01 12:24:32 +00:00
impl StrBuild for MessageID {
fn new(string: &[u8], slice: &[u8]) -> Self {
2017-09-01 12:24:32 +00:00
let offset = string.find(slice).unwrap();
MessageID(
string.to_owned(),
StrBuilder {
2018-08-23 12:36:52 +00:00
offset,
2018-07-27 18:37:56 +00:00
length: slice.len() + 1,
},
2018-07-27 18:37:56 +00:00
)
2017-09-01 12:24:32 +00:00
}
fn raw(&self) -> &[u8] {
2017-09-01 12:24:32 +00:00
let offset = self.1.offset;
let length = self.1.length;
2018-09-17 04:50:03 +00:00
&self.0[offset..offset + length.saturating_sub(1)]
2017-09-01 12:24:32 +00:00
}
fn val(&self) -> &[u8] {
2017-09-01 12:24:32 +00:00
&self.0
}
}
#[test]
fn test_strbuilder() {
2019-04-14 14:26:33 +00:00
let m_id = b"<20170825132332.6734-1@el13635@mail.ntua.gr>";
2018-08-13 21:13:08 +00:00
let (_, slice) = parser::message_id(m_id).unwrap();
assert_eq!(
MessageID::new(m_id, slice),
MessageID(
2018-08-13 21:13:08 +00:00
m_id.to_vec(),
StrBuilder {
offset: 1,
length: 43,
}
2018-07-27 18:37:56 +00:00
)
);
2017-09-01 12:24:32 +00:00
}
impl fmt::Display for MessageID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.val().is_ascii() {
write!(f, "{}", unsafe { str::from_utf8_unchecked(self.val()) })
} else {
write!(f, "{}", String::from_utf8_lossy(self.val()))
}
}
}
2017-09-01 12:24:32 +00:00
impl PartialEq for MessageID {
fn eq(&self, other: &MessageID) -> bool {
self.raw() == other.raw()
2017-09-01 12:24:32 +00:00
}
}
impl fmt::Debug for MessageID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", String::from_utf8(self.raw().to_vec()).unwrap())
2017-09-01 12:24:32 +00:00
}
}
#[derive(Clone, Serialize, Deserialize)]
2017-09-01 12:24:32 +00:00
struct References {
raw: Vec<u8>,
2017-09-01 12:24:32 +00:00
refs: Vec<MessageID>,
}
impl fmt::Debug for References {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:#?}", self.refs)
}
}
bitflags! {
#[derive(Default, Serialize, Deserialize)]
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;
}
}
#[derive(Debug, Clone, Default)]
pub struct EnvelopeWrapper {
envelope: Envelope,
buffer: Vec<u8>,
}
use std::ops::Deref;
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 {
envelope: Envelope::from_bytes(&buffer)?,
buffer,
2018-08-18 20:19:39 +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;
}
}
pub fn envelope(&self) -> &Envelope {
&self.envelope
}
pub fn buffer(&self) -> &[u8] {
&self.buffer
}
}
2018-09-05 13:08:11 +00:00
pub type UnixTimestamp = u64;
pub type EnvelopeHash = u64;
2017-10-01 14:31:20 +00:00
/// `Envelope` represents all the data of an email we need to know.
///
/// 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`.
#[derive(Clone, Default, Serialize, Deserialize)]
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>,
cc: Vec<Address>,
bcc: Vec<Address>,
subject: Option<Vec<u8>>,
2018-09-17 04:50:03 +00:00
message_id: MessageID,
2017-09-01 12:24:32 +00:00
in_reply_to: Option<MessageID>,
references: Option<References>,
other_headers: FnvHashMap<String, String>,
2017-09-01 12:24:32 +00:00
2018-09-05 13:08:11 +00:00
timestamp: UnixTimestamp,
2019-05-14 18:47:47 +00:00
thread: ThreadHash,
2018-09-05 13:08:11 +00:00
hash: EnvelopeHash,
flags: Flag,
has_attachments: bool,
2017-09-01 12:24:32 +00:00
}
impl fmt::Debug for Envelope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Envelope {{\ndate: {}\n,from:{:#?}\nto {:#?}\nmessage_id: {},\n references: {:#?}\nhash: {}\n
}}",
self.date,
self.from,
self.to,
self.message_id_display(),
self.references,
self.hash)
}
}
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 {
date: String::new(),
2018-07-27 15:01:52 +00:00
from: Vec::new(),
to: Vec::new(),
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,
other_headers: FnvHashMap::default(),
2017-10-01 14:31:20 +00:00
timestamp: 0,
2019-05-14 18:47:47 +00:00
thread: ThreadHash::null(),
2017-10-01 14:31:20 +00:00
hash,
has_attachments: false,
2017-10-01 14:31:20 +00:00
flags: Flag::default(),
}
}
2018-10-14 16:49:16 +00:00
pub fn set_hash(&mut self, new_hash: EnvelopeHash) {
self.hash = new_hash;
}
2018-08-18 20:19:39 +00:00
pub fn from_bytes(bytes: &[u8]) -> Result<Envelope> {
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() {
2018-08-18 20:19:39 +00:00
return Ok(e);
}
2018-08-18 20:19:39 +00:00
Err(MeliError::new("Couldn't parse mail."))
}
2018-09-05 13:08:11 +00:00
pub fn from_token(mut operation: Box<BackendOp>, hash: EnvelopeHash) -> Option<Envelope> {
let mut e = Envelope::new(hash);
2017-10-01 14:31:20 +00:00
e.flags = operation.fetch_flags();
if let Ok(bytes) = operation.as_bytes() {
let res = e.populate_headers(bytes).ok();
if res.is_some() {
return Some(e);
}
}
None
2017-10-01 14:31:20 +00:00
}
2018-09-05 13:08:11 +00:00
pub fn hash(&self) -> EnvelopeHash {
self.hash
}
pub fn populate_headers(&mut self, bytes: &[u8]) -> Result<()> {
let (headers, body) = match parser::mail(bytes).to_full_result() {
Ok(v) => v,
Err(e) => {
2019-05-01 16:20:33 +00:00
debug!("error in parsing mail\n{:?}\n", e);
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
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);
if parse_result.is_done() {
let value = parse_result.to_full_result().unwrap();
self.set_to(value);
};
} else if name.eq_ignore_ascii_case(b"cc") {
let parse_result = parser::rfc2822address_list(value);
if parse_result.is_done() {
let value = parse_result.to_full_result().unwrap();
self.set_cc(value);
};
} else if name.eq_ignore_ascii_case(b"bcc") {
let parse_result = parser::rfc2822address_list(value);
if parse_result.is_done() {
let value = parse_result.to_full_result().unwrap();
self.set_bcc(value);
};
} else if name.eq_ignore_ascii_case(b"from") {
let parse_result = parser::rfc2822address_list(value);
if parse_result.is_done() {
let value = parse_result.to_full_result().unwrap();
self.set_from(value);
}
} else if name.eq_ignore_ascii_case(b"subject") {
let parse_result = parser::phrase(value.trim());
if parse_result.is_done() {
let value = parse_result.to_full_result().unwrap();
self.set_subject(value);
};
} 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
}
}
}
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);
}
} else if name.eq_ignore_ascii_case(b"content-type") {
match parser::content_type(value).to_full_result() {
Ok((ct, cst, _))
if ct.eq_ignore_ascii_case(b"multipart")
&& cst.eq_ignore_ascii_case(b"mixed") =>
{
let mut builder = AttachmentBuilder::new(body);
builder.set_content_type(value);
let b = builder.build();
let subs = b.attachments();
2019-06-18 18:13:58 +00:00
self.has_attachments = subs.iter().any(|sub| !sub.is_text());
}
_ => {}
}
} else {
self.other_headers.insert(
String::from_utf8_lossy(name).into(),
String::from_utf8_lossy(value).into(),
);
2017-10-01 14:31:20 +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);
}
2018-08-19 09:51:15 +00:00
if let Some(d) = parser::date(&self.date.as_bytes()) {
self.set_datetime(d);
}
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());
}
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)
{
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
}
pub fn populate_headers_from_token(&mut self, mut operation: Box<BackendOp>) -> Result<()> {
2018-08-23 12:36:52 +00:00
let headers = operation.fetch_headers()?;
self.populate_headers(headers)
}
2018-09-05 13:08:11 +00:00
pub fn date(&self) -> UnixTimestamp {
self.timestamp
2017-09-01 12:24:32 +00:00
}
pub fn datetime(&self) -> chrono::DateTime<chrono::FixedOffset> {
2018-08-23 12:36:52 +00:00
if let Some(d) = parser::date(&self.date.as_bytes()) {
return d;
}
chrono::FixedOffset::west(0)
.ymd(1970, 1, 1)
.and_hms(0, 0, 0)
2017-09-01 12:24:32 +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
}
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 {
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
}
pub fn bytes(&self, mut operation: Box<BackendOp>) -> Vec<u8> {
2018-08-07 12:01:15 +00:00
operation
.as_bytes()
.map(|v| v.into())
.unwrap_or_else(|_| Vec::new())
2018-08-05 09:44:31 +00:00
}
pub fn body_bytes(&self, bytes: &[u8]) -> Attachment {
2018-08-29 11:11:59 +00:00
if bytes.is_empty() {
let builder = AttachmentBuilder::new(bytes);
return builder.build();
}
let (headers, body) = match parser::mail(bytes).to_full_result() {
Ok(v) => v,
Err(_) => {
2019-05-01 16:20:33 +00:00
debug!("error in parsing mail\n");
let error_msg = b"Mail cannot be shown because of errors.";
2019-04-04 12:06:48 +00:00
let builder = AttachmentBuilder::new(error_msg);
return builder.build();
}
};
let mut builder = AttachmentBuilder::new(body);
for (name, value) in headers {
if value.len() == 1 && value.is_empty() {
continue;
}
if name.eq_ignore_ascii_case(b"content-transfer-encoding") {
builder.set_content_transfer_encoding(value);
} else if name.eq_ignore_ascii_case(b"content-type") {
builder.set_content_type(value);
}
}
builder.build()
}
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();
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
})
})
}
pub fn body(&self, mut operation: Box<BackendOp>) -> Attachment {
2019-05-01 16:20:33 +00:00
debug!("searching body for {:?}", self.message_id_display());
let file = operation.as_bytes();
2018-08-29 11:11:59 +00:00
self.body_bytes(file.unwrap())
/*
2019-05-01 16:20:33 +00:00
let (headers, body) = match parser::mail(file.unwrap()).to_full_result() {
Ok(v) => v,
Err(_) => {
debug!("2error in parsing mail\n");
let error_msg = b"Mail cannot be shown because of errors.";
let mut builder = AttachmentBuilder::new(error_msg);
return builder.build();
}
};
let mut builder = AttachmentBuilder::new(body);
for (name, value) in headers {
if value.len() == 1 && value.is_empty() {
continue;
}
if name.eq_ignore_ascii_case(b"content-transfer-encoding") {
builder.content_transfer_encoding(value);
} else if name.eq_ignore_ascii_case(b"content-type") {
builder.content_type(value);
}
}
2019-05-01 16:20:33 +00:00
builder.build()
*/
2017-09-01 12:24:32 +00:00
}
pub fn subject(&self) -> Cow<str> {
match self.subject {
Some(ref s) => String::from_utf8_lossy(s),
_ => Cow::from(String::new()),
}
2017-09-01 12:24:32 +00:00
}
2018-10-14 16:49:16 +00:00
pub fn in_reply_to(&self) -> Option<&MessageID> {
self.in_reply_to.as_ref()
2018-09-17 04:50:03 +00:00
}
2018-10-14 16:49:16 +00:00
pub fn in_reply_to_display(&self) -> Option<Cow<str>> {
if let Some(ref m) = self.in_reply_to {
Some(String::from_utf8_lossy(m.val()))
} else {
None
}
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>> {
if let Some(ref m) = self.in_reply_to {
Some(String::from_utf8_lossy(m.raw()))
} else {
None
}
}
pub fn message_id(&self) -> &MessageID {
2018-09-17 04:50:03 +00:00
&self.message_id
}
pub fn message_id_display(&self) -> Cow<str> {
2018-09-17 04:50:03 +00:00
String::from_utf8_lossy(self.message_id.val())
}
pub fn message_id_raw(&self) -> Cow<str> {
2018-09-17 04:50:03 +00:00
String::from_utf8_lossy(self.message_id.raw())
}
2019-03-14 10:19:25 +00:00
fn set_date(&mut self, new_val: &[u8]) {
self.date = String::from_utf8_lossy(new_val).into_owned();
}
2019-03-14 10:19:25 +00:00
fn set_bcc(&mut self, new_val: Vec<Address>) {
self.bcc = new_val;
}
2019-03-14 10:19:25 +00:00
fn set_cc(&mut self, new_val: Vec<Address>) {
self.cc = new_val;
}
2019-03-14 10:19:25 +00:00
fn set_from(&mut self, new_val: Vec<Address>) {
2018-07-27 15:01:52 +00:00
self.from = new_val;
}
2019-03-14 10:19:25 +00:00
fn set_to(&mut self, new_val: Vec<Address>) {
2018-07-27 15:01:52 +00:00
self.to = new_val;
}
2019-03-14 10:19:25 +00:00
fn set_in_reply_to(&mut self, new_val: &[u8]) {
let slice = match parser::message_id(new_val).to_full_result() {
Ok(v) => v,
Err(_) => {
self.in_reply_to = None;
return;
}
};
self.in_reply_to = Some(MessageID::new(new_val, slice));
}
2019-03-14 10:19:25 +00:00
fn set_subject(&mut self, new_val: Vec<u8>) {
self.subject = Some(new_val);
}
2019-03-14 10:19:25 +00:00
fn set_message_id(&mut self, new_val: &[u8]) {
let slice = match parser::message_id(new_val).to_full_result() {
Ok(v) => v,
Err(_) => {
return;
}
};
2018-09-17 04:50:03 +00:00
self.message_id = MessageID::new(new_val, slice);
}
2019-03-14 10:19:25 +00:00
fn push_references(&mut self, new_val: &[u8]) {
let slice = match parser::message_id(new_val).to_full_result() {
Ok(v) => v,
Err(_) => {
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!();
}
} else {
return;
}
2017-09-01 12:24:32 +00:00
}
s.refs.push(new_ref);
}
None => {
let v = vec![new_ref];
self.references = Some(References {
raw: "".into(),
refs: v,
});
2017-09-01 12:24:32 +00:00
}
}
}
2019-03-14 10:19:25 +00:00
fn set_references(&mut self, new_val: &[u8]) {
match self.references {
Some(ref mut s) => {
s.raw = new_val.into();
}
None => {
self.references = Some(References {
raw: new_val.into(),
refs: Vec::new(),
});
}
}
}
pub fn references(&self) -> Vec<&MessageID> {
match self.references {
2018-08-07 12:01:15 +00:00
Some(ref s) => s
.refs
.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
}
}
pub fn other_headers(&self) -> &FnvHashMap<String, String> {
&self.other_headers
}
2019-05-14 18:47:47 +00:00
pub fn thread(&self) -> ThreadHash {
self.thread
}
2019-05-14 18:47:47 +00:00
pub fn set_thread(&mut self, new_val: ThreadHash) {
self.thread = new_val;
}
2019-03-14 10:19:25 +00:00
pub fn set_datetime(&mut self, new_val: chrono::DateTime<chrono::FixedOffset>) {
2018-09-05 13:08:11 +00:00
self.timestamp = new_val.timestamp() as UnixTimestamp;
}
pub fn set_flag(&mut self, f: Flag, mut operation: Box<BackendOp>) -> Result<()> {
self.flags.toggle(f);
2019-03-03 20:11:15 +00:00
operation.set_flag(self, f)?;
Ok(())
2018-07-27 18:37:56 +00:00
}
pub fn flags(&self) -> Flag {
self.flags
}
pub fn set_seen(&mut self, operation: Box<BackendOp>) -> Result<()> {
self.set_flag(Flag::SEEN, operation)
}
pub fn set_unseen(&mut self, operation: Box<BackendOp>) -> Result<()> {
self.set_flag(Flag::SEEN, operation)
}
pub fn is_seen(&self) -> bool {
self.flags.contains(Flag::SEEN)
}
pub fn has_attachments(&self) -> bool {
self.has_attachments
}
}
2017-09-01 12:24:32 +00:00
impl Eq for Envelope {}
impl Ord for Envelope {
fn cmp(&self, other: &Envelope) -> Ordering {
self.datetime().cmp(&other.datetime())
2017-09-01 12:24:32 +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))
}
}
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
}
}