|
|
|
@ -115,110 +115,6 @@ impl BackendOp for ImapOp {
|
|
|
|
|
Ok(self.bytes.as_ref().unwrap().as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fetch_headers(&mut self) -> Result<&[u8]> {
|
|
|
|
|
if self.bytes.is_some() {
|
|
|
|
|
let result =
|
|
|
|
|
parser::headers_raw(self.bytes.as_ref().unwrap().as_bytes()).to_full_result()?;
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
if self.headers.is_none() {
|
|
|
|
|
let mut bytes_cache = self.uid_store.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.read_response(&mut response)?;
|
|
|
|
|
debug!(
|
|
|
|
|
"fetch response is {} bytes and {} lines",
|
|
|
|
|
response.len(),
|
|
|
|
|
response.lines().collect::<Vec<&str>>().len()
|
|
|
|
|
);
|
|
|
|
|
match protocol_parser::uid_fetch_response(response.as_bytes())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
if v.len() != 1 {
|
|
|
|
|
debug!("responses len is {}", v.len());
|
|
|
|
|
/* TODO: Trigger cache invalidation here. */
|
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
|
"message with UID {} was not found",
|
|
|
|
|
self.uid
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
let (uid, flags, b) = v[0];
|
|
|
|
|
assert_eq!(uid, self.uid);
|
|
|
|
|
if flags.is_some() {
|
|
|
|
|
self.flags.set(flags);
|
|
|
|
|
cache.flags = flags;
|
|
|
|
|
}
|
|
|
|
|
cache.headers =
|
|
|
|
|
Some(unsafe { std::str::from_utf8_unchecked(b).to_string() });
|
|
|
|
|
}
|
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
}
|
|
|
|
|
self.headers = cache.headers.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(self.headers.as_ref().unwrap().as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fetch_body(&mut self) -> Result<&[u8]> {
|
|
|
|
|
if self.bytes.is_some() {
|
|
|
|
|
let result =
|
|
|
|
|
parser::body_raw(self.bytes.as_ref().unwrap().as_bytes()).to_full_result()?;
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
if self.body.is_none() {
|
|
|
|
|
let mut bytes_cache = self.uid_store.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.read_response(&mut response)?;
|
|
|
|
|
debug!(
|
|
|
|
|
"fetch response is {} bytes and {} lines",
|
|
|
|
|
response.len(),
|
|
|
|
|
response.lines().collect::<Vec<&str>>().len()
|
|
|
|
|
);
|
|
|
|
|
match protocol_parser::uid_fetch_response(response.as_bytes())
|
|
|
|
|
.to_full_result()
|
|
|
|
|
.map_err(MeliError::from)
|
|
|
|
|
{
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
if v.len() != 1 {
|
|
|
|
|
debug!("responses len is {}", v.len());
|
|
|
|
|
/* TODO: Trigger cache invalidation here. */
|
|
|
|
|
return Err(MeliError::new(format!(
|
|
|
|
|
"message with UID {} was not found",
|
|
|
|
|
self.uid
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
let (uid, flags, b) = v[0];
|
|
|
|
|
assert_eq!(uid, self.uid);
|
|
|
|
|
if flags.is_some() {
|
|
|
|
|
self.flags.set(flags);
|
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fetch_flags(&self) -> Flag {
|
|
|
|
|
if self.flags.get().is_some() {
|
|
|
|
|
return self.flags.get().unwrap();
|
|
|
|
|