1use std::mem::replace;
2use futures::{Future, Poll, Async, sink, Stream};
3use tokio_io::{AsyncRead, AsyncWrite};
4use xmpp_parsers::component::Handshake;
5
6use xmpp_codec::Packet;
7use xmpp_stream::XMPPStream;
8use {Error, AuthError};
9
10const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept";
11
12pub struct ComponentAuth<S: AsyncWrite> {
13 state: ComponentAuthState<S>,
14}
15
16enum ComponentAuthState<S: AsyncWrite> {
17 WaitSend(sink::Send<XMPPStream<S>>),
18 WaitRecv(XMPPStream<S>),
19 Invalid,
20}
21
22impl<S: AsyncWrite> ComponentAuth<S> {
23 // TODO: doesn't have to be a Result<> actually
24 pub fn new(stream: XMPPStream<S>, password: String) -> Result<Self, Error> {
25 // FIXME: huge hack, shouldn’t be an element!
26 let sid = stream.stream_features.name().to_owned();
27 let mut this = ComponentAuth {
28 state: ComponentAuthState::Invalid,
29 };
30 this.send(
31 stream,
32 Handshake::from_password_and_stream_id(&password, &sid)
33 );
34 Ok(this)
35 }
36
37 fn send(&mut self, stream: XMPPStream<S>, handshake: Handshake) {
38 let nonza = handshake;
39 let send = stream.send_stanza(nonza);
40
41 self.state = ComponentAuthState::WaitSend(send);
42 }
43}
44
45impl<S: AsyncRead + AsyncWrite> Future for ComponentAuth<S> {
46 type Item = XMPPStream<S>;
47 type Error = Error;
48
49 fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
50 let state = replace(&mut self.state, ComponentAuthState::Invalid);
51
52 match state {
53 ComponentAuthState::WaitSend(mut send) =>
54 match send.poll() {
55 Ok(Async::Ready(stream)) => {
56 self.state = ComponentAuthState::WaitRecv(stream);
57 self.poll()
58 },
59 Ok(Async::NotReady) => {
60 self.state = ComponentAuthState::WaitSend(send);
61 Ok(Async::NotReady)
62 },
63 Err(e) =>
64 Err(e)?
65 },
66 ComponentAuthState::WaitRecv(mut stream) =>
67 match stream.poll() {
68 Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
69 if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) =>
70 {
71 self.state = ComponentAuthState::Invalid;
72 Ok(Async::Ready(stream))
73 },
74 Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
75 if stanza.is("error", "http://etherx.jabber.org/streams") =>
76 {
77 Err(AuthError::ComponentFail.into())
78 },
79 Ok(Async::Ready(event)) => {
80 println!("ComponentAuth ignore {:?}", event);
81 Ok(Async::NotReady)
82 },
83 Ok(_) => {
84 self.state = ComponentAuthState::WaitRecv(stream);
85 Ok(Async::NotReady)
86 },
87 Err(e) =>
88 Err(e)?
89 },
90 ComponentAuthState::Invalid =>
91 unreachable!(),
92 }
93 }
94}