rac_rs 0.1.1

A Rust client library for RAC (Real Address Chat) protocol.
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use crate::shared::{ClientError, Credentials};
use native_tls::TlsConnector;
use std::borrow::Cow;
use std::io::{Read, Write};
use std::net::TcpStream;

trait Io: Read + Write {}
impl<T: Read + Write + ?Sized> Io for T {}

type DynStream = Box<dyn Io + Send>;

/// A client for interacting with a RAC server.
///
/// The `Client` provides methods to connect to a RAC server, send and receive messages,
/// and manage user registration for `RACv2` connections.
///
/// # Example
///
/// ```no_run
/// use rac_rs::client::Client;
/// use rac_rs::shared::Credentials;
///
/// let credentials = Credentials {
///     username: "test_user".to_string(),
///     password: Some("password123".to_string()),
/// };
///
/// let mut client = Client::new(
///     "127.0.0.1:1234",
///     credentials,
///     false
/// );
/// ```
#[derive(Debug, Clone)]
pub struct Client {
    /// The current size of messages in the client.
    current_messages_size: usize,
    /// The address of the RAC server.
    address: String,
    /// The username for authentication.
    username: String,
    /// The password for authentication, if required.
    password: Option<String>,
    /// Whether to use TLS encryption.
    use_tls: bool,
}

impl Client {
    /// Creates a new `Client` instance.
    ///
    /// # Arguments
    ///
    /// * `address` - The address of the RAC server (e.g., "127.0.0.1:42666").
    /// * `credentials` - The username and optional password.
    /// * `connection` - The type of connection (`RAC` or `RACv2`).
    /// * `use_tls` - Whether to use TLS encryption for the connection.
    pub fn new(address: &str, credentials: Credentials, use_tls: bool) -> Self {
        Self {
            current_messages_size: 0,
            address: address.to_string(),
            username: credentials.username,
            password: credentials.password,
            use_tls,
        }
    }

    /// Updates the client's credentials.
    ///
    /// This method allows you to change the username and password for the client.
    pub fn update_credentials(&mut self, credentials: Credentials) {
        self.username = credentials.username;
        self.password = credentials.password;
    }

    /// Updates the client's TLS usage.
    ///
    /// This method allows you to enable or disable TLS encryption for the connection.
    pub fn update_tls(&mut self, use_tls: bool) {
        self.use_tls = use_tls;
    }

    /// Updates the client's address to the server.
    ///
    /// This method allows you to change the address of the RAC server.
    pub fn update_address(&mut self, address: String) {
        self.address = address;
    }

    /// Attempts to establish a TCP connection to the RAC server.
    fn get_stream(&self) -> Result<DynStream, ClientError> {
        let stream = TcpStream::connect(&self.address).map_err(ClientError::ConnectionError)?;

        if !self.use_tls {
            return Ok(Box::new(stream));
        }

        let domain = self.address.split(':').next().unwrap_or("localhost");

        let connector =
            TlsConnector::new().map_err(|e| ClientError::TlsInitializationError(e.to_string()))?;
        let tls_stream = connector
            .connect(domain, stream)
            .map_err(|e| ClientError::TlsInitializationError(e.to_string()))?;

        Ok(Box::new(tls_stream))
    }

    /// Removes null bytes from the data vector.
    ///
    /// This is required because some servers that are written in C
    /// may send null bytes in the response, which can cause issues
    /// when parsing the response.
    fn remove_nulls(data: &mut Vec<u8>) {
        data.retain(|&x| x != 0);
    }

    /// Tests the connection to the RAC server.
    ///
    /// This method attempts to establish a TCP connection and returns `Ok(())` if successful.
    pub fn test_connection(&self) -> Result<(), ClientError> {
        self.get_stream()?;
        Ok(())
    }

