refactor: clean up encryption/decryption

This commit is contained in:
Ryan 2025-03-07 17:43:15 -05:00
parent 14e3781e05
commit 73e7e17da4
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
2 changed files with 12 additions and 12 deletions

View File

@ -24,12 +24,11 @@ impl CommandSource {
.chunks(if self.ncr_options.is_some() { 150 } else { 236 }) .chunks(if self.ncr_options.is_some() { 150 } else { 236 })
.map(|chars| chars.iter().collect::<String>()) .map(|chars| chars.iter().collect::<String>())
{ {
if let (Some(options), Ok(encrypt)) = ( if let Some(options) = &self.ncr_options
&self.ncr_options, && let Ok(encrypt) = self.state.lua.globals().get::<Function>("ncr_encrypt")
self.state.lua.globals().get::<Function>("ncr_encrypt"), && let Ok(ciphertext) = encrypt.call::<String>((options, prepend_header(&chunk)))
) && let Ok(encrypted) = encrypt.call::<String>((options, prepend_header(&chunk)))
{ {
chunk = encrypted; chunk = ciphertext;
} }
self.client.chat( self.client.chat(
&(if self.message.is_whisper() &(if self.message.is_whisper()

View File

@ -28,15 +28,16 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> anyhow:
if let Some(sender) = sender { if let Some(sender) = sender {
let mut ncr_options = None; let mut ncr_options = None;
if let (Ok(options), Ok(decrypt)) = ( if let Ok(options) = globals.get::<Table>("NcrOptions")
globals.get::<Table>("NcrOptions"), && let Ok(decrypt) = globals.get::<Function>("ncr_decrypt")
globals.get::<Function>("ncr_decrypt"), && let Some(plaintext) = decrypt
) && let Ok(decrypted) = .call::<String>((options.clone(), content.clone()))
decrypt.call::<String>((options.clone(), content.clone())) .ok()
&& let Ok(trimmed) = trim_header(&decrypted) .as_deref()
.and_then(|s| trim_header(s).ok())
{ {
ncr_options = Some(options); ncr_options = Some(options);
trimmed.clone_into(&mut content); plaintext.clone_into(&mut content);
info!("decrypted message from {sender}: {content}"); info!("decrypted message from {sender}: {content}");
} }