use std::str; use std::sync::Arc; use peanuts::{Reader, Writer}; use rsasl::prelude::SASLConfig; use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, ReadHalf, WriteHalf}; use tracing::{debug, info, trace}; use crate::connection::{Tls, Unencrypted}; use crate::error::Error; use crate::stanza::stream::Stream; use crate::stanza::XML_VERSION; use crate::Result; use crate::JID; pub struct Jabber where S: AsyncRead + AsyncWrite + Unpin, { reader: Reader>, writer: Writer>, jid: Option, auth: Option>, server: String, } impl Jabber where S: AsyncRead + AsyncWrite + Unpin, { pub fn new( reader: ReadHalf, writer: WriteHalf, jid: Option, auth: Option>, server: String, ) -> Self { let reader = Reader::new(reader); let writer = Writer::new(writer); Self { reader, writer, jid, auth, server, } } } impl Jabber where S: AsyncRead + AsyncWrite + Unpin + Send, { // pub async fn negotiate(self) -> Result> {} pub async fn start_stream(&mut self) -> Result<()> { // client to server // declaration self.writer.write_declaration(XML_VERSION).await?; // opening stream element let server = self.server.clone().try_into()?; let stream = Stream::new_client(None, server, None, "en".to_string()); // TODO: nicer function to serialize to xml writer self.writer.write_start(&stream).await?; // server to client // may or may not send a declaration let decl = self.reader.read_prolog().await?; // receive stream element and validate let stream: Stream = self.reader.read_start().await?; if let Some(from) = stream.from { self.server = from.to_string() } Ok(()) } } // pub async fn get_features(&mut self) -> Result> { // Element::read(&mut self.reader).await?.try_into() // } impl Jabber { pub async fn starttls(&mut self) -> Result> { todo!() } // let mut starttls_element = BytesStart::new("starttls"); // starttls_element.push_attribute(("xmlns", "urn:ietf:params:xml:ns:xmpp-tls")); // self.writer // .write_event_async(Event::Empty(starttls_element)) // .await // .unwrap(); // let mut buf = Vec::new(); // match self.reader.read_event_into_async(&mut buf).await.unwrap() { // Event::Empty(e) => match e.name() { // QName(b"proceed") => { // let connector = TlsConnector::new().unwrap(); // let stream = self // .reader // .into_inner() // .into_inner() // .unsplit(self.writer.into_inner()); // if let Ok(tlsstream) = tokio_native_tls::TlsConnector::from(connector) // .connect(&self.jabber.server, stream) // .await // { // let (read, write) = tokio::io::split(tlsstream); // let reader = Reader::from_reader(BufReader::new(read)); // let writer = Writer::new(write); // let mut client = // super::encrypted::JabberClient::new(reader, writer, self.jabber); // client.start_stream().await?; // return Ok(client); // } // } // QName(_) => return Err(JabberError::TlsNegotiation), // }, // _ => return Err(JabberError::TlsNegotiation), // } // Err(JabberError::TlsNegotiation) // } } impl std::fmt::Debug for Jabber { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Jabber") .field("connection", &"tls") .field("jid", &self.jid) .field("auth", &self.auth) .field("server", &self.server) .finish() } } impl std::fmt::Debug for Jabber { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Jabber") .field("connection", &"unencrypted") .field("jid", &self.jid) .field("auth", &self.auth) .field("server", &self.server) .finish() } } #[cfg(test)] mod tests { use super::*; use crate::connection::Connection; use test_log::test; #[test(tokio::test)] async fn start_stream() { let connection = Connection::connect("blos.sm").await.unwrap(); match connection { Connection::Encrypted(mut c) => c.start_stream().await.unwrap(), Connection::Unencrypted(mut c) => c.start_stream().await.unwrap(), } } }