From b96a4c896269b5f603459c5135e6ed60396e362e Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Fri, 17 Nov 2023 14:27:22 +0100 Subject: [PATCH] fixes to delete and removed sender entirely --- server/mail.cpp | 4 +-- server/mail.h | 1 - server/server.cpp | 13 +++++----- server/user.cpp | 62 +++++++++++++---------------------------------- server/user.h | 5 ++-- 5 files changed, 27 insertions(+), 58 deletions(-) diff --git a/server/mail.cpp b/server/mail.cpp index 0238c9d..3178861 100644 --- a/server/mail.cpp +++ b/server/mail.cpp @@ -1,4 +1,5 @@ #include "mail.h" +#include #include mail::mail(std::string filename, std::string subject) : @@ -40,8 +41,7 @@ json mail::mailToJson() jsonfile["id"] = this->id; jsonfile["timestamp"] = this->timestamp; - jsonfile["sender"] = this->sender; - jsonfile["recipients"] = this->recipient; + jsonfile["recipient"] = this->recipient; jsonfile["subject"] = this->subject; jsonfile["filename"] = this->filename; jsonfile["deleted"] = this->deleted; diff --git a/server/mail.h b/server/mail.h index aef398f..2d8a83c 100644 --- a/server/mail.h +++ b/server/mail.h @@ -17,7 +17,6 @@ struct mail { /* metadata */ u_int id; int64_t timestamp; - std::string sender; std::string recipient; std::string subject; diff --git a/server/server.cpp b/server/server.cpp index 90cde95..34cceeb 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -387,9 +387,8 @@ std::string cmdSEND(std::vector& received) } } - user_handler::getInstance().getOrCreateUser(received.at(1))->sendMail( - new struct mail(saveToFile(user_handler::getInstance().getSpoolDir()/"messages", received.at(4)), received.at(3)), - received.at(2) + user_handler::getInstance().getOrCreateUser(received.at(2))->addMail( + new struct mail(saveToFile(user_handler::getInstance().getSpoolDir()/"messages", received.at(4)), received.at(3)) ); return "OK\n"; // TODO: error handling @@ -429,14 +428,14 @@ std::string cmdREAD(std::vector& received) mail->deleted) return "ERR\n"; - /*try */{ + try { std::string path_str = user_handler::getInstance().getSpoolDir()/"messages"/mail->filename; - std::lock_guard guard(mail->m_file); + std::unique_lock lock(mail->m_file); response.append(read_file(path_str)).append("\n"); } - /*catch (...) { // TODO: more specific error handling - then again, it will respond with ERR either way + catch (...) { // TODO: more specific error handling - then again, it will respond with ERR either way return "ERR\n"; - }*/ + } return response; } diff --git a/server/user.cpp b/server/user.cpp index 3b9da31..402acc5 100644 --- a/server/user.cpp +++ b/server/user.cpp @@ -18,34 +18,19 @@ user::user(fs::path user_data_json) : m() json user_data = json::parse(ifs); this->name = user_data["name"]; - for ( auto& mail_json : user_data["mails"]["received"] ) { + for ( auto& mail_json : user_data["mails"] ) { 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->recipient = mail_json["recipient"]; mail->deleted = mail_json["deleted"]; 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>(); - mail->deleted = mail_json["deleted"]; - - this->sent.insert(mail); - }*/ - this->user_data = user_data; this->file_location = user_data_json; } @@ -55,8 +40,7 @@ user::user(std::string name, fs::path user_dir) m() { json user; - user["mails"]["sent"] = json::object(); - user["mails"]["received"] = json::object(); + user["mails"] = json::object(); user["name"] = name; std::ofstream ofs(user_dir/(name+".json")); @@ -69,45 +53,29 @@ user::~user() { for (auto& mail : this->inbox) { delete(mail); } - for (auto& mail : this->sent) { // depricated - delete(mail); - } } void user::addMail(mail* mail) { - std::lock_guard guard(this->m); + std::unique_lock lock(this->m); mail->id = this->inbox.size(); + mail->recipient = this->name; this->inbox.insert(mail); - this->user_data["mails"]["received"][std::to_string(mail->id)] = mail->mailToJson(); -} - -void user::sendMail(mail* mail, std::string recipient) -{ - std::lock_guard guard(this->m); - - mail->sender = this->name; - mail->recipient = recipient; - - mail->id = this->sent.size(); - - this->sent.insert(mail); - this->user_data["mails"]["sent"][std::to_string(mail->id)] = mail->mailToJson(); - - user_handler::getInstance().getOrCreateUser(recipient)->addMail(mail); + this->user_data["mails"][std::to_string(mail->id)] = mail->mailToJson(); } mail* user::getMail(u_int id) { + std::shared_lock lock(this->m); maillist::iterator it = std::find_if(this->inbox.begin(), this->inbox.end(), [id](auto& i){ return (*i)(id); }); return it == this->inbox.end() ? nullptr : (*it)->filename.empty() ? nullptr : *it; // TODO: potentially not thread safe, research if iterator points to } bool user::delMail(u_int id) { - std::lock_guard guard(this->m); + std::unique_lock lock(this->m); maillist::iterator it = std::find_if(this->inbox.begin(), this->inbox.end(), [id](auto& i){ return (*i)(id); }); @@ -117,13 +85,15 @@ bool user::delMail(u_int id) (*it)->deleted) return false; - if (!(*it)->filename.empty()) + if (!(*it)->filename.empty()) { + std::unique_lock lock((*it)->m_file); success = fs::remove(user_handler::getInstance().getSpoolDir()/"messages"/(*it)->filename); + } if (success) { - this->user_data["mails"]["received"][std::to_string((*it)->id)]["subject"] = ""; - this->user_data["mails"]["received"][std::to_string((*it)->id)]["filename"] = ""; - this->user_data["mails"]["received"][std::to_string((*it)->id)]["deleted"] = true; + this->user_data["mails"][std::to_string((*it)->id)]["subject"] = ""; + this->user_data["mails"][std::to_string((*it)->id)]["filename"] = ""; + this->user_data["mails"][std::to_string((*it)->id)]["deleted"] = true; (*it)->deleted = true; // other info will be deleted on shutdown } @@ -132,6 +102,8 @@ bool user::delMail(u_int id) void user::saveToFile() { - std::fstream fs(this->file_location); - fs << this->user_data.dump(); + std::shared_lock lock(this->m); + + std::ofstream ofs(this->file_location, std::ofstream::out | std::ofstream::trunc); + ofs << this->user_data.dump(); } \ No newline at end of file diff --git a/server/user.h b/server/user.h index dc1216e..792635b 100644 --- a/server/user.h +++ b/server/user.h @@ -2,7 +2,7 @@ #include "mail.h" -#include +#include #include #include #include @@ -22,7 +22,6 @@ public: ~user(); void addMail(mail* mail); - void sendMail(mail* mail, std::string recipient); mail* getMail(u_int id); bool delMail(u_int id); @@ -39,5 +38,5 @@ private: maillist inbox; maillist sent; - std::mutex m; + std::shared_mutex m; }; \ No newline at end of file