aerogramme/src/imap/session.rs

97 lines
3.4 KiB
Rust
Raw Normal View History

2024-01-03 14:00:05 +00:00
use crate::imap::capability::ServerCapability;
2022-06-30 11:36:21 +00:00
use crate::imap::command::{anonymous, authenticated, examined, selected};
2022-06-20 16:09:20 +00:00
use crate::imap::flow;
2024-01-02 19:23:33 +00:00
use crate::imap::response::Response;
2022-06-20 16:09:20 +00:00
use crate::login::ArcLoginProvider;
2024-01-02 19:23:33 +00:00
use imap_codec::imap_types::command::Command;
2022-06-13 09:44:02 +00:00
2022-06-17 16:39:36 +00:00
//-----
2022-06-13 09:44:02 +00:00
pub struct Instance {
2022-06-17 16:39:36 +00:00
pub login_provider: ArcLoginProvider,
2024-01-03 11:29:19 +00:00
pub server_capabilities: ServerCapability,
2022-06-17 16:39:36 +00:00
pub state: flow::State,
2022-06-13 09:44:02 +00:00
}
impl Instance {
2024-01-03 11:29:19 +00:00
pub fn new(login_provider: ArcLoginProvider, cap: ServerCapability) -> Self {
2022-06-14 08:19:24 +00:00
Self {
2022-06-15 16:40:39 +00:00
login_provider,
2022-06-17 16:39:36 +00:00
state: flow::State::NotAuthenticated,
2024-01-03 11:29:19 +00:00
server_capabilities: cap,
2022-06-14 08:19:24 +00:00
}
2022-06-13 09:44:02 +00:00
}
2024-01-02 19:23:33 +00:00
pub async fn command(&mut self, cmd: Command<'static>) -> Response<'static> {
// Command behavior is modulated by the state.
// To prevent state error, we handle the same command in separate code paths.
let (resp, tr) = match &mut self.state {
flow::State::NotAuthenticated => {
let ctx = anonymous::AnonymousContext {
req: &cmd,
login_provider: &self.login_provider,
2024-01-03 11:29:19 +00:00
server_capabilities: &self.server_capabilities,
2024-01-02 19:23:33 +00:00
};
anonymous::dispatch(ctx).await
}
flow::State::Authenticated(ref user) => {
2024-01-03 14:00:05 +00:00
let ctx = authenticated::AuthenticatedContext {
req: &cmd,
2024-01-03 11:29:19 +00:00
server_capabilities: &self.server_capabilities,
2024-01-03 14:00:05 +00:00
user,
};
2024-01-02 19:23:33 +00:00
authenticated::dispatch(ctx).await
}
flow::State::Selected(ref user, ref mut mailbox) => {
let ctx = selected::SelectedContext {
req: &cmd,
2024-01-03 11:29:19 +00:00
server_capabilities: &self.server_capabilities,
2024-01-02 19:23:33 +00:00
user,
mailbox,
};
selected::dispatch(ctx).await
2022-06-28 13:52:55 +00:00
}
2024-01-02 19:23:33 +00:00
flow::State::Examined(ref user, ref mut mailbox) => {
let ctx = examined::ExaminedContext {
req: &cmd,
2024-01-03 11:29:19 +00:00
server_capabilities: &self.server_capabilities,
2024-01-02 19:23:33 +00:00
user,
mailbox,
};
examined::dispatch(ctx).await
}
flow::State::Logout => Response::build()
.tag(cmd.tag.clone())
.message("No commands are allowed in the LOGOUT state.")
.bad()
.map(|r| (r, flow::Transition::None)),
}
.unwrap_or_else(|err| {
tracing::error!("Command error {:?} occured while processing {:?}", err, cmd);
(
Response::build()
.to_req(&cmd)
.message("Internal error while processing command")
.bad()
.unwrap(),
flow::Transition::None,
)
});
if let Err(e) = self.state.apply(tr) {
tracing::error!(
"Transition error {:?} occured while processing on command {:?}",
e,
cmd
);
return Response::build()
.to_req(&cmd)
.message(
"Internal error, processing command triggered an illegal IMAP state transition",
)
.bad()
.unwrap();
2022-06-22 15:26:52 +00:00
}
2022-06-20 16:09:20 +00:00
2024-01-02 19:23:33 +00:00
resp
2022-06-22 15:26:52 +00:00
}
2022-06-13 09:44:02 +00:00
}