From 8e35c18e2aac958837b2c8fcb782950f86e5f214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 01:50:20 +0200 Subject: add first notification --- Cargo.lock | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ mumd/Cargo.toml | 1 + mumd/src/main.rs | 1 + mumd/src/state.rs | 15 +++++++ mumd/src/state/user.rs | 6 ++- 5 files changed, 126 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1ba33d..239a3b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -419,6 +419,33 @@ dependencies = [ "slab", ] +[[package]] +name = "gdk-pixbuf" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16160d212ae91abe9f3324c3fb233929ba322dde63585d15cda3336f8c529ed1" +dependencies = [ + "gdk-pixbuf-sys", + "glib", + "glib-sys", + "gobject-sys", + "libc", +] + +[[package]] +name = "gdk-pixbuf-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "798f97101eea8180da363d0e80e07ec7ec6d1809306601c0100c1de5bc8b4f52" +dependencies = [ + "bitflags", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", +] + [[package]] name = "getrandom" version = "0.1.15" @@ -430,12 +457,61 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1", ] +[[package]] +name = "gio-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a303bbf7a5e75ab3b627117ff10e495d1b9e97e1d68966285ac2b1f6270091bc" +dependencies = [ + "bitflags", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", +] + +[[package]] +name = "glib" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9b0452824cc63066940f01adc721804919f0b76cdba3cfab977b00b87f16d4a" +dependencies = [ + "bitflags", + "glib-sys", + "gobject-sys", + "lazy_static", + "libc", +] + +[[package]] +name = "glib-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9693049613ff52b93013cc3d2590366d8e530366d288438724b73f6c7dc4be8" +dependencies = [ + "bitflags", + "libc", + "pkg-config", +] + [[package]] name = "glob" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "gobject-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60d507c87a71b1143c66ed21a969be9b99a76df234b342d733e787e6c9c7d7c2" +dependencies = [ + "bitflags", + "glib-sys", + "libc", + "pkg-config", +] + [[package]] name = "hermit-abi" version = "0.1.17" @@ -519,6 +595,34 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "libnotify" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10506a4f8bc6f8f7ccc6fde3a8290378d7aed3d1a26dca606a73e2ffe140cc2d" +dependencies = [ + "gdk-pixbuf", + "gdk-pixbuf-sys", + "glib", + "glib-sys", + "gobject-sys", + "libnotify-sys", +] + +[[package]] +name = "libnotify-sys" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0a716b9b7d24ed10f1eb431e1527fa13c9a4bf2d4fa68bb3e54da1d0747383c" +dependencies = [ + "bitflags", + "gdk-pixbuf-sys", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", +] + [[package]] name = "lock_api" version = "0.3.4" @@ -657,6 +761,7 @@ dependencies = [ "futures", "futures-util", "ipc-channel", + "libnotify", "log", "mumble-protocol", "mumlib", diff --git a/mumd/Cargo.toml b/mumd/Cargo.toml index ffb463a..c59c32b 100644 --- a/mumd/Cargo.toml +++ b/mumd/Cargo.toml @@ -17,6 +17,7 @@ cpal = { git = "https://github.com/RustAudio/cpal" } futures = "0.3" futures-util = "0.3" ipc-channel = "0.14" +libnotify = "1.0" log = "0.4" mumble-protocol = "0.3" native-tls = "0.2" diff --git a/mumd/src/main.rs b/mumd/src/main.rs index e88eede..79e44a2 100644 --- a/mumd/src/main.rs +++ b/mumd/src/main.rs @@ -22,6 +22,7 @@ use tokio::task::spawn_blocking; #[tokio::main] async fn main() { setup_logger(std::io::stderr(), true); + libnotify::init("mumd").unwrap(); // Oneshot channel for setting UDP CryptState from control task // For simplicity we don't deal with re-syncing, real applications would have to. diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 44d8b21..6180654 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -228,6 +228,21 @@ impl State { let user = self.server_mut().unwrap().users_mut().get_mut(&sess).unwrap(); let diff = mumlib::state::UserDiff::from(msg); user.apply_user_diff(&diff); + let user = self.server().unwrap().users().get(&sess).unwrap(); + + // send notification + if let Some(channel_id) = diff.channel_id { + if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { + libnotify::Notification::new("mumd", + Some(format!("{} moved to channel {}", + &user.name(), + channel.name()).as_str()), + None).show().unwrap(); + } else { + warn!("{} moved to invalid channel {}", &user.name(), channel_id); + } + } + Some(diff) } } diff --git a/mumd/src/state/user.rs b/mumd/src/state/user.rs index 679d0ff..ab867e6 100644 --- a/mumd/src/state/user.rs +++ b/mumd/src/state/user.rs @@ -1,6 +1,10 @@ +use crate::state::channel::Channel; + use log::*; use mumble_protocol::control::msgs; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::collections::hash_map::Entry; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct User { @@ -80,7 +84,6 @@ impl User { } pub fn apply_user_diff(&mut self, diff: &mumlib::state::UserDiff) { - debug!("applying user diff\n{:#?}", diff); if let Some(comment) = diff.comment.clone() { self.comment = Some(comment); } @@ -111,7 +114,6 @@ impl User { if let Some(deaf) = diff.deaf { self.deaf = deaf; } - if let Some(channel_id) = diff.channel_id { self.channel = channel_id; } -- cgit v1.2.1 From cccc98aa9c6962ba8908ff0854241fbeea8ed3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 02:33:12 +0200 Subject: remove user from users on disconnect --- mumd/src/network/tcp.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs index c2cb234..9a7b786 100644 --- a/mumd/src/network/tcp.rs +++ b/mumd/src/network/tcp.rs @@ -16,6 +16,7 @@ use tokio::time::{self, Duration}; use tokio_tls::{TlsConnector, TlsStream}; use tokio_util::codec::{Decoder, Framed}; use std::collections::HashMap; +use std::collections::hash_map::Entry; use std::future::Future; use std::rc::Rc; use std::cell::RefCell; @@ -268,6 +269,16 @@ async fn listen( .unwrap() .audio_mut() .remove_client(msg.get_session()); + match state.lock().unwrap().server_mut().unwrap().users_mut().entry(msg.get_session()) { + Entry::Occupied(o) => { + o.remove_entry(); + info!("Removed entry"); + }, + Entry::Vacant(_) => { + warn!("Tried to disconnect unknown user {}", msg.get_session()); + } + } + info!("{}", state.lock().unwrap().server().unwrap().users().contains_key(&msg.get_session())); } ControlPacket::ChannelState(msg) => { debug!("Channel state received"); @@ -367,4 +378,4 @@ async fn run_until_disconnection( }; join!(main_block, phase_transition_block); -} \ No newline at end of file +} -- cgit v1.2.1 From 867a59cdabb864007166bcf3075eddc3210a76e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 02:33:25 +0200 Subject: remove unused imports --- mumd/src/state/user.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mumd/src/state/user.rs b/mumd/src/state/user.rs index ab867e6..98f34bc 100644 --- a/mumd/src/state/user.rs +++ b/mumd/src/state/user.rs @@ -1,10 +1,5 @@ -use crate::state::channel::Channel; - -use log::*; use mumble_protocol::control::msgs; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; -use std::collections::hash_map::Entry; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct User { @@ -119,7 +114,6 @@ impl User { } } - pub fn name(&self) -> &str { &self.name } -- cgit v1.2.1 From f12b127c83da51cac99dbde1db091760ef8fe67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 02:33:35 +0200 Subject: send notif when someone connects --- mumd/src/state.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 6180654..39221a3 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -221,6 +221,22 @@ impl State { } else { // this is someone else self.audio_mut().add_client(sess); + + if *self.phase_receiver().borrow() == StatePhase::Connected { + let channel_id = if msg.has_channel_id() { + msg.get_channel_id() + } else { + 0 + }; + if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { + libnotify::Notification::new("mumd", + Some(format!("{} connected and joined {}", + &msg.get_name(), + channel.name()).as_str()), + None) + .show().unwrap(); + } + } } self.server_mut().unwrap().users_mut().insert(sess, user::User::new(msg)); None @@ -237,7 +253,8 @@ impl State { Some(format!("{} moved to channel {}", &user.name(), channel.name()).as_str()), - None).show().unwrap(); + None) + .show().unwrap(); } else { warn!("{} moved to invalid channel {}", &user.name(), channel_id); } -- cgit v1.2.1 From 83e144ef7ce0e922be14cd2ef2abc938ab971c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 02:40:07 +0200 Subject: send notif when someone disconnects --- mumd/src/network/tcp.rs | 17 +---------------- mumd/src/state.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs index 9a7b786..0432be1 100644 --- a/mumd/src/network/tcp.rs +++ b/mumd/src/network/tcp.rs @@ -263,22 +263,7 @@ async fn listen( info!("User {} connected to {}", user.name(), user.channel()); } ControlPacket::UserRemove(msg) => { - info!("User {} left", msg.get_session()); - state - .lock() - .unwrap() - .audio_mut() - .remove_client(msg.get_session()); - match state.lock().unwrap().server_mut().unwrap().users_mut().entry(msg.get_session()) { - Entry::Occupied(o) => { - o.remove_entry(); - info!("Removed entry"); - }, - Entry::Vacant(_) => { - warn!("Tried to disconnect unknown user {}", msg.get_session()); - } - } - info!("{}", state.lock().unwrap().server().unwrap().users().contains_key(&msg.get_session())); + state.lock().unwrap().remove_client(*msg); } ControlPacket::ChannelState(msg) => { debug!("Channel state received"); diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 39221a3..85245a6 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -222,6 +222,7 @@ impl State { // this is someone else self.audio_mut().add_client(sess); + // send notification only if we've passed the connecting phase if *self.phase_receiver().borrow() == StatePhase::Connected { let channel_id = if msg.has_channel_id() { msg.get_channel_id() @@ -269,6 +270,14 @@ impl State { warn!("Tried to remove user state without session"); return; } + if let Some(user) = self.server().unwrap().users().get(&msg.get_session()) { + libnotify::Notification::new("mumd", + Some(format!("{} disconnected", + &user.name()).as_str()), + None) + .show().unwrap(); + } + self.audio().remove_client(msg.get_session()); self.server_mut().unwrap().users_mut().remove(&msg.get_session()); info!("User {} disconnected", msg.get_session()); -- cgit v1.2.1 From 3294a1a552ab596a04191672c54b66deb73fe6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 02:48:16 +0200 Subject: refactor parsing initial and updated user state --- mumd/src/state.rs | 90 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 85245a6..b27d401 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -210,59 +210,67 @@ impl State { warn!("Can't parse user state without session"); return None; } - let sess = msg.get_session(); + let session = msg.get_session(); // check if this is initial state - if !self.server().unwrap().users().contains_key(&sess) { - if !msg.has_name() { - warn!("Missing name in initial user state"); - } else if msg.get_name() == self.server().unwrap().username().unwrap() { - // this is us - *self.server_mut().unwrap().session_id_mut() = Some(sess); - } else { - // this is someone else - self.audio_mut().add_client(sess); - - // send notification only if we've passed the connecting phase - if *self.phase_receiver().borrow() == StatePhase::Connected { - let channel_id = if msg.has_channel_id() { - msg.get_channel_id() - } else { - 0 - }; - if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { - libnotify::Notification::new("mumd", - Some(format!("{} connected and joined {}", - &msg.get_name(), - channel.name()).as_str()), - None) - .show().unwrap(); - } - } - } - self.server_mut().unwrap().users_mut().insert(sess, user::User::new(msg)); + if !self.server().unwrap().users().contains_key(&session) { + self.parse_initial_user_state(session, msg); None } else { - let user = self.server_mut().unwrap().users_mut().get_mut(&sess).unwrap(); - let diff = mumlib::state::UserDiff::from(msg); - user.apply_user_diff(&diff); - let user = self.server().unwrap().users().get(&sess).unwrap(); + Some(self.parse_updated_user_state(session, msg)) + } + } + + fn parse_initial_user_state(&mut self, session: u32, msg: msgs::UserState) { + if !msg.has_name() { + warn!("Missing name in initial user state"); + } else if msg.get_name() == self.server().unwrap().username().unwrap() { + // this is us + *self.server_mut().unwrap().session_id_mut() = Some(session); + } else { + // this is someone else + self.audio_mut().add_client(session); - // send notification - if let Some(channel_id) = diff.channel_id { + // send notification only if we've passed the connecting phase + if *self.phase_receiver().borrow() == StatePhase::Connected { + let channel_id = if msg.has_channel_id() { + msg.get_channel_id() + } else { + 0 + }; if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { libnotify::Notification::new("mumd", - Some(format!("{} moved to channel {}", - &user.name(), - channel.name()).as_str()), + Some(format!("{} connected and joined {}", + &msg.get_name(), + channel.name()).as_str()), None) .show().unwrap(); - } else { - warn!("{} moved to invalid channel {}", &user.name(), channel_id); } } + } + self.server_mut().unwrap().users_mut().insert(session, user::User::new(msg)); + } - Some(diff) + fn parse_updated_user_state(&mut self, session: u32, msg: msgs::UserState) -> mumlib::state::UserDiff { + let user = self.server_mut().unwrap().users_mut().get_mut(&session).unwrap(); + let diff = mumlib::state::UserDiff::from(msg); + user.apply_user_diff(&diff); + let user = self.server().unwrap().users().get(&session).unwrap(); + + // send notification + if let Some(channel_id) = diff.channel_id { + if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { + libnotify::Notification::new("mumd", + Some(format!("{} moved to channel {}", + &user.name(), + channel.name()).as_str()), + None) + .show().unwrap(); + } else { + warn!("{} moved to invalid channel {}", &user.name(), channel_id); + } } + + diff } pub fn remove_client(&mut self, msg: msgs::UserRemove) { -- cgit v1.2.1 From 61d578ad3e6fd1f59cb46436b45374c8fe3936c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 03:31:59 +0200 Subject: minor cleanup --- mumd/src/network/tcp.rs | 5 ----- mumd/src/state.rs | 15 ++++++++------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs index 0432be1..630f46a 100644 --- a/mumd/src/network/tcp.rs +++ b/mumd/src/network/tcp.rs @@ -16,7 +16,6 @@ use tokio::time::{self, Duration}; use tokio_tls::{TlsConnector, TlsStream}; use tokio_util::codec::{Decoder, Framed}; use std::collections::HashMap; -use std::collections::hash_map::Entry; use std::future::Future; use std::rc::Rc; use std::cell::RefCell; @@ -251,16 +250,12 @@ async fn listen( } ControlPacket::UserState(msg) => { let mut state = state.lock().unwrap(); - let session = msg.get_session(); if *state.phase_receiver().borrow() == StatePhase::Connecting { state.audio_mut().add_client(msg.get_session()); state.parse_user_state(*msg); } else { state.parse_user_state(*msg); } - let server = state.server_mut().unwrap(); - let user = server.users().get(&session).unwrap(); - info!("User {} connected to {}", user.name(), user.channel()); } ControlPacket::UserRemove(msg) => { state.lock().unwrap().remove_client(*msg); diff --git a/mumd/src/state.rs b/mumd/src/state.rs index b27d401..3aff353 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -214,9 +214,9 @@ impl State { // check if this is initial state if !self.server().unwrap().users().contains_key(&session) { self.parse_initial_user_state(session, msg); - None + return None; } else { - Some(self.parse_updated_user_state(session, msg)) + return Some(self.parse_updated_user_state(session, msg)); } } @@ -256,14 +256,15 @@ impl State { user.apply_user_diff(&diff); let user = self.server().unwrap().users().get(&session).unwrap(); - // send notification + // send notification if the user moved to or from any channel + //TODO our channel only if let Some(channel_id) = diff.channel_id { if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { libnotify::Notification::new("mumd", - Some(format!("{} moved to channel {}", - &user.name(), - channel.name()).as_str()), - None) + Some(format!("{} moved to channel {}", + &user.name(), + channel.name()).as_str()), + None) .show().unwrap(); } else { warn!("{} moved to invalid channel {}", &user.name(), channel_id); -- cgit v1.2.1 From 6a9e74def5010e29abe62be81c3f53e8943b2d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 23 Oct 2020 03:32:50 +0200 Subject: notif if user muted/unmuted --- mumd/src/state.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ mumd/src/state/user.rs | 8 ++++++++ 2 files changed, 51 insertions(+) diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 3aff353..2093c7c 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -252,6 +252,18 @@ impl State { fn parse_updated_user_state(&mut self, session: u32, msg: msgs::UserState) -> mumlib::state::UserDiff { let user = self.server_mut().unwrap().users_mut().get_mut(&session).unwrap(); + + let mute = if msg.has_self_mute() && user.self_mute() != msg.get_self_mute() { + Some(msg.get_self_mute()) + } else { + None + }; + let deaf = if msg.has_self_deaf() && user.self_deaf() != msg.get_self_deaf() { + Some(msg.get_self_deaf()) + } else { + None + }; + let diff = mumlib::state::UserDiff::from(msg); user.apply_user_diff(&diff); let user = self.server().unwrap().users().get(&session).unwrap(); @@ -271,6 +283,37 @@ impl State { } } + // send notification if a user muted/unmuted + //TODO our channel only + let notif_desc = + if let Some(deaf) = deaf { + if deaf { + Some(format!("{} muted and deafend themselves", &user.name())) + } else if !deaf { + Some(format!("{} unmuted and undeafend themselves", &user.name())) + } else { + warn!("Invalid user state received"); + None + } + } else if let Some(mute) = mute { + if mute { + Some(format!("{} muted themselves", &user.name())) + } else if !mute { + Some(format!("{} unmuted themselves", &user.name())) + } else { + warn!("Invalid user state received"); + None + } + } else { + None + }; + if let Some(notif_desc) = notif_desc { + libnotify::Notification::new("mumd", + notif_desc.as_str(), + None) + .show().unwrap(); + } + diff } diff --git a/mumd/src/state/user.rs b/mumd/src/state/user.rs index 98f34bc..848208c 100644 --- a/mumd/src/state/user.rs +++ b/mumd/src/state/user.rs @@ -121,6 +121,14 @@ impl User { pub fn channel(&self) -> u32 { self.channel } + + pub fn self_mute(&self) -> bool { + self.self_mute + } + + pub fn self_deaf(&self) -> bool { + self.self_deaf + } } impl From<&User> for mumlib::state::User { -- cgit v1.2.1 From 8cd74edc6079d26746cd07838c99d3b60228b87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 29 Oct 2020 14:27:16 +0100 Subject: dont use qualified path --- mumd/src/state.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 2093c7c..ea0213c 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -13,6 +13,7 @@ use mumble_protocol::voice::Serverbound; use mumlib::command::{Command, CommandResponse}; use mumlib::config::Config; use mumlib::error::{ChannelIdentifierError, Error}; +use mumlib::state::UserDiff; use std::net::ToSocketAddrs; use tokio::sync::{mpsc, watch}; use crate::network::tcp::{TcpEvent, TcpEventData}; @@ -205,7 +206,7 @@ impl State { } } - pub fn parse_user_state(&mut self, msg: msgs::UserState) -> Option { + pub fn parse_user_state(&mut self, msg: msgs::UserState) -> Option { if !msg.has_session() { warn!("Can't parse user state without session"); return None; @@ -250,7 +251,7 @@ impl State { self.server_mut().unwrap().users_mut().insert(session, user::User::new(msg)); } - fn parse_updated_user_state(&mut self, session: u32, msg: msgs::UserState) -> mumlib::state::UserDiff { + fn parse_updated_user_state(&mut self, session: u32, msg: msgs::UserState) -> UserDiff { let user = self.server_mut().unwrap().users_mut().get_mut(&session).unwrap(); let mute = if msg.has_self_mute() && user.self_mute() != msg.get_self_mute() { @@ -264,7 +265,7 @@ impl State { None }; - let diff = mumlib::state::UserDiff::from(msg); + let diff = UserDiff::from(msg); user.apply_user_diff(&diff); let user = self.server().unwrap().users().get(&session).unwrap(); -- cgit v1.2.1 From bddf2324b1fd1c440127432cbd93ac64afc7ca85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 29 Oct 2020 14:54:11 +0100 Subject: refactor and dont panic without x-server --- mumd/src/main.rs | 3 ++- mumd/src/state.rs | 29 ++++++++--------------------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/mumd/src/main.rs b/mumd/src/main.rs index 79e44a2..37ff0dd 100644 --- a/mumd/src/main.rs +++ b/mumd/src/main.rs @@ -1,6 +1,7 @@ mod audio; mod command; mod network; +mod notify; mod state; use crate::network::ConnectionInfo; @@ -22,7 +23,7 @@ use tokio::task::spawn_blocking; #[tokio::main] async fn main() { setup_logger(std::io::stderr(), true); - libnotify::init("mumd").unwrap(); + notify::init(); // Oneshot channel for setting UDP CryptState from control task // For simplicity we don't deal with re-syncing, real applications would have to. diff --git a/mumd/src/state.rs b/mumd/src/state.rs index ea0213c..ea081fc 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -4,6 +4,7 @@ pub mod user; use crate::audio::Audio; use crate::network::ConnectionInfo; +use crate::notify; use crate::state::server::Server; use log::*; @@ -239,12 +240,7 @@ impl State { 0 }; if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { - libnotify::Notification::new("mumd", - Some(format!("{} connected and joined {}", - &msg.get_name(), - channel.name()).as_str()), - None) - .show().unwrap(); + notify::send(format!("{} connected and joined {}", &msg.get_name(), channel.name())); } } } @@ -273,12 +269,10 @@ impl State { //TODO our channel only if let Some(channel_id) = diff.channel_id { if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { - libnotify::Notification::new("mumd", - Some(format!("{} moved to channel {}", - &user.name(), - channel.name()).as_str()), - None) - .show().unwrap(); + notify::send(format!( + "{} moved to channel {}", + &user.name(), + channel.name())); } else { warn!("{} moved to invalid channel {}", &user.name(), channel_id); } @@ -309,10 +303,7 @@ impl State { None }; if let Some(notif_desc) = notif_desc { - libnotify::Notification::new("mumd", - notif_desc.as_str(), - None) - .show().unwrap(); + notify::send(notif_desc); } diff @@ -324,11 +315,7 @@ impl State { return; } if let Some(user) = self.server().unwrap().users().get(&msg.get_session()) { - libnotify::Notification::new("mumd", - Some(format!("{} disconnected", - &user.name()).as_str()), - None) - .show().unwrap(); + notify::send(format!("{} disconnected", &user.name())); } self.audio().remove_client(msg.get_session()); -- cgit v1.2.1 From 69ec88d3fc2e3762cf596bbf0226b204ba35d874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 29 Oct 2020 15:04:12 +0100 Subject: add file --- mumd/src/notify.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 mumd/src/notify.rs diff --git a/mumd/src/notify.rs b/mumd/src/notify.rs new file mode 100644 index 0000000..5bb1a26 --- /dev/null +++ b/mumd/src/notify.rs @@ -0,0 +1,19 @@ +use log::*; + +pub fn init() { + libnotify::init("mumd").unwrap(); +} + +pub fn send(msg: String) -> bool { + match libnotify::Notification::new( + "mumd", + Some(msg.as_str()), + None, + ).show() { + Ok(_) => { true } + Err(_) => { + debug!("Unable to send notification"); + false + } + } +} -- cgit v1.2.1