50 lines
1.7 KiB
Rust
50 lines
1.7 KiB
Rust
use crate::helpers::bytes_to_privmsg_base64;
|
|
use crate::{encryption, helpers};
|
|
use eyre::Result;
|
|
use pgp::{Deserializable, SignedPublicKey};
|
|
use std::collections::HashMap;
|
|
use std::sync::mpsc::{Receiver, Sender};
|
|
|
|
pub fn handle_message_from_client(
|
|
recieved: &str,
|
|
public_key: &Vec<u8>,
|
|
server: &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>,
|
|
) -> Result<()> {
|
|
let command = &ircparser::parse(recieved).expect("Got an invalid IRC instruction")[0];
|
|
|
|
if command.command == "PRIVMSG" && !command.params[0].starts_with('#') {
|
|
let other = &command.params[0];
|
|
|
|
if !keys.contains_key(other) {
|
|
helpers::send_key(writer_channel_tx, other, public_key)?;
|
|
let key = helpers::recieve_message_base64(
|
|
writer_channel_rx,
|
|
listener_channel_tx,
|
|
"127.0.0.1",
|
|
server,
|
|
other,
|
|
"END_KEY",
|
|
)?;
|
|
let key = SignedPublicKey::from_bytes(key.as_slice())?;
|
|
keys.insert(other.to_string(), key);
|
|
}
|
|
|
|
let foreign_key = keys.get(other).unwrap();
|
|
|
|
writer_channel_tx.send(format!("PRIVMSG {other} START_MESSAGE\r\n"))?;
|
|
writer_channel_tx.send(bytes_to_privmsg_base64(
|
|
&encryption::encrypt(foreign_key, &command.params[1])?,
|
|
other,
|
|
))?;
|
|
writer_channel_tx.send(format!("PRIVMSG {other} END_MESSAGE\r\n"))?;
|
|
} else {
|
|
writer_channel_tx.send(recieved.replace("127.0.0.1", server))?;
|
|
}
|
|
Ok(())
|
|
}
|