fixed saving to json
This commit is contained in:
parent
0118f251a1
commit
1711b3d9d0
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
|||||||
CC = g++
|
CC = g++
|
||||||
ASAN_FLAGS = -fsanitize=address -fno-omit-frame-pointer -Wno-format-security
|
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
|
LDFLAGS += -fsanitize=address -lpthread
|
||||||
|
|
||||||
TARGET = client server
|
TARGET = client server
|
||||||
|
|||||||
22
mail.cpp
22
mail.cpp
@ -1,5 +1,11 @@
|
|||||||
#include "mail.h"
|
#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) :
|
mail::mail(std::string filename, int64_t timestamp, std::string subject) :
|
||||||
filename(filename),
|
filename(filename),
|
||||||
timestamp(timestamp),
|
timestamp(timestamp),
|
||||||
@ -25,14 +31,14 @@ void mail::remove()
|
|||||||
|
|
||||||
json mail::mailToJson()
|
json mail::mailToJson()
|
||||||
{
|
{
|
||||||
json json;
|
json jsonfile;
|
||||||
|
|
||||||
json["id"] = this->id;
|
jsonfile["id"] = this->id;
|
||||||
json["timestamp"] = this->timestamp;
|
jsonfile["timestamp"] = this->timestamp;
|
||||||
json["sender"] = this->sender;
|
jsonfile["sender"] = this->sender;
|
||||||
json["recipients"] = this->recipients;
|
jsonfile["recipients"] = this->recipients;
|
||||||
json["subject"] = this->subject;
|
jsonfile["subject"] = this->subject;
|
||||||
json["filename"] = this->filename;
|
jsonfile["filename"] = this->filename;
|
||||||
|
|
||||||
return json;
|
return jsonfile;
|
||||||
}
|
}
|
||||||
8
mail.h
8
mail.h
@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
#include "user_handler.h"
|
#include "user_handler.h"
|
||||||
|
|
||||||
#include <nlohmann/json_fwd.hpp>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
@ -21,14 +22,15 @@ struct mail {
|
|||||||
std::vector<std::string> recipients;
|
std::vector<std::string> recipients;
|
||||||
std::string subject;
|
std::string subject;
|
||||||
|
|
||||||
|
mail(std::string filename, std::string subject);
|
||||||
mail(std::string filename, int64_t timestamp, std::string subject);
|
mail(std::string filename, int64_t timestamp, std::string subject);
|
||||||
|
|
||||||
bool operator()(const u_int& id) const {
|
bool operator()(const u_int& id) const {
|
||||||
return id == this->id;
|
return id == this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const mail& left) const {
|
bool operator<(mail& left) const {
|
||||||
return this->timestamp < left.timestamp;
|
return left.timestamp > this->timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path getPath() { return this->filename; };
|
fs::path getPath() { return this->filename; };
|
||||||
|
|||||||
@ -248,7 +248,7 @@ void *clientCommunication(void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
user_handler::getInstance()->getUser(lines.at(1))->sendMail(
|
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)}
|
{lines.at(2)}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
33
user.cpp
33
user.cpp
@ -7,16 +7,17 @@
|
|||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
user::user(fs::path user_data_json)
|
user::user(fs::path user_data_json)
|
||||||
{
|
{
|
||||||
std::ifstream ifs(user_data_json);
|
std::ifstream ifs(user_data_json);
|
||||||
json user = json::parse(ifs);
|
json user_data = json::parse(ifs);
|
||||||
|
|
||||||
this->name = user["name"];
|
this->name = user_data["name"];
|
||||||
for ( auto& mail_json : user["mails"]["received"] ) {
|
for ( auto& mail_json : user_data["mails"]["received"] ) {
|
||||||
mail* mail = new struct mail(
|
mail* mail = new struct mail(
|
||||||
mail_json["filename"],
|
mail_json["filename"],
|
||||||
mail_json["timestamp"],
|
mail_json["timestamp"],
|
||||||
@ -28,6 +29,22 @@ user::user(fs::path user_data_json)
|
|||||||
|
|
||||||
this->inbox.insert(mail);
|
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)
|
user::user(std::string name, fs::path user_dir)
|
||||||
@ -54,8 +71,6 @@ void user::addMail(mail* mail)
|
|||||||
|
|
||||||
this->inbox.insert(mail);
|
this->inbox.insert(mail);
|
||||||
this->user_data["mails"]["received"][std::to_string(mail->id)] = mail->mailToJson();
|
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)
|
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->sender = this->name;
|
||||||
mail->recipients = recipients;
|
mail->recipients = recipients;
|
||||||
|
|
||||||
|
mail->id = this->sent.size();
|
||||||
|
|
||||||
|
this->sent.insert(mail);
|
||||||
this->user_data["mails"]["sent"][std::to_string(mail->id)] = mail->mailToJson();
|
this->user_data["mails"]["sent"][std::to_string(mail->id)] = mail->mailToJson();
|
||||||
|
|
||||||
for ( auto& user : users ) {
|
for ( auto& user : users ) {
|
||||||
@ -85,6 +103,7 @@ mail* user::getMail(u_int id)
|
|||||||
|
|
||||||
void user::saveToFile()
|
void user::saveToFile()
|
||||||
{
|
{
|
||||||
std::ofstream ofs(this->file_location);
|
printf("%s\n", this->user_data.dump().c_str());
|
||||||
ofs << this->user_data;
|
std::fstream fs(this->file_location);
|
||||||
|
fs << this->user_data.dump();
|
||||||
}
|
}
|
||||||
4
user.h
4
user.h
@ -6,8 +6,6 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
|
|
||||||
struct comp {
|
struct comp {
|
||||||
bool operator()(mail* left, mail* right) const { return *left < *right; };
|
bool operator()(mail* left, mail* right) const { return *left < *right; };
|
||||||
};
|
};
|
||||||
@ -37,5 +35,5 @@ private:
|
|||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
maillist inbox;
|
maillist inbox;
|
||||||
|
maillist sent;
|
||||||
};
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user