diff --git a/src/commands.rs b/src/commands.rs index 965b63a..ea7626d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,11 +1,15 @@ use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*}; use futures::lock::Mutex; -use mlua::{Function, Table}; -use ncr::utils::prepend_header; +use mlua::{Error, Result, Table, UserDataRef}; +use ncr::{ + encoding::{Base64Encoding, Base64rEncoding, NewBase64rEncoding}, + encryption::{CaesarEncryption, Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption}, + utils::prepend_header, +}; use crate::{ - State, - lua::{eval, exec, reload}, + State, crypt, + lua::{eval, exec, nochatreports::key::AesKey, reload}, }; pub type Ctx = CommandContext>; @@ -19,18 +23,20 @@ pub struct CommandSource { impl CommandSource { pub fn reply(&self, message: &str) { - let ncr_data = self - .ncr_options - .as_ref() - .zip(self.state.lua.globals().get::("ncr_encrypt").ok()); + fn encrypt(options: &Table, plaintext: &str) -> Result { + Ok(crypt!(encrypt, options, &prepend_header(plaintext))) + } + for mut chunk in message .chars() .collect::>() .chunks(if self.ncr_options.is_some() { 150 } else { 236 }) .map(|chars| chars.iter().collect::()) { - if let Some((options, ref encrypt)) = ncr_data - && let Ok(ciphertext) = encrypt.call::((options, prepend_header(&chunk))) + if let Some(ciphertext) = self + .ncr_options + .as_ref() + .and_then(|options| encrypt(options, &chunk).ok()) { chunk = ciphertext; } diff --git a/src/lua/nochatreports/crypt.rs b/src/lua/nochatreports/crypt.rs index c1b7c33..82860bc 100644 --- a/src/lua/nochatreports/crypt.rs +++ b/src/lua/nochatreports/crypt.rs @@ -3,9 +3,8 @@ macro_rules! crypt { ($op:ident, $options:expr, $text:expr) => {{ macro_rules! crypt_with { ($algo:ident) => {{ - let encoding = $options.get("encoding").unwrap_or_default(); let key = &$options.get::>("key")?.0; - match encoding { + match $options.get("encoding").unwrap_or_default() { 1 => $algo::::$op($text, &key), 2 => $algo::::$op($text, &key), _ => $algo::::$op($text, &key),