diff options
| -rw-r--r-- | mumctl/src/main.rs | 4 | ||||
| -rw-r--r-- | mumd/src/state.rs | 41 | ||||
| -rw-r--r-- | mumlib/src/command.rs | 2 | ||||
| -rw-r--r-- | mumlib/src/error.rs | 22 | ||||
| -rw-r--r-- | mumlib/src/state.rs | 7 |
5 files changed, 60 insertions, 16 deletions
diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index e0beb2f..82fcab6 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,7 +1,6 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; use colored::Colorize; use ipc_channel::ipc::{self, IpcSender}; -use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; use std::{fs, io}; @@ -16,7 +15,6 @@ macro_rules! err_print { fn main() { setup_logger(); - debug!("Logger up!"); let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) @@ -79,7 +77,7 @@ fn main() { } } else if let Some(matches) = matches.subcommand_matches("connect") { err_print!(send_command(Command::ChannelJoin { - channel_id: matches.value_of("channel").unwrap().parse::<u32>().unwrap() + channel_identifier: matches.value_of("channel").unwrap().to_string() })); } } else if let Some(_matches) = matches.subcommand_matches("status") { diff --git a/mumd/src/state.rs b/mumd/src/state.rs index b50c34c..fd1c831 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -9,7 +9,7 @@ use mumlib::command::{Command, CommandResponse}; use mumlib::state::Server; use std::net::ToSocketAddrs; use tokio::sync::{mpsc, watch}; -use mumlib::error::Error; +use mumlib::error::{ChannelIdentifierError, Error}; #[derive(Clone, Debug, Eq, PartialEq)] pub enum StatePhase { @@ -53,18 +53,38 @@ impl State { command: Command, ) -> (bool, mumlib::error::Result<Option<CommandResponse>>) { match command { - Command::ChannelJoin { channel_id } => { + Command::ChannelJoin { channel_identifier } => { if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) { return (false, Err(Error::DisconnectedError)); } - if let Some(server) = &self.server { - if !server.channels().contains_key(&channel_id) { - return (false, Err(Error::InvalidChannelIdError(channel_id))); - } - } + + let channels = self.server() + .unwrap() + .channels(); + + let matches = channels.iter() + .map(|e| (e.0, e.1.path(channels))) + .filter(|e| e.1.ends_with(&channel_identifier)) + .collect::<Vec<_>>(); + let id = match matches.len() { + 0 => { + let soft_matches = channels.iter() + .map(|e| (e.0, e.1.path(channels).to_lowercase())) + .filter(|e| e.1.ends_with(&channel_identifier.to_lowercase())) + .collect::<Vec<_>>(); + match soft_matches.len() { + 0 => return (false, Err(Error::ChannelIdentifierError(channel_identifier, ChannelIdentifierError::Invalid))), + 1 => *soft_matches.get(0).unwrap().0, + _ => return (false, Err(Error::ChannelIdentifierError(channel_identifier, ChannelIdentifierError::Invalid))), + } + }, + 1 => *matches.get(0).unwrap().0, + _ => return (false, Err(Error::ChannelIdentifierError(channel_identifier, ChannelIdentifierError::Ambiguous))), + }; + let mut msg = msgs::UserState::new(); msg.set_session(self.session_id.unwrap()); - msg.set_channel_id(channel_id); + msg.set_channel_id(id); self.packet_sender.send(msg.into()).unwrap(); (false, Ok(None)) } @@ -73,7 +93,7 @@ impl State { return (false, Err(Error::DisconnectedError)); } (false, Ok(Some(CommandResponse::ChannelList { - channels: self.server.as_ref().unwrap().channels().clone(), + channels: self.server().unwrap().channels().clone(), })), ) } @@ -192,6 +212,9 @@ impl State { pub fn phase_receiver(&self) -> watch::Receiver<StatePhase> { self.phase_watcher.1.clone() } + pub fn server(&self) -> Option<&Server> { + self.server.as_ref() + } pub fn server_mut(&mut self) -> Option<&mut Server> { self.server.as_mut() } diff --git a/mumlib/src/command.rs b/mumlib/src/command.rs index 9211656..b27d3ca 100644 --- a/mumlib/src/command.rs +++ b/mumlib/src/command.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; #[derive(Clone, Debug, Deserialize, Serialize)] pub enum Command { ChannelJoin { - channel_id: u32, + channel_identifier: String, }, ChannelList, InputVolumeSet(f32), diff --git a/mumlib/src/error.rs b/mumlib/src/error.rs index cb88aa7..6c66c1f 100644 --- a/mumlib/src/error.rs +++ b/mumlib/src/error.rs @@ -8,17 +8,33 @@ pub type Result<T> = std::result::Result<T, Error>; pub enum Error { DisconnectedError, AlreadyConnectedError, - InvalidChannelIdError(u32), + ChannelIdentifierError(String, ChannelIdentifierError), InvalidServerAddrError(String, u16), } + impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Error::DisconnectedError => write!(f, "Not connected to a server"), Error::AlreadyConnectedError => write!(f, "Already connected to a server"), - Error::InvalidChannelIdError(id) => write!(f, "Invalid channel id: {}", id), - Error::InvalidServerAddrError(addr, port) => write!(f, "Invalid server address: {}:{}", addr, port), + Error::ChannelIdentifierError(id, kind) => write!(f, "{}: {}", kind, id), + Error::InvalidServerAddrError(addr, port) => write!(f, "Invalid server address: {}: {}", addr, port), + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub enum ChannelIdentifierError { + Invalid, + Ambiguous, +} + +impl Display for ChannelIdentifierError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ChannelIdentifierError::Invalid => write!(f, "Invalid channel identifier"), + ChannelIdentifierError::Ambiguous => write!(f, "Ambiguous channel identifier"), } } }
\ No newline at end of file diff --git a/mumlib/src/state.rs b/mumlib/src/state.rs index f90634e..51fb492 100644 --- a/mumlib/src/state.rs +++ b/mumlib/src/state.rs @@ -128,6 +128,13 @@ impl Channel { pub fn name(&self) -> &str { &self.name } + + pub fn path(&self, channels: &HashMap<u32, Channel>) -> String { + match &self.parent { + Some(t) => format!("{}/{}", channels.get(t).unwrap().path(channels), self.name), + None => self.name.clone(), + } + } } #[derive(Clone, Debug, Deserialize, Serialize)] |
