fixed saving to json
This commit is contained in:
parent
0118f251a1
commit
1711b3d9d0
2
Makefile
2
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
|
||||
|
||||
22
mail.cpp
22
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;
|
||||
}
|
||||
8
mail.h
8
mail.h
@ -2,12 +2,13 @@
|
||||
|
||||
#include "user_handler.h"
|
||||
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
using json = nlohmann::json;
|
||||
|
||||
@ -21,14 +22,15 @@ struct mail {
|
||||
std::vector<std::string> 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; };
|
||||
|
||||
@ -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)}
|
||||
);
|
||||
|
||||
|
||||
33
user.cpp
33
user.cpp
@ -7,16 +7,17 @@
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
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<std::vector<std::string>>();
|
||||
|
||||
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<std::string> recipients)
|
||||
@ -70,6 +85,9 @@ void user::sendMail(mail* mail, std::vector<std::string> 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();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user