diff options
| author | Jokler <jokler@protonmail.com> | 2020-01-28 03:04:58 +0100 |
|---|---|---|
| committer | Jokler <jokler@protonmail.com> | 2020-01-29 20:39:27 +0100 |
| commit | 985d6bd787c07ea65804d5c079537fb8d805075f (patch) | |
| tree | ff8afb047d6292566189ca0ebaf8740bbc658f71 | |
| parent | 09fc3800030b971da8c68217c22f5a0444d7b278 (diff) | |
| download | pokebot-985d6bd787c07ea65804d5c079537fb8d805075f.tar.gz pokebot-985d6bd787c07ea65804d5c079537fb8d805075f.zip | |
Block bots from joining channels with other bots
| -rw-r--r-- | src/bot/master.rs | 49 | ||||
| -rw-r--r-- | src/bot/music.rs | 6 | ||||
| -rw-r--r-- | src/teamspeak.rs | 10 |
3 files changed, 56 insertions, 9 deletions
diff --git a/src/bot/master.rs b/src/bot/master.rs index 1f1ddfb..3e56b1a 100644 --- a/src/bot/master.rs +++ b/src/bot/master.rs @@ -82,7 +82,40 @@ impl MasterBot { } async fn spawn_bot(&self, id: ClientId) { - let channel = self.teamspeak.channel_path_of_user(id); + let channel = self + .teamspeak + .channel_of_user(id) + .expect("Can find poke sender"); + + if channel == self.teamspeak.my_channel() { + self.teamspeak.send_message_to_user( + id, + &format!( + "Joining the channel of \"{}\" is not allowed", + self.config.master_name + ), + ); + return; + } + + for (_, bot) in &*self.connected_bots.lock().expect("Mutex was not poisoned") { + if bot.my_channel() == channel { + self.teamspeak.send_message_to_user( + id, + &format!( + "\"{}\" is already in this channel. \ + Multiple bots in one channel are not allowed.", + bot.name() + ), + ); + return; + } + } + + let channel_path = self + .teamspeak + .channel_path_of_user(id) + .expect("can find poke sender"); let (name, name_index) = { let mut available_names = self.available_names.lock().expect("Mutex was not poisoned"); @@ -126,11 +159,17 @@ impl MasterBot { let disconnect_cb = Box::new(move |n, name_index, id_index| { let mut bots = cconnected_bots.lock().expect("Mutex was not poisoned"); bots.remove(&n); - cavailable_names.lock().expect("Mutex was not poisoned").push(name_index); - cavailable_ids.lock().expect("Mutex was not poisoned").push(id_index); + cavailable_names + .lock() + .expect("Mutex was not poisoned") + .push(name_index); + cavailable_ids + .lock() + .expect("Mutex was not poisoned") + .push(id_index); }); - info!("Connecting to {} on {}", channel, self.config.address); + info!("Connecting to {} on {}", channel_path, self.config.address); let bot_args = MusicBotArgs { name: name.clone(), name_index, @@ -138,7 +177,7 @@ impl MasterBot { local: self.config.local, address: self.config.address.clone(), id, - channel, + channel: channel_path, verbose: self.config.verbose, disconnect_cb, }; diff --git a/src/bot/music.rs b/src/bot/music.rs index 0bb5274..821087c 100644 --- a/src/bot/music.rs +++ b/src/bot/music.rs @@ -190,7 +190,11 @@ impl MusicBot { } } - fn my_channel(&self) -> ChannelId { + pub fn name(&self) -> &str { + &self.name + } + + pub fn my_channel(&self) -> ChannelId { self.teamspeak .as_ref() .map(|ts| ts.my_channel()) diff --git a/src/teamspeak.rs b/src/teamspeak.rs index 5dd80ba..b429869 100644 --- a/src/teamspeak.rs +++ b/src/teamspeak.rs @@ -115,10 +115,14 @@ impl TeamSpeakConnection { tokio::run(send_packet); } - pub fn channel_path_of_user(&self, id: ClientId) -> String { + pub fn channel_of_user(&self, id: ClientId) -> Option<ChannelId> { + Some(self.conn.lock().clients.get(&id)?.channel) + } + + pub fn channel_path_of_user(&self, id: ClientId) -> Option<String> { let conn = self.conn.lock(); - let channel_id = conn.clients.get(&id).expect("can find poke sender").channel; + let channel_id = conn.clients.get(&id)?.channel; let mut channel = conn .channels @@ -142,7 +146,7 @@ impl TeamSpeakConnection { path.push_str(name); } - path + Some(path) } pub fn my_channel(&self) -> ChannelId { |
