imap_client/client/
mod.rs1pub mod tokio;
2pub mod verifier;
3
4use std::time::Duration;
5
6use imap_next::{
7 client::Options as ClientOptions,
8 imap_types::{auth::AuthMechanism, core::Vec1, response::Capability},
9};
10
11use crate::tasks::resolver::Resolver;
12
13pub struct Client {
14 resolver: Resolver,
15 capabilities: Vec1<Capability<'static>>,
16 idle_timeout: Duration,
17 condstore_enabled: bool,
18}
19
20impl Client {
21 pub fn new(opts: ClientOptions) -> Self {
22 let client = imap_next::client::Client::new(opts);
23 let resolver = Resolver::new(client);
24
25 Self {
26 resolver,
27 capabilities: Vec1::from(Capability::Imap4Rev1),
28 idle_timeout: Duration::from_secs(5 * 60), condstore_enabled: false,
30 }
31 }
32
33 pub fn get_idle_timeout(&self) -> &Duration {
34 &self.idle_timeout
35 }
36
37 pub fn set_idle_timeout(&mut self, timeout: Duration) {
38 self.idle_timeout = timeout;
39 }
40
41 pub fn set_some_idle_timeout(&mut self, timeout: Option<Duration>) {
42 if let Some(timeout) = timeout {
43 self.set_idle_timeout(timeout)
44 }
45 }
46
47 pub fn with_idle_timeout(mut self, timeout: Duration) -> Self {
48 self.set_idle_timeout(timeout);
49 self
50 }
51
52 pub fn with_some_idle_timeout(mut self, timeout: Option<Duration>) -> Self {
53 self.set_some_idle_timeout(timeout);
54 self
55 }
56
57 pub fn capabilities(&self) -> &Vec1<Capability<'static>> {
64 &self.capabilities
65 }
66
67 pub fn capabilities_iter(&self) -> impl Iterator<Item = &Capability<'static>> + '_ {
72 self.capabilities().as_ref().iter()
73 }
74
75 pub fn supported_auth_mechanisms(&self) -> impl Iterator<Item = &AuthMechanism<'static>> + '_ {
77 self.capabilities_iter().filter_map(|capability| {
78 if let Capability::Auth(mechanism) = capability {
79 Some(mechanism)
80 } else {
81 None
82 }
83 })
84 }
85
86 pub fn supports_auth_mechanism(&self, mechanism: AuthMechanism<'static>) -> bool {
89 self.capabilities_iter().any(|capability| {
90 if let Capability::Auth(m) = capability {
91 m == &mechanism
92 } else {
93 false
94 }
95 })
96 }
97
98 pub fn login_supported(&self) -> bool {
100 !self
101 .capabilities_iter()
102 .any(|c| matches!(c, Capability::LoginDisabled))
103 }
104
105 pub fn ext_enable_supported(&self) -> bool {
108 self.capabilities_iter()
109 .any(|c| matches!(c, Capability::Enable))
110 }
111
112 pub fn ext_sasl_ir_supported(&self) -> bool {
115 self.capabilities_iter()
116 .any(|c| matches!(c, Capability::SaslIr))
117 }
118
119 pub fn ext_id_supported(&self) -> bool {
122 self.capabilities_iter()
123 .any(|c| matches!(c, Capability::Id))
124 }
125
126 pub fn ext_uidplus_supported(&self) -> bool {
129 self.capabilities_iter()
130 .any(|c| matches!(c, Capability::UidPlus))
131 }
132
133 pub fn ext_sort_supported(&self) -> bool {
136 self.capabilities_iter()
137 .any(|c| matches!(c, Capability::Sort(_)))
138 }
139
140 pub fn ext_thread_supported(&self) -> bool {
143 self.capabilities_iter()
144 .any(|c| matches!(c, Capability::Thread(_)))
145 }
146
147 pub fn ext_idle_supported(&self) -> bool {
150 self.capabilities_iter()
151 .any(|c| matches!(c, Capability::Idle))
152 }
153
154 pub fn ext_binary_supported(&self) -> bool {
157 self.capabilities_iter()
158 .any(|c| matches!(c, Capability::Binary))
159 }
160
161 pub fn ext_move_supported(&self) -> bool {
164 self.capabilities_iter()
165 .any(|c| matches!(c, Capability::Move))
166 }
167
168 pub fn ext_condstore_supported(&self) -> bool {
171 self.capabilities_iter()
172 .any(|c| matches!(c, Capability::CondStore))
173 }
174
175 pub fn condstore_enabled(&self) -> bool {
177 self.condstore_enabled
178 }
179}