From 1711b3d9d021c49ea0d8b1b06e0cb8c256968738 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Wed, 18 Oct 2023 00:19:34 +0200 Subject: [PATCH] fixed saving to json --- Makefile | 2 +- mail.cpp | 22 ++++++++++++++-------- mail.h | 8 +++++--- server.cpp | 2 +- user.cpp | 33 ++++++++++++++++++++++++++------- user.h | 4 +--- 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 67d12f6..7725c11 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = g++ ASAN_FLAGS = -fsanitize=address -fno-omit-frame-pointer -Wno-format-security -CFLAGS := -g -std=c++20 -Wall -lssl -lcrypto +CFLAGS := -g -std=c++20 -Wall LDFLAGS += -fsanitize=address -lpthread TARGET = client server diff --git a/mail.cpp b/mail.cpp index 474622c..8bcecb8 100644 --- a/mail.cpp +++ b/mail.cpp @@ -1,5 +1,11 @@ #include "mail.h" +mail::mail(std::string filename, std::string subject) : + filename(filename), + timestamp(std::time(NULL)), + subject(subject) +{} + mail::mail(std::string filename, int64_t timestamp, std::string subject) : filename(filename), timestamp(timestamp), @@ -25,14 +31,14 @@ void mail::remove() json mail::mailToJson() { - json json; + json jsonfile; - json["id"] = this->id; - json["timestamp"] = this->timestamp; - json["sender"] = this->sender; - json["recipients"] = this->recipients; - json["subject"] = this->subject; - json["filename"] = this->filename; + jsonfile["id"] = this->id; + jsonfile["timestamp"] = this->timestamp; + jsonfile["sender"] = this->sender; + jsonfile["recipients"] = this->recipients; + jsonfile["subject"] = this->subject; + jsonfile["filename"] = this->filename; - return json; + return jsonfile; } \ No newline at end of file diff --git a/mail.h b/mail.h index b8b0713..3212244 100644 --- a/mail.h +++ b/mail.h @@ -2,12 +2,13 @@ #include "user_handler.h" -#include #include #include #include #include +#include + namespace fs = std::filesystem; using json = nlohmann::json; @@ -21,14 +22,15 @@ struct mail { std::vector recipients; std::string subject; + mail(std::string filename, std::string subject); mail(std::string filename, int64_t timestamp, std::string subject); bool operator()(const u_int& id) const { return id == this->id; } - bool operator<(const mail& left) const { - return this->timestamp < left.timestamp; + bool operator<(mail& left) const { + return left.timestamp > this->timestamp; } fs::path getPath() { return this->filename; }; diff --git a/server.cpp b/server.cpp index 699077b..9c0b395 100644 --- a/server.cpp +++ b/server.cpp @@ -248,7 +248,7 @@ void *clientCommunication(void *data) } user_handler::getInstance()->getUser(lines.at(1))->sendMail( - new struct mail(saveToFile(user_handler::getInstance()->getSpoolDir()/"messages", lines.at(4)), std::time(0), lines.at(3)), + new struct mail(saveToFile(user_handler::getInstance()->getSpoolDir()/"messages", lines.at(4)), lines.at(3)), {lines.at(2)} ); diff --git a/user.cpp b/user.cpp index c051be0..eda9493 100644 --- a/user.cpp +++ b/user.cpp @@ -7,16 +7,17 @@ #include #include #include +#include using json = nlohmann::json; user::user(fs::path user_data_json) { std::ifstream ifs(user_data_json); - json user = json::parse(ifs); + json user_data = json::parse(ifs); - this->name = user["name"]; - for ( auto& mail_json : user["mails"]["received"] ) { + this->name = user_data["name"]; + for ( auto& mail_json : user_data["mails"]["received"] ) { mail* mail = new struct mail( mail_json["filename"], mail_json["timestamp"], @@ -28,6 +29,22 @@ user::user(fs::path user_data_json) this->inbox.insert(mail); } + + for ( auto& mail_json : user_data["mails"]["sent"] ) { + mail* mail = new struct mail( + mail_json["filename"], + mail_json["timestamp"], + mail_json["subject"] + ); + mail->id = mail_json["id"]; + mail->sender = mail_json["sender"]; + mail->recipients = mail_json["recipients"].get>(); + + this->sent.insert(mail); + } + + this->user_data = user_data; + this->file_location = user_data_json; } user::user(std::string name, fs::path user_dir) @@ -54,8 +71,6 @@ void user::addMail(mail* mail) this->inbox.insert(mail); this->user_data["mails"]["received"][std::to_string(mail->id)] = mail->mailToJson(); - - printf("%s\n", this->user_data.dump().c_str()); } void user::sendMail(mail* mail, std::vector recipients) @@ -70,6 +85,9 @@ void user::sendMail(mail* mail, std::vector recipients) mail->sender = this->name; mail->recipients = recipients; + mail->id = this->sent.size(); + + this->sent.insert(mail); this->user_data["mails"]["sent"][std::to_string(mail->id)] = mail->mailToJson(); for ( auto& user : users ) { @@ -85,6 +103,7 @@ mail* user::getMail(u_int id) void user::saveToFile() { - std::ofstream ofs(this->file_location); - ofs << this->user_data; + printf("%s\n", this->user_data.dump().c_str()); + std::fstream fs(this->file_location); + fs << this->user_data.dump(); } \ No newline at end of file diff --git a/user.h b/user.h index 50eaafc..4f35b6c 100644 --- a/user.h +++ b/user.h @@ -6,8 +6,6 @@ #include #include -#include - struct comp { bool operator()(mail* left, mail* right) const { return *left < *right; }; }; @@ -37,5 +35,5 @@ private: std::string name; maillist inbox; - + maillist sent; }; \ No newline at end of file