Skip to content

Commit

Permalink
few quick fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Dec 30, 2023
1 parent 86f5205 commit ea05fb2
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use base64::engine::general_purpose;
use base64::Engine;
use egui::{vec2, Align, Color32, Layout, RichText};

use std::fs::{self};
Expand Down
20 changes: 18 additions & 2 deletions src/app/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub fn encrypt_aes256(string_to_be_encrypted: String) -> aes_gcm::aead::Result<S

Ok(ciphertext)
}

#[inline]
pub fn decrypt_aes256(string_to_be_decrypted: String) -> Result<String, FromUtf8Error> {
let ciphertext = hex::decode(string_to_be_decrypted).unwrap();
let key: &[u8] = &[42; 32];
Expand All @@ -42,6 +44,8 @@ pub fn decrypt_aes256(string_to_be_decrypted: String) -> Result<String, FromUtf8
let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();
String::from_utf8(plaintext)
}

#[inline]
pub fn pass_encrypt(string_to_be_encrypted: String) -> String {
let password = string_to_be_encrypted.as_bytes();
let salt = b"c1eaa94ec38ab7aa16e9c41d029256d3e423f01defb0a2760b27117ad513ccd2";
Expand All @@ -58,9 +62,12 @@ pub fn pass_encrypt(string_to_be_encrypted: String) -> String {

argon2::hash_encoded(password, salt, &config).unwrap()
}

#[inline]
pub fn pass_hash_match(to_be_verified: String, file_ln: String) -> bool {
argon2::verify_encoded(&file_ln, to_be_verified.as_bytes()).unwrap()
}

pub fn login(username: String, passw: String) -> Result<PathBuf> {
let app_data = env::var("APPDATA")?;

Expand All @@ -86,6 +93,7 @@ pub fn login(username: String, passw: String) -> Result<PathBuf> {
ensure!(usr_check && pwd_check, "Invalid Password");
Ok(path)
}

pub fn register(username: String, passw: String) -> Result<()> {
let app_data = env::var("APPDATA")?;

Expand All @@ -109,6 +117,7 @@ pub fn register(username: String, passw: String) -> Result<()> {

Ok(())
}

pub fn append_to_file(path: PathBuf, write: String) -> Result<()> {
let mut file = std::fs::OpenOptions::new()
.create(false)
Expand All @@ -128,6 +137,7 @@ pub fn append_to_file(path: PathBuf, write: String) -> Result<()> {
)),
}
}

pub fn decrypt_lines_from_vec(mut file_ln: Vec<String>) -> Result<Vec<String>> {
//remove pass and user
file_ln.remove(0);
Expand All @@ -148,6 +158,7 @@ pub fn decrypt_lines_from_vec(mut file_ln: Vec<String>) -> Result<Vec<String>> {

Ok(output_vec)
}

pub fn read_from_file(path: PathBuf) -> Result<Vec<String>> {
let file: Vec<String> = fs::read_to_string(path)?
.lines()
Expand All @@ -156,6 +167,7 @@ pub fn read_from_file(path: PathBuf) -> Result<Vec<String>> {

Ok(file)
}

pub fn delete_line_from_file(line_number: usize, path: PathBuf) -> Result<()> {
//copy everything from orignal convert to vec representing lines delete line rewrite file
let mut file = std::fs::OpenOptions::new().read(true).open(path.clone())?;
Expand Down Expand Up @@ -183,6 +195,7 @@ pub fn delete_line_from_file(line_number: usize, path: PathBuf) -> Result<()> {

Ok(())
}

pub fn write_file(file_response: ServerFileReply) -> Result<()> {
let files = FileDialog::new()
.set_title("Save to")
Expand All @@ -209,13 +222,15 @@ pub fn write_file(file_response: ServerFileReply) -> Result<()> {

Ok(())
}

#[inline]
pub fn write_image(file_response: &ServerImageReply, ip: String) -> Result<()> {
//secondly create the folder labeled with the specified server ip

let path = format!(
"{}\\szeChat\\Client\\{}\\Images\\{}",
env!("APPDATA"),
general_purpose::URL_SAFE_NO_PAD.encode(&ip),
general_purpose::URL_SAFE_NO_PAD.encode(ip),
file_response.index
);

Expand All @@ -224,12 +239,13 @@ pub fn write_image(file_response: &ServerImageReply, ip: String) -> Result<()> {
Ok(())
}

#[inline]
pub fn write_audio(file_response: ServerAudioReply, ip: String) -> Result<()> {
//secondly create the folder labeled with the specified server ip
let path = format!(
"{}\\szeChat\\Client\\{}\\Audios\\{}",
env!("APPDATA"),
general_purpose::URL_SAFE_NO_PAD.encode(&ip),
general_purpose::URL_SAFE_NO_PAD.encode(ip),
file_response.index
);

Expand Down
2 changes: 1 addition & 1 deletion src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rand::rngs::ThreadRng;

use rodio::{OutputStream, OutputStreamHandle, Sink};
use std::collections::BTreeMap;
use std::fs::File;

use std::io;
use std::io::{Read, Seek, SeekFrom};

Expand Down
6 changes: 3 additions & 3 deletions src/app/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ impl MessageService {
})
.unwrap_or_default();

return Ok(Response::new(MessageResponse { message: output }));
Ok(Response::new(MessageResponse { message: output }))
}
ClientRequestTypeStruct::ClientFileRequest(file_request) => {
let (file_bytes, file_name) = &self.serve_file(file_request.index).await;
Expand All @@ -499,7 +499,7 @@ impl MessageService {
})
.unwrap_or_default();

return Ok(Response::new(MessageResponse { message: output }));
Ok(Response::new(MessageResponse { message: output }))
}
ClientRequestTypeStruct::ClientAudioRequest(audio_request) => {
let (file_bytes, file_name) = self.serve_audio(audio_request.index).await;
Expand All @@ -511,7 +511,7 @@ impl MessageService {
})
.unwrap_or_default();

return Ok(Response::new(MessageResponse { message: output }));
Ok(Response::new(MessageResponse { message: output }))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/ui/client_ui/client_actions/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl TemplateApp {
}
*/

#[inline]
pub fn send_file(&mut self, file: std::path::PathBuf) {
let passw = self.client_password.clone();
let ip = self.send_on_ip.clone();
Expand Down
22 changes: 12 additions & 10 deletions src/app/ui/client_ui/message_instances/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,20 @@ impl TemplateApp {
.as_mut()
.unwrap();

let source = Decoder::new(self.audio_playback.settings_list[current_index_in_message_list].cursor.clone() /*We can assume its always Some because we just set it to some above (lol)*/)
.unwrap_or_else(|e| {
dbg!(&e);
eprintln!("{}", e);

//Return an empty decoder to avoid panicing
Decoder::new(PlaybackCursor::new(vec![0])).unwrap()
});
let source = Decoder::new(self.audio_playback.settings_list[current_index_in_message_list].cursor.clone() /*We can assume its always Some because we just set it to some above (lol)*/);

sink.append(source);
match source {
Ok(source) => {

sink.append(source);
sink.play();

}
Err(err) => {
dbg!(err);
}
}

sink.play();
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/client_ui/message_instances/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl TemplateApp {
Area::new("image_overlay")
.movable(false)
.anchor(Align2::CENTER_CENTER, vec2(0., 0.))
.show(&ctx, |ui| {
.show(ctx, |ui| {
ui.allocate_ui(
vec2(ui.available_width() / 1.3, ui.available_height() / 1.3),
|ui| {
Expand Down
3 changes: 2 additions & 1 deletion src/app/ui/client_ui/widgets/emoji_tray/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ pub fn special_char_name(chr: char) -> Option<&'static str> {

impl backend::TemplateApp {
pub fn window_emoji(&mut self, ctx: &egui::Context) {
egui::Window::new("Emoji")
egui::Window::new("Character set")
.open(&mut self.emoji_mode)
.collapsible(false)
.show(ctx, |ui| {
ui.label("Click to paste".to_string());
Expand Down
Loading

0 comments on commit ea05fb2

Please sign in to comment.