aboutsummaryrefslogtreecommitdiffstats
path: root/src/bot/master.rs
blob: 2885dfec3e0618c3079c992d8681828b0aed0d8b (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
358
359
360
361
use std::collections::HashMap;

use async_trait::async_trait;
use futures::future;
use rand::{rngs::SmallRng, seq::SliceRandom, SeedableRng};
use serde::{Deserialize, Serialize};
use slog::{error, info, o, trace, Logger};
use tsclientlib::{ClientId, ConnectOptions, Connection, Identity, MessageTarget};
use xtra::{spawn::Tokio, Actor, Address, Context, Handler, Message, WeakAddress};

use crate::audio_player::AudioPlayerError;
use crate::teamspeak::TeamSpeakConnection;

use crate::Args;

use crate::bot::{GetBotData, GetChannel, GetName, MusicBot, MusicBotArgs, MusicBotMessage};

pub struct MasterBot {
    config: MasterConfig,
    my_addr: Option<WeakAddress<Self>>,
    teamspeak: TeamSpeakConnection,
    available_names: Vec<String>,
    available_ids: Vec<Identity>,
    connected_bots: HashMap<String, Address<MusicBot>>,
    rng: SmallRng,
    logger: Logger,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MasterArgs {
    #[serde(default = "default_name")]
    pub master_name: String,
    pub address: String,
    pub channel: Option<String>,
    #[serde(default = "default_verbose")]
    pub verbose: u8,
    pub domain: String,
    pub bind_address: String,
    pub webserver_enable: bool,
    pub names: Vec<String>,
    pub id: Option<Identity>,
    pub ids: Option<Vec<Identity>>,
}

impl MasterBot {
    pub async fn spawn(args: MasterArgs, logger: Logger) -> Address<Self> {
        info!(logger, "Starting in TeamSpeak mode");

        let mut con_config = Connection::build(args.address.clone())
            .logger(logger.clone())
            .version(tsclientlib::Version::Linux_3_3_2)
            .name(args.master_name.clone())
            .identity(args.id.expect("identity should exist"))
            .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 = TeamSpeakConnection::new(logger.clone()).await.unwrap();
        trace!(logger, "Created teamspeak connection");

        let config = MasterConfig {
            master_name: args.master_name,
            address: args.address,
            verbose: args.verbose,
        };

        let bot_addr = Self {
            config,
            my_addr: None,
            teamspeak: connection,
            logger: logger.clone(),
            rng: SmallRng::from_entropy(),
            available_names: args.names,
            available_ids: args.ids.expect("identities"),
            connected_bots: HashMap::new(),
        }
        .create(None)
        .spawn(&mut Tokio::Global);

        bot_addr.send(Connect(con_config)).await.unwrap().unwrap();
        trace!(logger, "Spawned master bot actor");

        bot_addr
    }

    async fn bot_args_for_client(
        &mut self,
        user_id: ClientId,
    ) -> Result<MusicBotArgs, BotCreationError> {
        let channel = match self.teamspeak.channel_of_user(user_id).await {
            Some(channel) => channel,
            None => return Err(BotCreationError::UnfoundUser),
        };

        if channel == self.teamspeak.current_channel().await.unwrap() {
            return Err(BotCreationError::MasterChannel(
                self.config.master_name.clone(),
            ));
        }

        for bot in self.connected_bots.values() {
            if bot.send(GetChannel).await.unwrap() == Some(channel) {
                return Err(BotCreationError::MultipleBots(
                    bot.send(GetName).await.unwrap(),
                ));
            }
        }

        let channel_path = self
            .teamspeak
            .channel_path_of_user(user_id)
            .await
            .expect("can find poke sender");

        self.available_names.shuffle(&mut self.rng);
        let name = match self.available_names.pop() {
            Some(v) => v,
            None => {
                return Err(BotCreationError::OutOfNames);
            }
        };

        self.available_ids.shuffle(&mut self.rng);
        let identity = match self.available_ids.pop() {
            Some(v) => v,
            None => {
                return Err(BotCreationError::OutOfIdentities);
            }
        };

        Ok(MusicBotArgs {
            name: name.clone(),
            master: self.my_addr.clone(),
            address: self.config.address.clone(),
            identity,
            local: false,
            channel: channel_path,
            verbose: self.config.verbose,
            logger: self.logger.new(o!("musicbot" => name)),
        })
    }

    async fn spawn_bot_for_client(&mut self, id: ClientId) {
        match self.bot_args_for_client(id).await {
            Ok(bot_args) => {
                let name = bot_args.name.clone();
                let bot = MusicBot::spawn(bot_args).await;
                self.connected_bots.insert(name, bot);
            }
            Err(e) => self.teamspeak.send_message_to_user(id, e.to_string()).await,
        }
    }

    async fn on_message(&mut self, message: MusicBotMessage) -> Result<(), AudioPlayerError> {
        match message {
            MusicBotMessage::TextMessage(message) => {
                if let MessageTarget::Poke(who) = message.target {
                    info!(
                        self.logger,
                        "Poked, creating bot"; "user" => %who
                    );
                    self.spawn_bot_for_client(who).await;
                }
            }
            MusicBotMessage::ClientAdded(id) => {
                if id == self.teamspeak.my_id().await {
                    self.teamspeak
                        .set_description(String::from("Poke me if you want a music bot!"))
                        .await;
                }
            }
            _ => (),
        }

        Ok(())
    }

    pub async fn bot_data(&self, name: String) -> Option<crate::web_server::BotData> {
        let bot = self.connected_bots.get(&name)?;

        bot.send(GetBotData).await.ok()
    }

    pub async fn bot_datas(&self) -> Vec<crate::web_server::BotData> {
        let len = self.connected_bots.len();
        let mut result = Vec::with_capacity(len);
        for bot in self.connected_bots.values() {
            let bot_data = bot.send(GetBotData).await.unwrap();
            result.push(bot_data);
        }

        result
    }

    pub fn bot_names(&self) -> Vec<String> {
        let len = self.connected_bots.len();
        let mut result = Vec::with_capacity(len);
        for name in self.connected_bots.keys() {
            result.push(name.clone());
        }

        result
    }

    fn on_bot_disconnect(&mut self, name: String, id: Identity) {
        self.connected_bots.remove(&name);
        self.available_names.push(name);
        self.available_ids.push(id);
    }

    pub async fn quit(&mut self, reason: String) -> Result<(), tsclientlib::Error> {
        let futures = self
            .connected_bots
            .values()
            .map(|b| b.send(Quit(reason.clone())));
        for res in future::join_all(futures).await {
            if let Err(e) = res {
                error!(self.logger, "Failed to shut down bot"; "error" => %e);
            }
        }
        self.teamspeak.disconnect(&reason).await
    }
}

#[async_trait]
impl Actor for MasterBot {
    async fn started(&mut self, ctx: &mut Context<Self>) {
        self.my_addr = Some(ctx.address().unwrap().downgrade());
    }
}

pub struct Connect(pub ConnectOptions);
impl Message for Connect {
    type Result = Result<(), tsclientlib::Error>;
}

#[async_trait]
impl Handler<Connect> for MasterBot {
    async fn handle(
        &mut self,
        opt: Connect,
        ctx: &mut Context<Self>,
    ) -> Result<(), tsclientlib::Error> {
        let addr = ctx.address().unwrap();
        self.teamspeak.connect_for_bot(opt.0, addr.downgrade())?;
        Ok(())
    }
}

pub struct Quit(pub String);
impl Message for Quit {
    type Result = Result<(), tsclientlib::Error>;
}

#[async_trait]
impl Handler<Quit> for MasterBot {
    async fn handle(&mut self, q: Quit, _: &mut Context<Self>) -> Result<(), tsclientlib::Error> {
        self.quit(q.0).await
    }
}

pub struct BotDisonnected {
    pub name: String,
    pub identity: Identity,
}

impl Message for BotDisonnected {
    type Result = ();
}

#[async_trait]
impl Handler<BotDisonnected> for MasterBot {
    async fn handle(&mut self, dc: BotDisonnected, _: &mut Context<Self>) {
        self.on_bot_disconnect(dc.name, dc.identity);
    }
}

#[async_trait]
impl Handler<MusicBotMessage> for MasterBot {
    async fn handle(
        &mut self,
        msg: MusicBotMessage,
        _: &mut Context<Self>,
    ) -> Result<(), AudioPlayerError> {
        self.on_message(msg).await
    }
}

#[derive(Debug)]
pub enum BotCreationError {
    UnfoundUser,
    MasterChannel(String),
    MultipleBots(String),
    OutOfNames,
    OutOfIdentities,
}

impl std::fmt::Display for BotCreationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use BotCreationError::*;
        match self {
            UnfoundUser => write!(
                f,
                "I can't find you in the channel list, \
                    either I am not subscribed to your channel or this is a bug.",
            ),
            MasterChannel(name) => write!(f, "Joining the channel of \"{}\" is not allowed", name),
            MultipleBots(name) => write!(
                f,
                "\"{}\" is already in this channel. \
                         Multiple bots in one channel are not allowed.",
                name
            ),
            OutOfNames => write!(f, "Out of names. Too many bots are already connected!"),
            OutOfIdentities => write!(f, "Out of identities. Too many bots are already connected!"),
        }
    }
}

fn default_name() -> String {
    String::from("PokeBot")
}

fn default_verbose() -> u8 {
    0
}

impl MasterArgs {
    pub fn merge(self, args: Args) -> Self {
        let address = args.address.unwrap_or(self.address);
        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,
            address,
            domain: self.domain,
            bind_address: self.bind_address,
            webserver_enable: self.webserver_enable,
            id: self.id,
            channel,
            verbose,
        }
    }
}

pub struct MasterConfig {
    pub master_name: String,
    pub address: String,
    pub verbose: u8,
}