2024-01-01 16:54:48 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use imap_codec::imap_types::command::Command;
|
|
|
|
use imap_codec::imap_types::core::Tag;
|
2024-01-02 14:35:23 +00:00
|
|
|
use imap_codec::imap_types::response::{Code, Data, Status};
|
2024-01-18 17:03:21 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::Notify;
|
2024-01-01 16:54:48 +00:00
|
|
|
|
2024-01-17 07:22:15 +00:00
|
|
|
#[derive(Debug)]
|
2024-01-02 14:35:23 +00:00
|
|
|
pub enum Body<'a> {
|
|
|
|
Data(Data<'a>),
|
|
|
|
Status(Status<'a>),
|
2024-01-01 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub struct ResponseBuilder<'a> {
|
|
|
|
tag: Option<Tag<'a>>,
|
|
|
|
code: Option<Code<'a>>,
|
|
|
|
text: String,
|
|
|
|
body: Vec<Body<'a>>,
|
2024-01-01 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
impl<'a> ResponseBuilder<'a> {
|
|
|
|
pub fn to_req(mut self, cmd: &Command<'a>) -> Self {
|
|
|
|
self.tag = Some(cmd.tag.clone());
|
2024-01-01 16:54:48 +00:00
|
|
|
self
|
|
|
|
}
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn tag(mut self, tag: Tag<'a>) -> Self {
|
2024-01-01 16:54:48 +00:00
|
|
|
self.tag = Some(tag);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn message(mut self, txt: impl Into<String>) -> Self {
|
|
|
|
self.text = txt.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn code(mut self, code: Code<'a>) -> Self {
|
2024-01-01 16:54:48 +00:00
|
|
|
self.code = Some(code);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn data(mut self, data: Data<'a>) -> Self {
|
|
|
|
self.body.push(Body::Data(data));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn many_data(mut self, data: Vec<Data<'a>>) -> Self {
|
|
|
|
for d in data.into_iter() {
|
|
|
|
self = self.data(d);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-02 19:23:33 +00:00
|
|
|
#[allow(dead_code)]
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn info(mut self, status: Status<'a>) -> Self {
|
|
|
|
self.body.push(Body::Status(status));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-02 19:23:33 +00:00
|
|
|
#[allow(dead_code)]
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn many_info(mut self, status: Vec<Status<'a>>) -> Self {
|
|
|
|
for d in status.into_iter() {
|
|
|
|
self = self.info(d);
|
|
|
|
}
|
2024-01-01 16:54:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn set_body(mut self, body: Vec<Body<'a>>) -> Self {
|
|
|
|
self.body = body;
|
2024-01-01 16:54:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn ok(self) -> Result<Response<'a>> {
|
2024-01-01 16:54:48 +00:00
|
|
|
Ok(Response {
|
2024-01-02 14:35:23 +00:00
|
|
|
completion: Status::ok(self.tag, self.code, self.text)?,
|
|
|
|
body: self.body,
|
2024-01-01 16:54:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn no(self) -> Result<Response<'a>> {
|
|
|
|
Ok(Response {
|
|
|
|
completion: Status::no(self.tag, self.code, self.text)?,
|
|
|
|
body: self.body,
|
|
|
|
})
|
2024-01-01 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn bad(self) -> Result<Response<'a>> {
|
|
|
|
Ok(Response {
|
|
|
|
completion: Status::bad(self.tag, self.code, self.text)?,
|
|
|
|
body: self.body,
|
|
|
|
})
|
2024-01-01 16:54:48 +00:00
|
|
|
}
|
2024-01-02 14:35:23 +00:00
|
|
|
}
|
2024-01-01 16:54:48 +00:00
|
|
|
|
2024-01-17 07:22:15 +00:00
|
|
|
#[derive(Debug)]
|
2024-01-02 14:35:23 +00:00
|
|
|
pub struct Response<'a> {
|
2024-01-02 19:23:33 +00:00
|
|
|
pub body: Vec<Body<'a>>,
|
|
|
|
pub completion: Status<'a>,
|
2024-01-02 14:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Response<'a> {
|
|
|
|
pub fn build() -> ResponseBuilder<'a> {
|
2024-01-01 16:54:48 +00:00
|
|
|
ResponseBuilder {
|
2024-01-02 14:35:23 +00:00
|
|
|
tag: None,
|
|
|
|
code: None,
|
|
|
|
text: "".to_string(),
|
|
|
|
body: vec![],
|
2024-01-01 16:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 14:35:23 +00:00
|
|
|
pub fn bye() -> Result<Response<'a>> {
|
2024-01-01 16:54:48 +00:00
|
|
|
Ok(Response {
|
2024-01-02 14:35:23 +00:00
|
|
|
completion: Status::bye(None, "bye")?,
|
|
|
|
body: vec![],
|
2024-01-01 16:54:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 07:22:15 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ResponseOrIdle {
|
|
|
|
Response(Response<'static>),
|
2024-02-22 16:30:40 +00:00
|
|
|
IdleAccept(Arc<Notify>),
|
|
|
|
IdleReject(Response<'static>),
|
2024-01-17 09:14:48 +00:00
|
|
|
IdleEvent(Vec<Body<'static>>),
|
2024-01-17 07:22:15 +00:00
|
|
|
}
|