11use azalea:: { brigadier:: prelude:: * , chat:: ChatPacket , prelude:: * } ;
22use 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
610use crate :: {
7- State ,
8- lua:: { eval, exec, reload} ,
11+ State , crypt ,
12+ lua:: { eval, exec, nochatreports :: key :: AesKey , reload} ,
913} ;
1014
1115pub type Ctx = CommandContext < Mutex < CommandSource > > ;
@@ -19,18 +23,20 @@ pub struct CommandSource {
1923
2024impl 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 }
0 commit comments