88 lines
2.7 KiB
Rust
88 lines
2.7 KiB
Rust
use crate::unwrap_or_return_option;
|
|
use crate::unwrap_or_return_result;
|
|
use crate::State;
|
|
use crate::{encryption, helpers};
|
|
use eyre::Result;
|
|
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
|
use std::collections::HashMap;
|
|
use std::sync::mpsc::{self, Receiver, Sender};
|
|
|
|
fn forward(
|
|
message: &str,
|
|
listener_channel_tx: &Sender<String>,
|
|
server: &str,
|
|
) -> Result<(), mpsc::SendError<String>> {
|
|
listener_channel_tx.send(message.replace(server, "127.0.0.1"))
|
|
}
|
|
|
|
pub fn handle_message_from_server(
|
|
recieved: &str,
|
|
public_key: &Vec<u8>,
|
|
secret_key: &SignedSecretKey,
|
|
server: &str,
|
|
passwd: &str,
|
|
keys: &mut HashMap<String, SignedPublicKey>,
|
|
writer_channel_tx: &Sender<String>,
|
|
writer_channel_rx: &Receiver<String>,
|
|
listener_channel_tx: &Sender<String>,
|
|
_listener_channel_rx: &Receiver<String>,
|
|
state: &State,
|
|
) -> Result<()> {
|
|
let recieved_parsed = &unwrap_or_return_result!(ircparser::parse(recieved))[0];
|
|
|
|
let default_reciever = String::new();
|
|
|
|
let reciever = match recieved_parsed.params.get(0) {
|
|
Some(val) => val,
|
|
None => &default_reciever,
|
|
};
|
|
|
|
if recieved_parsed.command != "PRIVMSG"
|
|
|| reciever.starts_with('#')
|
|
|| state.nicks_without_encryption.contains(reciever)
|
|
{
|
|
forward(recieved, listener_channel_tx, server)?;
|
|
return Ok(());
|
|
}
|
|
|
|
dbg!(&recieved_parsed);
|
|
|
|
let source = unwrap_or_return_option!(&recieved_parsed.source);
|
|
|
|
if recieved_parsed.params[1] == "START_MESSAGE" {
|
|
let sender = unwrap_or_return_option!(helpers::get_nick(source));
|
|
|
|
let message = helpers::recieve_message_base64(
|
|
writer_channel_rx,
|
|
listener_channel_tx,
|
|
server,
|
|
"127.0.0.1",
|
|
&sender,
|
|
"END_MESSAGE",
|
|
)?; // Get
|
|
let message = encryption::decrypt(secret_key, &message, passwd)?; // Decrypt
|
|
|
|
listener_channel_tx.send(recieved.replace("START_MESSAGE", &message))?; // Send
|
|
} else if recieved_parsed.params[1] == "START_KEY" {
|
|
let sender = unwrap_or_return_option!(helpers::get_nick(source));
|
|
let to_send = helpers::bytes_to_privmsg_base64(public_key, &sender);
|
|
writer_channel_tx.send(to_send)?;
|
|
writer_channel_tx.send(format!("PRIVMSG {sender} END_KEY\r\n"))?;
|
|
|
|
let foreign_key = helpers::recieve_message_base64(
|
|
writer_channel_rx,
|
|
listener_channel_tx,
|
|
server,
|
|
"127.0.0.1",
|
|
&sender,
|
|
"END_KEY",
|
|
)?;
|
|
dbg!(&foreign_key);
|
|
let key = SignedPublicKey::from_bytes(foreign_key.as_slice())?;
|
|
println!("Got a key from {sender}");
|
|
keys.insert(sender.to_string(), key);
|
|
}
|
|
|
|
Ok(())
|
|
}
|