From 0cc89730e82464e8f6c4ee69a4791fdd0135178c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 7 Jun 2021 00:21:35 +0200 Subject: store User{Connected,Disconnected}-events --- mumd/src/state.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 84583e0..f831258 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -60,6 +60,11 @@ pub enum ExecutionContext { ), } +pub enum Event { + UserConnected(String, Option), + UserDisconnected(String, Option), +} + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum StatePhase { Disconnected, @@ -75,6 +80,8 @@ pub struct State { message_buffer: Vec<(String, u32)>, phase_watcher: (watch::Sender, watch::Receiver), + + events: Vec, } impl State { @@ -95,6 +102,7 @@ impl State { audio_output, message_buffer: Vec::new(), phase_watcher, + events: Vec::new(), }; state.reload_config(); Ok(state) @@ -129,19 +137,21 @@ impl State { // this is someone else // send notification only if we've passed the connecting phase if matches!(*self.phase_receiver().borrow(), StatePhase::Connected(_)) { - let channel_id = msg.get_channel_id(); + let this_channel = msg.get_channel_id(); + let other_channel = self.get_users_channel(self.server().unwrap().session_id().unwrap()); + //TODO can this fail? + let this_channel_name = self.server().unwrap().channels().get(&this_channel).map(|c| c.name()); - if channel_id - == self.get_users_channel(self.server().unwrap().session_id().unwrap()) - { - if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { + if this_channel == other_channel { + if let Some(this_channel_name) = this_channel_name { notifications::send(format!( "{} connected and joined {}", - &msg.get_name(), - channel.name() + msg.get_name(), + this_channel_name, )); } - + let this_channel_name = this_channel_name.map(|s| s.to_string()); + self.push_event(Event::UserConnected(msg.get_name().to_string(), this_channel_name)); self.audio_output .play_effect(NotificationEvents::UserConnected); } @@ -233,10 +243,13 @@ impl State { let this_channel = self.get_users_channel(self.server().unwrap().session_id().unwrap()); let other_channel = self.get_users_channel(msg.get_session()); if this_channel == other_channel { + let channel_name = self.server().unwrap().channels().get(&this_channel).map(|c| c.name().to_string()); self.audio_output .play_effect(NotificationEvents::UserDisconnected); if let Some(user) = self.server().unwrap().users().get(&msg.get_session()) { notifications::send(format!("{} disconnected", &user.name())); + let user_name = user.name().to_string(); + self.push_event(Event::UserDisconnected(user_name, channel_name)); } } @@ -279,6 +292,10 @@ impl State { .play_effect(NotificationEvents::ServerConnect); } + pub fn push_event(&mut self, event: Event) { + self.events.push(event); + } + pub fn audio_input(&self) -> &AudioInput { &self.audio_input } -- cgit v1.2.1 From 2b63fa8ac1b7e7d995955758f8cd9ab2ec7d4e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 7 Jun 2021 00:50:15 +0200 Subject: events command --- mumd/src/state.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index f831258..c51c139 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -15,7 +15,7 @@ use mumble_protocol::control::msgs; use mumble_protocol::control::ControlPacket; use mumble_protocol::ping::PongPacket; use mumble_protocol::voice::Serverbound; -use mumlib::command::{Command, CommandResponse, MessageTarget}; +use mumlib::command::{Command, CommandResponse, Event, MessageTarget}; use mumlib::config::Config; use mumlib::Error; use std::{ @@ -60,11 +60,6 @@ pub enum ExecutionContext { ), } -pub enum Event { - UserConnected(String, Option), - UserDisconnected(String, Option), -} - #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum StatePhase { Disconnected, @@ -435,6 +430,19 @@ pub fn handle_command( new_deaf.map(|b| CommandResponse::DeafenStatus { is_deafened: b }) )) } + Command::Events { block } => { + if block { + warn!("Blocking event list is unimplemented"); + now!(Ok(None)) + } else { + let events: Vec<_> = state + .events + .iter() + .map(|event| Ok(Some(CommandResponse::Event { event: event.clone() }))) + .collect(); + ExecutionContext::Now(Box::new(move || Box::new(events.into_iter()))) + } + } Command::InputVolumeSet(volume) => { state.audio_input.set_volume(volume); now!(Ok(None)) @@ -618,12 +626,12 @@ pub fn handle_command( }), Box::new(move |pong| { Ok(pong.map(|pong| { - (CommandResponse::ServerStatus { + CommandResponse::ServerStatus { version: pong.version, users: pong.users, max_users: pong.max_users, bandwidth: pong.bandwidth, - }) + } })) }), ), -- cgit v1.2.1 From 17f077d48b361a4cf8f5743750ca7408a8800797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 7 Jun 2021 01:30:40 +0200 Subject: timestamps on mumble events --- mumd/src/state.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index c51c139..ec25204 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -15,7 +15,7 @@ use mumble_protocol::control::msgs; use mumble_protocol::control::ControlPacket; use mumble_protocol::ping::PongPacket; use mumble_protocol::voice::Serverbound; -use mumlib::command::{Command, CommandResponse, Event, MessageTarget}; +use mumlib::command::{Command, CommandResponse, MessageTarget, MumbleEvent, MumbleEventKind}; use mumlib::config::Config; use mumlib::Error; use std::{ @@ -76,7 +76,7 @@ pub struct State { phase_watcher: (watch::Sender, watch::Receiver), - events: Vec, + events: Vec, } impl State { @@ -146,7 +146,7 @@ impl State { )); } let this_channel_name = this_channel_name.map(|s| s.to_string()); - self.push_event(Event::UserConnected(msg.get_name().to_string(), this_channel_name)); + self.push_event(MumbleEventKind::UserConnected(msg.get_name().to_string(), this_channel_name)); self.audio_output .play_effect(NotificationEvents::UserConnected); } @@ -244,7 +244,7 @@ impl State { if let Some(user) = self.server().unwrap().users().get(&msg.get_session()) { notifications::send(format!("{} disconnected", &user.name())); let user_name = user.name().to_string(); - self.push_event(Event::UserDisconnected(user_name, channel_name)); + self.push_event(MumbleEventKind::UserDisconnected(user_name, channel_name)); } } @@ -287,8 +287,8 @@ impl State { .play_effect(NotificationEvents::ServerConnect); } - pub fn push_event(&mut self, event: Event) { - self.events.push(event); + pub fn push_event(&mut self, kind: MumbleEventKind) { + self.events.push(MumbleEvent { timestamp: chrono::Local::now().naive_local(), kind }); } pub fn audio_input(&self) -> &AudioInput { -- cgit v1.2.1 From f22d3847a23484122fad83b22d7ca48316c9d7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 7 Jun 2021 17:17:41 +0200 Subject: additional events --- mumd/src/network/tcp.rs | 3 +++ mumd/src/state.rs | 40 +++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs index 5cc2bf7..4140f2a 100644 --- a/mumd/src/network/tcp.rs +++ b/mumd/src/network/tcp.rs @@ -13,6 +13,7 @@ use mumble_protocol::control::{msgs, ClientControlCodec, ControlCodec, ControlPa use mumble_protocol::crypt::ClientCryptState; use mumble_protocol::voice::VoicePacket; use mumble_protocol::{Clientbound, Serverbound}; +use mumlib::command::MumbleEventKind; use std::collections::HashMap; use std::convert::{Into, TryInto}; use std::net::SocketAddr; @@ -337,6 +338,8 @@ async fn listen( if let Some(user) = user { notifications::send(format!("{}: {}", user, msg.get_message())); //TODO: probably want a config flag for this + let user = user.to_string(); + state.push_event(MumbleEventKind::TextMessageReceived(user)) //TODO also include message target } state.register_message((msg.get_message().to_owned(), msg.get_actor())); drop(state); diff --git a/mumd/src/state.rs b/mumd/src/state.rs index ec25204..3aa330f 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -170,6 +170,7 @@ impl State { .users_mut() .get_mut(&session) .unwrap(); + let username = user.name().to_string(); let mute = if msg.has_self_mute() && user.self_mute() != msg.get_self_mute() { Some(msg.get_self_mute()) @@ -185,35 +186,43 @@ impl State { let diff = UserDiff::from(msg); user.apply_user_diff(&diff); - let user = self.server().unwrap().users().get(&session).unwrap(); if Some(session) != self.server().unwrap().session_id() { - //send notification if the user moved to or from any channel + // Send notification if the user moved either to or from our channel if let Some(to_channel) = diff.channel_id { let this_channel = self.get_users_channel(self.server().unwrap().session_id().unwrap()); - if from_channel == this_channel || to_channel == this_channel { + + if from_channel == this_channel { + // User moved from our channel to somewhere else if let Some(channel) = self.server().unwrap().channels().get(&to_channel) { + let channel = channel.name().to_string(); notifications::send(format!( "{} moved to channel {}", - user.name(), - channel.name() + &username, + &channel, )); - } else { - warn!("{} moved to invalid channel {}", user.name(), to_channel); + self.push_event(MumbleEventKind::UserLeftChannel(username.clone(), channel)); } - self.audio_output - .play_effect(if from_channel == this_channel { - NotificationEvents::UserJoinedChannel - } else { - NotificationEvents::UserLeftChannel - }); + self.audio_output.play_effect(NotificationEvents::UserLeftChannel); + } else if to_channel == this_channel { + // User moved from somewhere else to our channel + if let Some(channel) = self.server().unwrap().channels().get(&from_channel) { + let channel = channel.name().to_string(); + notifications::send(format!( + "{} moved to your channel from {}", + &username, + &channel, + )); + self.push_event(MumbleEventKind::UserJoinedChannel(username.clone(), channel)); + } + self.audio_output.play_effect(NotificationEvents::UserJoinedChannel); } } //send notification if a user muted/unmuted if mute != None || deaf != None { - let mut s = user.name().to_string(); + let mut s = username; if let Some(mute) = mute { s += if mute { " muted" } else { " unmuted" }; } @@ -224,7 +233,8 @@ impl State { s += if deaf { " deafened" } else { " undeafened" }; } s += " themselves"; - notifications::send(s); + notifications::send(s.clone()); + self.push_event(MumbleEventKind::UserMuteStateChanged(s)); } } } -- cgit v1.2.1 From 2aafe6967e38851d56f747cb3e615da7e9fa9bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 8 Jun 2021 09:46:59 +0200 Subject: doc --- mumd/src/state.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'mumd/src') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 3aa330f..d0a7e4b 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -297,6 +297,7 @@ impl State { .play_effect(NotificationEvents::ServerConnect); } + /// Store a new event pub fn push_event(&mut self, kind: MumbleEventKind) { self.events.push(MumbleEvent { timestamp: chrono::Local::now().naive_local(), kind }); } -- cgit v1.2.1 From d3eb004bcca01c87cb12ba297e568eaf9d25cd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 8 Jun 2021 09:47:10 +0200 Subject: mumlib error unimplemented --- mumd/src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumd/src') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index d0a7e4b..cdd663d 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -444,7 +444,7 @@ pub fn handle_command( Command::Events { block } => { if block { warn!("Blocking event list is unimplemented"); - now!(Ok(None)) + now!(Err(Error::Unimplemented)) } else { let events: Vec<_> = state .events -- cgit v1.2.1 From 52a4740f252391c944bf910c1a81a07bd7aea610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 8 Jun 2021 10:00:33 +0200 Subject: always send user joined/left channels and events --- mumd/src/state.rs | 53 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 20 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index cdd663d..8d92877 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -134,21 +134,23 @@ impl State { if matches!(*self.phase_receiver().borrow(), StatePhase::Connected(_)) { let this_channel = msg.get_channel_id(); let other_channel = self.get_users_channel(self.server().unwrap().session_id().unwrap()); - //TODO can this fail? - let this_channel_name = self.server().unwrap().channels().get(&this_channel).map(|c| c.name()); + let this_channel_name = self + .server() + .unwrap() + .channels() + .get(&this_channel) + .map(|c| c.name()) + .unwrap_or("") + .to_string(); if this_channel == other_channel { - if let Some(this_channel_name) = this_channel_name { - notifications::send(format!( - "{} connected and joined {}", - msg.get_name(), - this_channel_name, - )); - } - let this_channel_name = this_channel_name.map(|s| s.to_string()); + notifications::send(format!( + "{} connected and joined {}", + msg.get_name(), + this_channel_name, + )); self.push_event(MumbleEventKind::UserConnected(msg.get_name().to_string(), this_channel_name)); - self.audio_output - .play_effect(NotificationEvents::UserConnected); + self.audio_output.play_effect(NotificationEvents::UserConnected); } } } @@ -248,14 +250,25 @@ impl State { let this_channel = self.get_users_channel(self.server().unwrap().session_id().unwrap()); let other_channel = self.get_users_channel(msg.get_session()); if this_channel == other_channel { - let channel_name = self.server().unwrap().channels().get(&this_channel).map(|c| c.name().to_string()); - self.audio_output - .play_effect(NotificationEvents::UserDisconnected); - if let Some(user) = self.server().unwrap().users().get(&msg.get_session()) { - notifications::send(format!("{} disconnected", &user.name())); - let user_name = user.name().to_string(); - self.push_event(MumbleEventKind::UserDisconnected(user_name, channel_name)); - } + let channel_name = self + .server() + .unwrap() + .channels() + .get(&this_channel) + .map(|c| c.name()) + .unwrap_or("") + .to_string(); + let user_name = self + .server() + .unwrap() + .users() + .get(&msg.get_session()) + .map(|u| u.name()) + .unwrap_or("") + .to_string(); + notifications::send(format!("{} disconnected", &user_name)); + self.push_event(MumbleEventKind::UserDisconnected(user_name, channel_name)); + self.audio_output.play_effect(NotificationEvents::UserDisconnected); } self.server_mut() -- cgit v1.2.1