summary refs log tree commit diff
path: root/src/system/mod.rs
blob: 4179e3bb453b1494b02648c7ef32dd0d578c69b7 (plain)
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
use std::{collections::HashMap, str::FromStr, time::Duration};

use tokio::{
    sync::mpsc::{channel, Sender},
    time::sleep,
};
use twilight_model::{
    channel::{
        Message,
    },
    id::{marker::UserMarker, Id},
};
use twilight_model::util::Timestamp;

use crate::config::{AutoproxyConfig, AutoproxyLatchScope, Member};

mod aggregator;
mod bot;
mod types;
use aggregator::MessageAggregator;
use bot::Bot;
pub use types::*;

use self::bot::MessageDuplicateError;

pub struct Manager {
    pub name: String,
    pub config: crate::config::System,
    pub bots: HashMap<MemberId, Bot>,
    pub latch_state: Option<(MemberId, Timestamp)>,
    pub system_sender: Option<Sender<SystemEvent>>,
}

impl Manager {
    pub fn new(system_name: String, system_config: crate::config::System) -> Self {
        Self {
            name: system_name,
            config: system_config,
            bots: HashMap::new(),
            latch_state: None,
            system_sender: None,
        }
    }

    pub fn find_member_by_name<'a>(
        &'a self,
        name: &String,
    ) -> Option<(MemberId, &'a crate::config::Member)> {
        self.config
            .members
            .iter()
            .enumerate()
            .find(|(_member_id, member)| member.name == *name)
    }

    pub fn find_member_by_id<'a>(&'a self, id: MemberId) -> Option<&'a Member> {
        self.config
            .members
            .iter()
            .enumerate()
            .find(|(member_id, _)| *member_id == id)
            .map_or(None, |(_member_id, member)| Some(member))
    }

    pub async fn start_clients(&mut self) {
        println!("Starting clients for system {}", self.name);

        let reference_user_id: Id<UserMarker> =
            Id::from_str(self.config.reference_user_id.as_str())
                .expect(format!("Invalid user ID: {}", self.config.reference_user_id).as_str());

        let (system_sender, mut system_receiver) = channel::<SystemEvent>(100);
        self.system_sender = Some(system_sender.clone());
        let mut aggregator = MessageAggregator::new();
        aggregator.set_handler(system_sender.clone());

        for (member_id, member) in self.config.members.iter().enumerate() {
            // Create gateway listener
            let mut bot = Bot::new(member_id, &member, reference_user_id);

            bot.set_message_handler(aggregator.get_sender());
            bot.set_system_handler(system_sender.clone());

            // Start gateway listener
            bot.start_listening();
            self.bots.insert(member_id, bot);
        }

        aggregator.start();

        let mut num_connected = 0;

        loop {
            match system_receiver.recv().await {
                Some(event) => match event {
                    SystemEvent::GatewayConnected(member_id) => {
                        let member = self
                            .find_member_by_id(member_id)
                            .expect("Could not find member");

                        num_connected += 1;
                        println!(
                            "Gateway client {} ({}) connected",
                            num_connected, member.name
                        );

                        if num_connected == self.config.members.len() {
                            let system_sender = system_sender.clone();
                            tokio::spawn(async move {
                                println!("All gateways connected");
                                sleep(Duration::from_secs(5)).await;
                                let _ = system_sender.send(SystemEvent::AllGatewaysConnected).await;
                            });
                        }
                    }
                    SystemEvent::GatewayClosed(member_id) => {
                        let member = self
                            .find_member_by_id(member_id)
                            .expect("Could not find member");

                        println!("Gateway client {} closed", member.name);

                        num_connected -= 1;
                    }
                    SystemEvent::NewMessage((event_time, message)) => {
                        self.handle_message(message, event_time).await;
                    }
                    SystemEvent::GatewayError(member_id, message) => {
                        let member = self
                            .find_member_by_id(member_id)
                            .expect("Could not find member");
                        println!("Gateway client {} ran into error {}", member.name, message);
                        return;
                    }
                    SystemEvent::AutoproxyTimeout(time_scheduled) => {
                        if let Some((_member, current_last_message)) = self.latch_state.clone() {
                            if current_last_message == time_scheduled {
                                println!("Autoproxy timeout has expired: {} (last sent), {} (timeout scheduled)", current_last_message.as_secs(), time_scheduled.as_secs());
                                self.latch_state = None;
                                self.update_status_of_system().await;
                            }
                        }
                    }
                    SystemEvent::AllGatewaysConnected => {
                        println!(
                            "Attempting to set startup status for system {}",
                            self.name.clone()
                        );
                        self.update_status_of_system().await;
                    }
                    _ => (),
                },
                None => return,
            }
        }
    }

    async fn handle_message(&mut self, message: Message, timestamp: Timestamp) {
        // TODO: Commands
        if message.content.eq("!panic") {
            panic!("Exiting due to user command");
        }

        // Escape sequence
        if message.content.starts_with(r"\") {
            if message.content == r"\\" {
                let bot = if let Some((current_member, _)) = self.latch_state.clone() {
                    self.bots
                        .get(&current_member)
                        .expect(format!("No client for member {}", current_member).as_str())
                } else {
                    self.bots.iter().next().expect("No clients!").1
                };

                // We don't really care about the outcome here, we don't proxy afterwards
                let _ = bot.delete_message(message.channel_id, message.id).await;
                self.latch_state = None
            } else if message.content.starts_with(r"\\") {
                self.latch_state = None;
            }

            return;
        }

        // TODO: Non-latching prefixes maybe?

        // Check for prefix
        let match_prefix =
            self.config
                .members
                .iter()
                .enumerate()
                .find_map(|(member_id, member)| {
                    Some((member_id, member.matches_proxy_prefix(&message)?))
                });
        if let Some((member_id, matched_content)) = match_prefix {
            if let Ok(_) = self.proxy_message(&message, member_id, matched_content).await {
                self.latch_state = Some((member_id, timestamp));
                self.update_autoproxy_state_after_message(member_id, timestamp);
                self.update_status_of_system().await;
            }
            return
        }

        // Check for autoproxy
        if let Some(autoproxy_config) = &self.config.autoproxy {
            match autoproxy_config {
                AutoproxyConfig::Member { name } => {
                    let (member_id, _member) = self
                        .find_member_by_name(&name)
                        .expect("Invalid autoproxy member name");
                    self.proxy_message(&message, member_id, message.content.as_str())
                        .await;
                }
                // TODO: Do something with the latch scope
                // TODO: Do something with presence setting
                AutoproxyConfig::Latch {
                    scope,
                    timeout_seconds,
                    presence_indicator,
                } => {
                    if let Some((member, last_timestamp)) = self.latch_state.clone() {
                        let time_since_last = timestamp.as_secs() - last_timestamp.as_secs();
                        if time_since_last <= (*timeout_seconds).into() {
                            if let Ok(_) = self.proxy_message(&message, member, message.content.as_str()).await {
                                self.latch_state = Some((member, timestamp));
                                self.update_autoproxy_state_after_message(member, timestamp);
                                self.update_status_of_system().await;
                            }
                        }
                    }
                }
            }
        }
    }

    async fn proxy_message(&self, message: &Message, member: MemberId, content: &str) -> Result<(), ()> {
        let bot = self.bots.get(&member).expect("No client for member");

        let duplicate_result = bot.duplicate_message(message, content).await;

        if duplicate_result.is_err() {
            return Err(())
        }

        // Try to delete message first as that fails more often
        let delete_result = bot.delete_message(message.channel_id, message.id).await;

        if delete_result.is_err() {
            // Delete the duplicated message if that failed
            let _ = bot.delete_message(message.channel_id, duplicate_result.unwrap().id).await;
            return Err(())
        }

        Ok(())
    }

    fn update_autoproxy_state_after_message(&mut self, member: MemberId, timestamp: Timestamp) {
        match &self.config.autoproxy {
            None => (),
            Some(AutoproxyConfig::Member { name: _ }) => (),
            Some(AutoproxyConfig::Latch {
                scope,
                timeout_seconds,
                presence_indicator: _,
            }) => {
                self.latch_state = Some((member, timestamp));

                if let Some(channel) = self.system_sender.clone() {
                    let last_message = timestamp.clone();
                    let timeout_seconds = timeout_seconds.clone();

                    tokio::spawn(async move {
                        sleep(Duration::from_secs(timeout_seconds.into())).await;
                        channel
                            .send(SystemEvent::AutoproxyTimeout(last_message))
                            .await
                            .expect("Channel has closed");
                    });
                }
            }
        }
    }

    async fn update_status_of_system(&mut self) {
        let member_states: Vec<(MemberId, Status)> = self
            .config
            .members
            .iter()
            .enumerate()
            .map(|(member_id, member)| {
                (
                    member_id,
                    match &self.config.autoproxy {
                        None => Status::Invisible,
                        Some(AutoproxyConfig::Member { name }) => {
                            if member.name == *name {
                                Status::Online
                            } else {
                                Status::Invisible
                            }
                        }
                        Some(AutoproxyConfig::Latch {
                            scope,
                            timeout_seconds: _,
                            presence_indicator,
                        }) => {
                            if let AutoproxyLatchScope::Server = scope {
                                Status::Invisible
                            } else if !presence_indicator {
                                Status::Invisible
                            } else {
                                match &self.latch_state {
                                    Some((latch_member, _last_timestamp)) => {
                                        if member_id == *latch_member {
                                            Status::Online
                                        } else {
                                            Status::Invisible
                                        }
                                    }
                                    None => Status::Invisible,
                                }
                            }
                        }
                    },
                )
            })
            .collect();

        for (member, status) in member_states {
            self.update_status_of_member(member, status).await;
        }
    }

    async fn update_status_of_member(&mut self, member: MemberId, status: Status) {
        let bot = self.bots.get(&member).expect("No client for member");
        bot.set_status(status).await;
    }
}

impl crate::config::Member {
    pub fn matches_proxy_prefix<'a>(&self, message: &'a Message) -> Option<&'a str> {
        match self.message_pattern.captures(message.content.as_str()) {
            None => None,
            Some(captures) => match captures.name("content") {
                None => None,
                Some(matched_content) => Some(matched_content.as_str()),
            },
        }
    }
}