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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use crate::security::*;
use crate::types::application::*;
use crate::types::interaction::*;
use crate::types::HttpError;
use crate::types::Snowflake;
use crate::{expect_specific_api_response, expect_successful_api_response_and_return};
use actix_web::http::StatusCode;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Result};
use reqwest::header;
use reqwest::Client;

use log::{debug, error};
use std::fmt;

use ed25519_dalek::PublicKey;

use rustls::ServerConfig;

use std::{collections::HashMap, future::Future, pin::Pin, sync::Mutex};

/// Alias for InteractionResponse
pub type HandlerResponse = InteractionResponse;

type HandlerFunction = fn(
    &mut InteractionHandler,
    Context,
) -> Pin<Box<dyn Future<Output = HandlerResponse> + Send + '_>>;

macro_rules! match_handler_response {
    ($value_name:expr, $response:ident) => {
        match $value_name {
            InteractionResponseType::None => {
                Ok(HttpResponse::build(StatusCode::NO_CONTENT).finish())
            }
            InteractionResponseType::DefferedChannelMessageWithSource
            | InteractionResponseType::DefferedUpdateMessage => {
                /* The use of HTTP code 202 is more appropriate when an Interaction is deffered.
                If an application is first sending a deffered channel message response, this usually means the system
                is still processing whatever it is doing.
                See the spec: https://tools.ietf.org/html/rfc7231#section-6.3.3 */
                Ok(HttpResponse::build(StatusCode::ACCEPTED).json($response))
            }
            _ => {
                // Send out a response to Discord
                let r = HttpResponse::build(StatusCode::OK).json($response);

                Ok(r)
            }
        }
    };
}

#[derive(Clone)]
/// The InteractionHandler is the 'thing' that will handle your incoming interactions.
/// It does interaction validation (as required by Discord) and provides a pre-defined actix-web server
/// with [`InteractionHandler::run`] and [`InteractionHandler::run_ssl`]
pub struct InteractionHandler {
    application_id: Snowflake,

    app_public_key: PublicKey,
    client: Client,

    global_handles: HashMap<&'static str, HandlerFunction>,
    component_handles: HashMap<&'static str, HandlerFunction>,
    guild_handles: HashMap<Snowflake, HandlerFunction>,
}

// Only here to make Debug less generic, so I can send a reference
impl fmt::Debug for InteractionHandler {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        return f
            .debug_struct("InteractionHandler")
            .field("app_public_key", &self.app_public_key)
            .field("global_handles_len", &self.global_handles.len())
            .field("component_handles_len", &self.component_handles.len())
            .finish();
    }
}

impl InteractionHandler {
    /// Initalizes a new `InteractionHandler`
    pub fn new(
        app_id: Snowflake,
        pbk_str: &str,
        token: Option<&'static str>,
    ) -> InteractionHandler {
        let pbk_bytes =
            hex::decode(pbk_str).expect("Failed to parse the public key from hexadecimal");

        let app_public_key =
            PublicKey::from_bytes(&pbk_bytes).expect("Failed to parse public key.");

        if let Some(token) = token {
            let mut headers = header::HeaderMap::new();
            let mut auth_value = header::HeaderValue::from_static(token);
            auth_value.set_sensitive(true);
            headers.insert(header::AUTHORIZATION, auth_value);
            let new_c = Client::builder().default_headers(headers).build().unwrap();

            InteractionHandler {
                application_id: app_id,
                app_public_key: app_public_key,
                client: new_c,
                global_handles: HashMap::new(),
                component_handles: HashMap::new(),
                guild_handles: HashMap::new(),
            }
        } else {
            InteractionHandler {
                application_id: app_id,
                app_public_key: app_public_key,
                client: Client::new(),
                global_handles: HashMap::new(),
                component_handles: HashMap::new(),
                guild_handles: HashMap::new(),
            }
        }
    }

