1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#![allow(clippy::wildcard_imports)]
use super::*;
impl ImapConnection {
// -----------------------------------------------------------------------
// COMPRESS (RFC 4978)
// -----------------------------------------------------------------------
/// COMPRESS DEFLATE — negotiate and activate compression (RFC 4978 Section 4).
///
/// Sends the `COMPRESS DEFLATE` command. If the server accepts, wraps the
/// current transport stream in a deflate compression layer using raw deflate
/// (RFC 1951) as required by RFC 4978 Section 3.
///
/// After successful return, all subsequent I/O on this connection is
/// compressed. Any already-buffered post-OK compressed bytes are preserved
/// so the first compressed server response is not lost.
///
/// Requires the `COMPRESS=DEFLATE` capability. RFC 4978 Section 3 permits
/// COMPRESS to be negotiated either before or after TLS; the effective
/// on-the-wire layering is still compression before encryption.
///
/// The upgrade is atomic via the `Poisoned` sentinel pattern (I9, I10)
/// — handled entirely by the driver task.
pub async fn compress(&self, timeout: Duration) -> Result<(), Error> {
// RFC 4978 Section 4: COMPRESS is valid in Authenticated and Selected states.
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
// Check COMPRESS=DEFLATE capability from the snapshot.
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::CompressDeflate) {
return Err(Error::MissingCapability("COMPRESS=DEFLATE".into()));
}
}
tokio::time::timeout(
timeout,
self.submit_upgrade(driver::UpgradePayload::Compress),
)
.await
.map_err(|_| Error::Timeout)?
}
// -----------------------------------------------------------------------
// NOTIFY (RFC 5465)
// -----------------------------------------------------------------------
/// NOTIFY SET — register interest in mailbox and message events
/// (RFC 5465 Section 3).
///
/// Replaces any previous NOTIFY configuration. A successful NOTIFY SET
/// has an implicit NOOP effect: the server flushes any pending changes
/// to the selected mailbox before the tagged OK (RFC 5465 Section 3).
///
/// Notifications arrive as typed events on the event queue and can be
/// retrieved via the event receiver.
///
/// # Errors
///
/// - [`Error::MissingCapability`] if the server does not advertise `NOTIFY`.
/// - [`Error::Protocol`] if the command is issued in an invalid state.
/// - [`Error::No`] if the server rejects the request (e.g. `[BADEVENT]`
/// for unsupported event types, RFC 5465 Section 5).
pub async fn notify_set(
&self,
params: NotifySetParams,
timeout: Duration,
) -> Result<(), Error> {
// RFC 5465 Section 3: NOTIFY is a `command-auth` extension, valid
// in Authenticated or Selected state.
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
self.require_notify()?;
// RFC 6855 Section 6: UTF8=ONLY servers reject commands that might
// require UTF-8 support until ENABLE UTF8=ACCEPT. Only check when
// the command actually carries mailbox strings (Subtree/Mailboxes
// filters). Atom-only registrations like `(selected (MessageNew))`
// contain no mailbox strings and are valid without ENABLE.
let has_mailbox_strings = params.event_groups.iter().any(|g| {
matches!(
g.filter,
MailboxFilter::Subtree(_) | MailboxFilter::Mailboxes(_)
)
});
if has_mailbox_strings {
self.check_utf8_only_enforced()?;
}
let cmd = Command::NotifySet(params);
let consumer = super::dispatch::NotifySetConsumer::default();
// Driver sets in_notify_set before sending; apply_tagged updates
// notify flags on tagged OK. NOTIFICATIONOVERFLOW clears them.
// submit_regular returns Result<Result<bool, Error>, Error>.
// Inner Result: consumer wraps NO/BAD as output (not finalize
// error) so reclassified_as_events is always emitted.
let overflow = tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)???;
if overflow {
// RFC 5465 Section 5.8: server cannot keep up. Notify flags
// already cleared by apply_side_effects in the driver.
warn!(
"NOTIFICATIONOVERFLOW during NOTIFY SET — registration \
cleared (RFC 5465 Section 5.8)"
);
}
debug!("NOTIFY SET completed (RFC 5465)");
Ok(())
}
/// NOTIFY NONE — cancel all event subscriptions (RFC 5465 Section 3).
///
/// Reverts to baseline IMAP behavior where the server only sends
/// notifications during command processing, and only for the selected
/// mailbox.
///
/// # Errors
///
/// - [`Error::MissingCapability`] if the server does not advertise `NOTIFY`.
/// - [`Error::Protocol`] if the command is issued in an invalid state.
pub async fn notify_none(&self, timeout: Duration) -> Result<(), Error> {
// RFC 5465 Section 3: NOTIFY is a `command-auth` extension.
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
self.require_notify()?;
let cmd = Command::NotifyNone;
// Driver sets in_notify_set(default) before sending;
// apply_tagged resets notify flags on tagged OK.
tokio::time::timeout(
timeout,
self.submit_regular(cmd, super::dispatch::TaggedOkConsumer::default()),
)
.await
.map_err(|_| Error::Timeout)??;
debug!("NOTIFY NONE — notifications disabled (RFC 5465)");
Ok(())
}
// -----------------------------------------------------------------------
// ENABLE (RFC 5161 Section 3 / RFC 9051 Section 6.3.1)
// -----------------------------------------------------------------------
/// ENABLE UTF8=ACCEPT — negotiate UTF-8 support (RFC 6855 Section 3).
///
/// Convenience wrapper around [`enable`](Self::enable) that sends
/// `ENABLE UTF8=ACCEPT` and returns `true` if the server confirmed
/// the extension, `false` if the server did not include it in its
/// `ENABLED` response.
///
/// Must be issued in authenticated state before SELECT/EXAMINE
/// (RFC 5161 Section 2).
pub async fn enable_utf8(&self, timeout: Duration) -> Result<bool, Error> {
let enabled = self.enable(&["UTF8=ACCEPT"], timeout).await?;
Ok(enabled
.iter()
.any(|e| e.eq_ignore_ascii_case("UTF8=ACCEPT")))
}
/// ENABLE — request the server to activate one or more IMAP extensions
/// (RFC 5161 Section 3 / RFC 9051 Section 6.3.1).
///
/// Sends the ENABLE command with the given capability atoms and returns
/// the list of extensions the server actually activated (per the
/// untagged `ENABLED` response).
///
/// # State requirement
///
/// RFC 5161 Section 2: ENABLE is valid only in the Authenticated state,
/// before any mailbox is selected. Attempting to ENABLE in the Selected
/// state returns [`Error::Protocol`].
///
/// # Ordering constraint
///
/// RFC 5161 Section 2.2: ENABLE MUST be issued before any command that
/// depends on the extension (e.g. SELECT with QRESYNC requires
/// `ENABLE QRESYNC` first).
///
/// # Return value
///
/// Returns only the extensions the server activated in this call. The
/// server may return a subset of the requested capabilities. Already-
/// enabled extensions are included in the cached state but may not
/// appear in the per-call return value.
///
/// # Snapshot timing
///
/// The `enabled` list in the cached state snapshot is updated by the
/// driver after the tagged OK is processed.
pub async fn enable(
&self,
capabilities: &[&str],
timeout: Duration,
) -> Result<Vec<String>, Error> {
// RFC 5161 Section 2: ENABLE is only valid in Authenticated state.
self.require_state(&[SessionState::Authenticated])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Enable)
&& !super::auth::is_rev2_from_snapshot(&snap)
{
return Err(Error::MissingCapability("ENABLE".into()));
}
}
let cmd = Command::Enable {
capabilities: capabilities.iter().map(|c| (*c).to_owned()).collect(),
};
let consumer = super::dispatch::EnableConsumer::default();
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
// -----------------------------------------------------------------------
// NAMESPACE (RFC 2342 / RFC 9051 Section 6.3.11)
// -----------------------------------------------------------------------
/// NAMESPACE — query the server's namespace configuration
/// (RFC 2342 Section 4 / RFC 9051 Section 6.3.11).
///
/// Returns the server's personal, other-users, and shared namespace
/// descriptors.
///
/// Requires the `NAMESPACE` capability or an `IMAP4rev2` connection
/// (RFC 9051 folds NAMESPACE into the base protocol).
pub async fn namespace(&self, timeout: Duration) -> Result<NamespaceResponse, Error> {
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Namespace)
&& !super::auth::is_rev2_from_snapshot(&snap)
{
return Err(Error::MissingCapability("NAMESPACE".into()));
}
}
tokio::time::timeout(
timeout,
self.submit_regular(
Command::Namespace,
super::dispatch::NamespaceConsumer::default(),
),
)
.await
.map_err(|_| Error::Timeout)?
}
// -----------------------------------------------------------------------
// ID (RFC 2971)
// -----------------------------------------------------------------------
/// ID — exchange client/server identification (RFC 2971 Section 3.1).
///
/// Sends client identification parameters to the server and returns
/// the server's identification parameters. Each parameter is a
/// `(field_name, value)` pair; a `None` value encodes as `NIL` on the
/// wire (RFC 2971 Section 3.1).
///
/// Valid in any state (RFC 2971 Section 3.1).
pub async fn id(
&self,
params: &[(&str, Option<&str>)],
timeout: Duration,
) -> Result<Vec<(String, Option<String>)>, Error> {
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Id) {
return Err(Error::MissingCapability("ID".into()));
}
}
let cmd = Command::Id(
params
.iter()
.map(|(k, v)| ((*k).to_owned(), v.map(str::to_owned)))
.collect(),
);
tokio::time::timeout(
timeout,
self.submit_regular(cmd, super::dispatch::IdConsumer::default()),
)
.await
.map_err(|_| Error::Timeout)?
}
// -----------------------------------------------------------------------
// GETMETADATA / SETMETADATA (RFC 5464)
// -----------------------------------------------------------------------
/// GETMETADATA — retrieve mailbox or server metadata
/// (RFC 5464 Section 4.2).
///
/// Fetches metadata entries for the given mailbox. Use an empty string
/// `""` for server-level metadata (RFC 5464 Section 4.2).
///
/// `max_size` limits the size of returned values in bytes
/// (RFC 5464 Section 4.2.2). `depth` controls entry hierarchy
/// traversal: `"0"` (default), `"1"`, or `"infinity"`
/// (RFC 5464 Section 4.2.2).
///
/// Requires `METADATA` or `METADATA-SERVER` capability
/// (RFC 5464 Section 1).
pub async fn get_metadata(
&self,
mailbox: &str,
entries: &[&str],
max_size: Option<u64>,
depth: Option<&str>,
timeout: Duration,
) -> Result<MetadataResult, Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Metadata)
&& !snap.capabilities.contains(&Capability::MetadataServer)
{
return Err(Error::MissingCapability("METADATA".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::GetMetadata {
mailbox: mailbox_name.clone(),
entries: entries.iter().map(|e| (*e).to_owned()).collect(),
max_size,
depth: depth.map(str::to_owned),
};
let consumer = super::dispatch::MetadataConsumer::new(mailbox_name.as_str().to_owned());
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
/// SETMETADATA — set or delete mailbox/server metadata entries
/// (RFC 5464 Section 4.3).
///
/// Each entry is a `(name, value)` pair. A `None` value deletes the
/// entry (RFC 5464 Section 4.3).
///
/// Requires `METADATA` or `METADATA-SERVER` capability
/// (RFC 5464 Section 1).
pub async fn set_metadata(
&self,
mailbox: &str,
entries: &[(&str, Option<&[u8]>)],
timeout: Duration,
) -> Result<(), Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Metadata)
&& !snap.capabilities.contains(&Capability::MetadataServer)
{
return Err(Error::MissingCapability("METADATA".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::SetMetadata {
mailbox: mailbox_name,
entries: entries
.iter()
.map(|(k, v)| ((*k).to_owned(), v.map(<[u8]>::to_vec)))
.collect(),
};
tokio::time::timeout(
timeout,
self.submit_regular(cmd, super::dispatch::TaggedOkConsumer::default()),
)
.await
.map_err(|_| Error::Timeout)?
}
// -----------------------------------------------------------------------
// QUOTA (RFC 2087 / RFC 9208)
// -----------------------------------------------------------------------
/// GETQUOTA — query quota resources for a quota root
/// (RFC 2087 Section 4.2 / RFC 9208 Section 4.2).
///
/// Returns the resource limits and usage for the specified quota root.
///
/// Requires the `QUOTA` capability (RFC 2087 Section 5.1).
pub async fn get_quota(
&self,
root: &str,
timeout: Duration,
) -> Result<Vec<QuotaResource>, Error> {
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Quota) {
return Err(Error::MissingCapability("QUOTA".into()));
}
}
let cmd = Command::GetQuota {
root: root.to_owned(),
};
let consumer = super::dispatch::QuotaConsumer::new(root.to_owned());
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
/// GETQUOTAROOT — query quota roots for a mailbox
/// (RFC 2087 Section 4.3 / RFC 9208 Section 4.3).
///
/// Returns the quota root names and their associated quota resources
/// for the given mailbox.
///
/// Requires the `QUOTA` capability (RFC 2087 Section 5.1).
pub async fn get_quota_root(
&self,
mailbox: &str,
timeout: Duration,
) -> Result<QuotaRootResponse, Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Quota) {
return Err(Error::MissingCapability("QUOTA".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::GetQuotaRoot {
mailbox: mailbox_name.clone(),
};
let consumer = super::dispatch::QuotaRootConsumer::new(mailbox_name.as_str().to_owned());
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
/// SETQUOTA — set resource limits on a quota root
/// (RFC 2087 Section 4.1 / RFC 9208 Section 4.1).
///
/// Each element of `resources` is a `(resource_name, limit)` pair —
/// e.g. `("STORAGE", 51200)`.
///
/// Requires the `QUOTASET` capability (RFC 9208 Section 3.2).
pub async fn set_quota(
&self,
root: &str,
resources: &[(&str, u64)],
timeout: Duration,
) -> Result<Vec<QuotaResource>, Error> {
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::QuotaSet) {
return Err(Error::MissingCapability("QUOTASET".into()));
}
}
let cmd = Command::SetQuota {
root: root.to_owned(),
resources: resources
.iter()
.map(|(name, limit)| ((*name).to_owned(), *limit))
.collect(),
};
let consumer = super::dispatch::QuotaConsumer::new(root.to_owned());
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
// -----------------------------------------------------------------------
// ACL (RFC 4314)
// -----------------------------------------------------------------------
/// SETACL — set access control list entries for a mailbox
/// (RFC 4314 Section 3.1).
///
/// Sets the rights for `identifier` on `mailbox`. The `rights` string
/// uses the format defined in RFC 4314 Section 2 (e.g., `"+lrswipkxte"`
/// to add rights, `"-d"` to remove, or a bare string to replace).
///
/// Requires the `ACL` capability (RFC 4314 Section 1).
pub async fn set_acl(
&self,
mailbox: &str,
identifier: &str,
rights: &str,
timeout: Duration,
) -> Result<(), Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Acl) {
return Err(Error::MissingCapability("ACL".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::SetAcl {
mailbox: mailbox_name,
identifier: identifier.to_owned(),
rights: rights.to_owned(),
};
tokio::time::timeout(
timeout,
self.submit_regular(cmd, super::dispatch::TaggedOkConsumer::default()),
)
.await
.map_err(|_| Error::Timeout)?
}
/// DELETEACL — remove an access control list entry for a mailbox
/// (RFC 4314 Section 3.2).
///
/// Removes all rights for `identifier` on `mailbox`.
///
/// Requires the `ACL` capability (RFC 4314 Section 1).
pub async fn delete_acl(
&self,
mailbox: &str,
identifier: &str,
timeout: Duration,
) -> Result<(), Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Acl) {
return Err(Error::MissingCapability("ACL".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::DeleteAcl {
mailbox: mailbox_name,
identifier: identifier.to_owned(),
};
tokio::time::timeout(
timeout,
self.submit_regular(cmd, super::dispatch::TaggedOkConsumer::default()),
)
.await
.map_err(|_| Error::Timeout)?
}
/// GETACL — retrieve the access control list for a mailbox
/// (RFC 4314 Section 3.3).
///
/// Returns the list of identifier/rights pairs for the given mailbox.
///
/// Requires the `ACL` capability (RFC 4314 Section 1).
pub async fn get_acl(&self, mailbox: &str, timeout: Duration) -> Result<Vec<AclEntry>, Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Acl) {
return Err(Error::MissingCapability("ACL".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::GetAcl {
mailbox: mailbox_name.clone(),
};
let consumer = super::dispatch::AclConsumer::new(mailbox_name.as_str().to_owned());
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
/// LISTRIGHTS — query the set of rights grantable to an identifier
/// on a mailbox (RFC 4314 Section 3.4).
///
/// Returns the required (always-granted) rights and the groups of
/// optional rights that can be independently granted or revoked.
///
/// Requires the `ACL` capability (RFC 4314 Section 1).
pub async fn list_rights(
&self,
mailbox: &str,
identifier: &str,
timeout: Duration,
) -> Result<ListRightsResponse, Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Acl) {
return Err(Error::MissingCapability("ACL".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::ListRights {
mailbox: mailbox_name.clone(),
identifier: identifier.to_owned(),
};
let consumer = super::dispatch::ListRightsConsumer::new(
mailbox_name.as_str().to_owned(),
identifier.to_owned(),
);
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
/// MYRIGHTS — query the logged-in user's rights on a mailbox
/// (RFC 4314 Section 3.5).
///
/// Returns the rights string for the current user on the given mailbox.
///
/// Requires the `ACL` capability (RFC 4314 Section 1).
pub async fn my_rights(&self, mailbox: &str, timeout: Duration) -> Result<String, Error> {
self.check_utf8_only_enforced()?;
self.require_state(&[SessionState::Authenticated, SessionState::Selected])?;
{
let snap = self.state_rx.borrow();
if !snap.capabilities.contains(&Capability::Acl) {
return Err(Error::MissingCapability("ACL".into()));
}
}
let mailbox_name = MailboxName::new(mailbox)?;
let cmd = Command::MyRights {
mailbox: mailbox_name.clone(),
};
let consumer = super::dispatch::MyRightsConsumer::new(mailbox_name.as_str().to_owned());
tokio::time::timeout(timeout, self.submit_regular(cmd, consumer))
.await
.map_err(|_| Error::Timeout)?
}
/// Verify that the server advertises the NOTIFY capability (RFC 5465).
fn require_notify(&self) -> Result<(), Error> {
let has_notify = self
.state_rx
.borrow()
.capabilities
.contains(&Capability::Notify);
if !has_notify {
return Err(Error::MissingCapability("NOTIFY".into()));
}
Ok(())
}
}
/// Compute which ongoing response types a NOTIFY registration can produce.
///
/// Returns `(list, status, metadata)` booleans for *ongoing* notifications:
/// - `list`: any non-selected events are registered. `MailboxName`
/// (RFC 5465 Section 5.4) and `SubscriptionChange` (RFC 5465 Section 5.5)
/// produce explicit LIST responses, but ALL non-selected registrations
/// can trigger LIST responses for ACL changes — RFC 5465 Section 5.9
/// requires `LIST \NoAccess` / `LIST` when the logged-in user loses or
/// regains the `l` (lookup) ACL right on any monitored mailbox,
/// regardless of which event types were requested.
/// - `status`: message events on non-selected mailboxes — `FlagChange`
/// and `AnnotationChange` (RFC 5465 Section 5.1), `MessageNew`
/// (RFC 5465 Section 5.2), and `MessageExpunge` (RFC 5465 Section 5.3),
/// all delivered as STATUS responses.
/// - `metadata`: `MailboxMetadataChange` (RFC 5465 Section 5.6) or
/// `ServerMetadataChange` (RFC 5465 Section 5.7) events registered —
/// delivered as METADATA.
///
/// The `params.status` indicator is NOT included here — it only triggers
/// an initial STATUS snapshot for non-selected mailboxes with message
/// events (RFC 5465 Section 4), not ongoing STATUS notifications. Those
/// initial responses are handled by the re-push logic in `notify_set()`.
pub(crate) fn compute_notify_flags(params: &NotifySetParams) -> (bool, bool, bool) {
let mut list = false;
let mut status = false;
let mut metadata = false;
for group in ¶ms.event_groups {
let is_non_selected = !matches!(
group.filter,
MailboxFilter::Selected | MailboxFilter::SelectedDelayed
);
// RFC 5465 Section 5.9: any non-selected registration can trigger
// LIST \NoAccess or LIST responses for ACL changes on monitored
// mailboxes. Enable LIST buffering for all non-selected groups.
if is_non_selected && !group.events.is_empty() {
list = true;
}
for event in &group.events {
match event {
NotifyEvent::MessageNew { .. }
| NotifyEvent::MessageExpunge
| NotifyEvent::FlagChange
| NotifyEvent::AnnotationChange => {
// RFC 5465 Sections 5.1–5.3: on non-selected mailboxes
// all message events are delivered as STATUS responses.
if is_non_selected {
status = true;
}
}
NotifyEvent::MailboxMetadataChange | NotifyEvent::ServerMetadataChange => {
// RFC 5465 Sections 5.6–5.7: delivered as METADATA.
metadata = true;
}
NotifyEvent::Other(_) => {
// Unknown extension event — we don't know which
// response type the server will use for delivery.
// Enable ALL known buffering flags defensively so
// the notification is not silently dropped regardless
// of delivery mechanism. Note: Other(_) can only
// appear under non-selected filters — the encoder
// rejects it for selected / selected-delayed per
// RFC 5465 Section 6.1 / Section 8 (event-ext is a
// separate ABNF production from message-event).
// Truly extension-defined responses
// (`UntaggedResponse::Unknown`) are also routed
// to the unsolicited buffer by the dispatcher.
list = true;
status = true;
metadata = true;
}
_ => {}
}
}
}
(list, status, metadata)
}