Skip to content

Commit 2cf4265

Browse files
refactor(commands): directly use native encryption function
1 parent ca8c9d4 commit 2cf4265

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

src/commands.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use azalea::{brigadier::prelude::*, chat::ChatPacket, prelude::*};
22
use futures::lock::Mutex;
3-
use mlua::{Function, Table};
4-
use ncr::utils::prepend_header;
3+
use mlua::{Error, Result, Table, UserDataRef};
4+
use ncr::{
5+
encoding::{Base64Encoding, Base64rEncoding, NewBase64rEncoding},
6+
encryption::{CaesarEncryption, Cfb8Encryption, EcbEncryption, Encryption, GcmEncryption},
7+
utils::prepend_header,
8+
};
59

610
use crate::{
7-
State,
8-
lua::{eval, exec, reload},
11+
State, crypt,
12+
lua::{eval, exec, nochatreports::key::AesKey, reload},
913
};
1014

1115
pub type Ctx = CommandContext<Mutex<CommandSource>>;
@@ -19,18 +23,20 @@ pub struct CommandSource {
1923

2024
impl CommandSource {
2125
pub fn reply(&self, message: &str) {
22-
let ncr_data = self
23-
.ncr_options
24-
.as_ref()
25-
.zip(self.state.lua.globals().get::<Function>("ncr_encrypt").ok());
26+
fn encrypt(options: &Table, plaintext: &str) -> Result<String> {
27+
Ok(crypt!(encrypt, options, &prepend_header(plaintext)))
28+
}
29+
2630
for mut chunk in message
2731
.chars()
2832
.collect::<Vec<char>>()
2933
.chunks(if self.ncr_options.is_some() { 150 } else { 236 })
3034
.map(|chars| chars.iter().collect::<String>())
3135
{
32-
if let Some((options, ref encrypt)) = ncr_data
33-
&& let Ok(ciphertext) = encrypt.call::<String>((options, prepend_header(&chunk)))
36+
if let Some(ciphertext) = self
37+
.ncr_options
38+
.as_ref()
39+
.and_then(|options| encrypt(options, &chunk).ok())
3440
{
3541
chunk = ciphertext;
3642
}

src/lua/nochatreports/crypt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ macro_rules! crypt {
33
($op:ident, $options:expr, $text:expr) => {{
44
macro_rules! crypt_with {
55
($algo:ident) => {{
6-
let encoding = $options.get("encoding").unwrap_or_default();
76
let key = &$options.get::<UserDataRef<AesKey>>("key")?.0;
8-
match encoding {
7+
match $options.get("encoding").unwrap_or_default() {
98
1 => $algo::<Base64Encoding>::$op($text, &key),
109
2 => $algo::<Base64rEncoding>::$op($text, &key),
1110
_ => $algo::<NewBase64rEncoding>::$op($text, &key),

0 commit comments

Comments
 (0)