diff options
| author | Fuwn <[email protected]> | 2021-04-30 17:59:18 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-04-30 17:59:18 +0000 |
| commit | 5ec0a47413d8623660156dc4049f98dffee7b35b (patch) | |
| tree | b1f5d5b860ffd48ad9c36fdc8b378bb1326d855e /src/server/cmd/commands/text.rs | |
| parent | feat(hub): *implement* new commands; subscribe room, subscribe distance, tele... (diff) | |
| download | whirl-5ec0a47413d8623660156dc4049f98dffee7b35b.tar.xz whirl-5ec0a47413d8623660156dc4049f98dffee7b35b.zip | |
refactor(cmds): more orphan module functions to methods within struct
The rest of the current commands will also receive this refactor... when I have time.
Diffstat (limited to 'src/server/cmd/commands/text.rs')
| -rw-r--r-- | src/server/cmd/commands/text.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/server/cmd/commands/text.rs b/src/server/cmd/commands/text.rs new file mode 100644 index 0000000..3029eac --- /dev/null +++ b/src/server/cmd/commands/text.rs @@ -0,0 +1,46 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +use std::str::from_utf8; + +use bytes::{BufMut, BytesMut}; + +use crate::server::cmd::constants::TEXT; + +pub struct Text { + pub sender: String, + pub content: String, +} +impl Text { + pub fn parse(data: Vec<u8>, username: &str) -> Self { + Self { + sender: username.to_string(), + content: from_utf8(&data[6..]).unwrap().to_string(), + } + } + + pub fn create(self) -> Vec<u8> { + let mut command = BytesMut::new(); + + // Header + command.put_u8(0x01); + command.put_i8(TEXT as i8); + + // Content + // TODO: Find a way to parse ObjIds. + // The below byte is suspected to be the sender's short ObjId. + command.put_i8(0x00); + + command.put_u8(self.sender.len() as u8); + command.put_slice(self.sender.as_bytes()); + command.put_u8(self.content.len() as u8); + command.put_slice(self.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 + } +} |