1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
use crate::state::channel::{into_channel, Channel};
use crate::state::user::User;
use log::*;
use mumble_protocol::control::msgs;
use mumlib::error::ChannelIdentifierError;
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>,
password: Option<String>,
session_id: Option<u32>,
muted: bool,
deafened: bool,
host: Option<String>,
}
impl Server {
pub fn new() -> Self {
Self {
channels: HashMap::new(),
users: HashMap::new(),
welcome_text: None,
username: None,
password: None,
session_id: None,
muted: false,
deafened: false,
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
}
/// Takes a channel name and returns either a tuple with the channel id and a reference to the
/// channel struct if the channel name unambiguosly refers to a channel, or an error describing
/// if the channel identifier was ambigous or invalid.
/// note that doctests currently aren't run in binary crates yet (see #50784)
/// ```
/// use crate::state::channel::Channel;
/// let mut server = Server::new();
/// let channel = Channel {
/// name: "Foobar".to_owned(),
/// ..Default::default(),
/// };
/// server.channels.insert(0, channel.clone);
/// assert_eq!(server.channel_name("Foobar"), Ok((0, &channel)));
/// ```
pub fn channel_name(
&self,
channel_name: &str,
) -> Result<(u32, &Channel), ChannelIdentifierError> {
let matches = self
.channels
.iter()
.map(|e| ((*e.0, e.1), e.1.path(&self.channels)))
.filter(|e| e.1.ends_with(channel_name))
.collect::<Vec<_>>();
Ok(match matches.len() {
0 => {
let soft_matches = self
.channels
.iter()
.map(|e| ((*e.0, e.1), e.1.path(&self.channels).to_lowercase()))
.filter(|e| e.1.ends_with(&channel_name.to_lowercase()))
.collect::<Vec<_>>();
match soft_matches.len() {
0 => return Err(ChannelIdentifierError::Invalid),
1 => soft_matches.get(0).unwrap().0,
_ => return Err(ChannelIdentifierError::Ambiguous),
}
}
1 => matches.get(0).unwrap().0,
_ => return Err(ChannelIdentifierError::Ambiguous),
})
}
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_deref()
}
pub fn username_mut(&mut self) -> &mut Option<String> {
&mut self.username
}
pub fn password(&self) -> Option<&str> {
self.password.as_deref()
}
pub fn password_mut(&mut self) -> &mut Option<String> {
&mut self.password
}
pub fn muted(&self) -> bool {
self.muted
}
pub fn deafened(&self) -> bool {
self.deafened
}
pub fn set_muted(&mut self, value: bool) {
self.muted = value;
}
pub fn set_deafened(&mut self, value: bool) {
self.deafened = value;
}
}
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(),
}
}
}
|