Browse Source

More imap async fixes

tags/alpha-0.6.0
Manos Pitsidianakis 3 months ago
parent
commit
bbedeed3e3
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS. GPG Key ID: 73627C2F690DF710
18 changed files with 739 additions and 490 deletions
  1. +1
    -0
      Cargo.lock
  2. +3
    -0
      melib/src/backends.rs
  3. +0
    -2
      melib/src/backends/imap/operations.rs
  4. +15
    -12
      melib/src/backends/imap/protocol_parser.rs
  5. +51
    -58
      melib/src/backends/imap_async.rs
  6. +63
    -72
      melib/src/backends/imap_async/connection.rs
  7. +49
    -26
      melib/src/backends/imap_async/protocol_parser.rs
  8. +1
    -1
      melib/src/backends/imap_async/untagged.rs
  9. +25
    -21
      melib/src/backends/imap_async/watch.rs
  10. +0
    -2
      melib/src/backends/jmap/operations.rs
  11. +95
    -16
      src/components/mail/compose.rs
  12. +81
    -62
      src/components/mail/listing.rs
  13. +1
    -1
      src/components/mail/listing/compact.rs
  14. +120
    -84
      src/components/mail/listing/conversations.rs
  15. +108
    -73
      src/components/mail/listing/plain.rs
  16. +6
    -2
      src/components/mail/view.rs
  17. +119
    -57
      src/conf/accounts.rs
  18. +1
    -1
      src/jobs.rs

+ 1
- 0
Cargo.lock View File

