From 87973e2dd3d28293e661045cde1a5e90bc017613 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 24 Jan 2020 18:29:17 +0100 Subject: Initial multibot draft --- src/teamspeak.rs | 76 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 22 deletions(-) (limited to 'src/teamspeak.rs') diff --git a/src/teamspeak.rs b/src/teamspeak.rs index 79dc1bc..f1abaec 100644 --- a/src/teamspeak.rs +++ b/src/teamspeak.rs @@ -1,14 +1,20 @@ +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + use futures::compat::Future01CompatExt; use futures01::{future::Future, sink::Sink}; use tokio02::sync::mpsc::UnboundedSender; -use crate::{ApplicationMessage, Message}; -use std::sync::{Arc, Mutex}; use tsclientlib::Event::ConEvents; -use tsclientlib::{events::Event, ClientId, ConnectOptions, Connection, MessageTarget}; +use tsclientlib::{ + events::Event, ChannelId, ClientId, ConnectOptions, Connection, DisconnectOptions, + MessageTarget, Reason, +}; use log::error; +use crate::bot::{Message, MusicBotMessage}; + pub struct TeamSpeakConnection { conn: Connection, } @@ -30,7 +36,7 @@ fn get_message<'a>(event: &Event) -> Option { impl TeamSpeakConnection { pub async fn new( - tx: Arc>>, + tx: Arc>>, options: ConnectOptions, ) -> Result { let conn = Connection::new(options).compat().await?; @@ -44,7 +50,7 @@ impl TeamSpeakConnection { for event in *events { if let Some(msg) = get_message(event) { let tx = tx.lock().unwrap(); - tx.send(ApplicationMessage::TextMessage(msg)).unwrap(); + tx.send(MusicBotMessage::TextMessage(msg)).unwrap(); } } } @@ -72,23 +78,34 @@ impl TeamSpeakConnection { tokio::run(send_packet); } - pub fn join_channel_of_user(&self, id: ClientId) { - let channel = self - .conn - .lock() - .clients - .get(&id) - .expect("can find poke sender") - .channel; - tokio::spawn( - self.conn - .lock() - .to_mut() - .get_client(&self.conn.lock().own_client) - .expect("can get myself") - .set_channel(channel) - .map_err(|e| error!("Failed to switch channel: {}", e)), - ); + pub fn channel_path_of_user(&self, id: ClientId) -> String { + let conn = self.conn.lock(); + + let channel_id = conn.clients.get(&id).expect("can find poke sender").channel; + + let mut channel = conn + .channels + .get(&channel_id) + .expect("can find user channel"); + + let mut names = vec![&channel.name[..]]; + + // Channel 0 is the root channel + while channel.parent != ChannelId(0) { + names.push("/"); + channel = conn + .channels + .get(&channel.parent) + .expect("can find user channel"); + names.push(&channel.name); + } + + let mut path = String::new(); + while let Some(name) = names.pop() { + path.push_str(name); + } + + path } pub fn set_nickname(&self, name: &str) { @@ -122,4 +139,19 @@ impl TeamSpeakConnection { .map_err(|e| error!("Failed to send message: {}", e)), ); } + + pub fn disconnect(&self, reason: &str) { + let opt = DisconnectOptions::new() + .reason(Reason::Clientdisconnect) + .message(reason); + tokio::spawn( + self.conn + .disconnect(opt) + .map_err(|e| error!("Failed to send message: {}", e)), + ); + // Might or might not be required to keep tokio running while the bot disconnects + tokio::spawn( + tokio::timer::Delay::new(Instant::now() + Duration::from_secs(1)).map_err(|_| ()), + ); + } } -- cgit v1.2.3-70-g09d2 From 32686ba4a31ecf7c9aedad65c1a6be0be7d96ea2 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 25 Jan 2020 19:51:15 +0100 Subject: Let the music bot leave once its channel is empty --- src/bot/master.rs | 6 ++--- src/bot/music.rs | 42 +++++++++++++++++++++++++++++++-- src/teamspeak.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 106 insertions(+), 11 deletions(-) (limited to 'src/teamspeak.rs') diff --git a/src/bot/master.rs b/src/bot/master.rs index 641938a..007abea 100644 --- a/src/bot/master.rs +++ b/src/bot/master.rs @@ -1,10 +1,10 @@ +use std::collections::HashMap; use std::future::Future; use std::sync::{Arc, Mutex}; -use std::collections::HashMap; use futures::future::{FutureExt, TryFutureExt}; use futures01::future::Future as Future01; -use log::{info}; +use log::info; use serde::{Deserialize, Serialize}; use tsclientlib::{ClientId, ConnectOptions, Identity, MessageTarget}; @@ -13,7 +13,7 @@ use crate::teamspeak::TeamSpeakConnection; use crate::Args; -use crate::bot::{MusicBot, MusicBotMessage, MusicBotArgs}; +use crate::bot::{MusicBot, MusicBotArgs, MusicBotMessage}; pub struct MasterBot { config: MasterConfig, diff --git a/src/bot/music.rs b/src/bot/music.rs index 4d67f88..94e7350 100644 --- a/src/bot/music.rs +++ b/src/bot/music.rs @@ -6,9 +6,9 @@ use std::thread; use log::{debug, info}; use structopt::StructOpt; use tokio02::sync::mpsc::UnboundedSender; -use tsclientlib::{ClientId, ConnectOptions, Identity, Invoker, MessageTarget}; +use tsclientlib::{data, ChannelId, ClientId, ConnectOptions, Identity, Invoker, MessageTarget}; -use crate::audio_player::{AudioPlayerError, AudioPlayer, PollResult}; +use crate::audio_player::{AudioPlayer, AudioPlayerError, PollResult}; use crate::command::Command; use crate::playlist::Playlist; use crate::teamspeak::TeamSpeakConnection; @@ -32,6 +32,14 @@ pub enum State { #[derive(Debug)] pub enum MusicBotMessage { TextMessage(Message), + ClientChannel { + client: ClientId, + old_channel: ChannelId, + }, + ClientDisconnected { + id: ClientId, + client: data::Client, + }, StateChange(State), Quit(String), } @@ -179,6 +187,20 @@ impl MusicBot { } } + fn my_channel(&self) -> ChannelId { + self.teamspeak + .as_ref() + .map(|ts| ts.my_channel()) + .expect("my_channel needs ts") + } + + fn user_count(&self, channel: ChannelId) -> u32 { + self.teamspeak + .as_ref() + .map(|ts| ts.user_count(channel)) + .expect("user_count needs ts") + } + fn send_message(&self, text: &str) { debug!("Sending message to TeamSpeak: {}", text); @@ -307,6 +329,22 @@ impl MusicBot { self.on_text(message).await?; } } + MusicBotMessage::ClientChannel { + client: _, + old_channel, + } => { + let my_channel = self.my_channel(); + if old_channel == my_channel && self.user_count(my_channel) <= 1 { + self.quit(String::from("Channel is empty")); + } + } + MusicBotMessage::ClientDisconnected { id: _, client } => { + let old_channel = client.channel; + let my_channel = self.my_channel(); + if old_channel == my_channel && self.user_count(my_channel) <= 1 { + self.quit(String::from("Channel is empty")); + } + } MusicBotMessage::StateChange(state) => { self.on_state(state)?; } diff --git a/src/teamspeak.rs b/src/teamspeak.rs index f1abaec..f49f7a6 100644 --- a/src/teamspeak.rs +++ b/src/teamspeak.rs @@ -19,17 +19,53 @@ pub struct TeamSpeakConnection { conn: Connection, } -fn get_message<'a>(event: &Event) -> Option { +fn get_message<'a>(event: &Event) -> Option { + use tsclientlib::events::{PropertyId, PropertyValue}; + match event { Event::Message { from: target, invoker: sender, message: msg, - } => Some(Message { - target: target.clone(), + } => Some(MusicBotMessage::TextMessage(Message { + target: *target, invoker: sender.clone(), text: msg.clone(), - }), + })), + Event::PropertyChanged { + id: property, + old: from, + invoker: _, + } => match property { + PropertyId::ClientChannel(client) => { + if let PropertyValue::ChannelId(from) = from { + Some(MusicBotMessage::ClientChannel { + client: *client, + old_channel: *from, + }) + } else { + None + } + } + _ => None, + }, + Event::PropertyRemoved { + id: property, + old: client, + invoker: _, + } => match property { + PropertyId::Client(id) => { + if let PropertyValue::Client(client) = client { + Some(MusicBotMessage::ClientDisconnected { + id: *id, + client: client.clone(), + }) + } else { + None + } + } + _ => None, + }, _ => None, } } @@ -49,8 +85,9 @@ impl TeamSpeakConnection { if let ConEvents(_conn, events) = e { for event in *events { if let Some(msg) = get_message(event) { - let tx = tx.lock().unwrap(); - tx.send(MusicBotMessage::TextMessage(msg)).unwrap(); + let tx = tx.lock().expect("Mutex was not poisoned"); + // Ignore the result because the receiver might get dropped first. + let _ = tx.send(msg); } } } @@ -108,6 +145,26 @@ impl TeamSpeakConnection { path } + pub fn my_channel(&self) -> ChannelId { + let conn = self.conn.lock(); + conn.clients + .get(&conn.own_client) + .expect("can find myself") + .channel + } + + pub fn user_count(&self, channel: ChannelId) -> u32 { + let conn = self.conn.lock(); + let mut count = 0; + for (_, client) in &conn.clients { + if client.channel == channel { + count += 1; + } + } + + count + } + pub fn set_nickname(&self, name: &str) { tokio::spawn( self.conn -- cgit v1.2.3-70-g09d2 From af0a1b81707536caf4e498d86912f65981342072 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sun, 26 Jan 2020 06:58:42 +0100 Subject: Randomly choose from lists of identities and names --- Cargo.lock | 19 +++++-- Cargo.toml | 1 + src/bot/master.rs | 163 ++++++++++++++++++++++++++++-------------------------- src/bot/music.rs | 19 ++++--- src/main.rs | 23 +++----- src/teamspeak.rs | 10 ++++ 6 files changed, 130 insertions(+), 105 deletions(-) (limited to 'src/teamspeak.rs') diff --git a/Cargo.lock b/Cargo.lock index 90cca45..8d57a14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1786,6 +1786,7 @@ dependencies = [ "gstreamer-audio", "log", "log4rs", + "rand 0.7.3", "serde", "serde_json", "structopt", @@ -1914,22 +1915,23 @@ dependencies = [ "rand_isaac", "rand_jitter", "rand_os", - "rand_pcg", + "rand_pcg 0.1.2", "rand_xorshift", "winapi 0.3.8", ] [[package]] name = "rand" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ "getrandom", "libc", "rand_chacha 0.2.1", "rand_core 0.5.1", "rand_hc 0.2.0", + "rand_pcg 0.2.1", ] [[package]] @@ -2038,6 +2040,15 @@ dependencies = [ "rand_core 0.4.2", ] +[[package]] +name = "rand_pcg" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" +dependencies = [ + "rand_core 0.5.1", +] + [[package]] name = "rand_xorshift" version = "0.1.1" @@ -2569,7 +2580,7 @@ checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ "cfg-if", "libc", - "rand 0.7.2", + "rand 0.7.3", "redox_syscall", "remove_dir_all", "winapi 0.3.8", diff --git a/Cargo.toml b/Cargo.toml index 6622793..34726ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,4 @@ gstreamer-audio = "0.15.0" byte-slice-cast = "0.3.5" serde_json = "1.0.44" serde = "1.0.104" +rand = { version = "0.7.3", features = ["small_rng"] } diff --git a/src/bot/master.rs b/src/bot/master.rs index 007abea..1f1ddfb 100644 --- a/src/bot/master.rs +++ b/src/bot/master.rs @@ -5,6 +5,7 @@ use std::sync::{Arc, Mutex}; use futures::future::{FutureExt, TryFutureExt}; use futures01::future::Future as Future01; use log::info; +use rand::{rngs::SmallRng, seq::SliceRandom, SeedableRng}; use serde::{Deserialize, Serialize}; use tsclientlib::{ClientId, ConnectOptions, Identity, MessageTarget}; @@ -16,8 +17,11 @@ use crate::Args; use crate::bot::{MusicBot, MusicBotArgs, MusicBotMessage}; pub struct MasterBot { - config: MasterConfig, - teamspeak: Option>, + config: Arc, + rng: Arc>, + available_names: Arc>>, + available_ids: Arc>>, + teamspeak: Arc, connected_bots: Arc>>>, } @@ -25,44 +29,42 @@ impl MasterBot { pub async fn new(args: MasterArgs) -> (Arc, impl Future) { let (tx, mut rx) = tokio02::sync::mpsc::unbounded_channel(); let tx = Arc::new(Mutex::new(tx)); - let connection = if args.local { - info!("Starting in CLI mode"); - - None - } else { - info!("Starting in TeamSpeak mode"); - - let mut con_config = ConnectOptions::new(args.address.clone()) - .version(tsclientlib::Version::Linux_3_3_2) - .name(args.name.clone()) - .identity(args.id) - .log_commands(args.verbose >= 1) - .log_packets(args.verbose >= 2) - .log_udp_packets(args.verbose >= 3); - - if let Some(channel) = args.channel { - con_config = con_config.channel(channel); - } - - let connection = Arc::new( - TeamSpeakConnection::new(tx.clone(), con_config) - .await - .unwrap(), - ); + info!("Starting in TeamSpeak mode"); + + let mut con_config = ConnectOptions::new(args.address.clone()) + .version(tsclientlib::Version::Linux_3_3_2) + .name(args.master_name.clone()) + .identity(args.id) + .log_commands(args.verbose >= 1) + .log_packets(args.verbose >= 2) + .log_udp_packets(args.verbose >= 3); + + if let Some(channel) = args.channel { + con_config = con_config.channel(channel); + } - Some(connection) - }; + let connection = Arc::new( + TeamSpeakConnection::new(tx.clone(), con_config) + .await + .unwrap(), + ); - let config = MasterConfig { - name: args.name, + let config = Arc::new(MasterConfig { + master_name: args.master_name, address: args.address, - bots: args.bots, + names: args.names, + ids: args.ids, local: args.local, verbose: args.verbose, - }; + }); + let name_count = config.names.len(); + let id_count = config.ids.len(); let bot = Arc::new(Self { config, + rng: Arc::new(Mutex::new(SmallRng::from_entropy())), + available_names: Arc::new(Mutex::new((0..name_count).collect())), + available_ids: Arc::new(Mutex::new((0..id_count).collect())), teamspeak: connection, connected_bots: Arc::new(Mutex::new(HashMap::new())), }); @@ -80,28 +82,62 @@ impl MasterBot { } async fn spawn_bot(&self, id: ClientId) { - let channel = if let Some(ts) = &self.teamspeak { - ts.channel_path_of_user(id) - } else { - String::from("local") + let channel = self.teamspeak.channel_path_of_user(id); + + let (name, name_index) = { + let mut available_names = self.available_names.lock().expect("Mutex was not poisoned"); + let mut rng = self.rng.lock().expect("Mutex was not poisoned"); + available_names.shuffle(&mut *rng); + let name_index = match available_names.pop() { + Some(v) => v, + None => { + self.teamspeak.send_message_to_user( + id, + "Out of names. Too many bots are already connected!", + ); + return; + } + }; + + (self.config.names[name_index].clone(), name_index) }; - let preset = self.config.bots[0].clone(); - let name = format!("{}({})", preset.name, self.config.name); + let (id, id_index) = { + let mut available_ids = self.available_ids.lock().expect("Mutex was not poisoned"); + let mut rng = self.rng.lock().expect("Mutex was not poisoned"); + available_ids.shuffle(&mut *rng); + let id_index = match available_ids.pop() { + Some(v) => v, + None => { + self.teamspeak.send_message_to_user( + id, + "Out of identities. Too many bots are already connected!", + ); + return; + } + }; + + (self.config.ids[id_index].clone(), id_index) + }; let cconnected_bots = self.connected_bots.clone(); - let disconnect_cb = Box::new(move |n| { + let cavailable_names = self.available_names.clone(); + let cavailable_ids = self.available_ids.clone(); + 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); }); info!("Connecting to {} on {}", channel, self.config.address); let bot_args = MusicBotArgs { name: name.clone(), - owner: preset.owner, + name_index, + id_index, local: self.config.local, address: self.config.address.clone(), - id: preset.id, + id, channel, verbose: self.config.verbose, disconnect_cb, @@ -128,15 +164,16 @@ impl MasterBot { #[derive(Debug, Serialize, Deserialize)] pub struct MasterArgs { #[serde(default = "default_name")] - pub name: String, + pub master_name: String, #[serde(default = "default_local")] pub local: bool, pub address: String, pub channel: Option, #[serde(default = "default_verbose")] pub verbose: u8, + pub names: Vec, pub id: Identity, - pub bots: Vec, + pub ids: Vec, } fn default_name() -> String { @@ -163,8 +200,9 @@ impl MasterArgs { }; Self { - name: self.name, - bots: self.bots, + master_name: self.master_name, + names: self.names, + ids: self.ids, local, address, id: self.id, @@ -175,39 +213,10 @@ impl MasterArgs { } pub struct MasterConfig { - pub name: String, + pub master_name: String, pub address: String, - pub bots: Vec, + pub names: Vec, + pub ids: Vec, pub local: bool, pub verbose: u8, } - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct BotConfig { - pub name: String, - #[serde( - deserialize_with = "client_id_deserialize", - serialize_with = "client_id_serialize" - )] - pub owner: Option, - pub id: Identity, -} - -fn client_id_serialize(c: &Option, s: S) -> Result -where - S: serde::Serializer, -{ - match c { - Some(c) => s.serialize_some(&c.0), - None => s.serialize_none(), - } -} - -fn client_id_deserialize<'de, D>(deserializer: D) -> Result, D::Error> -where - D: serde::Deserializer<'de>, -{ - let id: Option = Deserialize::deserialize(deserializer)?; - - Ok(id.map(|id| ClientId(id))) -} diff --git a/src/bot/music.rs b/src/bot/music.rs index 94e7350..fd1a7da 100644 --- a/src/bot/music.rs +++ b/src/bot/music.rs @@ -54,13 +54,14 @@ pub struct MusicBot { pub struct MusicBotArgs { pub name: String, - pub owner: Option, + pub name_index: usize, + pub id_index: usize, pub local: bool, pub address: String, pub id: Identity, pub channel: String, pub verbose: u8, - pub disconnect_cb: Box, + pub disconnect_cb: Box, } impl MusicBot { @@ -77,7 +78,7 @@ impl MusicBot { let con_config = ConnectOptions::new(args.address) .version(tsclientlib::Version::Linux_3_3_2) - .name(args.name.clone()) + .name(format!("🎵 {}", args.name)) .identity(args.id) .log_commands(args.verbose >= 1) .log_packets(args.verbose >= 2) @@ -122,12 +123,14 @@ impl MusicBot { let cbot = bot.clone(); let mut disconnect_cb = args.disconnect_cb; let name = args.name; + let name_index = args.name_index; + let id_index = args.id_index; let msg_loop = async move { 'outer: loop { while let Some(msg) = rx.recv().await { if let MusicBotMessage::Quit(reason) = msg { cbot.with_teamspeak(|ts| ts.disconnect(&reason)); - disconnect_cb(name); + disconnect_cb(name, name_index, id_index); break 'outer; } cbot.on_message(msg).await.unwrap(); @@ -294,13 +297,13 @@ impl MusicBot { if *current_state != state { match state { State::Playing => { - self.set_nickname(&format!("{} - Playing", self.name)); + self.set_nickname(&format!("🎵 {} - Playing", self.name)); } State::Paused => { - self.set_nickname(&format!("{} - Paused", self.name)); + self.set_nickname(&format!("🎵 {} - Paused", self.name)); } State::Stopped => { - self.set_nickname(&self.name); + self.set_nickname(&format!("🎵 {}", self.name)); self.set_description(""); } State::EndOfStream => { @@ -310,7 +313,7 @@ impl MusicBot { self.start_playing_audio(request); } else { - self.set_nickname(&self.name); + self.set_nickname(&format!("🎵 {}", self.name)); self.set_description(""); } } diff --git a/src/main.rs b/src/main.rs index bff40f8..ae1bed7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod playlist; mod teamspeak; mod youtube_dl; -use bot::{BotConfig, MasterArgs, MasterBot}; +use bot::{MasterArgs, MasterBot}; #[derive(StructOpt, Debug)] #[structopt(raw(global_settings = "&[AppSettings::ColoredHelp]"))] @@ -61,22 +61,18 @@ pub struct Args { } fn main() { - //let example = BotConfig { - //name: String::from("asd"), - //id: Identity::create().unwrap(), - //owner: Some(ClientId(12)), - //}; - //let bots = vec![example]; + //let ids = vec![Identity::create().unwrap()]; //println!( //"{}", //toml::to_string(&MasterArgs { - //name: String::from("PokeBot"), + //master_name: String::from("PokeBot"), //id: Identity::create().unwrap(), + //names: vec![String::from("test")], //address: String::from("localhost"), //channel: Some(String::from("Poke If Needed")), //local: false, //verbose: 0, - //bots, + //ids, //}) //.map_err(|e| panic!(e.to_string())) //.unwrap() @@ -102,14 +98,9 @@ fn run() -> Result<(), Box> { let mut config: MasterArgs = toml::from_str(&toml)?; if let Some(count) = args.gen_id_count { - for i in 0..count { + for _ in 0..count { let id = Identity::create().expect("Failed to create id"); - let bot = BotConfig { - name: format!("{}", i), - owner: None, - id, - }; - config.bots.push(bot); + config.ids.push(id); } let toml = toml::to_string(&config)?; diff --git a/src/teamspeak.rs b/src/teamspeak.rs index f49f7a6..5dd80ba 100644 --- a/src/teamspeak.rs +++ b/src/teamspeak.rs @@ -197,6 +197,16 @@ impl TeamSpeakConnection { ); } + pub fn send_message_to_user(&self, id: ClientId, text: &str) { + tokio::spawn( + self.conn + .lock() + .to_mut() + .send_message(MessageTarget::Client(id), text) + .map_err(|e| error!("Failed to send message: {}", e)), + ); + } + pub fn disconnect(&self, reason: &str) { let opt = DisconnectOptions::new() .reason(Reason::Clientdisconnect) -- cgit v1.2.3-70-g09d2 From 985d6bd787c07ea65804d5c079537fb8d805075f Mon Sep 17 00:00:00 2001 From: Jokler Date: Tue, 28 Jan 2020 03:04:58 +0100 Subject: Block bots from joining channels with other bots --- src/bot/master.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++----- src/bot/music.rs | 6 +++++- src/teamspeak.rs | 10 +++++++--- 3 files changed, 56 insertions(+), 9 deletions(-) (limited to 'src/teamspeak.rs') 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 { + Some(self.conn.lock().clients.get(&id)?.channel) + } + + pub fn channel_path_of_user(&self, id: ClientId) -> Option { 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 { -- cgit v1.2.3-70-g09d2