2022-06-22 12:58:57 +00:00
|
|
|
use std::error::Error as StdError;
|
2022-06-22 15:26:52 +00:00
|
|
|
use std::fmt;
|
2022-06-30 12:02:57 +00:00
|
|
|
use std::sync::Arc;
|
2024-01-17 09:14:48 +00:00
|
|
|
use tokio::sync::Notify;
|
2022-06-20 16:09:20 +00:00
|
|
|
|
2022-06-29 13:39:54 +00:00
|
|
|
use crate::imap::mailbox_view::MailboxView;
|
2022-06-29 11:16:58 +00:00
|
|
|
use crate::mail::user::User;
|
2022-06-17 16:39:36 +00:00
|
|
|
|
2022-06-22 12:58:57 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
ForbiddenTransition,
|
|
|
|
}
|
|
|
|
impl fmt::Display for Error {
|
2022-06-22 15:26:52 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "Forbidden Transition")
|
|
|
|
}
|
2022-06-22 12:58:57 +00:00
|
|
|
}
|
2022-06-22 15:26:52 +00:00
|
|
|
impl StdError for Error {}
|
2022-06-22 12:58:57 +00:00
|
|
|
|
2022-06-17 16:39:36 +00:00
|
|
|
pub enum State {
|
|
|
|
NotAuthenticated,
|
2022-06-30 12:02:57 +00:00
|
|
|
Authenticated(Arc<User>),
|
2024-01-17 07:22:15 +00:00
|
|
|
Selected(Arc<User>, MailboxView, MailboxPerm),
|
2024-01-17 15:56:05 +00:00
|
|
|
Idle(Arc<User>, MailboxView, MailboxPerm, Arc<Notify>),
|
2022-06-22 15:26:52 +00:00
|
|
|
Logout,
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
2022-06-22 12:58:57 +00:00
|
|
|
|
2024-01-17 07:22:15 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum MailboxPerm {
|
|
|
|
ReadOnly,
|
|
|
|
ReadWrite,
|
|
|
|
}
|
|
|
|
|
2022-06-22 12:58:57 +00:00
|
|
|
pub enum Transition {
|
2022-06-29 10:50:44 +00:00
|
|
|
None,
|
2022-06-30 12:02:57 +00:00
|
|
|
Authenticate(Arc<User>),
|
2024-01-17 07:22:15 +00:00
|
|
|
Select(MailboxView, MailboxPerm),
|
2024-01-17 09:14:48 +00:00
|
|
|
Idle(Notify),
|
2024-01-17 07:22:15 +00:00
|
|
|
UnIdle,
|
2022-06-22 15:26:52 +00:00
|
|
|
Unselect,
|
|
|
|
Logout,
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See RFC3501 section 3.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc3501#page-13
|
|
|
|
impl State {
|
2024-01-02 19:23:33 +00:00
|
|
|
pub fn apply(&mut self, tr: Transition) -> Result<(), Error> {
|
2024-01-17 07:22:15 +00:00
|
|
|
let new_state = match (std::mem::replace(self, State::NotAuthenticated), tr) {
|
2024-01-02 19:23:33 +00:00
|
|
|
(_s, Transition::None) => return Ok(()),
|
|
|
|
(State::NotAuthenticated, Transition::Authenticate(u)) => State::Authenticated(u),
|
2022-07-13 13:26:00 +00:00
|
|
|
(
|
2024-01-17 07:22:15 +00:00
|
|
|
State::Authenticated(u) | State::Selected(u, _, _),
|
|
|
|
Transition::Select(m, p),
|
|
|
|
) => State::Selected(u, m, p),
|
|
|
|
(State::Selected(u, _, _) , Transition::Unselect) => {
|
2024-01-02 19:23:33 +00:00
|
|
|
State::Authenticated(u.clone())
|
2022-07-13 13:26:55 +00:00
|
|
|
}
|
2024-01-17 09:14:48 +00:00
|
|
|
(State::Selected(u, m, p), Transition::Idle(s)) => {
|
2024-01-17 15:56:05 +00:00
|
|
|
State::Idle(u, m, p, Arc::new(s))
|
2024-01-17 07:22:15 +00:00
|
|
|
},
|
2024-01-17 09:14:48 +00:00
|
|
|
(State::Idle(u, m, p, _), Transition::UnIdle) => {
|
2024-01-17 07:22:15 +00:00
|
|
|
State::Selected(u, m, p)
|
|
|
|
},
|
2024-01-02 19:23:33 +00:00
|
|
|
(_, Transition::Logout) => State::Logout,
|
|
|
|
_ => return Err(Error::ForbiddenTransition),
|
|
|
|
};
|
|
|
|
|
|
|
|
*self = new_state;
|
|
|
|
|
|
|
|
Ok(())
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
|
|
|
}
|