    /// Registers a new user on the RAC server.
    ///
    /// # Errors
    ///
    /// Returns `ClientError::NoPassword` if no password specified for the client.
    /// Returns `ClientError::UsernameAlreadyTaken` if the username is already in use.
    /// Returns `ClientError::UnexpectedResponse` if got unexpected response from server.
    pub fn register_user(&mut self) -> Result<(), ClientError> {
        // Getting the TCP stream to the RAC server.
        let mut stream = self.get_stream()?;

        // Sending the username and password to the RAC server.
        if self.password.is_some() {
            stream
                .write_all(
                    format!(
                        "\x03{}\n{}",
                        self.username,
                        self.password.as_deref().unwrap()
                    )
                    .as_bytes(),
                )
                .map_err(ClientError::StreamWriteError)?;
            let mut buf = [0u8; 2];
            let n = stream
                .read(&mut buf)
                .map_err(ClientError::StreamReadError)?;
            if n == 0 {
                return Ok(());
            }
            match buf[0] {
                0x01 => Err(ClientError::UsernameAlreadyTaken),
                _ => Err(ClientError::UnexpectedResponse(
                    String::from_utf8_lossy(&buf[..n]).to_string(),
                )),
            }
        } else {
            Err(ClientError::NoPassword)
        }
    }

    /// Fetches the total size of all messages on the server and updates the client's internal state.
    ///
    /// This is useful for determining the amount of data if you want to know current size.
    pub fn fetch_messages_size(&mut self) -> Result<(), ClientError> {
        // Getting the TCP stream to the RAC server.
        let mut stream = self.get_stream()?;

        // Trying to send 0x00 byte to get the size of messages.
        stream
            .write_all(&[0x00])
            .map_err(ClientError::StreamWriteError)?;

        let mut buf = vec![0u8; 1024];
        let n = stream
            .read(&mut buf)
            .map_err(ClientError::StreamReadError)?;

        if n == 0 {
            return Err(ClientError::ServerClosedConnection);
        }

        Self::remove_nulls(&mut buf);

        // Then, converting it to utf8 and parsing the size to usize.
        let response = String::from_utf8_lossy(&buf[..n]);
        if let Ok(size) = response.parse::<usize>() {
            self.current_messages_size = size;
            Ok(())
        } else {
            Err(ClientError::ParseError(
                "Failed to parse messages size".to_string(),
            ))
        }
    }

    /// Fetches all messages from the RAC server.
    ///
    /// This method retrieves all messages stored on the server and updates the
    /// client's internal message size tracker.
    pub fn fetch_all_messages(&mut self) -> Result<Vec<Cow<str>>, ClientError> {
        let mut stream = self.get_stream()?;

        // Sending 0x00 byte to get the size of messages.
        stream
            .write_all(&[0x00])
            .map_err(ClientError::StreamWriteError)?;
        let mut head = vec![0u8; 1024];
        let n = stream
            .read(&mut head)
            .map_err(ClientError::StreamReadError)?;

        if n == 0 {
            return Err(ClientError::ServerClosedConnection);
        }

        Self::remove_nulls(&mut head);

        let response = String::from_utf8_lossy(&head[..n]);
        let size = response
            .parse::<usize>()
            .map_err(|_| ClientError::ParseError("Failed to parse messages size".to_string()))?;
        self.current_messages_size = size;

        // Sending 0x01 byte to get all messages.
        stream
            .write_all(&[0x01])
            .map_err(ClientError::StreamWriteError)?;

        let mut buffer = vec![0u8; self.current_messages_size];
        stream
            .read_exact(&mut buffer)
            .map_err(ClientError::StreamReadError)?;

        Self::remove_nulls(&mut buffer);

        let response = String::from_utf8_lossy(&buffer).into_owned();

        let vec_messages = response
            .lines()
            .filter(|l| !l.is_empty())
            .map(|s| Cow::Owned(s.to_string()))
            .collect();

        Ok(vec_messages)
    }

