deltachat/imap/
idle.rs

1use std::time::Duration;
2
3use anyhow::{Context as _, Result};
4use async_channel::Receiver;
5use async_imap::extensions::idle::IdleResponse;
6use tokio::time::timeout;
7
8use super::session::Session;
9use super::Imap;
10use crate::context::Context;
11use crate::log::{info, warn};
12use crate::net::TIMEOUT;
13use crate::tools::{self, time_elapsed};
14
15/// Timeout after which IDLE is finished
16/// if there are no responses from the server.
17///
18/// If `* OK Still here` keepalives are sent more frequently
19/// than this duration, timeout should never be triggered.
20/// For example, Dovecot sends keepalives every 2 minutes by default.
21const IDLE_TIMEOUT: Duration = Duration::from_secs(5 * 60);
22
23impl Session {
24    pub async fn idle(
25        mut self,
26        context: &Context,
27        idle_interrupt_receiver: Receiver<()>,
28        folder: &str,
29    ) -> Result<Self> {
30        let create = true;
31        self.select_with_uidvalidity(context, folder, create)
32            .await?;
33
34        if self.drain_unsolicited_responses(context)? {
35            self.new_mail = true;
36        }
37
38        if self.new_mail {
39            info!(
40                context,
41                "Skipping IDLE in {folder:?} because there may be new mail."
42            );
43            return Ok(self);
44        }
45
46        if let Ok(()) = idle_interrupt_receiver.try_recv() {
47            info!(context, "Skip IDLE in {folder:?} because we got interrupt.");
48            return Ok(self);
49        }
50
51        let mut handle = self.inner.idle();
52        handle
53            .init()
54            .await
55            .with_context(|| format!("IMAP IDLE protocol failed to init in folder {folder:?}"))?;
56
57        // At this point IDLE command was sent and we received a "+ idling" response. We will now
58        // read from the stream without getting any data for up to `IDLE_TIMEOUT`. If we don't
59        // disable read timeout, we would get a timeout after `crate::net::TIMEOUT`, which is a lot
60        // shorter than `IDLE_TIMEOUT`.
61        handle.as_mut().set_read_timeout(None);
62        let (idle_wait, interrupt) = handle.wait_with_timeout(IDLE_TIMEOUT);
63
64        info!(
65            context,
66            "IDLE entering wait-on-remote state in folder {folder:?}."
67        );
68
69        // Spawn a task to relay interrupts from `idle_interrupt_receiver`
70        // into interruptions of IDLE.
71        let interrupt_relay = {
72            let context = context.clone();
73            let folder = folder.to_string();
74
75            tokio::spawn(async move {
76                idle_interrupt_receiver.recv().await.ok();
77
78                info!(context, "{folder:?}: Received interrupt, stopping IDLE.");
79
80                // Drop `interrupt` in order to stop the IMAP IDLE.
81                drop(interrupt);
82            })
83        };
84
85        match idle_wait.await {
86            Ok(IdleResponse::NewData(x)) => {
87                info!(context, "{folder:?}: Idle has NewData {x:?}");
88            }
89            Ok(IdleResponse::Timeout) => {
90                info!(context, "{folder:?}: Idle-wait timeout or interruption.");
91            }
92            Ok(IdleResponse::ManualInterrupt) => {
93                info!(context, "{folder:?}: Idle wait was interrupted manually.");
94            }
95            Err(err) => {
96                warn!(context, "{folder:?}: Idle wait errored: {err:?}.");
97            }
98        }
99
100        // Abort the task, then await to ensure the future is dropped.
101        interrupt_relay.abort();
102        interrupt_relay.await.ok();
103
104        let mut session = tokio::time::timeout(Duration::from_secs(15), handle.done())
105            .await
106            .with_context(|| format!("{folder}: IMAP IDLE protocol timed out"))?
107            .with_context(|| format!("{folder}: IMAP IDLE failed"))?;
108        session.as_mut().set_read_timeout(Some(TIMEOUT));
109        self.inner = session;
110
111        // Fetch mail once we exit IDLE.
112        self.new_mail = true;
113
114        Ok(self)
115    }
116}
117
118impl Imap {
119    /// Idle using polling.
120    pub(crate) async fn fake_idle(
121        &mut self,
122        context: &Context,
123        watch_folder: String,
124    ) -> Result<()> {
125        let fake_idle_start_time = tools::Time::now();
126
127        info!(context, "IMAP-fake-IDLEing folder={:?}", watch_folder);
128
129        // Wait for 60 seconds or until we are interrupted.
130        match timeout(Duration::from_secs(60), self.idle_interrupt_receiver.recv()).await {
131            Err(_) => info!(context, "Fake IDLE finished."),
132            Ok(_) => info!(context, "Fake IDLE interrupted."),
133        }
134
135        info!(
136            context,
137            "IMAP-fake-IDLE done after {:.4}s",
138            time_elapsed(&fake_idle_start_time).as_millis() as f64 / 1000.,
139        );
140        Ok(())
141    }
142}