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
|
use std::collections::HashMap;
use std::future::Future;
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};
use crate::audio_player::AudioPlayerError;
use crate::teamspeak::TeamSpeakConnection;
use crate::Args;
use crate::bot::{MusicBot, MusicBotArgs, MusicBotMessage};
pub struct MasterBot {
config: Arc<MasterConfig>,
music_bots: Arc<Mutex<MusicBots>>,
teamspeak: Arc<TeamSpeakConnection>,
}
struct MusicBots {
rng: SmallRng,
available_names: Vec<usize>,
available_ids: Vec<usize>,
connected_bots: HashMap<String, Arc<MusicBot>>,
}
impl MasterBot {
pub async fn new(args: MasterArgs) -> (Arc<Self>, impl Future) {
let (tx, mut rx) = tokio02::sync::mpsc::unbounded_channel();
let tx = Arc::new(Mutex::new(tx));
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);
}
let connection = Arc::new(
TeamSpeakConnection::new(tx.clone(), con_config)
.await
.unwrap(),
);
let config = Arc::new(MasterConfig {
master_name: args.master_name,
address: args.address,
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 music_bots = Arc::new(Mutex::new(MusicBots {
rng: SmallRng::from_entropy(),
available_names: (0..name_count).collect(),
available_ids: (0..id_count).collect(),
connected_bots: HashMap::new(),
}));
let bot = Arc::new(Self {
config,
music_bots,
teamspeak: connection,
});
bot.teamspeak
.set_description("Poke me if you want a music bot!");
let cbot = bot.clone();
let msg_loop = async move {
loop {
while let Some(msg) = rx.recv().await {
cbot.on_message(msg).await.unwrap();
}
}
};
(bot, msg_loop)
}
fn build_bot_args_for(&self, id: ClientId) -> Option<MusicBotArgs> {
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 None;
}
let MusicBots {
ref mut rng,
ref mut available_names,
ref mut available_ids,
ref connected_bots,
} = &mut *self.music_bots.lock().expect("Mutex was not poisoned");
for (_, bot) in connected_bots {
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 None;
}
}
let channel_path = self
.teamspeak
.channel_path_of_user(id)
.expect("can find poke sender");
available_names.shuffle(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 None;
}
};
let name = self.config.names[name_index].clone();
available_ids.shuffle(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 None;
}
};
let id = self.config.ids[id_index].clone();
let cmusic_bots = self.music_bots.clone();
let disconnect_cb = Box::new(move |n, name_index, id_index| {
let mut music_bots = cmusic_bots.lock().expect("Mutex was not poisoned");
music_bots.connected_bots.remove(&n);
music_bots.available_names.push(name_index);
music_bots.available_ids.push(id_index);
});
info!("Connecting to {} on {}", channel_path, self.config.address);
Some(MusicBotArgs {
name,
name_index,
id_index,
local: self.config.local,
address: self.config.address.clone(),
id,
channel: channel_path,
verbose: self.config.verbose,
disconnect_cb,
})
}
async fn spawn_bot_for(&self, id: ClientId) {
if let Some(bot_args) = self.build_bot_args_for(id) {
let (bot, fut) = MusicBot::new(bot_args).await;
tokio::spawn(fut.unit_error().boxed().compat().map(|_| ()));
let mut music_bots = self.music_bots.lock().expect("Mutex was not poisoned");
music_bots
.connected_bots
.insert(bot.name().to_string(), bot);
}
}
async fn on_message(&self, message: MusicBotMessage) -> Result<(), AudioPlayerError> {
if let MusicBotMessage::TextMessage(message) = message {
if let MessageTarget::Poke(who) = message.target {
info!("Poked by {}, creating bot for their channel", who);
self.spawn_bot_for(who).await;
}
}
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MasterArgs {
#[serde(default = "default_name")]
pub master_name: String,
#[serde(default = "default_local")]
pub local: bool,
pub address: String,
pub channel: Option<String>,
#[serde(default = "default_verbose")]
pub verbose: u8,
pub names: Vec<String>,
pub id: Identity,
pub ids: Vec<Identity>,
}
fn default_name() -> String {
String::from("PokeBot")
}
fn default_local() -> bool {
false
}
fn default_verbose() -> u8 {
0
}
impl MasterArgs {
pub fn merge(self, args: Args) -> Self {
let address = args.address.unwrap_or(self.address);
let local = args.local || self.local;
let channel = args.master_channel.or(self.channel);
let verbose = if args.verbose > 0 {
args.verbose
} else {
self.verbose
};
Self {
master_name: self.master_name,
names: self.names,
ids: self.ids,
local,
address,
id: self.id,
channel,
verbose,
}
}
}
pub struct MasterConfig {
pub master_name: String,
pub address: String,
pub names: Vec<String>,
pub ids: Vec<Identity>,
pub local: bool,
pub verbose: u8,
}
|