It compiles again!
This commit is contained in:
parent
c04b16a601
commit
98f55be730
7 changed files with 95 additions and 76 deletions
|
@ -10,19 +10,19 @@ use crate::imap::session::InnerContext;
|
||||||
|
|
||||||
//--- dispatching
|
//--- dispatching
|
||||||
|
|
||||||
pub async fn dispatch<'a>(ctx: &'a InnerContext<'a>) -> Result<Response> {
|
pub async fn dispatch<'a>(ctx: InnerContext<'a>) -> Result<(Response, flow::Transition)> {
|
||||||
match ctx.req.body {
|
match &ctx.req.body {
|
||||||
CommandBody::Capability => capability(ctx).await,
|
CommandBody::Capability => capability(ctx).await,
|
||||||
CommandBody::Login { username, password } => login(ctx, username, password).await,
|
CommandBody::Login { username, password } => login(ctx, username.clone(), password.clone()).await,
|
||||||
_ => Status::no(Some(ctx.req.tag.clone()), None, "This command is not available in the ANONYMOUS state.")
|
_ => Status::no(Some(ctx.req.tag.clone()), None, "This command is not available in the ANONYMOUS state.")
|
||||||
.map(|s| vec![ImapRes::Status(s)])
|
.map(|s| (vec![ImapRes::Status(s)], flow::Transition::No))
|
||||||
.map_err(Error::msg),
|
.map_err(Error::msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- Command controllers, private
|
//--- Command controllers, private
|
||||||
|
|
||||||
async fn capability<'a>(ctx: InnerContext<'a>) -> Result<Response> {
|
async fn capability<'a>(ctx: InnerContext<'a>) -> Result<(Response, flow::Transition)> {
|
||||||
let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
|
let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
|
||||||
let res = vec![
|
let res = vec![
|
||||||
ImapRes::Data(Data::Capability(capabilities)),
|
ImapRes::Data(Data::Capability(capabilities)),
|
||||||
|
@ -31,20 +31,20 @@ async fn capability<'a>(ctx: InnerContext<'a>) -> Result<Response> {
|
||||||
.map_err(Error::msg)?,
|
.map_err(Error::msg)?,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
Ok(res)
|
Ok((res, flow::Transition::No))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn login<'a>(ctx: InnerContext<'a>, username: AString, password: AString) -> Result<Response> {
|
async fn login<'a>(ctx: InnerContext<'a>, username: AString, password: AString) -> Result<(Response, flow::Transition)> {
|
||||||
let (u, p) = (String::try_from(username)?, String::try_from(password)?);
|
let (u, p) = (String::try_from(username)?, String::try_from(password)?);
|
||||||
tracing::info!(user = %u, "command.login");
|
tracing::info!(user = %u, "command.login");
|
||||||
|
|
||||||
let creds = match ctx.login_provider.login(&u, &p).await {
|
let creds = match ctx.login.login(&u, &p).await {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::debug!(error=%e, "authentication failed");
|
tracing::debug!(error=%e, "authentication failed");
|
||||||
return Ok(vec![ImapRes::Status(
|
return Ok((vec![ImapRes::Status(
|
||||||
Status::no(Some(ctx.req.tag.clone()), None, "Authentication failed")
|
Status::no(Some(ctx.req.tag.clone()), None, "Authentication failed")
|
||||||
.map_err(Error::msg)?,
|
.map_err(Error::msg)?,
|
||||||
)]);
|
)], flow::Transition::No));
|
||||||
}
|
}
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
};
|
};
|
||||||
|
@ -53,13 +53,13 @@ async fn login<'a>(ctx: InnerContext<'a>, username: AString, password: AString)
|
||||||
creds,
|
creds,
|
||||||
name: u.clone(),
|
name: u.clone(),
|
||||||
};
|
};
|
||||||
ctx.state.authenticate(user)?;
|
let tr = flow::Transition::Authenticate(user);
|
||||||
|
|
||||||
tracing::info!(username=%u, "connected");
|
tracing::info!(username=%u, "connected");
|
||||||
Ok(vec![
|
Ok((vec![
|
||||||
//@FIXME we could send a capability status here too
|
//@FIXME we could send a capability status here too
|
||||||
ImapRes::Status(
|
ImapRes::Status(
|
||||||
Status::ok(Some(ctx.req.tag.clone()), None, "completed").map_err(Error::msg)?,
|
Status::ok(Some(ctx.req.tag.clone()), None, "completed").map_err(Error::msg)?,
|
||||||
),
|
),
|
||||||
])
|
], tr))
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,21 +11,22 @@ use crate::imap::session::InnerContext;
|
||||||
use crate::imap::flow::User;
|
use crate::imap::flow::User;
|
||||||
use crate::mailbox::Mailbox;
|
use crate::mailbox::Mailbox;
|
||||||
|
|
||||||
pub async fn dispatch<'a>(inner: &'a InnerContext<'a>, user: &'a User) -> Result<Response> {
|
/*pub async fn dispatch<'a>(inner: &'a mut InnerContext<'a>, user: &'a User) -> Result<Response> {
|
||||||
let ctx = StateContext { inner, user, tag: &inner.req.tag };
|
let ctx = StateContext { inner, user, tag: &inner.req.tag };
|
||||||
|
|
||||||
match ctx.req.body.as_ref() {
|
match &ctx.inner.req.body {
|
||||||
CommandBody::Lsub { reference, mailbox_wildcard, } => ctx.lsub(reference, mailbox_wildcard).await,
|
CommandBody::Lsub { reference, mailbox_wildcard, } => ctx.lsub(reference.clone(), mailbox_wildcard.clone()).await,
|
||||||
CommandBody::List { reference, mailbox_wildcard, } => ctx.list(reference, mailbox_wildcard).await,
|
CommandBody::List { reference, mailbox_wildcard, } => ctx.list(reference.clone(), mailbox_wildcard.clone()).await,
|
||||||
CommandBody::Select { mailbox } => ctx.select(mailbox).await,
|
CommandBody::Select { mailbox } => ctx.select(mailbox.clone()).await,
|
||||||
_ => anonymous::dispatch(ctx.inner).await,
|
_ => anonymous::dispatch(ctx.inner).await,
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
// --- PRIVATE ---
|
// --- PRIVATE ---
|
||||||
|
|
||||||
|
/*
|
||||||
struct StateContext<'a> {
|
struct StateContext<'a> {
|
||||||
inner: InnerContext<'a>,
|
inner: &'a mut InnerContext<'a>,
|
||||||
user: &'a User,
|
user: &'a User,
|
||||||
tag: &'a Tag,
|
tag: &'a Tag,
|
||||||
}
|
}
|
||||||
|
@ -70,7 +71,7 @@ impl<'a> StateContext<'a> {
|
||||||
async fn select(&self, mailbox: MailboxCodec) -> Result<Response> {
|
async fn select(&self, mailbox: MailboxCodec) -> Result<Response> {
|
||||||
let name = String::try_from(mailbox)?;
|
let name = String::try_from(mailbox)?;
|
||||||
|
|
||||||
let mut mb = Mailbox::new(self.user.creds, name.clone())?;
|
let mut mb = Mailbox::new(&self.user.creds, name.clone())?;
|
||||||
tracing::info!(username=%self.user.name, mailbox=%name, "mailbox.selected");
|
tracing::info!(username=%self.user.name, mailbox=%name, "mailbox.selected");
|
||||||
|
|
||||||
let sum = mb.summary().await?;
|
let sum = mb.summary().await?;
|
||||||
|
@ -80,7 +81,7 @@ impl<'a> StateContext<'a> {
|
||||||
|
|
||||||
self.inner.state.select(mb)?;
|
self.inner.state.select(mb)?;
|
||||||
|
|
||||||
let r_unseen = Status::ok(None, Some(Code::Unseen(0)), "").map_err(Error::msg)?;
|
let r_unseen = Status::ok(None, Some(Code::Unseen(std::num::NonZeroU32::new(1)?)), "").map_err(Error::msg)?;
|
||||||
//let r_permanentflags = Status::ok(None, Some(Code::
|
//let r_permanentflags = Status::ok(None, Some(Code::
|
||||||
|
|
||||||
Ok(vec![
|
Ok(vec![
|
||||||
|
@ -99,3 +100,4 @@ impl<'a> StateContext<'a> {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -12,7 +12,8 @@ use crate::imap::session::InnerContext;
|
||||||
use crate::imap::flow::User;
|
use crate::imap::flow::User;
|
||||||
use crate::mailbox::Mailbox;
|
use crate::mailbox::Mailbox;
|
||||||
|
|
||||||
pub async fn dispatch<'a>(inner: &'a InnerContext<'a>, user: &'a User, mailbox: &'a Mailbox) -> Result<Response> {
|
/*
|
||||||
|
pub async fn dispatch<'a>(inner: InnerContext<'a>, user: &'a User, mailbox: &'a Mailbox) -> Result<Response> {
|
||||||
let ctx = StateContext { inner, user, mailbox, tag: &inner.req.tag };
|
let ctx = StateContext { inner, user, mailbox, tag: &inner.req.tag };
|
||||||
|
|
||||||
match ctx.inner.req.body {
|
match ctx.inner.req.body {
|
||||||
|
@ -20,9 +21,11 @@ pub async fn dispatch<'a>(inner: &'a InnerContext<'a>, user: &'a User, mailbox:
|
||||||
_ => authenticated::dispatch(inner, user).await,
|
_ => authenticated::dispatch(inner, user).await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// --- PRIVATE ---
|
// --- PRIVATE ---
|
||||||
|
|
||||||
|
/*
|
||||||
struct StateContext<'a> {
|
struct StateContext<'a> {
|
||||||
inner: InnerContext<'a>,
|
inner: InnerContext<'a>,
|
||||||
user: &'a User,
|
user: &'a User,
|
||||||
|
@ -43,3 +46,4 @@ impl<'a> StateContext<'a> {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
|
use std::fmt;
|
||||||
|
use std::error::Error as StdError;
|
||||||
|
|
||||||
use crate::login::Credentials;
|
use crate::login::Credentials;
|
||||||
use crate::mailbox::Mailbox;
|
use crate::mailbox::Mailbox;
|
||||||
|
@ -8,46 +9,45 @@ pub struct User {
|
||||||
pub creds: Credentials,
|
pub creds: Credentials,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
ForbiddenTransition,
|
||||||
|
}
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "Forbidden Transition")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl StdError for Error { }
|
||||||
|
|
||||||
|
|
||||||
pub enum State {
|
pub enum State {
|
||||||
NotAuthenticated,
|
NotAuthenticated,
|
||||||
Authenticated(User),
|
Authenticated(User),
|
||||||
Selected(User, Mailbox),
|
Selected(User, Mailbox),
|
||||||
Logout
|
Logout
|
||||||
}
|
}
|
||||||
pub enum Error {
|
|
||||||
ForbiddenTransition,
|
pub enum Transition {
|
||||||
|
No,
|
||||||
|
Authenticate(User),
|
||||||
|
Select(Mailbox),
|
||||||
|
Unselect,
|
||||||
|
Logout,
|
||||||
}
|
}
|
||||||
|
|
||||||
// See RFC3501 section 3.
|
// See RFC3501 section 3.
|
||||||
// https://datatracker.ietf.org/doc/html/rfc3501#page-13
|
// https://datatracker.ietf.org/doc/html/rfc3501#page-13
|
||||||
impl State {
|
impl State {
|
||||||
pub fn authenticate(&mut self, user: User) -> Result<(), Error> {
|
pub fn apply(self, tr: Transition) -> Result<Self, Error> {
|
||||||
self = match self {
|
match (self, tr) {
|
||||||
State::NotAuthenticated => State::Authenticated(user),
|
(s, Transition::No) => Ok(s),
|
||||||
_ => return Err(Error::ForbiddenTransition),
|
(State::NotAuthenticated, Transition::Authenticate(u)) => Ok(State::Authenticated(u)),
|
||||||
};
|
(State::Authenticated(u), Transition::Select(m)) => Ok(State::Selected(u, m)),
|
||||||
Ok(())
|
(State::Selected(u, _), Transition::Unselect) => Ok(State::Authenticated(u)),
|
||||||
}
|
(_, Transition::Logout) => Ok(State::Logout),
|
||||||
|
_ => Err(Error::ForbiddenTransition),
|
||||||
pub fn logout(&mut self) -> Self {
|
}
|
||||||
self = State::Logout;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn select(&mut self, mailbox: Mailbox) -> Result<(), Error> {
|
|
||||||
self = match self {
|
|
||||||
State::Authenticated(user) => State::Selected(user, mailbox),
|
|
||||||
_ => return Err(Error::ForbiddenTransition),
|
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unselect(&mut self) -> Result<(), Error> {
|
|
||||||
self = match self {
|
|
||||||
State::Selected(user, _) => State::Authenticated(user),
|
|
||||||
_ => return Err(Error::ForbiddenTransition),
|
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ pub async fn new(
|
||||||
|
|
||||||
//@FIXME add a configuration parameter
|
//@FIXME add a configuration parameter
|
||||||
let incoming = AddrIncoming::new(config.bind_addr).await?;
|
let incoming = AddrIncoming::new(config.bind_addr).await?;
|
||||||
tracing::info!("IMAP activated, will listen on {:#}", imap.incoming.local_addr);
|
tracing::info!("IMAP activated, will listen on {:#}", incoming.local_addr);
|
||||||
|
|
||||||
let imap = ImapServer::new(incoming).serve(Instance::new(login.clone()));
|
let imap = ImapServer::new(incoming).serve(Instance::new(login.clone()));
|
||||||
Ok(Server(imap))
|
Ok(Server(imap))
|
||||||
|
|
|
@ -86,9 +86,9 @@ impl Manager {
|
||||||
//-----
|
//-----
|
||||||
|
|
||||||
pub struct InnerContext<'a> {
|
pub struct InnerContext<'a> {
|
||||||
req: &'a Request,
|
pub req: &'a Request,
|
||||||
state: &'a flow::State,
|
pub state: &'a flow::State,
|
||||||
login: &'a ArcLoginProvider,
|
pub login: &'a ArcLoginProvider,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
|
@ -113,7 +113,7 @@ impl Instance {
|
||||||
// to ease debug
|
// to ease debug
|
||||||
// fn name(&self) -> String { }
|
// fn name(&self) -> String { }
|
||||||
|
|
||||||
async fn start(&mut self) {
|
async fn start(mut self) {
|
||||||
//@FIXME add more info about the runner
|
//@FIXME add more info about the runner
|
||||||
tracing::debug!("starting runner");
|
tracing::debug!("starting runner");
|
||||||
|
|
||||||
|
@ -123,28 +123,38 @@ impl Instance {
|
||||||
// Command behavior is modulated by the state.
|
// Command behavior is modulated by the state.
|
||||||
// To prevent state error, we handle the same command in separate code path depending
|
// To prevent state error, we handle the same command in separate code path depending
|
||||||
// on the State.
|
// on the State.
|
||||||
let cmd_res = match ctx.state {
|
let ctrl = match &self.state {
|
||||||
flow::State::NotAuthenticated => anonymous::dispatch(&ctx).await,
|
flow::State::NotAuthenticated => anonymous::dispatch(ctx).await,
|
||||||
flow::State::Authenticated(user) => authenticated::dispatch(&ctx, &user).await,
|
/*flow::State::Authenticated(user) => authenticated::dispatch(ctx, user).await,
|
||||||
flow::State::Selected(user, mailbox) => selected::dispatch(&ctx, &user, &mailbox).await,
|
flow::State::Selected(user, mailbox) => selected::dispatch(ctx, user, mailbox).await,*/
|
||||||
flow::State::Logout => Status::bad(Some(ctx.req.tag.clone()), None, "No commands are allowed in the LOGOUT state.")
|
_ => Status::bad(Some(ctx.req.tag.clone()), None, "No commands are allowed in the LOGOUT state.")
|
||||||
.map(|s| vec![ImapRes::Status(s)])
|
.map(|s| (vec![ImapRes::Status(s)], flow::Transition::No))
|
||||||
.map_err(Error::msg),
|
.map_err(Error::msg),
|
||||||
};
|
};
|
||||||
|
|
||||||
let imap_res = cmd_res.or_else(|e| match e.downcast::<BalError>() {
|
// Process result
|
||||||
Ok(be) => Err(be),
|
let res = match ctrl {
|
||||||
Err(e) => {
|
Ok((res, tr)) => {
|
||||||
tracing::warn!(error=%e, "internal.error");
|
//@FIXME unwrap
|
||||||
Status::bad(Some(msg.req.tag.clone()), None, "Internal error")
|
self.state = self.state.apply(tr).unwrap();
|
||||||
.map(|s| vec![ImapRes::Status(s)])
|
Ok(res)
|
||||||
.map_err(|e| BalError::Text(e.to_string()))
|
},
|
||||||
|
// Cast from anyhow::Error to Bal::Error
|
||||||
|
// @FIXME proper error handling would be great
|
||||||
|
Err(e) => match e.downcast::<BalError>() {
|
||||||
|
Ok(be) => Err(be),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!(error=%e, "internal.error");
|
||||||
|
Status::bad(Some(msg.req.tag.clone()), None, "Internal error")
|
||||||
|
.map(|s| vec![ImapRes::Status(s)])
|
||||||
|
.map_err(|e| BalError::Text(e.to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
//@FIXME I think we should quit this thread on error and having our manager watch it,
|
//@FIXME I think we should quit this thread on error and having our manager watch it,
|
||||||
// and then abort the session as it is corrupted.
|
// and then abort the session as it is corrupted.
|
||||||
msg.tx.send(imap_res).unwrap_or_else(|e| {
|
msg.tx.send(res).unwrap_or_else(|e| {
|
||||||
tracing::warn!("failed to send imap response to manager: {:#?}", e)
|
tracing::warn!("failed to send imap response to manager: {:#?}", e)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,10 @@ impl Server {
|
||||||
let (login, lmtp_conf, imap_conf) = build(config)?;
|
let (login, lmtp_conf, imap_conf) = build(config)?;
|
||||||
|
|
||||||
let lmtp_server = lmtp_conf.map(|cfg| LmtpServer::new(cfg, login.clone()));
|
let lmtp_server = lmtp_conf.map(|cfg| LmtpServer::new(cfg, login.clone()));
|
||||||
let imap_server = imap_conf.map(|cfg| imap::new(cfg, login.clone()));
|
let imap_server = match imap_conf {
|
||||||
|
Some(cfg) => Some(imap::new(cfg, login.clone()).await?),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Self { lmtp_server, imap_server })
|
Ok(Self { lmtp_server, imap_server })
|
||||||
}
|
}
|
||||||
|
@ -44,7 +47,7 @@ impl Server {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async {
|
async {
|
||||||
match self.imap_server.as_ref() {
|
match self.imap_server {
|
||||||
None => Ok(()),
|
None => Ok(()),
|
||||||
Some(s) => s.run(exit_signal.clone()).await,
|
Some(s) => s.run(exit_signal.clone()).await,
|
||||||
}
|
}
|
||||||
|
@ -74,7 +77,7 @@ fn build(config: Config) -> Result<(ArcLoginProvider, Option<LmtpConfig>, Option
|
||||||
(None, None) => bail!("No login provider is set up in config file"),
|
(None, None) => bail!("No login provider is set up in config file"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(lp, config.lmtp_config, config.imap_config)
|
Ok((lp, config.lmtp, config.imap))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn watch_ctrl_c() -> (watch::Receiver<bool>, Arc<watch::Sender<bool>>) {
|
pub fn watch_ctrl_c() -> (watch::Receiver<bool>, Arc<watch::Sender<bool>>) {
|
||||||
|
|
Loading…
Reference in a new issue