@ -943,6 +943,7 @@ dependencies = [
"nix",
"nom",
"notify",
"pin-utils",
"reqwest",
"rusqlite",
"serde",

+ 3
- 0
melib/src/backends.rs View File

@ -329,6 +329,9 @@ pub trait MailBackend: ::std::fmt::Debug + Send + Sync {
sender: RefreshEventConsumer,
work_context: WorkContext,
) -> Result<std::thread::ThreadId>;
fn watch_async(&self, sender: RefreshEventConsumer) -> ResultFuture<()> {
Err(MeliError::new("Unimplemented."))
}
fn mailboxes(&self) -> Result<HashMap<MailboxHash, Mailbox>>;
fn mailboxes_async(&self) -> ResultFuture<HashMap<MailboxHash, Mailbox>> {
Err(MeliError::new("Unimplemented."))

+ 0
- 2
melib/src/backends/imap/operations.rs View File

@ -20,8 +20,6 @@
*/
use super::*;
use futures::lock::Mutex as FutureMutex;
use crate::backends::*;
use crate::email::*;
use crate::error::{MeliError, Result};

+ 15
- 12
melib/src/backends/imap/protocol_parser.rs View File

@ -658,13 +658,13 @@ pub fn uid_fetch_response_(
let (input, _) = take_while(is_digit)(input)?;
let (input, result) = permutation((
preceded(
tag("UID "),
alt((tag("UID "), tag(" UID "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
),
opt(preceded(
tag("FLAGS "),
alt((tag("FLAGS "), tag(" FLAGS "))),
delimited(tag("("), byte_flags, tag(")")),
)),
length_data(delimited(
@ -685,13 +685,16 @@ pub fn uid_fetch_flags_response(input: &[u8]) -> IResult<&[u8], Vec<(usize, (Fla
many0(|input| -> IResult<&[u8], (usize, (Flag, Vec<String>))> {
let (input, _) = tag("* ")(input)?;
let (input, _) = take_while(is_digit)(input)?;
let (input, _) = tag(" FETCH ( ")(input)?;
let (input, _) = tag(" FETCH (")(input)?;
let (input, uid_flags) = permutation((
preceded(
tag("UID "),
alt((tag("UID "), tag(" UID "))),
map_res(digit1, |s| usize::from_str(to_str!(s))),
),
preceded(tag("FLAGS "), delimited(tag("("), byte_flags, tag(")"))),
preceded(
alt((tag("FLAGS "), tag(" FLAGS "))),
delimited(tag("("), byte_flags, tag(")")),
),
))(input.ltrim())?;
let (input, _) = tag(")\r\n")(input)?;
Ok((input, (uid_flags.0, uid_flags.1)))
@ -1356,13 +1359,13 @@ pub fn uid_fetch_envelopes_response(
let (input, _) = tag(" FETCH (")(input)?;
let (input, uid_flags) = permutation((
preceded(
tag("UID "),
alt((tag("UID "), tag(" UID "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
),
opt(preceded(
tag("FLAGS "),
alt((tag("FLAGS "), tag(" FLAGS "))),
delimited(tag("("), byte_flags, tag(")")),
)),
))(input.ltrim())?;
@ -1424,31 +1427,31 @@ pub fn status_response(input: &[u8]) -> IResult<&[u8], StatusResponse> {
let (input, _) = tag(" (")(input)?;
let (input, result) = permutation((
opt(preceded(
tag("MESSAGES "),
alt((tag("MESSAGES "), tag(" MESSAGES "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("RECENT "),
alt((tag("RECENT "), tag(" RECENT "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("UIDNEXT "),
alt((tag("UIDNEXT "), tag(" UIDNEXT "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("UIDVALIDITY "),
alt((tag("UIDVALIDITY "), tag(" UIDVALIDITY "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("UNSEEN "),
alt((tag("UNSEEN "), tag(" UNSEEN "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),

+ 51
- 58
melib/src/backends/imap_async.rs View File

@ -135,7 +135,7 @@ pub struct UIDStore {
byte_cache: Arc<Mutex<HashMap<UID, EnvelopeCache>>>,
tag_index: Arc<RwLock<BTreeMap<u64, String>>>,
mailboxes: Arc<RwLock<HashMap<MailboxHash, ImapMailbox>>>,
mailboxes: Arc<FutureMutex<HashMap<MailboxHash, ImapMailbox>>>,
is_online: Arc<Mutex<(Instant, Result<()>)>>,
refresh_events: Arc<Mutex<Vec<RefreshEvent>>>,
sender: Arc<RwLock<Option<RefreshEventConsumer>>>,
@ -151,7 +151,7 @@ impl Default for UIDStore {
uid_index: Default::default(),
msn_index: Default::default(),
byte_cache: Default::default(),
mailboxes: Arc::new(RwLock::new(Default::default())),
mailboxes: Arc::new(FutureMutex::new(Default::default())),
tag_index: Arc::new(RwLock::new(Default::default())),
is_online: Arc::new(Mutex::new((
Instant::now(),
@ -204,18 +204,17 @@ impl MailBackend for ImapType {
mailbox_hash: MailboxHash,
sender: RefreshEventConsumer,
) -> ResultFuture<()> {
let inbox = self
.uid_store
.mailboxes
.read()
.unwrap()
.get(&mailbox_hash)
.map(std::clone::Clone::clone)
.unwrap();
let main_conn = self.connection.clone();
*self.uid_store.sender.write().unwrap() = Some(sender);
let uid_store = self.uid_store.clone();
Ok(Box::pin(async move {
let inbox = uid_store
.mailboxes
.lock()
.await
.get(&mailbox_hash)
.map(std::clone::Clone::clone)
.unwrap();
let mut conn = main_conn.lock().await;
watch::examine_updates(&inbox, &mut conn, &uid_store).await?;
Ok(())
@ -227,7 +226,7 @@ impl MailBackend for ImapType {
let connection = self.connection.clone();
Ok(Box::pin(async move {
{
let mailboxes = uid_store.mailboxes.read().unwrap();
let mailboxes = uid_store.mailboxes.lock().await;
if !mailboxes.is_empty() {
return Ok(mailboxes
.iter()
@ -236,7 +235,7 @@ impl MailBackend for ImapType {
}
}
let new_mailboxes = ImapType::imap_mailboxes(&connection).await?;
let mut mailboxes = uid_store.mailboxes.write()?;
let mut mailboxes = uid_store.mailboxes.lock().await;
*mailboxes = new_mailboxes;
/*
let mut invalid_configs = vec![];
@ -314,7 +313,7 @@ impl MailBackend for ImapType {
_mailbox_hash: MailboxHash,
_sender: RefreshEventConsumer,
) -> Result<Async<()>> {
unimplemented!()
Err(MeliError::new("Unimplemented."))
}
fn watch(
@ -323,45 +322,39 @@ impl MailBackend for ImapType {
_work_context: WorkContext,
) -> Result<std::thread::ThreadId> {
Ok(std::thread::current().id())
//Err(MeliError::new("Unimplemented."))
//unimplemented!()
/*
}
fn watch_async(&self, sender: RefreshEventConsumer) -> ResultFuture<()> {
debug!("watch_async called");
let conn = ImapConnection::new_connection(&self.server_conf, self.uid_store.clone());
let main_conn = self.connection.clone();
*self.uid_store.sender.write().unwrap() = Some(sender);
let uid_store = self.uid_store.clone();
let handle = std::thread::Builder::new()
.name(format!("{} imap connection", self.account_name.as_str(),))
.spawn(move || {
let thread = std::thread::current();
work_context
.set_status
.send((thread.id(), "watching".to_string()))
.unwrap();
let has_idle: bool = main_conn
.lock()
.unwrap()
.capabilities
.iter()
.any(|cap| cap.eq_ignore_ascii_case(b"IDLE"));
//let kit = ImapWatchKit {
// conn,
// main_conn,
// uid_store,
// work_context,
//};
//if has_idle {
// idle(kit).ok().take();
//} else {
// poll_with_examine(kit).ok().take();
//}
})?;
Ok(handle.thread().id())
*/
Ok(Box::pin(async move {
let has_idle: bool = main_conn
.lock()
.await
.capabilities
.iter()
.any(|cap| cap.eq_ignore_ascii_case(b"IDLE"));
debug!(has_idle);
let kit = ImapWatchKit {
conn,
main_conn,
uid_store,
};
if has_idle {
idle(kit).await?;
} else {
poll_with_examine(kit).await?;
}
debug!("watch_async future returning");
Ok(())
}))
}
fn mailboxes(&self) -> Result<HashMap<MailboxHash, Mailbox>> {
unimplemented!()
Err(MeliError::new("Unimplemented."))
}
fn operation(&self, hash: EnvelopeHash) -> Result<Box<dyn BackendOp>> {
@ -392,7 +385,7 @@ impl MailBackend for ImapType {
let connection = self.connection.clone();
Ok(Box::pin(async move {
let path = {
let mailboxes = uid_store.mailboxes.read().unwrap();
let mailboxes = uid_store.mailboxes.lock().await;
let mailbox = mailboxes.get(&mailbox_hash).ok_or(MeliError::new(format!(
"Mailbox with hash {} not found.",
@ -468,7 +461,7 @@ impl MailBackend for ImapType {
*/
{
let mailboxes = uid_store.mailboxes.write().unwrap();
let mailboxes = uid_store.mailboxes.lock().await;
for root_mailbox in mailboxes.values().filter(|f| f.parent.is_none()) {
if path.starts_with(&root_mailbox.name) {
debug!("path starts with {:?}", &root_mailbox);
@ -508,7 +501,7 @@ impl MailBackend for ImapType {
let ret: Result<()> = ImapResponse::from(&response).into();
ret?;
let new_hash = get_path_hash!(path.as_str());
uid_store.mailboxes.write().unwrap().clear();
uid_store.mailboxes.lock().await.clear();
Ok((new_hash, new_mailbox_fut?.await.map_err(|err| MeliError::new(format!("Mailbox create was succesful (returned `{}`) but listing mailboxes afterwards returned `{}`", response, err)))?))
}))
}
@ -525,7 +518,7 @@ impl MailBackend for ImapType {
let no_select: bool;
let is_subscribed: bool;
{
let mailboxes = uid_store.mailboxes.read().unwrap();
let mailboxes = uid_store.mailboxes.lock().await;
no_select = mailboxes[&mailbox_hash].no_select;
is_subscribed = mailboxes[&mailbox_hash].is_subscribed();
imap_path = mailboxes[&mailbox_hash].imap_path().to_string();
@ -563,7 +556,7 @@ impl MailBackend for ImapType {
}
let ret: Result<()> = ImapResponse::from(&response).into();
ret?;
uid_store.mailboxes.write().unwrap().clear();
uid_store.mailboxes.lock().await.clear();
new_mailbox_fut?.await.map_err(|err| format!("Mailbox delete was succesful (returned `{}`) but listing mailboxes afterwards returned `{}`", response, err).into())
}))
}
@ -578,7 +571,7 @@ impl MailBackend for ImapType {
Ok(Box::pin(async move {
let command: String;
{
let mailboxes = uid_store.mailboxes.write().unwrap();
let mailboxes = uid_store.mailboxes.lock().await;
if mailboxes[&mailbox_hash].is_subscribed() == new_val {
return Ok(());
}
@ -604,8 +597,8 @@ impl MailBackend for ImapType {
if ret.is_ok() {
uid_store
.mailboxes
.write()
.unwrap()
.lock()
.await
.entry(mailbox_hash)
.and_modify(|entry| {
let _ = entry.set_is_subscribed(new_val);
@ -627,7 +620,7 @@ impl MailBackend for ImapType {
let command: String;
let mut response = String::with_capacity(8 * 1024);
{
let mailboxes = uid_store.mailboxes.write().unwrap();
let mailboxes = uid_store.mailboxes.lock().await;
let permissions = mailboxes[&mailbox_hash].permissions();
if !permissions.delete_mailbox {
return Err(MeliError::new(format!("You do not have permission to rename mailbox `{}` (rename is equivalent to delete + create). Set permissions for this mailbox are {}", mailboxes[&mailbox_hash].name(), permissions)));
@ -654,10 +647,10 @@ impl MailBackend for ImapType {
let new_hash = get_path_hash!(new_path.as_str());
let ret: Result<()> = ImapResponse::from(&response).into();
ret?;
uid_store.mailboxes.write().unwrap().clear();
uid_store.mailboxes.lock().await.clear();
new_mailbox_fut?.await.map_err(|err| format!("Mailbox rename was succesful (returned `{}`) but listing mailboxes afterwards returned `{}`", response, err))?;
Ok(BackendMailbox::clone(
&uid_store.mailboxes.read().unwrap()[&new_hash],
&uid_store.mailboxes.lock().await[&new_hash],
))
}))
}
@ -670,7 +663,7 @@ impl MailBackend for ImapType {
let uid_store = self.uid_store.clone();
let connection = self.connection.clone();
Ok(Box::pin(async move {
let mailboxes = uid_store.mailboxes.write().unwrap();
let mailboxes = uid_store.mailboxes.lock().await;
let permissions = mailboxes[&mailbox_hash].permissions();
if !permissions.change_permissions {
return Err(MeliError::new(format!("You do not have permission to change permissions for mailbox `{}`. Set permissions for this mailbox are {}", mailboxes[&mailbox_hash].name(), permissions)));
@ -1076,7 +1069,7 @@ async fn get_hlpr(
max_uid: &mut Option<usize>,
) -> Result<Vec<Envelope>> {
let (permissions, mailbox_path, mailbox_exists, no_select, unseen) = {
let f = &uid_store.mailboxes.read().unwrap()[&mailbox_hash];
let f = &uid_store.mailboxes.lock().await[&mailbox_hash];
(
f.permissions.clone(),
f.imap_path().to_string(),

+ 63
- 72
melib/src/backends/imap_async/connection.rs View File

@ -569,7 +569,7 @@ impl ImapConnection {
self.send_command(
format!(
"SELECT \"{}\"",
self.uid_store.mailboxes.read().unwrap()[&mailbox_hash].imap_path()
self.uid_store.mailboxes.lock().await[&mailbox_hash].imap_path()
)
.as_bytes(),
)
@ -593,7 +593,7 @@ impl ImapConnection {
self.send_command(
format!(
"EXAMINE \"{}\"",
self.uid_store.mailboxes.read().unwrap()[&mailbox_hash].imap_path()
self.uid_store.mailboxes.lock().await[&mailbox_hash].imap_path()
)
.as_bytes(),
)
@ -679,27 +679,7 @@ pub struct ImapBlockingConnection {
}
impl From<ImapConnection> for ImapBlockingConnection {
fn from(_conn: ImapConnection) -> Self {
unimplemented!()
/*
conn.set_nonblocking(false)
.expect("set_nonblocking call failed");
conn.stream
.as_mut()
.map(|s| {
s.stream
.set_write_timeout(Some(std::time::Duration::new(30, 0)))
.expect("set_write_timeout call failed")
})
.expect("set_write_timeout call failed");
conn.stream
.as_mut()
.map(|s| {
s.stream
.set_read_timeout(Some(std::time::Duration::new(30, 0)))
.expect("set_read_timeout call failed")
})
.expect("set_read_timeout call failed");
fn from(conn: ImapConnection) -> Self {
ImapBlockingConnection {
buf: [0; 1024],
conn,
@ -707,7 +687,6 @@ impl From for ImapBlockingConnection {
result: Vec::with_capacity(8 * 1024),
err: None,
}
*/
}
}
@ -719,67 +698,79 @@ impl ImapBlockingConnection {
pub fn err(&self) -> Option<&str> {
self.err.as_ref().map(String::as_str)
}
}
impl Iterator for ImapBlockingConnection {
type Item = Vec<u8>;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
/*
pub fn into_stream<'a>(&'a mut self) -> impl Future<Output = Option<Vec<u8>>> + 'a {
self.result.drain(0..self.prev_res_length);
self.prev_res_length = 0;
let mut break_flag = false;
let mut prev_failure = None;
let ImapBlockingConnection {
ref mut prev_res_length,
ref mut result,
ref mut conn,
ref mut buf,
ref mut err,
} = self;
loop {
if conn.stream.is_err() {
debug!(&conn.stream);
async move {
if self.conn.stream.is_err() {
debug!(&self.conn.stream);
return None;
}
match conn.stream.as_mut().unwrap().stream.read(buf) {
Ok(0) => return None,
Ok(b) => {
result.extend_from_slice(&buf[0..b]);
debug!(unsafe { std::str::from_utf8_unchecked(result) });
if let Some(pos) = result.find(b"\r\n") {
*prev_res_length = pos + b"\r\n".len();
return Some(result[0..*prev_res_length].to_vec());
}
prev_failure = None;
loop {
if let Some(y) = read(self, &mut break_flag, &mut prev_failure).await {
return Some(y);
}
Err(e)
if e.kind() == std::io::ErrorKind::WouldBlock
|| e.kind() == std::io::ErrorKind::Interrupted =>
{
debug!(&e);
if let Some(prev_failure) = prev_failure.as_ref() {
if Instant::now().duration_since(*prev_failure)
>= std::time::Duration::new(60 * 5, 0)
{
*err = Some(e.to_string());
return None;
}
} else {
prev_failure = Some(Instant::now());
}
continue;
if break_flag {
return None;
}
Err(e) => {
debug!(&conn.stream);
debug!(&e);
}
}
}
}
async fn read(
conn: &mut ImapBlockingConnection,
break_flag: &mut bool,
prev_failure: &mut Option<std::time::Instant>,
) -> Option<Vec<u8>> {
let ImapBlockingConnection {
ref mut prev_res_length,
ref mut result,
ref mut conn,
ref mut buf,
ref mut err,
} = conn;
match conn.stream.as_mut().unwrap().stream.read(buf).await {
Ok(0) => {
*break_flag = true;
}
Ok(b) => {
result.extend_from_slice(&buf[0..b]);
debug!(unsafe { std::str::from_utf8_unchecked(result) });
if let Some(pos) = result.find(b"\r\n") {
*prev_res_length = pos + b"\r\n".len();
return Some(result[0..*prev_res_length].to_vec());
}
*prev_failure = None;
}
Err(e)
if e.kind() == std::io::ErrorKind::WouldBlock
|| e.kind() == std::io::ErrorKind::Interrupted =>
{
debug!(&e);
if let Some(prev_failure) = prev_failure.as_ref() {
if Instant::now().duration_since(*prev_failure)
>= std::time::Duration::new(60 * 5, 0)
{
*err = Some(e.to_string());
return None;
*break_flag = true;
}
} else {
*prev_failure = Some(Instant::now());
}
}
*/
Err(e) => {
debug!(&conn.stream);
debug!(&e);
*err = Some(e.to_string());
*break_flag = true;
}
}
None
}
fn lookup_ipv4(host: &str, port: u16) -> Result<SocketAddr> {

+ 49
- 26
melib/src/backends/imap_async/protocol_parser.rs View File

@ -126,6 +126,26 @@ impl RequiredResponses {
}
}
#[test]
fn test_imap_required_responses() {
let mut ret = String::new();
let required_responses = RequiredResponses::FETCH_REQUIRED;
let response =
&"* 1040 FETCH (UID 1064 FLAGS ())\r\nM15 OK Fetch completed (0.001 + 0.299 secs).\r\n"
[0..];
for l in response.split_rn() {
/*debug!("check line: {}", &l);*/
if required_responses.check(l) {
ret.push_str(l);
}
}
assert_eq!(&ret, "* 1040 FETCH (UID 1064 FLAGS ())\r\n");
let v = protocol_parser::uid_fetch_flags_response(response.as_bytes())
.unwrap()
.1;
assert_eq!(v.len(), 1);
}
#[derive(Debug)]
pub struct Alert(String);
pub type ImapParseResult<'a, T> = Result<(&'a str, T, Option<Alert>)>;
@ -171,18 +191,18 @@ impl std::fmt::Display for ResponseCode {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
use ResponseCode::*;
match self {
Alert(s)=> write!(fmt, "ALERT: {}", s),
Badcharset(None)=> write!(fmt, "Given charset is not supported by this server."),
Badcharset(Some(s))=> write!(fmt, "Given charset is not supported by this server. Supported ones are: {}", s),
Capability => write!(fmt, "Capability response"),
Parse(s) => write!(fmt, "Server error in parsing message headers: {}", s),
Permanentflags(s) => write!(fmt, "Mailbox supports these flags: {}", s),
ReadOnly=> write!(fmt, "This mailbox is selected read-only."),
ReadWrite => write!(fmt, "This mailbox is selected with read-write permissions."),
Trycreate => write!(fmt, "Failed to operate on the target mailbox because it doesn't exist. Try creating it first."),
Uidnext(uid) => write!(fmt, "Next UID value is {}", uid),
Uidvalidity(uid) => write!(fmt, "Next UIDVALIDITY value is {}", uid),
Unseen(uid) => write!(fmt, "First message without the \\Seen flag is {}", uid),
Alert(s)=> write!(fmt, "ALERT: {}", s),
Badcharset(None)=> write!(fmt, "Given charset is not supported by this server."),
Badcharset(Some(s))=> write!(fmt, "Given charset is not supported by this server. Supported ones are: {}", s),
Capability => write!(fmt, "Capability response"),
Parse(s) => write!(fmt, "Server error in parsing message headers: {}", s),
Permanentflags(s) => write!(fmt, "Mailbox supports these flags: {}", s),
ReadOnly=> write!(fmt, "This mailbox is selected read-only."),
ReadWrite => write!(fmt, "This mailbox is selected with read-write permissions."),
Trycreate => write!(fmt, "Failed to operate on the target mailbox because it doesn't exist. Try creating it first."),
Uidnext(uid) => write!(fmt, "Next UID value is {}", uid),
Uidvalidity(uid) => write!(fmt, "Next UIDVALIDITY value is {}", uid),
Unseen(uid) => write!(fmt, "First message without the \\Seen flag is {}", uid),
}
}
}
@ -658,13 +678,13 @@ pub fn uid_fetch_response_(
let (input, _) = take_while(is_digit)(input)?;
let (input, result) = permutation((
preceded(
tag("UID "),
alt((tag("UID "), tag(" UID "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
),
opt(preceded(
tag("FLAGS "),
alt((tag("FLAGS "), tag(" FLAGS "))),
delimited(tag("("), byte_flags, tag(")")),
)),
length_data(delimited(
@ -684,15 +704,18 @@ pub fn uid_fetch_response_(
pub fn uid_fetch_flags_response(input: &[u8]) -> IResult<&[u8], Vec<(usize, (Flag, Vec<String>))>> {
many0(|input| -> IResult<&[u8], (usize, (Flag, Vec<String>))> {
let (input, _) = tag("* ")(input)?;
let (input, _) = take_while(is_digit)(input)?;
let (input, _) = tag(" FETCH ( ")(input)?;
let (input, _msn) = take_while(is_digit)(input)?;
let (input, _) = tag(" FETCH (")(input)?;
let (input, uid_flags) = permutation((
preceded(
tag("UID "),
alt((tag("UID "), tag(" UID "))),
map_res(digit1, |s| usize::from_str(to_str!(s))),
),
preceded(tag("FLAGS "), delimited(tag("("), byte_flags, tag(")"))),
))(input.ltrim())?;
preceded(
alt((tag("FLAGS "), tag(" FLAGS "))),
delimited(tag("("), byte_flags, tag(")")),
),
))(input)?;
let (input, _) = tag(")\r\n")(input)?;
Ok((input, (uid_flags.0, uid_flags.1)))
})(input)
@ -1357,13 +1380,13 @@ pub fn uid_fetch_envelopes_response(
let (input, _) = tag(" FETCH (")(input)?;
let (input, uid_flags) = permutation((
preceded(
tag("UID "),
alt((tag("UID "), tag(" UID "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
),
opt(preceded(
tag("FLAGS "),
alt((tag("FLAGS "), tag(" FLAGS "))),
delimited(tag("("), byte_flags, tag(")")),
)),
))(input.ltrim())?;
@ -1425,31 +1448,31 @@ pub fn status_response(input: &[u8]) -> IResult<&[u8], StatusResponse> {
let (input, _) = tag(" (")(input)?;
let (input, result) = permutation((
opt(preceded(
tag("MESSAGES "),
alt((tag("MESSAGES "), tag(" MESSAGES "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("RECENT "),
alt((tag("RECENT "), tag(" RECENT "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("UIDNEXT "),
alt((tag("UIDNEXT "), tag(" UIDNEXT "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("UIDVALIDITY "),
alt((tag("UIDVALIDITY "), tag(" UIDVALIDITY "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),
)),
opt(preceded(
tag("UNSEEN "),
alt((tag("UNSEEN "), tag(" UNSEEN "))),
map_res(digit1, |s| {
usize::from_str(unsafe { std::str::from_utf8_unchecked(s) })
}),

+ 1
- 1
melib/src/backends/imap_async/untagged.rs View File

@ -57,7 +57,7 @@ impl ImapConnection {
MailboxSelection::None => return Ok(false),
};
let mailbox =
std::clone::Clone::clone(&self.uid_store.mailboxes.read().unwrap()[&mailbox_hash]);
std::clone::Clone::clone(&self.uid_store.mailboxes.lock().await[&mailbox_hash]);
let mut response = String::with_capacity(8 * 1024);
let untagged_response =

+ 25
- 21
melib/src/backends/imap_async/watch.rs View File

@ -60,7 +60,7 @@ pub async fn poll_with_examine(kit: ImapWatchKit) -> Result<()> {
conn.connect().await?;
let mut response = String::with_capacity(8 * 1024);
loop {
let mailboxes = uid_store.mailboxes.read()?;
let mailboxes = uid_store.mailboxes.lock().await;
for mailbox in mailboxes.values() {
examine_updates(mailbox, &mut conn, &uid_store).await?;
}
@ -84,8 +84,8 @@ pub async fn idle(kit: ImapWatchKit) -> Result<()> {
conn.connect().await?;
let mailbox: ImapMailbox = match uid_store
.mailboxes
.read()
.unwrap()
.lock()
.await
.values()
.find(|f| f.parent.is_none() && (f.special_usage() == SpecialUsageMailbox::Inbox))
.map(std::clone::Clone::clone)
@ -158,37 +158,38 @@ pub async fn idle(kit: ImapWatchKit) -> Result<()> {
}
Err(e) => {
debug!("{:?}", e);
panic!("could not select mailbox");
return Err(e).chain_err_summary(|| "could not select mailbox");
}
};
}
exit_on_error!(conn, mailbox_hash, conn.send_command(b"IDLE").await);
let mut iter = ImapBlockingConnection::from(conn);
let mut blockn = ImapBlockingConnection::from(conn);
let mut beat = std::time::Instant::now();
let mut watch = std::time::Instant::now();
/* duration interval to send heartbeat */
let _26_mins = std::time::Duration::from_secs(26 * 60);
const _26_MINS: std::time::Duration = std::time::Duration::from_secs(26 * 60);
/* duration interval to check other mailboxes for changes */
let _5_mins = std::time::Duration::from_secs(5 * 60);
while let Some(line) = iter.next() {
const _5_MINS: std::time::Duration = std::time::Duration::from_secs(5 * 60);
while let Some(line) = blockn.into_stream().await {
let now = std::time::Instant::now();
if now.duration_since(beat) >= _26_mins {
if now.duration_since(beat) >= _26_MINS {
let mut main_conn_lck = main_conn.lock().await;
exit_on_error!(
iter.conn,
blockn.conn,
mailbox_hash,
iter.conn.send_raw(b"DONE").await
iter.conn.read_response(&mut response, RequiredResponses::empty()).await
iter.conn.send_command(b"IDLE").await
blockn.conn.send_raw(b"DONE").await
blockn.conn.read_response(&mut response, RequiredResponses::empty()).await
blockn.conn.send_command(b"IDLE").await
main_conn_lck.send_command(b"NOOP").await
main_conn_lck.read_response(&mut response, RequiredResponses::empty()).await
);
beat = now;
}
if now.duration_since(watch) >= _5_mins {
if now.duration_since(watch) >= _5_MINS {
/* Time to poll all inboxes */
let mut conn = main_conn.lock().await;
for mailbox in uid_store.mailboxes.read().unwrap().values() {
let mailboxes = uid_store.mailboxes.lock().await;
for mailbox in mailboxes.values() {
exit_on_error!(
conn,
mailbox_hash,
@ -243,6 +244,10 @@ pub async fn idle(kit: ImapWatchKit) -> Result<()> {
.contains_key(&(mailbox_hash, uid))
{
if let Ok(mut env) = Envelope::from_bytes(
/* unwrap() is safe since we ask for RFC822 in the
* above FETCH, thus uid_fetch_responses() if
* returns a successful parse, it will include the
* RFC822 response */
body.unwrap(),
flags.as_ref().map(|&(f, _)| f),
) {
@ -346,9 +351,8 @@ pub async fn idle(kit: ImapWatchKit) -> Result<()> {
let mut conn = main_conn.lock().await;
/* UID FETCH ALL UID, cross-ref, then FETCH difference headers
* */
let mut prev_exists = mailbox.exists.lock().unwrap();
debug!("exists {}", n);
if n > prev_exists.len() {
if n > mailbox.exists.lock().unwrap().len() {
exit_on_error!(
conn,
mailbox_hash,
@ -356,7 +360,7 @@ pub async fn idle(kit: ImapWatchKit) -> Result<()> {
conn.send_command(
&[
b"FETCH",
format!("{}:{}", prev_exists.len() + 1, n).as_bytes(),
format!("{}:{}", mailbox.exists.lock().unwrap().len() + 1, n).as_bytes(),
b"(UID FLAGS RFC822)",
]
.join(&b' '),
@ -418,7 +422,7 @@ pub async fn idle(kit: ImapWatchKit) -> Result<()> {
&[(uid, &env)],
)?;
}
prev_exists.insert_new(env.hash());
mailbox.exists.lock().unwrap().insert_new(env.hash());
conn.add_refresh_event(RefreshEvent {
account_hash: uid_store.account_hash,
@ -488,7 +492,7 @@ pub async fn idle(kit: ImapWatchKit) -> Result<()> {
}
}
debug!("IDLE connection dropped");
let err: &str = iter.err().unwrap_or("Unknown reason.");
let err: &str = blockn.err().unwrap_or("Unknown reason.");
main_conn.lock().await.add_refresh_event(RefreshEvent {
account_hash: uid_store.account_hash,
mailbox_hash,
@ -759,7 +763,7 @@ pub async fn examine_updates(
}
Err(e) => {
debug!("{:?}", e);
panic!("could not select mailbox");
return Err(e).chain_err_summary(|| "could not select mailbox");
}
};
Ok(())

+ 0
- 2
melib/src/backends/jmap/operations.rs View File

@ -20,8 +20,6 @@
*/
use super::*;
use crate::error::Result;
use std::cell::Cell;
use std::sync::{Arc, RwLock};

+ 95
- 16
src/components/mail/compose.rs View File

@ -21,9 +21,11 @@
use super::*;
use melib::list_management;
use melib::Draft;
use crate::conf::accounts::JobRequest;
use crate::jobs::{oneshot, JobId};
use crate::terminal::embed::EmbedGrid;
use melib::Draft;
use nix::sys::wait::WaitStatus;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
@ -64,6 +66,7 @@ impl std::ops::DerefMut for EmbedStatus {
#[derive(Debug)]
pub struct Composer {
reply_context: Option<(MailboxHash, EnvelopeHash)>,
reply_bytes_request: Option<(JobId, oneshot::Receiver<Result<Vec<u8>>>)>,
account_cursor: usize,
cursor: Cursor,
@ -89,6 +92,7 @@ impl Default for Composer {
pager.set_reflow(text_processing::Reflow::FormatFlowed);
Composer {
reply_context: None,
reply_bytes_request: None,
account_cursor: 0,
cursor: Cursor::Headers,
@ -229,21 +233,6 @@ impl Composer {
}
}
}
match account.operation(msg) {
Err(err) => {
context.replies.push_back(UIEvent::Notification(
None,
err.to_string(),
Some(NotificationType::ERROR),
));
}
Ok(op) => {
//FIXME
//let parent_bytes = op.as_bytes();
//ret.draft = Draft::new_reply(&parent_message, parent_bytes.unwrap());
}
}
let subject = parent_message.subject();
ret.draft.headers_mut().insert(
"Subject".into(),
@ -254,6 +243,54 @@ impl Composer {
},
);
drop(parent_message);
drop(account);
match context.accounts[coordinates.0]
.operation(msg)
.and_then(|mut op| op.as_bytes())
{
Err(err) => {
context.replies.push_back(UIEvent::Notification(
None,
err.to_string(),
Some(NotificationType::ERROR),
));
}
Ok(fut) => {
let (mut rcvr, job_id) = context.accounts[coordinates.0]
.job_executor
.spawn_specialized(fut);
context.accounts[coordinates.0]
.active_jobs
.insert(job_id.clone(), JobRequest::AsBytes);
if let Ok(Some(parent_bytes)) = try_recv_timeout!(&mut rcvr) {
match parent_bytes {
Err(err) => {
context.replies.push_back(UIEvent::Notification(
None,
err.to_string(),
Some(NotificationType::ERROR),
));
}
Ok(parent_bytes) => {
let env_hash = msg;
let parent_message =
context.accounts[coordinates.0].collection.get_env(env_hash);
let mut new_draft = Draft::new_reply(&parent_message, &parent_bytes);
new_draft
.headers_mut()
.extend(ret.draft.headers_mut().drain());
new_draft
.attachments_mut()
.extend(ret.draft.attachments_mut().drain(..));
ret.set_draft(new_draft);
}
}
} else {
ret.reply_bytes_request = Some((job_id, rcvr));
}
}
}
ret.account_cursor = coordinates.0;
ret.reply_context = Some((coordinates.1, msg));
ret
@ -585,6 +622,48 @@ impl Component for Composer {
fn process_event(&mut self, mut event: &mut UIEvent, context: &mut Context) -> bool {
let shortcuts = self.get_shortcuts(context);
match (&mut self.mode, &mut event) {
(_, UIEvent::StatusEvent(StatusEvent::JobFinished(ref job_id)))
if self
.reply_bytes_request
.as_ref()
.map(|(j, _)| j == job_id)
.unwrap_or(false) =>
{
let bytes = self
.reply_bytes_request
.take()
.unwrap()
.1
.try_recv()
.unwrap()
.unwrap();
match bytes {
Ok(parent_bytes) => {
let env_hash = self.reply_context.unwrap().1;
let parent_message = context.accounts[self.account_cursor]
.collection
.get_env(env_hash);
let mut new_draft = Draft::new_reply(&parent_message, &parent_bytes);
new_draft
.headers_mut()
.extend(self.draft.headers_mut().drain());
new_draft
.attachments_mut()
.extend(self.draft.attachments_mut().drain(..));
self.set_draft(new_draft);
self.set_dirty(true);
self.initialized = false;
}
Err(err) => {
context.replies.push_back(UIEvent::Notification(
Some(format!("Failed to load parent envelope")),
err.to_string(),
Some(NotificationType::ERROR),
));
}
}
return true;
}
(ViewMode::Edit, _) => {
if self.pager.process_event(event, context) {
return true;

+ 81
- 62
src/components/mail/listing.rs View File

@ -20,6 +20,7 @@
*/
use super::*;
use crate::conf::accounts::JobRequest;
use crate::types::segment_tree::SegmentTree;
use smallvec::SmallVec;
use std::collections::{HashMap, HashSet};
@ -175,7 +176,10 @@ pub trait MailListingTrait: ListingTrait {
));
}
Ok(fut) => {
//accout.job_executor.spawn_specialized(fut);
let (rcvr, job_id) = account.job_executor.spawn_specialized(fut);
account
.active_jobs
.insert(job_id, JobRequest::SetFlags(env_hash, rcvr));
}
},
ListingAction::SetUnseen => match envelope.set_unseen(op) {
@ -184,7 +188,12 @@ pub trait MailListingTrait: ListingTrait {
StatusEvent::DisplayMessage(e.to_string()),
));
}
Ok(fut) => {}
Ok(fut) => {
let (rcvr, job_id) = account.job_executor.spawn_specialized(fut);
account
.active_jobs
.insert(job_id, JobRequest::SetFlags(env_hash, rcvr));
}
},
ListingAction::Delete => {
drop(envelope);
@ -197,20 +206,21 @@ pub trait MailListingTrait: ListingTrait {
));
return;
}
Ok(fut) => {}
Ok(fut) => {
let (rcvr, job_id) = account.job_executor.spawn_specialized(fut);
account
.active_jobs
.insert(job_id, JobRequest::DeleteMessage(env_hash, rcvr));
}
}
continue;
}
ListingAction::CopyTo(ref mailbox_path) => {
drop(envelope);
/*
* FIXME
match account
.mailbox_by_path(mailbox_path)
.and_then(|mailbox_hash| {
op.as_bytes()
.and_then(|bytes| account.save(bytes, mailbox_hash, None))
}) {
.and_then(|mailbox_hash| op.copy_to(mailbox_hash))
{
Err(err) => {
context.replies.push_back(UIEvent::Notification(
Some("Could not copy.".to_string()),
@ -219,16 +229,18 @@ pub trait MailListingTrait: ListingTrait {
));
return;
}
Ok(fut) => {}
Ok(fut) => {
let (rcvr, job_id) = account.job_executor.spawn_specialized(fut);
account
.active_jobs
.insert(job_id, JobRequest::SaveMessage(mailbox_hash, rcvr));
}
}
*/
continue;
}
ListingAction::CopyToOtherAccount(ref account_name, ref mailbox_path) => {
drop(envelope);
/*
* FIXME
if let Err(err) = op.as_bytes().map(|b| b.to_vec()).and_then(|bytes| {
if let Err(err) = op.as_bytes().and_then(|bytes_fut| {
let account_pos = context
.accounts
.iter()
@ -241,7 +253,11 @@ pub trait MailListingTrait: ListingTrait {
})?;
let account = &mut context.accounts[account_pos];
let mailbox_hash = account.mailbox_by_path(mailbox_path)?;
account.save(&bytes, mailbox_hash, None)
let (rcvr, job_id) = account.job_executor.spawn_specialized(bytes_fut);
account
.active_jobs
.insert(job_id, JobRequest::CopyTo(mailbox_hash, rcvr));
Ok(())
}) {
context.replies.push_back(UIEvent::Notification(
Some("Could not copy.".to_string()),
@ -250,21 +266,52 @@ pub trait MailListingTrait: ListingTrait {
));
return;
}
*/
continue;
}
ListingAction::MoveTo(ref mailbox_path) => {
drop(envelope);
/*
* FIXME
if let Err(err) =
account
.mailbox_by_path(mailbox_path)
.and_then(|mailbox_hash| {
op.as_bytes()
.and_then(|bytes| account.save(bytes, mailbox_hash, None))
})
match account
.mailbox_by_path(mailbox_path)
.and_then(|mailbox_hash| op.copy_to(mailbox_hash))
{
Err(err) => {
context.replies.push_back(UIEvent::Notification(
Some("Could not copy.".to_string()),
err.to_string(),
Some(NotificationType::ERROR),
));
return;
}
Ok(fut) => {
let (rcvr, job_id) = account.job_executor.spawn_specialized(fut);
account
.active_jobs
.insert(job_id, JobRequest::SaveMessage(mailbox_hash, rcvr));
}
}
continue;
}
ListingAction::MoveToOtherAccount(ref account_name, ref mailbox_path) => {
drop(envelope);
if let Err(err) = op.as_bytes().and_then(|bytes_fut| {
let account_pos = context
.accounts
.iter()
.position(|el| el.name() == account_name)
.ok_or_else(|| {
MeliError::new(format!(
"`{}` is not a valid account name",
account_name
))
})?;
let account = &mut context.accounts[account_pos];
let mailbox_hash = account.mailbox_by_path(mailbox_path)?;
let (rcvr, job_id) = account.job_executor.spawn_specialized(bytes_fut);
account
.active_jobs
.insert(job_id, JobRequest::CopyTo(mailbox_hash, rcvr));
Ok(())
}) {
context.replies.push_back(UIEvent::Notification(
Some("Could not copy.".to_string()),
err.to_string(),
@ -272,46 +319,12 @@ pub trait MailListingTrait: ListingTrait {
));
return;
}
*/
continue;
}
ListingAction::MoveToOtherAccount(ref account_name, ref mailbox_path) => {
drop(envelope);
/* FIXME
if let Err(err) = op
.as_bytes()
.map(|b| b.to_vec())
.and_then(|bytes| {
let account_pos = context
.accounts
.iter()
.position(|el| el.name() == account_name)
.ok_or_else(|| {
MeliError::new(format!(
"`{}` is not a valid account name",
account_name
))
})?;
let account = &mut context.accounts[account_pos];
let mailbox_hash = account.mailbox_by_path(mailbox_path)?;
account.save(&bytes, mailbox_hash, None)
})
.and_then(|()| {
let account = &mut context.accounts[account_pos];
/*
account
.delete(env_hash, mailbox_hash)
.chain_err_summary(|| {
"Envelope was copied but removal from original account failed"
})
})
{
context.replies.push_back(UIEvent::Notification(
Some("Could not move.".to_string()),
err.to_string(),
Some(NotificationType::ERROR),
));
return;
}
*/
continue;
}
@ -326,7 +339,10 @@ pub trait MailListingTrait: ListingTrait {