aboutsummaryrefslogtreecommitdiff
path: root/src/server/cmd/commands/text
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-04-26 13:30:51 +0000
committerFuwn <[email protected]>2021-04-26 13:30:51 +0000
commitaf68ae63e4b0f5f6237bbda4fed9fd2a6022357d (patch)
tree02bbd59bf0a54808e335c1d703d3dac4cdc0773b /src/server/cmd/commands/text
parentfix: Actually remove legacy server module (diff)
downloadwhirl-af68ae63e4b0f5f6237bbda4fed9fd2a6022357d.tar.xz
whirl-af68ae63e4b0f5f6237bbda4fed9fd2a6022357d.zip
fmt: Rename re_server module to server
Diffstat (limited to 'src/server/cmd/commands/text')
-rw-r--r--src/server/cmd/commands/text/create.rs31
-rw-r--r--src/server/cmd/commands/text/mod.rs6
-rw-r--r--src/server/cmd/commands/text/parse.rs13
-rw-r--r--src/server/cmd/commands/text/structure.rs7
4 files changed, 57 insertions, 0 deletions
diff --git a/src/server/cmd/commands/text/create.rs b/src/server/cmd/commands/text/create.rs
new file mode 100644
index 0000000..879f9bd
--- /dev/null
+++ b/src/server/cmd/commands/text/create.rs
@@ -0,0 +1,31 @@
+// Copyleft 2021-2021 Whirlsplash
+// SPDX-License-Identifier: GPL-3.0-only
+
+use bytes::{BufMut, BytesMut};
+
+use crate::server::cmd::{commands::text::structure::Text, constants::TEXT};
+
+pub fn create_text(text: Text) -> Vec<u8> {
+ let mut command = BytesMut::new();
+
+ // Header
+ command.put_u8(0x01);
+ command.put_u8(TEXT as u8);
+
+ // Content
+ // The fourth and fifth elements are presumed to be interpreted as a short by
+ // the client, however, usernames aren't (?) allowed to be long enough that
+ // they reach a number high enough to be converted to a short.
+ command.put_u8(0x00);
+ command.put_u8(text.sender.len() as u8);
+ command.put_slice(text.sender.as_bytes());
+ command.put_u8(text.content.len() as u8);
+ command.put_slice(text.content.as_bytes());
+
+ // Convert to vector and insert the length
+ let mut command_as_vec = command.to_vec();
+ command_as_vec.insert(0, command.len() as u8 + 1);
+
+ // Return bytes
+ command_as_vec
+}
diff --git a/src/server/cmd/commands/text/mod.rs b/src/server/cmd/commands/text/mod.rs
new file mode 100644
index 0000000..14440dc
--- /dev/null
+++ b/src/server/cmd/commands/text/mod.rs
@@ -0,0 +1,6 @@
+// Copyleft 2021-2021 Whirlsplash
+// SPDX-License-Identifier: GPL-3.0-only
+
+pub mod create;
+pub mod parse;
+pub mod structure;
diff --git a/src/server/cmd/commands/text/parse.rs b/src/server/cmd/commands/text/parse.rs
new file mode 100644
index 0000000..a7f66d0
--- /dev/null
+++ b/src/server/cmd/commands/text/parse.rs
@@ -0,0 +1,13 @@
+// Copyleft 2021-2021 Whirlsplash
+// SPDX-License-Identifier: GPL-3.0-only
+
+use std::str::from_utf8;
+
+use crate::server::cmd::commands::text::structure::Text;
+
+pub fn parse_text(data: Vec<u8>, username: &str) -> Text {
+ Text {
+ sender: username.to_string(),
+ content: from_utf8(&data[6..]).unwrap().to_string(),
+ }
+}
diff --git a/src/server/cmd/commands/text/structure.rs b/src/server/cmd/commands/text/structure.rs
new file mode 100644
index 0000000..eaeef38
--- /dev/null
+++ b/src/server/cmd/commands/text/structure.rs
@@ -0,0 +1,7 @@
+// Copyleft 2021-2021 Whirlsplash
+// SPDX-License-Identifier: GPL-3.0-only
+
+pub struct Text {
+ pub sender: String,
+ pub content: String,
+}