aerogramme/src/imap/flow.rs

115 lines
3.2 KiB
Rust
Raw Normal View History

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-02-22 16:30:40 +00:00
2024-01-17 09:14:48 +00:00
use tokio::sync::Notify;
2024-02-22 16:30:40 +00:00
use imap_codec::imap_types::core::Tag;
2022-06-20 16:09:20 +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-18 17:03:21 +00:00
Idle(
Arc<User>,
MailboxView,
MailboxPerm,
Tag<'static>,
Arc<Notify>,
),
2022-06-22 15:26:52 +00:00
Logout,
2022-06-17 16:39:36 +00:00
}
2024-02-22 16:30:40 +00:00
impl State {
pub fn notify(&self) -> Option<Arc<Notify>> {
match self {
Self::Idle(_, _, _, _, anotif) => Some(anotif.clone()),
_ => None,
}
}
}
2024-01-18 17:02:24 +00:00
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use State::*;
match self {
NotAuthenticated => write!(f, "NotAuthenticated"),
Authenticated(..) => write!(f, "Authenticated"),
Selected(..) => write!(f, "Selected"),
Idle(..) => write!(f, "Idle"),
Logout => write!(f, "Logout"),
}
}
}
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-18 16:33:57 +00:00
Idle(Tag<'static>, 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
}
2024-01-18 17:02:24 +00:00
impl fmt::Display for Transition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Transition::*;
match self {
None => write!(f, "None"),
Authenticate(..) => write!(f, "Authenticated"),
Select(..) => write!(f, "Selected"),
Idle(..) => write!(f, "Idle"),
UnIdle => write!(f, "UnIdle"),
Unselect => write!(f, "Unselect"),
Logout => write!(f, "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-18 17:02:24 +00:00
tracing::debug!(state=%self, transition=%tr, "try change state");
let new_state = match (std::mem::replace(self, State::Logout), tr) {
(s, Transition::None) => s,
2024-01-02 19:23:33 +00:00
(State::NotAuthenticated, Transition::Authenticate(u)) => State::Authenticated(u),
2024-01-18 17:03:21 +00:00
(State::Authenticated(u) | State::Selected(u, _, _), Transition::Select(m, p)) => {
State::Selected(u, m, p)
2022-07-13 13:26:55 +00:00
}
2024-01-18 17:03:21 +00:00
(State::Selected(u, _, _), Transition::Unselect) => State::Authenticated(u.clone()),
2024-01-18 16:33:57 +00:00
(State::Selected(u, m, p), Transition::Idle(t, s)) => {
State::Idle(u, m, p, t, Arc::new(s))
2024-01-18 17:03:21 +00:00
}
(State::Idle(u, m, p, _, _), Transition::UnIdle) => State::Selected(u, m, p),
2024-01-02 19:23:33 +00:00
(_, Transition::Logout) => State::Logout,
2024-01-18 17:02:24 +00:00
(s, t) => {
tracing::error!(state=%s, transition=%t, "forbidden transition");
2024-01-18 17:03:21 +00:00
return Err(Error::ForbiddenTransition);
2024-01-18 17:02:24 +00:00
}
2024-01-02 19:23:33 +00:00
};
*self = new_state;
2024-01-18 17:02:24 +00:00
tracing::debug!(state=%self, "transition succeeded");
2024-01-02 19:23:33 +00:00
Ok(())
2022-06-17 16:39:36 +00:00
}
}