fixes to delete and removed sender entirely
This commit is contained in:
parent
a2b17e6dd6
commit
b96a4c8962
@ -1,4 +1,5 @@
|
|||||||
#include "mail.h"
|
#include "mail.h"
|
||||||
|
#include <cstdio>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
mail::mail(std::string filename, std::string subject) :
|
mail::mail(std::string filename, std::string subject) :
|
||||||
@ -40,8 +41,7 @@ json mail::mailToJson()
|
|||||||
|
|
||||||
jsonfile["id"] = this->id;
|
jsonfile["id"] = this->id;
|
||||||
jsonfile["timestamp"] = this->timestamp;
|
jsonfile["timestamp"] = this->timestamp;
|
||||||
jsonfile["sender"] = this->sender;
|
jsonfile["recipient"] = this->recipient;
|
||||||
jsonfile["recipients"] = this->recipient;
|
|
||||||
jsonfile["subject"] = this->subject;
|
jsonfile["subject"] = this->subject;
|
||||||
jsonfile["filename"] = this->filename;
|
jsonfile["filename"] = this->filename;
|
||||||
jsonfile["deleted"] = this->deleted;
|
jsonfile["deleted"] = this->deleted;
|
||||||
|
|||||||
@ -17,7 +17,6 @@ struct mail {
|
|||||||
/* metadata */
|
/* metadata */
|
||||||
u_int id;
|
u_int id;
|
||||||
int64_t timestamp;
|
int64_t timestamp;
|
||||||
std::string sender;
|
|
||||||
std::string recipient;
|
std::string recipient;
|
||||||
std::string subject;
|
std::string subject;
|
||||||
|
|
||||||
|
|||||||
@ -387,9 +387,8 @@ std::string cmdSEND(std::vector<std::string>& received)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
user_handler::getInstance().getOrCreateUser(received.at(1))->sendMail(
|
user_handler::getInstance().getOrCreateUser(received.at(2))->addMail(
|
||||||
new struct mail(saveToFile(user_handler::getInstance().getSpoolDir()/"messages", received.at(4)), received.at(3)),
|
new struct mail(saveToFile(user_handler::getInstance().getSpoolDir()/"messages", received.at(4)), received.at(3))
|
||||||
received.at(2)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return "OK\n"; // TODO: error handling
|
return "OK\n"; // TODO: error handling
|
||||||
@ -429,14 +428,14 @@ std::string cmdREAD(std::vector<std::string>& received)
|
|||||||
mail->deleted)
|
mail->deleted)
|
||||||
return "ERR\n";
|
return "ERR\n";
|
||||||
|
|
||||||
/*try */{
|
try {
|
||||||
std::string path_str = user_handler::getInstance().getSpoolDir()/"messages"/mail->filename;
|
std::string path_str = user_handler::getInstance().getSpoolDir()/"messages"/mail->filename;
|
||||||
std::lock_guard<std::mutex> guard(mail->m_file);
|
std::unique_lock<std::mutex> lock(mail->m_file);
|
||||||
response.append(read_file(path_str)).append("\n");
|
response.append(read_file(path_str)).append("\n");
|
||||||
}
|
}
|
||||||
/*catch (...) { // TODO: more specific error handling - then again, it will respond with ERR either way
|
catch (...) { // TODO: more specific error handling - then again, it will respond with ERR either way
|
||||||
return "ERR\n";
|
return "ERR\n";
|
||||||
}*/
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,34 +18,19 @@ user::user(fs::path user_data_json) : m()
|
|||||||
json user_data = json::parse(ifs);
|
json user_data = json::parse(ifs);
|
||||||
|
|
||||||
this->name = user_data["name"];
|
this->name = user_data["name"];
|
||||||
for ( auto& mail_json : user_data["mails"]["received"] ) {
|
for ( auto& mail_json : user_data["mails"] ) {
|
||||||
mail* mail = new struct mail(
|
mail* mail = new struct mail(
|
||||||
mail_json["filename"],
|
mail_json["filename"],
|
||||||
mail_json["timestamp"],
|
mail_json["timestamp"],
|
||||||
mail_json["subject"]
|
mail_json["subject"]
|
||||||
);
|
);
|
||||||
mail->id = mail_json["id"];
|
mail->id = mail_json["id"];
|
||||||
mail->sender = mail_json["sender"];
|
|
||||||
mail->recipient = mail_json["recipient"];
|
mail->recipient = mail_json["recipient"];
|
||||||
mail->deleted = mail_json["deleted"];
|
mail->deleted = mail_json["deleted"];
|
||||||
|
|
||||||
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>>();
|
|
||||||
mail->deleted = mail_json["deleted"];
|
|
||||||
|
|
||||||
this->sent.insert(mail);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
this->user_data = user_data;
|
this->user_data = user_data;
|
||||||
this->file_location = user_data_json;
|
this->file_location = user_data_json;
|
||||||
}
|
}
|
||||||
@ -55,8 +40,7 @@ user::user(std::string name, fs::path user_dir)
|
|||||||
m()
|
m()
|
||||||
{
|
{
|
||||||
json user;
|
json user;
|
||||||
user["mails"]["sent"] = json::object();
|
user["mails"] = 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"));
|
||||||
@ -69,45 +53,29 @@ user::~user() {
|
|||||||
for (auto& mail : this->inbox) {
|
for (auto& mail : this->inbox) {
|
||||||
delete(mail);
|
delete(mail);
|
||||||
}
|
}
|
||||||
for (auto& mail : this->sent) { // depricated
|
|
||||||
delete(mail);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void user::addMail(mail* mail)
|
void user::addMail(mail* mail)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(this->m);
|
std::unique_lock<std::shared_mutex> lock(this->m);
|
||||||
|
|
||||||
mail->id = this->inbox.size();
|
mail->id = this->inbox.size();
|
||||||
|
mail->recipient = this->name;
|
||||||
|
|
||||||
this->inbox.insert(mail);
|
this->inbox.insert(mail);
|
||||||
this->user_data["mails"]["received"][std::to_string(mail->id)] = mail->mailToJson();
|
this->user_data["mails"][std::to_string(mail->id)] = mail->mailToJson();
|
||||||
}
|
|
||||||
|
|
||||||
void user::sendMail(mail* mail, std::string recipient)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> guard(this->m);
|
|
||||||
|
|
||||||
mail->sender = this->name;
|
|
||||||
mail->recipient = recipient;
|
|
||||||
|
|
||||||
mail->id = this->sent.size();
|
|
||||||
|
|
||||||
this->sent.insert(mail);
|
|
||||||
this->user_data["mails"]["sent"][std::to_string(mail->id)] = mail->mailToJson();
|
|
||||||
|
|
||||||
user_handler::getInstance().getOrCreateUser(recipient)->addMail(mail);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mail* user::getMail(u_int id)
|
mail* user::getMail(u_int id)
|
||||||
{
|
{
|
||||||
|
std::shared_lock<std::shared_mutex> lock(this->m);
|
||||||
maillist::iterator it = std::find_if(this->inbox.begin(), this->inbox.end(), [id](auto& i){ return (*i)(id); });
|
maillist::iterator it = std::find_if(this->inbox.begin(), this->inbox.end(), [id](auto& i){ return (*i)(id); });
|
||||||
return it == this->inbox.end() ? nullptr : (*it)->filename.empty() ? nullptr : *it; // TODO: potentially not thread safe, research if iterator points to
|
return it == this->inbox.end() ? nullptr : (*it)->filename.empty() ? nullptr : *it; // TODO: potentially not thread safe, research if iterator points to
|
||||||
}
|
}
|
||||||
|
|
||||||
bool user::delMail(u_int id)
|
bool user::delMail(u_int id)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(this->m);
|
std::unique_lock<std::shared_mutex> lock(this->m);
|
||||||
|
|
||||||
maillist::iterator it = std::find_if(this->inbox.begin(), this->inbox.end(), [id](auto& i){ return (*i)(id); });
|
maillist::iterator it = std::find_if(this->inbox.begin(), this->inbox.end(), [id](auto& i){ return (*i)(id); });
|
||||||
|
|
||||||
@ -117,13 +85,15 @@ bool user::delMail(u_int id)
|
|||||||
(*it)->deleted)
|
(*it)->deleted)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!(*it)->filename.empty())
|
if (!(*it)->filename.empty()) {
|
||||||
|
std::unique_lock<std::mutex> lock((*it)->m_file);
|
||||||
success = fs::remove(user_handler::getInstance().getSpoolDir()/"messages"/(*it)->filename);
|
success = fs::remove(user_handler::getInstance().getSpoolDir()/"messages"/(*it)->filename);
|
||||||
|
}
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
this->user_data["mails"]["received"][std::to_string((*it)->id)]["subject"] = "";
|
this->user_data["mails"][std::to_string((*it)->id)]["subject"] = "";
|
||||||
this->user_data["mails"]["received"][std::to_string((*it)->id)]["filename"] = "";
|
this->user_data["mails"][std::to_string((*it)->id)]["filename"] = "";
|
||||||
this->user_data["mails"]["received"][std::to_string((*it)->id)]["deleted"] = true;
|
this->user_data["mails"][std::to_string((*it)->id)]["deleted"] = true;
|
||||||
(*it)->deleted = true; // other info will be deleted on shutdown
|
(*it)->deleted = true; // other info will be deleted on shutdown
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,6 +102,8 @@ bool user::delMail(u_int id)
|
|||||||
|
|
||||||
void user::saveToFile()
|
void user::saveToFile()
|
||||||
{
|
{
|
||||||
std::fstream fs(this->file_location);
|
std::shared_lock<std::shared_mutex> lock(this->m);
|
||||||
fs << this->user_data.dump();
|
|
||||||
|
std::ofstream ofs(this->file_location, std::ofstream::out | std::ofstream::trunc);
|
||||||
|
ofs << this->user_data.dump();
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "mail.h"
|
#include "mail.h"
|
||||||
|
|
||||||
#include <mutex>
|
#include <shared_mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -22,7 +22,6 @@ public:
|
|||||||
~user();
|
~user();
|
||||||
|
|
||||||
void addMail(mail* mail);
|
void addMail(mail* mail);
|
||||||
void sendMail(mail* mail, std::string recipient);
|
|
||||||
|
|
||||||
mail* getMail(u_int id);
|
mail* getMail(u_int id);
|
||||||
bool delMail(u_int id);
|
bool delMail(u_int id);
|
||||||
@ -39,5 +38,5 @@ private:
|
|||||||
maillist inbox;
|
maillist inbox;
|
||||||
maillist sent;
|
maillist sent;
|
||||||
|
|
||||||
std::mutex m;
|
std::shared_mutex m;
|
||||||
};
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user