aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/state.rs
blob: ef1cd6d73e10e58640c0ffd7e0c9a0f310e82b02 (plain) (blame)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use log::*;
use crate::audio::Audio;
use crate::command::{Command, CommandResponse};
use crate::network::ConnectionInfo;
use mumble_protocol::control::msgs;
use mumble_protocol::control::ControlPacket;
use mumble_protocol::voice::Serverbound;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::net::ToSocketAddrs;
use tokio::sync::{mpsc, watch};

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StatePhase {
    Disconnected,
    Connecting,
    Connected,
}

pub struct State {
    server: Server,
    audio: Audio,

    packet_sender: mpsc::UnboundedSender<ControlPacket<Serverbound>>,
    command_sender: mpsc::UnboundedSender<Command>,
    connection_info_sender: watch::Sender<Option<ConnectionInfo>>,

    phase_watcher: (watch::Sender<StatePhase>, watch::Receiver<StatePhase>),

    username: Option<String>,
    session_id: Option<u32>,
}

impl State {
    pub fn new(
        packet_sender: mpsc::UnboundedSender<ControlPacket<Serverbound>>,
        command_sender: mpsc::UnboundedSender<Command>,
        connection_info_sender: watch::Sender<Option<ConnectionInfo>>,
        username: String,
    ) -> Self {
        Self {
            server: Server::new(),
            audio: Audio::new(),
            packet_sender,
            command_sender,
            connection_info_sender,
            phase_watcher: watch::channel(StatePhase::Disconnected),
            username: None,
            session_id: None,
        }
    }

    //TODO? move bool inside Result
    pub async fn handle_command(&mut self, command: Command) -> (bool, Result<Option<CommandResponse>, ()>) {
        match command {
            Command::ChannelJoin{channel_id} => {
                if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) {
                    warn!("Not connected");
                    return (false, Err(()));
                }
                let mut msg = msgs::UserState::new();
                msg.set_session(self.session_id.unwrap());
                msg.set_channel_id(channel_id);
                self.packet_sender.send(msg.into()).unwrap();
                (false, Ok(None))
            }
            Command::ChannelList => {
                if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) {
                    warn!("Not connected");
                    return (false, Err(()));
                }
                (false, Ok(Some(CommandResponse::ChannelList{channels: self.server.channels.clone()})))
            }
            Command::ServerConnect{host, port, username, accept_invalid_cert} => {
                if !matches!(*self.phase_receiver().borrow(), StatePhase::Disconnected) {
                    warn!("Tried to connect to a server while already connected");
                    return (false, Err(()));
                }
                self.username = Some(username);
                self.phase_watcher.0.broadcast(StatePhase::Connecting).unwrap();
                let socket_addr = (host.as_ref(), port)
                    .to_socket_addrs()
                    .expect("Failed to parse server address")
                    .next()
                    .expect("Failed to resolve server address");
                self.connection_info_sender.broadcast(Some(ConnectionInfo::new(
                    socket_addr,
                    host,
                    accept_invalid_cert,
                )));
                (true, Ok(None))
            }
            Command::Status => {
                if !matches!(*self.phase_receiver().borrow(), StatePhase::Connected) {
                    warn!("Not connected");
                    return (false, Err(()));
                }
                (false, Ok(Some(CommandResponse::Status{
                    username: self.username.clone(),
                    server_state: self.server.clone(),
                })))
            }
            _ => { (false, Ok(None)) }
        }
    }

    pub fn parse_initial_user_state(&mut self, msg: Box<msgs::UserState>) {
        if !msg.has_session() {
            warn!("Can't parse user state without session");
            return;
        }
        if !msg.has_name() {
            warn!("Missing name in initial user state");
        } else {
            if msg.get_name() == self.username.as_ref().unwrap() {
                match self.session_id {
                    None => {
                        debug!("Found our session id: {}", msg.get_session());
                        self.session_id = Some(msg.get_session());
                    }
                    Some(session) => {
                        if session != msg.get_session() {
                            error!("Got two different session IDs ({} and {}) for ourselves",
                                session,
                                msg.get_session());
                        } else {
                            debug!("Got our session ID twice");
                        }
                    }
                }
            }
        }
        self.server.parse_user_state(msg);
    }

    pub fn initialized(&self) {
        self.phase_watcher.0.broadcast(StatePhase::Connected).unwrap();
    }

    pub fn audio(&self) -> &Audio { &self.audio }
    pub fn audio_mut(&mut self) -> &mut Audio { &mut self.audio }
    pub fn packet_sender(&self) -> mpsc::UnboundedSender<ControlPacket<Serverbound>> { self.packet_sender.clone() }
    pub fn phase_receiver(&self) -> watch::Receiver<StatePhase> { self.phase_watcher.1.clone() }
    pub fn server_mut(&mut self) -> &mut Server { &mut self.server }
    pub fn username(&self) -> Option<&String> { self.username.as_ref() }
}