    /// Fetches only new messages that have arrived since the last fetch.
    ///
    /// This method compares the current message size on the server with the client's
    /// stored size and retrieves only the difference. The client's internal message
    /// size tracker is updated upon successful fetch.
    pub fn fetch_new_messages(&mut self) -> Result<Vec<Cow<str>>, ClientError> {
        // For this approach, we will not use fetch_messages_size function,
        // because it is necessary to fetch messages size AND THEN get new messages
        // IN THE SAME STREAM. Welcome to the Sugoma's bullshit protocol.

        let mut stream = self.get_stream()?;

        // Sending 0x00 byte to get the size of messages.
        stream
            .write_all(&[0x00])
            .map_err(ClientError::StreamWriteError)?;
        let mut head = vec![0u8; 1024];
        let n = stream
            .read(&mut head)
            .map_err(ClientError::StreamReadError)?;

        if n == 0 {
            return Err(ClientError::ServerClosedConnection);
        }

        Self::remove_nulls(&mut head);

        // Then, converting it to utf8 and parsing the size to usize.
        let response = String::from_utf8_lossy(&head[..n]);
        let size = response
            .parse::<usize>()
            .map_err(|_| ClientError::ParseError("Failed to parse messages size".to_string()))?;

        // Now, we can get new messages.
        stream
            .write_all(format!("\x02{}", self.current_messages_size).as_bytes())
            .map_err(ClientError::StreamWriteError)?;

        let mut buffer = vec![0u8; size - self.current_messages_size];
        stream
            .read_exact(&mut buffer)
            .map_err(ClientError::StreamReadError)?;

        Self::remove_nulls(&mut buffer);

        let response = String::from_utf8_lossy(&buffer).into_owned();

        let vec_messages = response
            .lines()
            .filter(|l| !l.is_empty())
            .map(|s| Cow::Owned(s.to_string()))
            .collect();

        // Setting the new messages size.
        self.current_messages_size = size;

        Ok(vec_messages)
    }

    /// Sends a message to the server.
    ///
    /// The placeholder `{username}` in the message will be replaced with the client's username.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use rac_rs::client::Client;
    /// # use rac_rs::shared::ClientError;
    /// # let mut client = Client::new("", Default::default(), false);
    /// client.send_message("<{username}> Hello everyone!")?;
    /// # Ok::<(), ClientError>(())
    /// ```
    pub fn send_message(&self, message: &str) -> Result<(), ClientError> {
        // Replacing the `{username}` placeholder with the actual username.
        let message = message.replace("{username}", &self.username);
        self.send_custom_message(&message)
    }

    /// Sends a raw message to the server without any modifications.
    pub fn send_custom_message(&self, message: &str) -> Result<(), ClientError> {
        let mut stream = self.get_stream()?;

        // Sending the message to the RAC server.

        if self.password.is_some() {
            stream
                .write_all(
                    format!(
                        "\x02{}\n{}\n{}",
                        self.username,
                        self.password.as_deref().unwrap(),
                        message
                    )
                    .as_bytes(),
                )
                .map_err(ClientError::StreamWriteError)?;
            let mut buf = [0u8; 2];
            let n = stream
                .read(&mut buf)
                .map_err(ClientError::StreamReadError)?;
            if n == 0 {
                return Ok(());
            }
            return match buf[0] {
                0x01 => Err(ClientError::UserDoesNotExist),
                0x02 => Err(ClientError::IncorrectPassword),
                _ => Err(ClientError::UnexpectedResponse(
                    String::from_utf8_lossy(&buf[..n]).to_string(),
                )),
            };
        }

        // If the connection is RAC, we can send the message directly, without an attempt to authorize.
        stream
            .write_all(format!("\x01{}", message).as_bytes())
            .map_err(ClientError::StreamWriteError)?;

        Ok(())
    }

    /// Resets the client's state to its default values.
    ///
    /// This clears the address, username, password, and message size.
    pub fn reset(&mut self) {
        self.current_messages_size = 0;
        self.address.clear();
        self.username.clear();
        self.password = None;
    }

    /// Returns the current size of messages known to the client.
    ///
    /// This value is updated after calls to `fetch_all_messages` or `fetch_new_messages`.
    pub fn current_messages_size(&self) -> usize {
        self.current_messages_size
    }

    /// Returns the current state of TLS usage.
    ///
    /// This indicates whether the client is configured to use TLS for its connections.
    pub fn tls(&self) -> bool {
        self.use_tls
    }

    /// Returns a reference to the server address.
    pub fn address(&self) -> &str {
        &self.address
    }

    /// Returns a reference to the client's username.
    pub fn username(&self) -> &str {
        &self.username
    }
}