aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.rs
blob: 290834631c8f7227d935d9672441c09b96c32779 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::sync::Arc;

use futures::{Sink, Stream};
use rsasl::config::SASLConfig;

use crate::{
    connection::{Tls, Unencrypted},
    stanza::{
        client::Stanza,
        sasl::Mechanisms,
        stream::{Feature, Features},
    },
    Connection, Error, JabberStream, Result, JID,
};

// feed it client stanzas, receive client stanzas
pub struct JabberClient {
    connection: JabberState,
    jid: JID,
    password: Arc<SASLConfig>,
    server: String,
}

pub enum JabberState {
    Disconnected,
    InsecureConnectionEstablised(Unencrypted),
    InsecureStreamStarted(JabberStream<Unencrypted>),
    InsecureGotFeatures((Features, JabberStream<Unencrypted>)),
    StartTls(JabberStream<Unencrypted>),
    ConnectionEstablished(Tls),
    StreamStarted(JabberStream<Tls>),
    GotFeatures((Features, JabberStream<Tls>)),
    Sasl(Mechanisms, JabberStream<Tls>),
    Bind(JabberStream<Tls>),
    // when it's bound, can stream stanzas and sink stanzas
    Bound(JabberStream<Tls>),
}

impl JabberState {
    pub async fn advance_state(
        self,
        jid: &mut JID,
        auth: Arc<SASLConfig>,
        server: &mut String,
    ) -> Result<JabberState> {
        match self {
            JabberState::Disconnected => match Connection::connect(server).await? {
                Connection::Encrypted(tls_stream) => {
                    Ok(JabberState::ConnectionEstablished(tls_stream))
                }
                Connection::Unencrypted(tcp_stream) => {
                    Ok(JabberState::InsecureConnectionEstablised(tcp_stream))
                }
            },
            JabberState::InsecureConnectionEstablised(tcp_stream) => Ok({
                JabberState::InsecureStreamStarted(
                    JabberStream::start_stream(tcp_stream, server).await?,
                )
            }),
            JabberState::InsecureStreamStarted(jabber_stream) => Ok(
                JabberState::InsecureGotFeatures(jabber_stream.get_features().await?),
            ),
            JabberState::InsecureGotFeatures((features, jabber_stream)) => {
                match features.negotiate()? {
                    Feature::StartTls(_start_tls) => Ok(JabberState::StartTls(jabber_stream)),
                    // TODO: better error
                    _ => return Err(Error::TlsRequired),
                }
            }
            JabberState::StartTls(jabber_stream) => Ok(JabberState::ConnectionEstablished(
                jabber_stream.starttls(server).await?,
            )),
            JabberState::ConnectionEstablished(tls_stream) => Ok(JabberState::StreamStarted(
                JabberStream::start_stream(tls_stream, server).await?,
            )),
            JabberState::StreamStarted(jabber_stream) => Ok(JabberState::GotFeatures(
                jabber_stream.get_features().await?,
            )),
            JabberState::GotFeatures((features, jabber_stream)) => match features.negotiate()? {
                Feature::StartTls(_start_tls) => return Err(Error::AlreadyTls),
                Feature::Sasl(mechanisms) => {
                    return Ok(JabberState::Sasl(mechanisms, jabber_stream))
                }
                Feature::Bind => return Ok(JabberState::Bind(jabber_stream)),
                Feature::Unknown => return Err(Error::Unsupported),
            },
            JabberState::Sasl(mechanisms, jabber_stream) => {
                return Ok(JabberState::ConnectionEstablished(
                    jabber_stream.sasl(mechanisms, auth).await?,
                ))
            }
            JabberState::Bind(jabber_stream) => {
                Ok(JabberState::Bound(jabber_stream.bind(jid).await?))
            }
            JabberState::Bound(jabber_stream) => Ok(JabberState::Bound(jabber_stream)),
        }
    }
}

impl Features {
    pub fn negotiate(self) -> Result<Feature> {
        if let Some(Feature::StartTls(s)) = self
            .features
            .iter()
            .find(|feature| matches!(feature, Feature::StartTls(_s)))
        {
            // TODO: avoid clone
            return Ok(Feature::StartTls(s.clone()));
        } else if let Some(Feature::Sasl(mechanisms)) = self
            .features
            .iter()
            .find(|feature| matches!(feature, Feature::Sasl(_)))
        {
            // TODO: avoid clone
            return Ok(Feature::Sasl(mechanisms.clone()));
        } else if let Some(Feature::Bind) = self
            .features
            .into_iter()
            .find(|feature| matches!(feature, Feature::Bind))
        {
            Ok(Feature::Bind)
        } else {
            // TODO: better error
            return Err(Error::Negotiation);
        }
    }
}

pub enum InsecureJabberConnection {
    Disconnected,
    ConnectionEstablished(Connection),
    PreStarttls(JabberStream<Unencrypted>),
    PreAuthenticated(JabberStream<Tls>),
    Authenticated(Tls),
    PreBound(JabberStream<Tls>),
    Bound(JabberStream<Tls>),
}

impl Stream for JabberClient {
    type Item = Stanza;

    fn poll_next(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        todo!()
    }
}

impl Sink<Stanza> for JabberClient {
    type Error = Error;

    fn poll_ready(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::result::Result<(), Self::Error>> {
        todo!()
    }

    fn start_send(
        self: std::pin::Pin<&mut Self>,
        item: Stanza,
    ) -> std::result::Result<(), Self::Error> {
        todo!()
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::result::Result<(), Self::Error>> {
        todo!()
    }

    fn poll_close(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::result::Result<(), Self::Error>> {
        todo!()
    }
}