summaryrefslogblamecommitdiffstats
path: root/src/client/encrypted.rs
blob: 08439b2efceca50f23e431e5f758053ab7cd9990 (plain) (tree)


























































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

use crate::Jabber;
use crate::Result;

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

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

    pub async fn start_stream(&mut self) -> Result<()> {
        let declaration = BytesDecl::new("1.0", None, None);
        let mut stream_element = BytesStart::new("stream:stream");
        stream_element.push_attribute(("from".as_bytes(), self.jabber.jid.to_string().as_bytes()));
        stream_element.push_attribute(("to".as_bytes(), self.jabber.server.as_bytes()));
        stream_element.push_attribute(("version", "1.0"));
        stream_element.push_attribute(("xml:lang", "en"));
        stream_element.push_attribute(("xmlns", "jabber:client"));
        stream_element.push_attribute(("xmlns:stream", "http://etherx.jabber.org/streams"));
        self.writer
            .write_event_async(Event::Decl(declaration))
            .await;
        self.writer
            .write_event_async(Event::Start(stream_element))
            .await
            .unwrap();
        let mut buf = Vec::new();
        loop {
            match self.reader.read_event_into_async(&mut buf).await.unwrap() {
                Event::Start(e) => {
                    println!("{:?}", e);
                    break;
                }
                e => println!("decl: {:?}", e),
            };
        }
        Ok(())
    }
}