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::Imap;
9use super::session::Session;
10use crate::context::Context;
11use crate::log::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 transport_id = self.transport_id();
31
32        self.select_with_uidvalidity(context, folder).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                "Transport {transport_id}: 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!(
48                context,
49                "Transport {transport_id}: Skip IDLE in {folder:?} because we got interrupt."
50            );
51            return Ok(self);
52        }
53
54        let mut handle = self.inner.idle();
55        handle
56            .init()
57            .await
58            .with_context(|| format!("IMAP IDLE protocol failed to init in folder {folder:?}"))?;
59
60        // At this point IDLE command was sent and we received a "+ idling" response. We will now
61        // read from the stream without getting any data for up to `IDLE_TIMEOUT`. If we don't
62        // disable read timeout, we would get a timeout after `crate::net::TIMEOUT`, which is a lot
63        // shorter than `IDLE_TIMEOUT`.
64        handle.as_mut().set_read_timeout(None);
65        let (idle_wait, interrupt) = handle.wait_with_timeout(IDLE_TIMEOUT);
66
67        info!(
68            context,
69            "Transport {transport_id}: IDLE entering wait-on-remote state in folder {folder:?}."
70        );
71
72        // Spawn a task to relay interrupts from `idle_interrupt_receiver`
73        // into interruptions of IDLE.
74        let interrupt_relay = {
75            let context = context.clone();
76            let folder = folder.to_string();
77
78            tokio::spawn(async move {
79                idle_interrupt_receiver.recv().await.ok();
80
81                info!(context, "{folder:?}: Received interrupt, stopping IDLE.");
82
83                // Drop `interrupt` in order to stop the IMAP IDLE.
84                drop(interrupt);
85            })
86        };
87
88        match idle_wait.await {
89            Ok(IdleResponse::NewData(x)) => {
90                info!(context, "{folder:?}: Idle has NewData {x:?}");
91            }
92            Ok(IdleResponse::Timeout) => {
93                info!(context, "{folder:?}: Idle-wait timeout or interruption.");
94            }
95            Ok(IdleResponse::ManualInterrupt) => {
96                info!(context, "{folder:?}: Idle wait was interrupted manually.");
97            }
98            Err(err) => {
99                warn!(context, "{folder:?}: Idle wait errored: {err:?}.");
100            }
101        }
102
103        // Abort the task, then await to ensure the future is dropped.
104        interrupt_relay.abort();
105        interrupt_relay.await.ok();
106
107        let mut session = tokio::time::timeout(Duration::from_secs(15), handle.done())
108            .await
109            .with_context(|| format!("{folder}: IMAP IDLE protocol timed out"))?
110            .with_context(|| format!("{folder}: IMAP IDLE failed"))?;
111        session.as_mut().set_read_timeout(Some(TIMEOUT));
112        self.inner = session;
113
114        // Fetch mail once we exit IDLE.
115        self.new_mail = true;
116
117        Ok(self)
118    }
119}
120
121impl Imap {
122    /// Idle using polling.
123    pub(crate) async fn fake_idle(
124        &mut self,
125        context: &Context,
126        watch_folder: String,
127    ) -> Result<()> {
128        let fake_idle_start_time = tools::Time::now();
129
130        info!(context, "IMAP-fake-IDLEing folder={:?}", watch_folder);
131
132        // Wait for 60 seconds or until we are interrupted.
133        match timeout(Duration::from_secs(60), self.idle_interrupt_receiver.recv()).await {
134            Err(_) => info!(context, "Fake IDLE finished."),
135            Ok(_) => info!(context, "Fake IDLE interrupted."),
136        }
137
138        info!(
139            context,
140            "IMAP-fake-IDLE done after {:.4}s",
141            time_elapsed(&fake_idle_start_time).as_millis() as f64 / 1000.,
142        );
143        Ok(())
144    }
145}