summaryrefslogblamecommitdiffstats
path: root/src/client/unencrypted.rs
blob: 27b0a5fe861f16e352b346b2e93b56bb13881960 (plain) (tree)
1
2
3
4
5
6
7
8
9
                



                                           



                                                
                                         
                           
                  
                  
                                                        




















                                                        
                           
                                                            


                                                                                       

                                                        



                                                                
                                 

                                                                            


              

                                                                        






















                                                                                          
                                                                        
                                                                               
                                                        



                                                                                             








                                                                    
use quick_xml::{
    events::{BytesDecl, BytesStart, Event},
    name::QName,
    Reader, Writer,
};
use tokio::io::{BufReader, ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use tokio_native_tls::native_tls::TlsConnector;

use crate::stanza::stream::StreamFeature;
use crate::stanza::Element;
use crate::Jabber;
use crate::Result;
use crate::{error::JabberError, stanza::stream::Stream};

pub struct JabberClient<'j> {
    reader: Reader<BufReader<ReadHalf<TcpStream>>>,
    writer: Writer<WriteHalf<TcpStream>>,
    jabber: &'j mut Jabber<'j>,
}

impl<'j> JabberClient<'j> {
    pub fn new(
        reader: Reader<BufReader<ReadHalf<TcpStream>>>,
        writer: Writer<WriteHalf<TcpStream>>,
        jabber: &'j mut Jabber<'j>,
    ) -> Self {
        Self {
            reader,
            writer,
            jabber,
        }
    }

    pub async fn start_stream(&mut self) -> Result<()> {
        // client to server
        let declaration = BytesDecl::new("1.0", None, None);
        let server = &self.jabber.server.to_owned().try_into()?;
        let stream_element =
            Stream::new_client(&self.jabber.jid, server, None, Some("en".to_string()));
        self.writer
            .write_event_async(Event::Decl(declaration))
            .await?;
        let stream_element: Element<'_> = stream_element.into();
        stream_element.write_start(&mut self.writer).await?;
        // server to client
        let mut buf = Vec::new();
        self.reader.read_event_into_async(&mut buf).await?;
        let _stream_response = Element::read_start(&mut self.reader).await?;
        Ok(())
    }

    pub async fn get_features(&mut self) -> Result<Vec<StreamFeature>> {
        Element::read(&mut self.reader).await?.try_into()
    }

    pub async fn starttls(mut self) -> Result<super::encrypted::JabberClient<'j>> {
        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)
    }
}