create a first list_scripts command
This commit is contained in:
parent
f78f46b2f3
commit
cad7822c16
88
src/lib.rs
88
src/lib.rs
@ -1,9 +1,10 @@
|
|||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
use managesieve::Capabilities;
|
use managesieve::Capabilities;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{split, AsyncBufReadExt, BufReader, WriteHalf},
|
io::{split, AsyncBufReadExt, AsyncWriteExt, BufReader, WriteHalf},
|
||||||
net::TcpStream,
|
net::TcpStream,
|
||||||
sync::Mutex,
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
@ -93,7 +94,7 @@ impl ConnectionInfo {
|
|||||||
trace!("Capabilities read: {:#?}", &caps);
|
trace!("Capabilities read: {:#?}", &caps);
|
||||||
Ok(ConnectionConnected {
|
Ok(ConnectionConnected {
|
||||||
info: self,
|
info: self,
|
||||||
writer: Arc::new(writer),
|
writer: Arc::new(Mutex::new(writer)),
|
||||||
buffer,
|
buffer,
|
||||||
server_settings: caps,
|
server_settings: caps,
|
||||||
})
|
})
|
||||||
@ -104,7 +105,7 @@ impl ConnectionInfo {
|
|||||||
pub struct ConnectionConnected {
|
pub struct ConnectionConnected {
|
||||||
info: ConnectionInfo,
|
info: ConnectionInfo,
|
||||||
//reader: Arc<ReadHalf<TlsStream<TcpStream>>>,
|
//reader: Arc<ReadHalf<TlsStream<TcpStream>>>,
|
||||||
writer: Arc<WriteHalf<TlsStream<TcpStream>>>,
|
writer: Arc<Mutex<WriteHalf<TlsStream<TcpStream>>>>,
|
||||||
buffer: Arc<Mutex<String>>,
|
buffer: Arc<Mutex<String>>,
|
||||||
//pub join_handle: Arc<JoinHandle<()>>,
|
//pub join_handle: Arc<JoinHandle<()>>,
|
||||||
server_settings: Capabilities,
|
server_settings: Capabilities,
|
||||||
@ -130,54 +131,51 @@ impl ConnectionConnected {
|
|||||||
pub async fn log_server_settings(&self) {
|
pub async fn log_server_settings(&self) {
|
||||||
info!("Serversettings:\n{:?}", self.server_settings)
|
info!("Serversettings:\n{:?}", self.server_settings)
|
||||||
}
|
}
|
||||||
#[tracing::instrument(name = "gettin the intro")]
|
|
||||||
pub async fn read_introduction(self) -> Result<IsComplete<Self>, anyhow::Error> {
|
pub async fn list_scripts(self) -> anyhow::Result<(Vec<(String, bool)>, Self)> {
|
||||||
let Self {
|
let Self {
|
||||||
info,
|
|
||||||
//reader: Arc<ReadHalf<TlsStream<TcpStream>>>,
|
|
||||||
writer,
|
writer,
|
||||||
buffer,
|
buffer,
|
||||||
|
info,
|
||||||
server_settings,
|
server_settings,
|
||||||
} = self;
|
} = self;
|
||||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
let command = managesieve::Command::list_scripts();
|
||||||
let mut bf = buffer.lock().await;
|
writer
|
||||||
let response = match managesieve::response_capability(&bf.to_string()) {
|
.lock()
|
||||||
Ok((rest, capability, response)) => {
|
.await
|
||||||
info!("Successfully read the introduction {:?}", response);
|
.write_all(command.to_string().as_bytes())
|
||||||
bf.clear();
|
.await
|
||||||
bf.push_str(rest);
|
.context("Failed to write LISTSCRIPTS command")?;
|
||||||
drop(bf);
|
|
||||||
Ok(IsComplete::Yes(Self {
|
|
||||||
info,
|
|
||||||
writer: writer.clone(),
|
|
||||||
buffer: buffer.clone(),
|
|
||||||
server_settings: capability,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
Err(managesieve::Error::IncompleteResponse) => {
|
|
||||||
trace!("incomplete introduction");
|
|
||||||
Ok(IsComplete::No(Self {
|
|
||||||
info,
|
|
||||||
writer,
|
|
||||||
buffer: buffer.clone(),
|
|
||||||
server_settings,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
Err(managesieve::Error::InvalidResponse) => {
|
|
||||||
error!("invalid response");
|
|
||||||
Err(managesieve::Error::InvalidResponse)?
|
|
||||||
}
|
|
||||||
Err(managesieve::Error::InvalidInput) => {
|
|
||||||
error!("invalid input");
|
|
||||||
Err(managesieve::Error::InvalidResponse)?
|
|
||||||
}
|
|
||||||
Err(managesieve::Error::MissingLine(line)) => {
|
|
||||||
error!("a capability line was missing: {:?}", line);
|
|
||||||
Err(managesieve::Error::InvalidResponse)?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
response
|
let scriptslist = loop {
|
||||||
|
let mut buf = buffer.lock().await;
|
||||||
|
trace!("reading new input:\n{}", buf);
|
||||||
|
match managesieve::response_listscripts(&buf.to_string()) {
|
||||||
|
Ok((rest, scriptslist, resp)) => match resp.tag {
|
||||||
|
managesieve::OkNoBye::Ok => {
|
||||||
|
buf.clear();
|
||||||
|
buf.push_str(rest);
|
||||||
|
info!("Read the introduction");
|
||||||
|
break scriptslist;
|
||||||
|
}
|
||||||
|
managesieve::OkNoBye::No | managesieve::OkNoBye::Bye => {
|
||||||
|
trace!("Connection closed!");
|
||||||
|
//panic!("Invalid Response")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => trace!("(temporary) error: {}", e),
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||||
|
};
|
||||||
|
Ok((
|
||||||
|
scriptslist,
|
||||||
|
Self {
|
||||||
|
writer,
|
||||||
|
buffer,
|
||||||
|
info,
|
||||||
|
server_settings,
|
||||||
|
},
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_greeting(&self) -> String {
|
pub fn get_greeting(&self) -> String {
|
||||||
|
Loading…
Reference in New Issue
Block a user