    /// Binds an async function to a **global** command.
    /// Your function must take a [`Context`] as an argument and must return a [`InteractionResponse`].
    /// Make sure to use the `#[slash_command]` procedural macro to make it usable for the handler.
    ///
    /// Like:
    /// ```ignore
    /// # use rusty_interaction::types::interaction::{Context, InteractionResponse};
    /// # use attributes::slash_command;
    /// #[slash_command]
    /// async fn do_work(ctx: Context) -> InteractionResponse {
    ///     return todo!("Do work and return a response");
    /// }
    /// ```
    ///
    /// # Note
    /// The handler will first check if a guild-specific handler is available. If not, it will try to match a global command. If that fails too, an error will be returned.
    ///
    /// # Example
    /// ```ignore
    /// # use rusty_interaction::types::interaction::{Context, InteractionResponse};
    /// # use rusty_interaction::handler::InteractionHandler;
    /// # use attributes::slash_command;
    /// const PUB_KEY: &str = "my_public_key";
    ///
    /// #[slash_command]
    /// async fn pong_handler(ctx: Context) -> InteractionResponse {
    ///     return ctx.respond()
    ///             .content("Pong!")
    ///             .finish();
    /// }
    ///
    /// #[actix_web::main]
    /// async fn main() -> std::io::Result<()> {
    ///
    ///     let mut handle = InteractionHandler::new(PUB_KEY);
    ///     handle.add_command("ping", pong_handler);
    ///     
    ///     return handle.run().await;
    /// }
    /// ```
    pub fn add_global_command(&mut self, name: &'static str, func: HandlerFunction) {
        self.global_handles.insert(name, func);
    }

    /// Binds an async function to a **component**.
    /// Your function must take a [`Context`] as an argument and must return a [`InteractionResponse`].
    /// Use the `#[component_handler]` procedural macro for your own convinence.eprintln!
    ///
    /// # Example
    /// ```ignore
    /// use rusty_interaction::handler::InteractionHandler;
    /// use rusty_interaction::types::components::*;
    /// use rusty_interaction::types::interaction::*;
    ///
    /// #[component_handler]
    /// async fn comp_hand(ctx: Context) -> InteractionResponse {
    ///     return ctx.respond().content("Some message content").finish();
    /// }
    ///
    /// #[slash_command]
    /// async fn spawn_buttons(ctx: Context) -> InteractionResponse {
    ///     // Let's build our message!
    ///     let resp = ctx.respond()
    ///     // Set message content
    ///     .content("Not edited")
    ///     // add a component action row using it's builder
    ///     .add_component_row(
    ///         ComponentRowBuilder::default()
    ///        // Add buttons using it's builder
    ///        .add_button(
    ///           ComponentButtonBuilder::default()
    ///                            .label("Edit")
    ///                            .custom_id("HEHE")
    ///                            .style(ComponentButtonStyle::Primary)
    ///                            .finish()
    ///        )
    ///        .add_button(
    ///            ComponentButtonBuilder::default()
    ///                            .label("Delete")
    ///                            .custom_id("DELETE")
    ///                            .style(ComponentButtonStyle::Danger)
    ///                            .finish()
    ///         )
    ///         .finish()
    ///     )
    ///     .finish();

    ///     return resp;
    ///
    /// }
    /// #[actix_web::main]
    /// async fn main() -> std::io::Result<()> {
    ///
    ///     let mut handle = InteractionHandler::new(PUB_KEY);
    ///     handle.add_command("ping", pong_handler);
    ///     handle.add_component_handle("HEHE", comp_hand);
    ///     return handle.run().await;
    /// }
    /// ```
    pub fn add_component_handle(&mut self, custom_id: &'static str, func: HandlerFunction) {
        self.component_handles.insert(custom_id, func);
    }

