From 1974db9d719640f5cd5a8b51fa1b97f801f5a809 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 01:19:45 +0200 Subject: change connecting with channel name instead of id --- mumctl/src/main.rs | 6 +++--- mumd/src/state.rs | 26 ++++++++++++++++++-------- mumlib/src/command.rs | 2 +- mumlib/src/error.rs | 4 ++-- mumlib/src/state.rs | 7 +++++++ 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index ae4acc5..c96db36 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,5 +1,5 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; -use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; +use ipc_channel::ipc::{self, IpcSender}; use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; @@ -16,7 +16,6 @@ macro_rules! err_print { fn main() { setup_logger(); - debug!("Logger up!"); let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) @@ -73,8 +72,9 @@ fn main() { Err(e) => println!("{} {}", "error:".red(), e), } } else if let Some(matches) = matches.subcommand_matches("connect") { + let channel_arg = matches.value_of("channel").unwrap(); err_print!(send_command(Command::ChannelJoin { - channel_id: matches.value_of("channel").unwrap().parse::().unwrap() + channel_identifier: channel_arg.to_string() })); } } else if let Some(_matches) = matches.subcommand_matches("status") { diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 82d671e..d3f9a8e 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -53,18 +53,25 @@ impl State { command: Command, ) -> (bool, mumlib::error::Result>) { 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 mut idents = channels.iter() + .map(|e| (e.0, e.1.path(channels))); + + let id = match idents.find(|e| e.1.ends_with(&channel_identifier)) { + Some(v) => *v.0, + None => return (false, Err(Error::InvalidChannelIdentifierError(channel_identifier))), + }; + 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 +80,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(), })), ) } @@ -188,6 +195,9 @@ impl State { pub fn phase_receiver(&self) -> watch::Receiver { 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 b2ac321..a0673f4 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, ServerConnect { diff --git a/mumlib/src/error.rs b/mumlib/src/error.rs index cb88aa7..3e514fd 100644 --- a/mumlib/src/error.rs +++ b/mumlib/src/error.rs @@ -8,7 +8,7 @@ pub type Result = std::result::Result; pub enum Error { DisconnectedError, AlreadyConnectedError, - InvalidChannelIdError(u32), + InvalidChannelIdentifierError(String), InvalidServerAddrError(String, u16), } @@ -17,7 +17,7 @@ impl Display for Error { 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::InvalidChannelIdentifierError(id) => write!(f, "Couldn't find channel {}", id), Error::InvalidServerAddrError(addr, port) => write!(f, "Invalid server address: {}:{}", addr, port), } } 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) -> String { + match &self.parent { + Some(t) => format!("{}/{}", channels.get(t).unwrap().path(channels), self.name), + None => self.name.clone(), + } + } } #[derive(Clone, Debug, Deserialize, Serialize)] -- cgit v1.2.1 From 9a9db2473928de240a38349b9726af9ae557374d Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 01:25:02 +0200 Subject: clean up imports --- mumctl/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index c96db36..0020c55 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,6 +1,5 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; use ipc_channel::ipc::{self, IpcSender}; -use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; use std::{fs, io}; -- cgit v1.2.1 From b1c1c227a0f5c171726f16f4f474536a718fd56f Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 01:37:53 +0200 Subject: add error checking if there are multiple valid channels to connect to --- mumd/src/state.rs | 15 +++++++++------ mumlib/src/error.rs | 22 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/mumd/src/state.rs b/mumd/src/state.rs index d3f9a8e..623cfba 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::{Error, ChannelIdentifierError}; #[derive(Clone, Debug, Eq, PartialEq)] pub enum StatePhase { @@ -61,12 +61,15 @@ impl State { let channels = self.server() .unwrap() .channels(); - let mut idents = channels.iter() - .map(|e| (e.0, e.1.path(channels))); - let id = match idents.find(|e| e.1.ends_with(&channel_identifier)) { - Some(v) => *v.0, - None => return (false, Err(Error::InvalidChannelIdentifierError(channel_identifier))), + let matches = channels.iter() + .map(|e| (e.0, e.1.path(channels))) + .filter(|e| e.1.ends_with(&channel_identifier)) + .collect::>(); + let id = match matches.len() { + 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(); diff --git a/mumlib/src/error.rs b/mumlib/src/error.rs index 3e514fd..6c66c1f 100644 --- a/mumlib/src/error.rs +++ b/mumlib/src/error.rs @@ -8,17 +8,33 @@ pub type Result = std::result::Result; pub enum Error { DisconnectedError, AlreadyConnectedError, - InvalidChannelIdentifierError(String), + 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::InvalidChannelIdentifierError(id) => write!(f, "Couldn't find channel {}", 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 -- cgit v1.2.1 From 74a64c249e0371890f4bb03ee8cd17b5059ddc4c Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 18:45:51 +0200 Subject: minor changes --- mumctl/src/main.rs | 3 +-- mumd/src/state.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 0020c55..2db9ded 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -71,9 +71,8 @@ fn main() { Err(e) => println!("{} {}", "error:".red(), e), } } else if let Some(matches) = matches.subcommand_matches("connect") { - let channel_arg = matches.value_of("channel").unwrap(); err_print!(send_command(Command::ChannelJoin { - channel_identifier: channel_arg.to_string() + 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 623cfba..62ba865 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, ChannelIdentifierError}; +use mumlib::error::{ChannelIdentifierError, Error}; #[derive(Clone, Debug, Eq, PartialEq)] pub enum StatePhase { -- cgit v1.2.1 From 39b13833187e4331fa32c1601f1bf1b1d6fa036a Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 21:12:31 +0200 Subject: add soft matching for channel joining --- mumd/src/state.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 62ba865..2c48955 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -67,7 +67,17 @@ impl State { .filter(|e| e.1.ends_with(&channel_identifier)) .collect::>(); let id = match matches.len() { - 0 => return (false, Err(Error::ChannelIdentifierError(channel_identifier, ChannelIdentifierError::Invalid))), + 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::>(); + 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))), }; -- cgit v1.2.1