From b250ac84da436cf7dcff21a06b1a07cc319d4ad3 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Tue, 17 Oct 2023 18:04:05 +0200 Subject: [PATCH] saving userdata to file --- mail.cpp | 14 ++++++++++++++ mail.h | 2 ++ server.cpp | 16 +++++++++++++--- user.cpp | 16 +++++++++++++++- user.h | 3 +++ user_handler.cpp | 7 +++++++ user_handler.h | 2 ++ 7 files changed, 56 insertions(+), 4 deletions(-) diff --git a/mail.cpp b/mail.cpp index 19e3d00..474622c 100644 --- a/mail.cpp +++ b/mail.cpp @@ -21,4 +21,18 @@ void mail::remove() std::remove((user_handler::getInstance()->getSpoolDir()/"objects"/fs::path(this->filename.insert(2, "/"))).c_str()); this->filename = ""; +} + +json mail::mailToJson() +{ + json json; + + json["id"] = this->id; + json["timestamp"] = this->timestamp; + json["sender"] = this->sender; + json["recipients"] = this->recipients; + json["subject"] = this->subject; + json["filename"] = this->filename; + + return json; } \ No newline at end of file diff --git a/mail.h b/mail.h index 7920941..5b27bda 100644 --- a/mail.h +++ b/mail.h @@ -31,5 +31,7 @@ struct mail { fs::path getPath() { return this->filename; }; + json mailToJson(); + void remove(); }; \ No newline at end of file diff --git a/server.cpp b/server.cpp index 9697bc3..ada99c1 100644 --- a/server.cpp +++ b/server.cpp @@ -52,6 +52,8 @@ void *clientCommunication(void *data); inline std::string recvToStr(int __fd, size_t __n, int __flags); void signalHandler(int sig); +inline void exiting(); + user_handler* user_handler::instancePtr = nullptr; int main (int argc, char* argv[]) @@ -64,12 +66,14 @@ int main (int argc, char* argv[]) return EXIT_FAILURE; } - fs::path spool_dir; - user_handler::getInstance()->setSpoolDir(spool_dir = fs::path(argv[2])); - + fs::path spool_dir = fs::path(argv[2]); fs::create_directory(spool_dir/"users"); fs::create_directory(spool_dir/"messages"); + user_handler::getInstance()->setSpoolDir(spool_dir); + + std::atexit(exiting); + char* p; u_long PORT = strtoul(argv[1], &p, 10); @@ -361,4 +365,10 @@ std::string get_sha1(const std::string& p_arg) } return std::string(buf); +} + +inline void exiting() +{ + user_handler::getInstance()->saveAll(); + printf("Saving...\n"); } \ No newline at end of file diff --git a/user.cpp b/user.cpp index 95f853d..200c010 100644 --- a/user.cpp +++ b/user.cpp @@ -2,7 +2,9 @@ #include "user_handler.h" #include "mail.h" +#include #include +#include #include #include @@ -20,12 +22,14 @@ user::user(std::string name, fs::path user_dir) : name(name) { json user; - user["mails"] = json::object(); + user["mails"]["sent"] = json::object(); + user["mails"]["received"] = json::object(); user["name"] = name; std::ofstream ofs(user_dir/(name+".json")); ofs << user; this->user_data = user; + this->file_location = user_dir/(name+".json"); } user::~user() { @@ -35,7 +39,9 @@ user::~user() { void user::addMail(mail* mail) { mail->id = this->mails.size(); + this->mails.insert(mail); + this->user_data["mails"]["received"][std::to_string(mail->id)] = mail->mailToJson(); } void user::sendMail(mail* mail, std::vector recipients) @@ -50,6 +56,8 @@ void user::sendMail(mail* mail, std::vector recipients) mail->sender = this->name; mail->recipients = recipients; + this->user_data["mails"]["sent"][std::to_string(mail->id)] = mail->mailToJson(); + for ( auto& user : users ) { user->addMail(mail); } @@ -59,4 +67,10 @@ mail* user::getMail(u_int id) { maillist::iterator it = std::find_if(this->mails.begin(), this->mails.end(), [id](auto& i){ return (*i)(id); }); return it == this->mails.end() ? nullptr : (*it)->filename.empty() ? nullptr : *it; +} + +void user::saveToFile() +{ + std::ofstream ofs(this->file_location); + ofs << this->user_data; } \ No newline at end of file diff --git a/user.h b/user.h index 59c75f8..0c2aa76 100644 --- a/user.h +++ b/user.h @@ -29,9 +29,12 @@ public: mail* getMail(u_int id); maillist getMails() { return this->mails; }; + + void saveToFile(); private: + fs::path file_location; json user_data; std::string name; diff --git a/user_handler.cpp b/user_handler.cpp index cc84aa4..c5f6461 100644 --- a/user_handler.cpp +++ b/user_handler.cpp @@ -21,4 +21,11 @@ user* user_handler::getUser(std::string name) new user(name, this->spool_dir/"users"); } return this->users[name]; +} + +void user_handler::saveAll() +{ + for ( auto& user : this->users ) { + user.second->saveToFile(); + } } \ No newline at end of file diff --git a/user_handler.h b/user_handler.h index 0d856b0..f18395f 100644 --- a/user_handler.h +++ b/user_handler.h @@ -28,6 +28,8 @@ public: fs::path getSpoolDir() { return this->spool_dir; }; user* getUser(std::string name); + void saveAll(); + private: static user_handler* instancePtr;