saving userdata to file
This commit is contained in:
parent
8021d7c6cb
commit
b250ac84da
14
mail.cpp
14
mail.cpp
@ -22,3 +22,17 @@ void mail::remove()
|
||||
|
||||
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
2
mail.h
@ -31,5 +31,7 @@ struct mail {
|
||||
|
||||
fs::path getPath() { return this->filename; };
|
||||
|
||||
json mailToJson();
|
||||
|
||||
void remove();
|
||||
};
|
||||
16
server.cpp
16
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);
|
||||
|
||||
@ -362,3 +366,9 @@ std::string get_sha1(const std::string& p_arg)
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
inline void exiting()
|
||||
{
|
||||
user_handler::getInstance()->saveAll();
|
||||
printf("Saving...\n");
|
||||
}
|
||||
16
user.cpp
16
user.cpp
@ -2,7 +2,9 @@
|
||||
#include "user_handler.h"
|
||||
#include "mail.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@ -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<std::string> recipients)
|
||||
@ -50,6 +56,8 @@ void user::sendMail(mail* mail, std::vector<std::string> 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);
|
||||
}
|
||||
@ -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); });
|
||||
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
3
user.h
@ -30,8 +30,11 @@ public:
|
||||
mail* getMail(u_int id);
|
||||
maillist getMails() { return this->mails; };
|
||||
|
||||
void saveToFile();
|
||||
|
||||
private:
|
||||
|
||||
fs::path file_location;
|
||||
json user_data;
|
||||
|
||||
std::string name;
|
||||
|
||||
@ -22,3 +22,10 @@ user* user_handler::getUser(std::string name)
|
||||
}
|
||||
return this->users[name];
|
||||
}
|
||||
|
||||
void user_handler::saveAll()
|
||||
{
|
||||
for ( auto& user : this->users ) {
|
||||
user.second->saveToFile();
|
||||
}
|
||||
}
|
||||
@ -28,6 +28,8 @@ public:
|
||||
fs::path getSpoolDir() { return this->spool_dir; };
|
||||
user* getUser(std::string name);
|
||||
|
||||
void saveAll();
|
||||
|
||||
private:
|
||||
|
||||
static user_handler* instancePtr;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user