Compare commits

...

3 Commits

3 changed files with 18 additions and 10 deletions

View File

@ -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<Mutex<CommandSource>>;
@ -19,15 +23,20 @@ pub struct CommandSource {
impl CommandSource {
pub fn reply(&self, message: &str) {
fn encrypt(options: &Table, plaintext: &str) -> Result<String> {
Ok(crypt!(encrypt, options, &prepend_header(plaintext)))
}
for mut chunk in message
.chars()
.collect::<Vec<char>>()
.chunks(if self.ncr_options.is_some() { 150 } else { 236 })
.map(|chars| chars.iter().collect::<String>())
{
if let Some(options) = &self.ncr_options
&& let Ok(encrypt) = self.state.lua.globals().get::<Function>("ncr_encrypt")
&& let Ok(ciphertext) = encrypt.call::<String>((options, prepend_header(&chunk)))
if let Some(ciphertext) = self
.ncr_options
.as_ref()
.and_then(|options| encrypt(options, &chunk).ok())
{
chunk = ciphertext;
}

View File

@ -23,7 +23,7 @@ use crate::{
replay::recorder::Recorder,
};
#[allow(clippy::too_many_lines)]
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]
pub async fn handle_event(client: Client, event: Event, state: State) -> Result<()> {
match event {
Event::AddPlayer(player_info) => {

View File

@ -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::<UserDataRef<AesKey>>("key")?.0;
match encoding {
match $options.get("encoding").unwrap_or_default() {
1 => $algo::<Base64Encoding>::$op($text, &key),
2 => $algo::<Base64rEncoding>::$op($text, &key),
_ => $algo::<NewBase64rEncoding>::$op($text, &key),