1use rusmes_proto::Username;
4use rusmes_storage::MailboxId;
5use std::time::Duration;
6
7#[derive(Debug, Clone, PartialEq)]
9pub enum ImapState {
10 NotAuthenticated,
12 Authenticated,
14 Selected { mailbox_id: MailboxId },
16 Idle { mailbox_id: MailboxId },
18 Logout,
20}
21
22#[derive(Debug, Clone)]
24pub struct MailboxSnapshot {
25 pub exists: u32,
26 pub recent: u32,
27}
28
29pub struct ImapSession {
31 pub state: ImapState,
32 pub tag: Option<String>,
33 pub username: Option<Username>,
34 pub mailbox_snapshot: Option<MailboxSnapshot>,
35 pub idle_timeout: Duration,
36}
37
38impl ImapSession {
39 pub fn new() -> Self {
41 Self::new_with_timeout(Duration::from_secs(1800))
42 }
43
44 pub fn new_with_timeout(idle_timeout: Duration) -> Self {
46 Self {
47 state: ImapState::NotAuthenticated,
48 tag: None,
49 username: None,
50 mailbox_snapshot: None,
51 idle_timeout,
52 }
53 }
54
55 pub fn state(&self) -> &ImapState {
57 &self.state
58 }
59
60 pub fn update_snapshot(&mut self, exists: u32, recent: u32) {
62 self.mailbox_snapshot = Some(MailboxSnapshot { exists, recent });
63 }
64
65 pub fn mailbox_id(&self) -> Option<&MailboxId> {
67 match &self.state {
68 ImapState::Selected { mailbox_id } | ImapState::Idle { mailbox_id } => Some(mailbox_id),
69 _ => None,
70 }
71 }
72}
73
74impl Default for ImapSession {
75 fn default() -> Self {
76 Self::new()
77 }
78}