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