1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
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(())
}
}
|