aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/state/server.rs
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-10-21 19:40:28 +0200
committerGustav Sörnäs <gustav@sornas.net>2020-10-21 19:40:28 +0200
commit161f58f3e6a0088fa2612d9af54c7b2939be37e7 (patch)
tree78d3532e46525db78a48b5f8bcd27ebdac693f6e /mumd/src/state/server.rs
parent92d64a493ced079645e7f0601ff9a573c1616203 (diff)
parent962af6bd39f77381aeca874a40fa0b2fb36f33c4 (diff)
downloadmum-161f58f3e6a0088fa2612d9af54c7b2939be37e7.tar.gz
Merge remote-tracking branch 'origin/refactor-state' into main
Diffstat (limited to 'mumd/src/state/server.rs')
-rw-r--r--mumd/src/state/server.rs123
1 files changed, 123 insertions, 0 deletions
diff --git a/mumd/src/state/server.rs b/mumd/src/state/server.rs
new file mode 100644
index 0000000..b99c7e6
--- /dev/null
+++ b/mumd/src/state/server.rs
@@ -0,0 +1,123 @@
+use crate::state::channel::{into_channel, Channel};
+use crate::state::user::User;
+
+use log::*;
+use mumble_protocol::control::msgs;
+use serde::{Deserialize, Serialize};
+use std::collections::hash_map::Entry;
+use std::collections::HashMap;
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub struct Server {
+ channels: HashMap<u32, Channel>,
+ users: HashMap<u32, User>,
+ pub welcome_text: Option<String>,
+
+ username: Option<String>,
+ session_id: Option<u32>,
+
+ host: Option<String>,
+}
+
+impl Server {
+ pub fn new() -> Self {
+ Self {
+ channels: HashMap::new(),
+ users: HashMap::new(),
+ welcome_text: None,
+ username: None,
+ session_id: None,
+ host: None,
+ }
+ }
+
+ pub fn parse_server_sync(&mut self, mut msg: msgs::ServerSync) {
+ if msg.has_welcome_text() {
+ self.welcome_text = Some(msg.take_welcome_text());
+ }
+ }
+
+ pub fn parse_channel_state(&mut self, msg: msgs::ChannelState) {
+ if !msg.has_channel_id() {
+ warn!("Can't parse channel state without channel id");
+ return;
+ }
+ match self.channels.entry(msg.get_channel_id()) {
+ Entry::Vacant(e) => {
+ e.insert(Channel::new(msg));
+ }
+ Entry::Occupied(mut e) => e.get_mut().parse_channel_state(msg),
+ }
+ }
+
+ pub fn parse_channel_remove(&mut self, msg: msgs::ChannelRemove) {
+ if !msg.has_channel_id() {
+ warn!("Can't parse channel remove without channel id");
+ return;
+ }
+ match self.channels.entry(msg.get_channel_id()) {
+ Entry::Vacant(_) => {
+ warn!("Attempted to remove channel that doesn't exist");
+ }
+ Entry::Occupied(e) => {
+ e.remove();
+ }
+ }
+ }
+
+ pub fn parse_user_state(&mut self, msg: msgs::UserState) {
+ if !msg.has_session() {
+ warn!("Can't parse user state without session");
+ return;
+ }
+ match self.users.entry(msg.get_session()) {
+ Entry::Vacant(e) => {
+ e.insert(User::new(msg));
+ }
+ Entry::Occupied(mut e) => e.get_mut().parse_user_state(msg),
+ }
+ }
+
+ pub fn channels(&self) -> &HashMap<u32, Channel> {
+ &self.channels
+ }
+
+ pub fn host_mut(&mut self) -> &mut Option<String> {
+ &mut self.host
+ }
+
+ pub fn session_id(&self) -> Option<u32> {
+ self.session_id
+ }
+
+ pub fn session_id_mut(&mut self) -> &mut Option<u32> {
+ &mut self.session_id
+ }
+
+ pub fn users(&self) -> &HashMap<u32, User> {
+ &self.users
+ }
+
+ pub fn users_mut(&mut self) -> &mut HashMap<u32, User> {
+ &mut self.users
+ }
+
+ pub fn username(&self) -> Option<&str> {
+ self.username.as_ref().map(|e| e.as_str())
+ }
+
+ pub fn username_mut(&mut self) -> &mut Option<String> {
+ &mut self.username
+ }
+}
+
+impl From<&Server> for mumlib::state::Server {
+ fn from(server: &Server) -> Self {
+ mumlib::state::Server {
+ channels: into_channel(server.channels(), server.users()),
+ welcome_text: server.welcome_text.clone(),
+ username: server.username.clone().unwrap(),
+ host: server.host.as_ref().unwrap().clone(),
+ }
+ }
+}