|
|
|
@ -37,10 +37,16 @@ pub struct ImapOp {
|
|
|
|
|
folder_path: String,
|
|
|
|
|
flags: Cell<Option<Flag>>,
|
|
|
|
|
connection: Arc<Mutex<ImapConnection>>,
|
|
|
|
|
byte_cache: Arc<Mutex<FnvHashMap<UID, EnvelopeCache>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ImapOp {
|
|
|
|
|
pub fn new(uid: usize, folder_path: String, connection: Arc<Mutex<ImapConnection>>) -> Self {
|
|
|
|
|
pub fn new(
|
|
|
|
|
uid: usize,
|
|
|
|
|
folder_path: String,
|
|
|
|
|
connection: Arc<Mutex<ImapConnection>>,
|
|
|
|
|
byte_cache: Arc<Mutex<FnvHashMap<UID, EnvelopeCache>>>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
ImapOp {
|
|
|
|
|
uid,
|
|
|
|
|
connection,
|
|
|
|
@ -49,6 +55,7 @@ impl ImapOp {
|
|
|
|
|
body: None,
|
|
|
|
|
folder_path,
|
|
|
|
|
flags: Cell::new(None),
|
|
|
|
|
byte_cache,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -60,6 +67,11 @@ impl BackendOp for ImapOp {
|
|
|
|
|
|
|
|
|
|
fn as_bytes(&mut self) -> Result<&[u8]> {
|
|
|
|
|
if self.bytes.is_none() {
|
|
|
|
|
let mut bytes_cache = self.byte_cache.lock()?;
|
|
|
|
|
let cache = bytes_cache.entry(self.uid).or_default();
|
|
|
|
|
if cache.bytes.is_some() {
|
|
|
|
|
self.bytes = cache.bytes.clone();
|
|
|
|
|
} else {
|
|
|
|
|
let mut response = String::with_capacity(8 * 1024);
|
|
|
|
|
{
|
|
|
|
|
let mut conn = self.connection.lock().unwrap();
|
|
|
|
@ -90,11 +102,15 @@ impl BackendOp for ImapOp {
|
|
|
|
|
assert_eq!(uid, self.uid);
|
|
|
|
|
if flags.is_some() {
|
|
|
|
|
self.flags.set(flags);
|
|
|
|
|
cache.flags = flags;
|
|
|
|
|
}
|
|
|
|
|
self.bytes = Some(unsafe { std::str::from_utf8_unchecked(b).to_string() });
|
|
|
|
|
cache.bytes = Some(unsafe { std::str::from_utf8_unchecked(b).to_string() });
|
|
|
|
|
}
|
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.bytes = cache.bytes.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(self.bytes.as_ref().unwrap().as_bytes())
|
|
|
|
|
}
|
|
|
|
@ -106,9 +122,16 @@ impl BackendOp for ImapOp {
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
if self.headers.is_none() {
|
|
|
|
|
let mut bytes_cache = self.byte_cache.lock()?;
|
|
|
|
|
let cache = bytes_cache.entry(self.uid).or_default();
|
|
|
|
|
if cache.headers.is_some() {
|
|
|
|
|
self.headers = cache.headers.clone();
|
|
|
|
|
} else {
|
|
|
|
|
let mut response = String::with_capacity(8 * 1024);
|
|
|
|
|
let mut conn = self.connection.lock().unwrap();
|
|
|
|
|
conn.send_command(format!("UID FETCH {} (FLAGS RFC822.HEADER)", self.uid).as_bytes())?;
|
|
|
|
|
conn.send_command(
|
|
|
|
|
format!("UID FETCH {} (FLAGS RFC822.HEADER)", self.uid).as_bytes(),
|
|
|
|
|
)?;
|
|
|
|
|
conn.read_response(&mut response)?;
|
|
|
|
|
debug!(
|
|
|
|
|
"fetch response is {} bytes and {} lines",
|
|
|
|
@ -132,13 +155,17 @@ impl BackendOp for ImapOp {
|
|
|
|
|
assert_eq!(uid, self.uid);
|
|
|
|
|
if flags.is_some() {
|
|
|
|
|
self.flags.set(flags);
|
|
|
|
|
cache.flags = flags;
|
|
|
|
|
}
|
|
|
|
|
self.body = Some(unsafe { std::str::from_utf8_unchecked(b).to_string() });
|
|
|
|
|
cache.headers =
|
|
|
|
|
Some(unsafe { std::str::from_utf8_unchecked(b).to_string() });
|
|
|
|
|
}
|
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
}
|
|
|
|
|
self.headers = cache.headers.clone();
|
|
|
|
|
}
|
|
|
|
|
Ok(self.body.as_ref().unwrap().as_bytes())
|
|
|
|
|
}
|
|
|
|
|
Ok(self.headers.as_ref().unwrap().as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fetch_body(&mut self) -> Result<&[u8]> {
|
|
|
|
@ -148,9 +175,16 @@ impl BackendOp for ImapOp {
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
if self.body.is_none() {
|
|
|
|
|
let mut bytes_cache = self.byte_cache.lock()?;
|
|
|
|
|
let cache = bytes_cache.entry(self.uid).or_default();
|
|
|
|
|
if cache.body.is_some() {
|
|
|
|
|
self.body = cache.body.clone();
|
|
|
|
|
} else {
|
|
|
|
|
let mut response = String::with_capacity(8 * 1024);
|
|
|
|
|
let mut conn = self.connection.lock().unwrap();
|
|
|
|
|
conn.send_command(format!("UID FETCH {} (FLAGS RFC822.TEXT)", self.uid).as_bytes())?;
|
|
|
|
|
conn.send_command(
|
|
|
|
|
format!("UID FETCH {} (FLAGS RFC822.TEXT)", self.uid).as_bytes(),
|
|
|
|
|
)?;
|
|
|
|
|
conn.read_response(&mut response)?;
|
|
|
|
|
debug!(
|
|
|
|
|
"fetch response is {} bytes and {} lines",
|
|
|
|
@ -175,10 +209,12 @@ impl BackendOp for ImapOp {
|
|
|
|
|
if flags.is_some() {
|
|
|
|
|
self.flags.set(flags);
|
|
|
|
|
}
|
|
|
|
|
self.body = Some(unsafe { std::str::from_utf8_unchecked(b).to_string() });
|
|
|
|
|
cache.body = Some(unsafe { std::str::from_utf8_unchecked(b).to_string() });
|
|
|
|
|
}
|
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
}
|
|
|
|
|
self.body = cache.body.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(self.body.as_ref().unwrap().as_bytes())
|
|
|
|
|
}
|
|
|
|
@ -187,6 +223,11 @@ impl BackendOp for ImapOp {
|
|
|
|
|
if self.flags.get().is_some() {
|
|
|
|
|
return self.flags.get().unwrap();
|
|
|
|
|
}
|
|
|
|
|
let mut bytes_cache = self.byte_cache.lock().unwrap();
|
|
|
|
|
let cache = bytes_cache.entry(self.uid).or_default();
|
|
|
|
|
if cache.flags.is_some() {
|
|
|
|
|
self.flags.set(cache.flags);
|
|
|
|
|
} else {
|
|
|
|
|
let mut response = String::with_capacity(8 * 1024);
|
|
|
|
|
let mut conn = self.connection.lock().unwrap();
|
|
|
|
|
conn.send_command(format!("UID FETCH {} FLAGS", self.uid).as_bytes())
|
|
|
|
@ -210,11 +251,13 @@ impl BackendOp for ImapOp {
|
|
|
|
|
let (uid, flags, _) = v[0];
|
|
|
|
|
assert_eq!(uid, self.uid);
|
|
|
|
|
if flags.is_some() {
|
|
|
|
|
cache.flags = flags;
|
|
|
|
|
self.flags.set(flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(e).unwrap(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.flags.get().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -252,6 +295,9 @@ impl BackendOp for ImapOp {
|
|
|
|
|
}
|
|
|
|
|
conn.send_command(format!("EXAMINE \"{}\"", &self.folder_path,).as_bytes())?;
|
|
|
|
|
conn.read_response(&mut response)?;
|
|
|
|
|
let mut bytes_cache = self.byte_cache.lock()?;
|
|
|
|
|
let cache = bytes_cache.entry(self.uid).or_default();
|
|
|
|
|
cache.flags = Some(flag);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|