saving userdata to file

This commit is contained in:
Benedikt Galbavy 2023-10-17 18:04:05 +02:00
parent 8021d7c6cb
commit b250ac84da
7 changed files with 56 additions and 4 deletions

View File

@ -22,3 +22,17 @@ void mail::remove()
this->filename = ""; 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;
}

2
mail.h
View File

@ -31,5 +31,7 @@ struct mail {
fs::path getPath() { return this->filename; }; fs::path getPath() { return this->filename; };
json mailToJson();
void remove(); void remove();
}; };

View File

@ -52,6 +52,8 @@ void *clientCommunication(void *data);
inline std::string recvToStr(int __fd, size_t __n, int __flags); inline std::string recvToStr(int __fd, size_t __n, int __flags);
void signalHandler(int sig); void signalHandler(int sig);
inline void exiting();
user_handler* user_handler::instancePtr = nullptr; user_handler* user_handler::instancePtr = nullptr;
int main (int argc, char* argv[]) int main (int argc, char* argv[])
@ -64,12 +66,14 @@ int main (int argc, char* argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
fs::path spool_dir; fs::path spool_dir = fs::path(argv[2]);
user_handler::getInstance()->setSpoolDir(spool_dir = fs::path(argv[2]));
fs::create_directory(spool_dir/"users"); fs::create_directory(spool_dir/"users");
fs::create_directory(spool_dir/"messages"); fs::create_directory(spool_dir/"messages");
user_handler::getInstance()->setSpoolDir(spool_dir);
std::atexit(exiting);
char* p; char* p;
u_long PORT = strtoul(argv[1], &p, 10); u_long PORT = strtoul(argv[1], &p, 10);
@ -362,3 +366,9 @@ std::string get_sha1(const std::string& p_arg)
return std::string(buf); return std::string(buf);
} }
inline void exiting()
{
user_handler::getInstance()->saveAll();
printf("Saving...\n");
}

View File

@ -2,7 +2,9 @@
#include "user_handler.h" #include "user_handler.h"
#include "mail.h" #include "mail.h"
#include <cstdio>
#include <fstream> #include <fstream>
#include <ostream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -20,12 +22,14 @@ user::user(std::string name, fs::path user_dir)
: name(name) : name(name)
{ {
json user; json user;
user["mails"] = json::object(); user["mails"]["sent"] = json::object();
user["mails"]["received"] = json::object();
user["name"] = name; user["name"] = name;
std::ofstream ofs(user_dir/(name+".json")); std::ofstream ofs(user_dir/(name+".json"));
ofs << user; ofs << user;
this->user_data = user; this->user_data = user;
this->file_location = user_dir/(name+".json");
} }
user::~user() { user::~user() {
@ -35,7 +39,9 @@ user::~user() {
void user::addMail(mail* mail) void user::addMail(mail* mail)
{ {
mail->id = this->mails.size(); mail->id = this->mails.size();
this->mails.insert(mail); this->mails.insert(mail);
this->user_data["mails"]["received"][std::to_string(mail->id)] = mail->mailToJson();
} }
void user::sendMail(mail* mail, std::vector<std::string> recipients) void user::sendMail(mail* mail, std::vector<std::string> recipients)
@ -50,6 +56,8 @@ void user::sendMail(mail* mail, std::vector<std::string> recipients)
mail->sender = this->name; mail->sender = this->name;
mail->recipients = recipients; mail->recipients = recipients;
this->user_data["mails"]["sent"][std::to_string(mail->id)] = mail->mailToJson();
for ( auto& user : users ) { for ( auto& user : users ) {
user->addMail(mail); user->addMail(mail);
} }
@ -60,3 +68,9 @@ mail* user::getMail(u_int id)
maillist::iterator it = std::find_if(this->mails.begin(), this->mails.end(), [id](auto& i){ return (*i)(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; return it == this->mails.end() ? nullptr : (*it)->filename.empty() ? nullptr : *it;
} }
void user::saveToFile()
{
std::ofstream ofs(this->file_location);
ofs << this->user_data;
}

3
user.h
View File

@ -30,8 +30,11 @@ public:
mail* getMail(u_int id); mail* getMail(u_int id);
maillist getMails() { return this->mails; }; maillist getMails() { return this->mails; };
void saveToFile();
private: private:
fs::path file_location;
json user_data; json user_data;
std::string name; std::string name;

View File

@ -22,3 +22,10 @@ user* user_handler::getUser(std::string name)
} }
return this->users[name]; return this->users[name];
} }
void user_handler::saveAll()
{
for ( auto& user : this->users ) {
user.second->saveToFile();
}
}

View File

@ -28,6 +28,8 @@ public:
fs::path getSpoolDir() { return this->spool_dir; }; fs::path getSpoolDir() { return this->spool_dir; };
user* getUser(std::string name); user* getUser(std::string name);
void saveAll();
private: private:
static user_handler* instancePtr; static user_handler* instancePtr;