    #[cfg(feature = "extended-handler")]
    #[cfg_attr(docsrs, doc(cfg(feature = "extended-handler")))]
    /// Register an command with Discord!
    pub async fn register_command_handle(
        &mut self,
        guild: impl Into<Snowflake>,
        cmd: ApplicationCommand,
        func: HandlerFunction,
    ) -> Result<ApplicationCommand, HttpError> {
        let g = guild.into();

        let url = format!(
            "{}/applications/{}/guilds/{}/commands",
            crate::BASE_URL,
            self.application_id,
            g
        );

        let r = self.client.post(&url).json(&cmd).send().await;

        expect_successful_api_response_and_return!(r, ApplicationCommand, a, {
            if let Some(id) = a.id {
                // Already overwrites current key if it exists, so no need to check.
                self.guild_handles.insert(id, func);
                Ok(a)
            } else {
                // Pretty bad if this code reaches...
                Err(HttpError {
                    code: 0,
                    message: "Command registration response did not have an ID.".to_string(),
                })
            }
        })
    }

    #[cfg(feature = "extended-handler")]
    #[cfg_attr(docsrs, doc(cfg(feature = "extended-handler")))]
    /// Remove a guild handle
    pub async fn deregister_command_handle(
        &mut self,
        guild: impl Into<Snowflake>,
        id: impl Into<Snowflake>,
    ) -> Result<(), HttpError> {
        let g = guild.into();
        let i = id.into();

        let url = format!(
            "{}/applications/{}/guilds/{}/commands/{}",
            crate::BASE_URL,
            self.application_id,
            g,
            i
        );

        let r = self.client.delete(&url).send().await;

        expect_specific_api_response!(r, StatusCode::NO_CONTENT, {
            self.guild_handles.remove(&i);
            Ok(())
        })
    }

    /// Entry point function for handling `Interactions`
    pub async fn interaction(&mut self, req: HttpRequest, body: String) -> Result<HttpResponse> {
        // Check for good content type --> must be application/json

        if let Some(ct) = req.headers().get("Content-Type") {
            if ct != "application/json" {
                debug!(
                    "Incoming interaction rejected, bad Content-Type specified. Origin: {:?}",
                    req.connection_info().realip_remote_addr()
                );
                return ERROR_RESPONSE!(400, "Bad Content-Type");
            }
        } else {
            debug!(
                "Incoming interaction rejected, no Content-Type specified. Origin: {:?}",
                req.connection_info().realip_remote_addr()
            );
            return ERROR_RESPONSE!(400, "Bad Content-Type");
        }

        let se = get_header(&req, "X-Signature-Ed25519");
        let st = get_header(&req, "X-Signature-Timestamp");

        // TODO: Domain check might be a good one.

        if let Some((se, st)) = se.zip(st) {
            // Verify timestamp + body against given signature
            if verify_discord_message(self.app_public_key, se, st, &body).is_ok() {
                // Signature OK. Continue with interaction processing.
            } else {
                // Verification failed, reject.
                // TODO: Switch error response
                debug!(
                    "Incoming interaction rejected, invalid signature. Origin: {:?}",
                    req.connection_info().realip_remote_addr()
                );
                return ERROR_RESPONSE!(401, "Invalid request signature");
            }
        } else {
            // If proper headers are not present reject.
            debug!(
                "Incoming interaction rejected, missing headers. Origin: {:?}",
                req.connection_info().realip_remote_addr()
            );
            return ERROR_RESPONSE!(400, "Bad signature data");
        }

        // Security checks passed, try deserializing request to Interaction.
        match serde_json::from_str::<Interaction>(&body) {
            Err(e) => {
                // It's probably bad on our end if this code is reached.
                error!("Failed to decode interaction! Error: {}", e);
                debug!("Body sent: {}", body);
                return ERROR_RESPONSE!(400, format!("Bad body: {}", e));
            }
            Ok(interaction) => {
                match interaction.r#type {
                    InteractionType::Ping => {
                        let response =
                            InteractionResponse::new(InteractionResponseType::Pong, None);
                        debug!("Got a ping, responding with pong.");
                        return Ok(HttpResponse::build(StatusCode::OK)
                            .content_type("application/json")
                            .json(response));
                    }

                    InteractionType::ApplicationCommand => {
                        let data = if let Some(ref data) = interaction.data {
                            data
                        } else {
                            error!("Failed to unwrap Interaction!");
                            return ERROR_RESPONSE!(500, "Failed to unwrap");
                        };

                        // Check for matches in guild handler map
                        if let Some(handler) = self.guild_handles.get(data.id.as_ref().unwrap()) {
                            // construct a Context
                            let ctx = Context::new(self.client.clone(), interaction);

                            // Call the handler
                            let response = handler(self, ctx).await;

                            match_handler_response!(response.r#type, response)
                            
                        } 
                        // Welp, nothing found. Check for matches in the global map
                        else if let Some(handler) = self
                            .global_handles
                            .get(data.name.as_ref().unwrap().as_str())
                        {

                            // construct a Context
                            let ctx = Context::new(self.client.clone(), interaction);

                            // Call the handler
                            let response = handler(self, ctx).await;

                            match_handler_response!(response.r#type, response)
                        } 
                        // Still nothing, return an error
                        else {
                            error!(
                                "No associated handler found for {}",
                                data.name.as_ref().unwrap().as_str()
                            );
                            ERROR_RESPONSE!(500, "No associated handler found")
                        }
                    }
                    InteractionType::MessageComponent => {
                        let data = if let Some(ref data) = interaction.data {
                            data
                        } else {
                            error!("Failed to unwrap Interaction!");
                            return ERROR_RESPONSE!(500, "Failed to unwrap");
                        };

                        if let Some(handler) = self
                            .component_handles
                            .get(data.custom_id.as_ref().unwrap().as_str())
                        {
                            // construct a Context
                            let ctx = Context::new(self.client.clone(), interaction);

                            // Call the handler
                            let response = handler(self, ctx).await;

                            if response.r#type == InteractionResponseType::DefferedUpdateMessage {
                                /* The use of HTTP code 202 is more appropriate when an Interaction is deffered.
                                If an application is first sending a deffered channel message response, this usually means the system
                                is still processing whatever it is doing.
                                See the spec: https://tools.ietf.org/html/rfc7231#section-6.3.3 */
                                Ok(HttpResponse::build(StatusCode::ACCEPTED).json(response))
                            } else {
                                // Send out a response to Discord
                                let r = HttpResponse::build(StatusCode::OK).json(response);

                                Ok(r)
                            }
                        } else {
                            error!(
                                "No associated handler found for {}",
                                data.custom_id.as_ref().unwrap().as_str()
                            );
                            ERROR_RESPONSE!(500, "No associated handler found")
                        }
                    }
                }
            }
        }
    }

