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

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

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