diff options
| author | Fuwn <[email protected]> | 2021-04-30 18:10:15 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-04-30 18:10:15 -0700 |
| commit | 31de5328fd4379f6cbe490c6bad9515660e046ae (patch) | |
| tree | ea0e9add6cbf7f11dda50d2c43419563b7a3fecb /src/server | |
| parent | refactor(cmds): more orphan module functions to methods within struct (diff) | |
| download | whirl-31de5328fd4379f6cbe490c6bad9515660e046ae.tar.xz whirl-31de5328fd4379f6cbe490c6bad9515660e046ae.zip | |
refactor(cmds): orphan functions to methods
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/cmd/commands/buddy_list.rs | 44 | ||||
| -rw-r--r-- | src/server/cmd/commands/buddy_list/create.rs | 24 | ||||
| -rw-r--r-- | src/server/cmd/commands/buddy_list/mod.rs | 6 | ||||
| -rw-r--r-- | src/server/cmd/commands/buddy_list/parse.rs | 17 | ||||
| -rw-r--r-- | src/server/cmd/commands/buddy_list/structure.rs | 7 | ||||
| -rw-r--r-- | src/server/distributor.rs | 9 | ||||
| -rw-r--r-- | src/server/hub.rs | 9 |
7 files changed, 54 insertions, 62 deletions
diff --git a/src/server/cmd/commands/buddy_list.rs b/src/server/cmd/commands/buddy_list.rs new file mode 100644 index 0000000..2601f1b --- /dev/null +++ b/src/server/cmd/commands/buddy_list.rs @@ -0,0 +1,44 @@ +// 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::BUDDYLISTNOTIFY; + +#[derive(Clone)] +pub struct BuddyList { + pub buddy: String, + pub add: i8, +} +impl BuddyList { + pub fn parse(data: Vec<u8>) -> Self { + Self { + buddy: from_utf8(&data[4..data[0] as usize - 1]) + .unwrap() + .to_string(), + + // Get the last byte + add: data[data[0] as usize - 1] as i8, + } + } + + pub fn create(self) -> Vec<u8> { + let mut command = BytesMut::new(); + + // Header + command.put_u8(0x01); // ObjId + command.put_i8(BUDDYLISTNOTIFY as i8); // Type + + // Content + command.put_u8(self.buddy.len() as u8); // Buddy (name) length + command.put_slice(self.buddy.as_bytes()); // Buddy (name) + command.put_u8(self.add as u8); // "Is buddy logged on?" (?) + + let mut command_as_vec = command.to_vec(); + command_as_vec.insert(0, command.len() as u8 + 1); + + command_as_vec + } +} diff --git a/src/server/cmd/commands/buddy_list/create.rs b/src/server/cmd/commands/buddy_list/create.rs deleted file mode 100644 index 166c3bc..0000000 --- a/src/server/cmd/commands/buddy_list/create.rs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -use bytes::{BufMut, BytesMut}; - -use crate::server::cmd::{commands::buddy_list::structure::Buddy, constants::BUDDYLISTNOTIFY}; - -pub fn create_buddy_list_notify(buddy: &Buddy) -> Vec<u8> { - let mut command = BytesMut::new(); - - // Header - command.put_u8(0x01); // ObjId - command.put_u8(BUDDYLISTNOTIFY as u8); // Type - - // Content - command.put_u8(buddy.buddy.len() as u8); // Buddy (name) length - command.put_slice(buddy.buddy.as_bytes()); // Buddy (name) - command.put_u8(buddy.add); // "Is buddy logged on?" (?) - - let mut command_as_vec = command.to_vec(); - command_as_vec.insert(0, command.len() as u8 + 1); - - command_as_vec -} diff --git a/src/server/cmd/commands/buddy_list/mod.rs b/src/server/cmd/commands/buddy_list/mod.rs deleted file mode 100644 index b2820e2..0000000 --- a/src/server/cmd/commands/buddy_list/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -pub mod create; -pub mod parse; -pub mod structure; diff --git a/src/server/cmd/commands/buddy_list/parse.rs b/src/server/cmd/commands/buddy_list/parse.rs deleted file mode 100644 index 45bb3cb..0000000 --- a/src/server/cmd/commands/buddy_list/parse.rs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -use std::str::from_utf8; - -use crate::server::cmd::commands::buddy_list::structure::Buddy; - -pub fn parse_buddy_list_update(data: Vec<u8>) -> Buddy { - Buddy { - buddy: from_utf8(&data[4..data[0] as usize - 1]) - .unwrap() - .to_string(), - - // Get the last byte - add: data[data[0] as usize - 1], - } -} diff --git a/src/server/cmd/commands/buddy_list/structure.rs b/src/server/cmd/commands/buddy_list/structure.rs deleted file mode 100644 index cc89a8d..0000000 --- a/src/server/cmd/commands/buddy_list/structure.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -pub struct Buddy { - pub buddy: String, - pub add: u8, -} diff --git a/src/server/distributor.rs b/src/server/distributor.rs index 0f7952f..0b9c24d 100644 --- a/src/server/distributor.rs +++ b/src/server/distributor.rs @@ -23,7 +23,7 @@ use crate::{ cmd::{ commands::{ action::create_action, - buddy_list::{create::create_buddy_list_notify, parse::parse_buddy_list_update}, + buddy_list::BuddyList, property::{ create::{create_property_request_as_distributor, create_property_update_as_distributor}, parse::find_property_in_property_list, @@ -95,10 +95,11 @@ impl Server for Distributor { trace!("sent text to {}", username); } BUDDYLISTUPDATE => { - let buddy = parse_buddy_list_update(msg.to_vec()); + let buddy = BuddyList::parse(msg.to_vec()); trace!("received buddy list update from {}: {}", username, buddy.buddy); - peer.bytes.get_mut() - .write_all(&create_buddy_list_notify(&buddy)).await?; + peer.bytes.get_mut().write_all(&BuddyList { + ..buddy.clone() + }.create()).await?; trace!("sent buddy list notify to {}: {}", username, buddy.buddy); } ROOMIDRQ => { diff --git a/src/server/hub.rs b/src/server/hub.rs index 7d0bb0a..eb15c77 100644 --- a/src/server/hub.rs +++ b/src/server/hub.rs @@ -19,7 +19,7 @@ use crate::{ cmd::{ commands::{ action::create_action, - buddy_list::{create::create_buddy_list_notify, parse::parse_buddy_list_update}, + buddy_list::BuddyList, property::{ create::{create_property_request_as_hub, create_property_update_as_hub}, parse::find_property_in_property_list, @@ -96,10 +96,11 @@ impl Server for Hub { trace!("sent text to {}", username); } BUDDYLISTUPDATE => { - let buddy = parse_buddy_list_update(msg.to_vec()); + let buddy = BuddyList::parse(msg.to_vec()); trace!("received buddy list update from {}: {}", username, buddy.buddy); - peer.bytes.get_mut() - .write_all(&create_buddy_list_notify(&buddy)).await?; + peer.bytes.get_mut().write_all(&BuddyList { + ..buddy.clone() + }.create()).await?; trace!("sent buddy list notify to {}: {}", username, buddy.buddy); } ROOMIDRQ => { |