From dbe54294567a681b26f2c3e06b675b5f30cc9c5d Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Tue, 18 May 2021 02:37:07 +0200 Subject: add command to mumctl to query past messages --- mumctl/src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 5abed50..5f1d8f7 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -88,6 +88,8 @@ enum Command { Deafen, /// Undeafen yourself Undeafen, + /// Get messages + Messages, } #[derive(Debug, StructOpt)] @@ -349,6 +351,16 @@ fn match_opt() -> Result<(), Error> { Command::Undeafen => { send_command(MumCommand::DeafenSelf(Some(false)))??; } + Command::Messages => { + match send_command(MumCommand::PastMessages)?? { + Some(CommandResponse::PastMessages { messages }) => { + for (msg, sender) in messages { + println!("{}: {}", sender, msg); + } + } + _ => unreachable!("Response should only be a PastMessages"), + } + } } if !config::cfg_exists() { -- cgit v1.2.1 From f856694aa55672e6f2fa93fbce5d47fce2d08d1e Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Wed, 19 May 2021 00:21:01 +0200 Subject: add frontend support for sending messages --- mumctl/src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 5f1d8f7..5d3f332 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,6 +1,6 @@ use colored::Colorize; use log::*; -use mumlib::command::{Command as MumCommand, CommandResponse}; +use mumlib::command::{Command as MumCommand, CommandResponse, MessageTarget}; use mumlib::config::{self, Config, ServerConfig}; use mumlib::state::Channel as MumChannel; use std::fmt; @@ -90,6 +90,27 @@ enum Command { Undeafen, /// Get messages Messages, + /// Send a message to a channel or a user + Message(Target), +} + +#[derive(Debug, StructOpt)] +enum Target { + Channel { + /// The message to send + message: String, + /// If the message should be sent recursivley to sub-channels + #[structopt(short = "r", long = "recursive")] + recursive: bool, + /// Which channels to send to + names: Vec, + }, + User { + /// The message to send + message: String, + /// Which channels to send to + names: Vec, + }, } #[derive(Debug, StructOpt)] @@ -361,6 +382,31 @@ fn match_opt() -> Result<(), Error> { _ => unreachable!("Response should only be a PastMessages"), } } + Command::Message(target) => { + match target { + Target::Channel { + message, + recursive, + names, + } => { + let msg = MumCommand::SendMessage { + message, + targets: names.into_iter().map(|name| MessageTarget::Channel { name, recursive }).collect(), + }; + send_command(msg)??; + }, + Target::User { + message, + names + } => { + let msg = MumCommand::SendMessage { + message, + targets: names.into_iter().map(|name| MessageTarget::User { name }).collect(), + }; + send_command(msg)??; + }, + } + } } if !config::cfg_exists() { -- cgit v1.2.1 From f551de2bbc5e41c5cd76e36c2b0a6f10d9b4cddf Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Wed, 19 May 2021 02:09:58 +0200 Subject: remove event_register_handler from tcp stack --- mumctl/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 5d3f332..4e1249e 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -373,7 +373,7 @@ fn match_opt() -> Result<(), Error> { send_command(MumCommand::DeafenSelf(Some(false)))??; } Command::Messages => { - match send_command(MumCommand::PastMessages)?? { + match send_command(MumCommand::PastMessages { block: false })?? { Some(CommandResponse::PastMessages { messages }) => { for (msg, sender) in messages { println!("{}: {}", sender, msg); -- cgit v1.2.1 From aa710a3420ef4d834ee1df4099b25f3c83b9c31d Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 22 May 2021 01:27:17 +0200 Subject: rework command response mechanism --- mumctl/Cargo.toml | 1 + mumctl/src/main.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 9 deletions(-) (limited to 'mumctl') diff --git a/mumctl/Cargo.toml b/mumctl/Cargo.toml index fff2a1c..3467ffc 100644 --- a/mumctl/Cargo.toml +++ b/mumctl/Cargo.toml @@ -18,5 +18,6 @@ bincode = "1" colored = "2" log = "0.4" structopt = "0.3" +serde = "1" #cursive = "0.15" diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 4e1249e..318aa3c 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -4,11 +4,13 @@ use mumlib::command::{Command as MumCommand, CommandResponse, MessageTarget}; use mumlib::config::{self, Config, ServerConfig}; use mumlib::state::Channel as MumChannel; use std::fmt; +use std::marker::PhantomData; use std::io::{self, BufRead, Read, Write}; use std::iter; use std::os::unix::net::UnixStream; use std::thread; use structopt::{clap::Shell, StructOpt}; +use serde::de::DeserializeOwned; const INDENTATION: &str = " "; @@ -88,8 +90,11 @@ enum Command { Deafen, /// Undeafen yourself Undeafen, - /// Get messages - Messages, + /// Get messages sent to the server you're currently connected to + Messages { + #[structopt(short = "i", long = "interactive")] + interactive: bool, + }, /// Send a message to a channel or a user Message(Target), } @@ -372,14 +377,15 @@ fn match_opt() -> Result<(), Error> { Command::Undeafen => { send_command(MumCommand::DeafenSelf(Some(false)))??; } - Command::Messages => { - match send_command(MumCommand::PastMessages { block: false })?? { - Some(CommandResponse::PastMessages { messages }) => { - for (msg, sender) in messages { - println!("{}: {}", sender, msg); - } + Command::Messages { + interactive + } => { + for response in send_command_multi(MumCommand::PastMessages { block: interactive })? { + match response { + Ok(Some(CommandResponse::PastMessage { message })) => println!("{}: {}", message.1, message.0), + Ok(_) => unreachable!("Response should only be a Some(PastMessages)"), + Err(e) => error!("{}", e), } - _ => unreachable!("Response should only be a PastMessages"), } } Command::Message(target) => { @@ -660,6 +666,54 @@ fn send_command( bincode::deserialize_from(&mut connection).map_err(|_| CliError::ConnectionError) } +fn send_command_multi( + command: MumCommand, +) -> Result>>, CliError> { + let mut connection = + UnixStream::connect(mumlib::SOCKET_PATH).map_err(|_| CliError::ConnectionError)?; + + let serialized = bincode::serialize(&command).unwrap(); + + connection + .write(&(serialized.len() as u32).to_be_bytes()) + .map_err(|_| CliError::ConnectionError)?; + connection + .write(&serialized) + .map_err(|_| CliError::ConnectionError)?; + + connection.shutdown(std::net::Shutdown::Write) + .map_err(|_| CliError::ConnectionError)?; + + Ok(BincodeIter::new(connection)) +} + +struct BincodeIter { + reader: R, + phantom: PhantomData<*const I>, +} + +impl BincodeIter { + fn new(reader: R) -> Self { + Self { + reader, + phantom: PhantomData, + } + } +} + +impl Iterator for BincodeIter + where R: Read, I: DeserializeOwned { + type Item = I; + + #[inline] + fn next(&mut self) -> Option { + self.reader + .read_exact(&mut [0; 4]) + .ok()?; + bincode::deserialize_from(&mut self.reader).ok() + } +} + fn print_channel(channel: &MumChannel, depth: usize) { println!( "{}{}{}", -- cgit v1.2.1 From 3cba90118b900706ea356eee601195b6bf4b50ee Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 22 May 2021 01:31:00 +0200 Subject: add some documentation --- mumctl/src/main.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 318aa3c..4ef30e7 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -645,6 +645,7 @@ fn parse_state(server_state: &mumlib::state::Server) { } } +/// Tries to find a running mumd instance and tries to receive one response from it. fn send_command( command: MumCommand, ) -> Result>, CliError> { @@ -666,6 +667,8 @@ fn send_command( bincode::deserialize_from(&mut connection).map_err(|_| CliError::ConnectionError) } +/// Tries to find a running mumd instance and sends a single command to it. Returns an iterator which +/// yields all responses that mumd sends for that particular command. fn send_command_multi( command: MumCommand, ) -> Result>>, CliError> { @@ -687,12 +690,14 @@ fn send_command_multi( Ok(BincodeIter::new(connection)) } +/// A struct to represent an iterator that deserializes bincode-encoded data from a `Reader`. struct BincodeIter { reader: R, phantom: PhantomData<*const I>, } impl BincodeIter { + /// Creates a new `BincodeIter` from a reader. fn new(reader: R) -> Self { Self { reader, -- cgit v1.2.1 From bfae92c25b0bb319d463c5632f21cc9cf02a9390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kapten=20Z=E2=88=85=E2=88=85m?= <55669224+default-username-852@users.noreply.github.com> Date: Sun, 6 Jun 2021 23:13:42 +0200 Subject: Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gustav Sörnäs --- mumctl/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 4ef30e7..6fca6a4 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -645,7 +645,7 @@ fn parse_state(server_state: &mumlib::state::Server) { } } -/// Tries to find a running mumd instance and tries to receive one response from it. +/// Tries to find a running mumd instance and receive one response from it. fn send_command( command: MumCommand, ) -> Result>, CliError> { @@ -667,7 +667,7 @@ fn send_command( bincode::deserialize_from(&mut connection).map_err(|_| CliError::ConnectionError) } -/// Tries to find a running mumd instance and sends a single command to it. Returns an iterator which +/// Tries to find a running mumd instance and send a single command to it. Returns an iterator which /// yields all responses that mumd sends for that particular command. fn send_command_multi( command: MumCommand, -- cgit v1.2.1 From 8f5acbc7e7fc3652f5321a31be351221542faf80 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sun, 6 Jun 2021 23:16:38 +0200 Subject: change interactive flag to follow --- mumctl/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 6fca6a4..745fafa 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -92,8 +92,8 @@ enum Command { Undeafen, /// Get messages sent to the server you're currently connected to Messages { - #[structopt(short = "i", long = "interactive")] - interactive: bool, + #[structopt(short = "f", long = "follow")] + follow: bool, }, /// Send a message to a channel or a user Message(Target), @@ -378,9 +378,9 @@ fn match_opt() -> Result<(), Error> { send_command(MumCommand::DeafenSelf(Some(false)))??; } Command::Messages { - interactive + follow } => { - for response in send_command_multi(MumCommand::PastMessages { block: interactive })? { + for response in send_command_multi(MumCommand::PastMessages { block: follow })? { match response { Ok(Some(CommandResponse::PastMessage { message })) => println!("{}: {}", message.1, message.0), Ok(_) => unreachable!("Response should only be a Some(PastMessages)"), -- cgit v1.2.1