#[derive(Clone, Debug)]
pub struct Server {
    channels: HashMap<u32, Channel>,
    users: HashMap<u32, User>,
    pub welcome_text: Option<String>,
}

impl Server {
    pub fn new() -> Self {
        Self {
            channels: HashMap::new(),
            users: HashMap::new(),
            welcome_text: None,
        }
    }

    pub fn parse_server_sync(&mut self, mut msg: Box<msgs::ServerSync>) {
        if msg.has_welcome_text() {
            self.welcome_text = Some(msg.take_welcome_text());
        }
    }

    pub fn parse_channel_state(&mut self, msg: Box<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: Box<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: Box<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 users(&self) -> &HashMap<u32, User> {
        &self.users
    }
}

#[derive(Clone, Debug)]
pub struct Channel {
    description: Option<String>,
    links: Vec<u32>,
    max_users: u32,
    name: String,
    parent: Option<u32>,
    position: i32,
}

impl Channel {
    pub fn new(mut msg: Box<msgs::ChannelState>) -> Self {
        Self {
            description: if msg.has_description() {
                Some(msg.take_description())
            } else {
                None
            },
            links: Vec::new(),
            max_users: msg.get_max_users(),
            name: msg.take_name(),
            parent: if msg.has_parent() {
                Some(msg.get_parent())
            } else {
                None
            },
            position: msg.get_position(),
        }
    }

    pub fn parse_channel_state(&mut self, mut msg: Box<msgs::ChannelState>) {
        if msg.has_description() {
            self.description = Some(msg.take_description());
        }
        self.links = msg.take_links();
        if msg.has_max_users() {
            self.max_users = msg.get_max_users();
        }
        if msg.has_name() {
            self.name = msg.take_name();
        }
        if msg.has_parent() {
            self.parent = Some(msg.get_parent());
        }
        if msg.has_position() {
            self.position = msg.get_position();
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }
}

#[derive(Clone, Debug)]
pub struct User {
    channel: u32,
    comment: Option<String>,
    hash: Option<String>,
    name: String,
    priority_speaker: bool,
    recording: bool,

    suppress: bool, // by me
    self_mute: bool, // by self
    self_deaf: bool, // by self
    mute: bool, // by admin
    deaf: bool, // by admin
}

impl User {
    pub fn new(mut msg: Box<msgs::UserState>) -> Self {
        Self {
            channel: msg.get_channel_id(),
            comment: if msg.has_comment() {
                Some(msg.take_comment())
            } else {
                None
            },
            hash: if msg.has_hash() {
                Some(msg.take_hash())
            } else {
                None
            },
            name: msg.take_name(),
            priority_speaker: msg.has_priority_speaker()
                              && msg.get_priority_speaker(),
            recording: msg.has_recording()
                       && msg.get_recording(),
            suppress: msg.has_suppress()
                      && msg.get_suppress(),
            self_mute: msg.has_self_mute()
                       && msg.get_self_mute(),
            self_deaf: msg.has_self_deaf()
                       && msg.get_self_deaf(),
            mute: msg.has_mute()
                  && msg.get_mute(),
            deaf: msg.has_deaf()
                  && msg.get_deaf(),
        }
    }

    pub fn parse_user_state(&mut self, mut msg: Box<msgs::UserState>) {
        if msg.has_channel_id() {
            self.channel = msg.get_channel_id();
        }
        if msg.has_comment() {
            self.comment = Some(msg.take_comment());
        }
        if msg.has_hash() {
            self.hash = Some(msg.take_hash());
        }
        if msg.has_name() {
            self.name = msg.take_name();
        }
        if msg.has_priority_speaker() {
            self.priority_speaker = msg.get_priority_speaker();
        }
        if msg.has_recording() {
            self.recording = msg.get_recording();
        }
        if msg.has_suppress() {
            self.suppress = msg.get_suppress();
        }
        if msg.has_self_mute() {
            self.self_mute = msg.get_self_mute();
        }
        if msg.has_self_deaf() {
            self.self_deaf = msg.get_self_deaf();
        }
        if msg.has_mute() {
            self.mute = msg.get_mute();
        }
        if msg.has_deaf() {
            self.deaf = msg.get_deaf();
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn channel(&self) -> u32 {
        self.channel
    }
}