    /// This is a predefined function that starts an `actix_web::HttpServer` and binds `self.interaction` to `/api/discord/interactions`.
    /// Note that you'll eventually have to switch to an HTTPS server. This function does not provide this.
    pub async fn run(self, port: u16) -> std::io::Result<()> {
        let data = web::Data::new(Mutex::new(self));
        HttpServer::new(move || {
            App::new().app_data(data.clone()).route(
                "/api/discord/interactions",
                web::post().to(
                    |data: web::Data<Mutex<InteractionHandler>>, req: HttpRequest, body: String| async move {
                        data.lock().unwrap().interaction(req, body).await
                    },
                ),
            )
        })
        .bind(("0.0.0.0:{}", port))?
        .run()
        .await
    }

    /// Same as [`InteractionHandler::run`] but starts a server with SSL/TLS.
    pub async fn run_ssl(self, server_conf: ServerConfig, port: u16) -> std::io::Result<()> {
        let data = web::Data::new(Mutex::new(self));
        HttpServer::new(move || {
            App::new().app_data(data.clone()).route(
                "/api/discord/interactions",
                web::post().to(
                    |data: web::Data<Mutex<InteractionHandler>>, req: HttpRequest, body: String| async move {
                        data.lock().unwrap().interaction(req, body).await
                    },
                ),
            )
        })
        .bind_rustls(format!("0.0.0.0:{}", port), server_conf)?
        .run()
        .await
    }
}

/// Simpler header getter from a HTTP request
fn get_header<'a>(req: &'a HttpRequest, header: &str) -> Option<&'a str> {
    req.headers().get(header)?.to_str().ok()
}