1use crate::state::channel::{into_channel, Channel};
2use crate::state::user::User;
3
4use log::*;
5use mumble_protocol::control::msgs;
6use mumlib::error::ChannelIdentifierError;
7use serde::{Deserialize, Serialize};
8use std::collections::hash_map::Entry;
9use std::collections::HashMap;
10
11#[derive(Clone, Debug, Deserialize, Serialize)]
12pub(crate) enum Server {
13 Disconnected,
14 Connecting(ConnectingServer),
15 Connected(ConnectedServer),
16}
17
18#[derive(Clone, Debug, Deserialize, Serialize)]
19pub(crate) struct ConnectingServer {
20 channels: HashMap<u32, Channel>,
21 users: HashMap<u32, User>,
22 welcome_text: Option<String>,
23
24 username: String,
25 password: Option<String>,
26
27 host: String,
28}
29
30impl ConnectingServer {
31 pub(crate) fn new(host: String, username: String, password: Option<String>) -> Self {
32 Self {
33 channels: HashMap::new(),
34 users: HashMap::new(),
35 welcome_text: None,
36 username,
37 password,
38 host,
39 }
40 }
41
42 pub(crate) fn channel_state(&mut self, msg: msgs::ChannelState) {
43 channel_state(msg, &mut self.channels);
44 }
45
46 pub(crate) fn channel_remove(&mut self, msg: msgs::ChannelRemove) {
47 channel_remove(msg, &mut self.channels);
48 }
49
50 pub(crate) fn user_state(&mut self, msg: msgs::UserState) {
51 user_state(msg, &mut self.users);
52 }
53
54 pub(crate) fn user_remove(&mut self, msg: msgs::UserRemove) {
55 self.users.remove(&msg.get_session());
56 }
57
58 pub(crate) fn server_sync(self, mut msg: msgs::ServerSync) -> ConnectedServer {
59 ConnectedServer {
60 channels: self.channels,
61 users: self.users,
62 welcome_text: msg.has_welcome_text().then(|| msg.take_welcome_text()),
63 username: self.username,
64 session_id: msg.get_session(),
65 muted: false,
66 deafened: false,
67 host: self.host,
68 }
69 }
70
71 pub(crate) fn username(&self) -> &str {
72 &self.username
73 }
74
75 pub(crate) fn password(&self) -> Option<&str> {
76 self.password.as_deref()
77 }
78}
79
80#[derive(Clone, Debug, Deserialize, Serialize)]
81pub(crate) struct ConnectedServer {
82 channels: HashMap<u32, Channel>,
83 users: HashMap<u32, User>,
84 welcome_text: Option<String>,
85
86 username: String,
87 session_id: u32,
88 muted: bool,
89 deafened: bool,
90
91 host: String,
92}
93
94impl ConnectedServer {
95 pub(crate) fn channel_remove(&mut self, msg: msgs::ChannelRemove) {
96 channel_remove(msg, &mut self.channels);
97 }
98
99 pub(crate) fn user_state(&mut self, msg: msgs::UserState) {
100 user_state(msg, &mut self.users);
101 }
102
103 pub(crate) fn user_remove(&mut self, msg: msgs::UserRemove) {
104 self.users.remove(&msg.get_session());
105 }
106
107 pub(crate) fn channels(&self) -> &HashMap<u32, Channel> {
108 &self.channels
109 }
110
111 pub(crate) fn channel_name(
115 &self,
116 channel_name: &str,
117 ) -> Result<(u32, &Channel), ChannelIdentifierError> {
118 let matches = self
119 .channels
120 .iter()
121 .map(|e| ((*e.0, e.1), e.1.path(&self.channels)))
122 .filter(|e| e.1.ends_with(channel_name))
123 .collect::<Vec<_>>();
124 Ok(match matches.len() {
125 0 => {
126 let soft_matches = self
127 .channels
128 .iter()
129 .map(|e| ((*e.0, e.1), e.1.path(&self.channels).to_lowercase()))
130 .filter(|e| e.1.ends_with(&channel_name.to_lowercase()))
131 .collect::<Vec<_>>();
132 match soft_matches.len() {
133 0 => return Err(ChannelIdentifierError::Invalid),
134 1 => soft_matches.get(0).unwrap().0,
135 _ => return Err(ChannelIdentifierError::Ambiguous),
136 }
137 }
138 1 => matches.get(0).unwrap().0,
139 _ => return Err(ChannelIdentifierError::Ambiguous),
140 })
141 }
142
143 pub(crate) fn current_channel(&self) -> (u32, &Channel) {
145 let channel_id = self.users().get(&self.session_id()).unwrap().channel();
146 let channel = self.channels().get(&channel_id).unwrap();
147 (channel_id, channel)
148 }
149
150 pub(crate) fn session_id(&self) -> u32 {
151 self.session_id
152 }
153
154 pub(crate) fn users(&self) -> &HashMap<u32, User> {
155 &self.users
156 }
157
158 pub(crate) fn users_mut(&mut self) -> &mut HashMap<u32, User> {
159 &mut self.users
160 }
161
162 pub(crate) fn username(&self) -> &str {
163 &self.username
164 }
165
166 pub(crate) fn muted(&self) -> bool {
167 self.muted
168 }
169
170 pub(crate) fn deafened(&self) -> bool {
171 self.deafened
172 }
173
174 pub(crate) fn set_muted(&mut self, value: bool) {
175 self.muted = value;
176 }
177
178 pub(crate) fn set_deafened(&mut self, value: bool) {
179 self.deafened = value;
180 }
181
182 pub(crate) fn users_channel(&self, user: u32) -> u32 {
183 self.users().get(&user).unwrap().channel()
184 }
185}
186
187fn user_state(msg: msgs::UserState, users: &mut HashMap<u32, User>) {
188 if !msg.has_session() {
189 warn!("Can't parse user state without session");
190 return;
191 }
192 match users.entry(msg.get_session()) {
193 Entry::Vacant(e) => {
194 e.insert(User::new(msg));
195 }
196 Entry::Occupied(mut e) => e.get_mut().parse_user_state(msg),
197 }
198}
199
200fn channel_state(msg: msgs::ChannelState, channels: &mut HashMap<u32, Channel>) {
201 if !msg.has_channel_id() {
202 warn!("Can't parse channel state without channel id");
203 return;
204 }
205 match channels.entry(msg.get_channel_id()) {
206 Entry::Vacant(e) => {
207 e.insert(Channel::new(msg));
208 }
209 Entry::Occupied(mut e) => e.get_mut().parse_channel_state(msg),
210 }
211}
212
213fn channel_remove(msg: msgs::ChannelRemove, channels: &mut HashMap<u32, Channel>) {
214 if !msg.has_channel_id() {
215 warn!("Can't parse channel remove without channel id");
216 return;
217 }
218 match channels.entry(msg.get_channel_id()) {
219 Entry::Vacant(_) => {
220 warn!("Attempted to remove channel that doesn't exist");
221 }
222 Entry::Occupied(e) => {
223 e.remove();
224 }
225 }
226}
227
228impl From<&ConnectedServer> for mumlib::state::Server {
229 fn from(server: &ConnectedServer) -> Self {
230 mumlib::state::Server {
231 channels: into_channel(server.channels(), server.users()),
232 welcome_text: server.welcome_text.clone(),
233 username: server.username.clone(),
234 host: server.host.clone(),
235 }
236 }
237}