added send/add mail functions

This commit is contained in:
Benedikt Galbavy 2023-10-16 14:53:17 +02:00
parent 99e34a1faa
commit 3c04fc5998
3 changed files with 54 additions and 9 deletions

View File

@ -1,24 +1,61 @@
#include "user.h" #include "user.h"
#include "user_handler.h"
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector>
using json = nlohmann::json; using json = nlohmann::json;
user::user(fs::path user_data) user::user(fs::path user_data_json)
{ {
// json stuff go here std::ifstream ifs(user_data_json);
json user = json::parse(ifs);
this->name = user["name"];
} }
user::user(std::string name, fs::path user_dir) user::user(std::string name, fs::path user_dir)
: name(name) : name(name)
{ {
json user; json user;
user["mails"] = json::array(); user["mails"] = 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;
} }
user::~user() {} user::~user() {
}
void user::addMail(mail* mail)
{
mail->id = this->mails.size();
this->mails.insert(mail);
}
void user::sendMail(mail* mail, std::vector<std::string> recipients)
{
user_handler* user_handler = user_handler::getInstance();
std::vector<user*> users;
for ( auto& name : recipients) {
// TODO: error handling for non existing user
users.push_back(user_handler->getUser(name));
}
mail->sender = this->name;
mail->recipients = recipients;
for ( auto& user : users ) {
user->addMail(mail);
}
}
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;
}

17
user.h
View File

@ -6,28 +6,35 @@
#include <set> #include <set>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <vector>
namespace fs = std::filesystem; namespace fs = std::filesystem;
using json = nlohmann::json;
template <typename T> template <typename T>
static const bool ptr_cmp = [](T* left, T* right) { return *left < *right; }; static const bool ptr_cmp = [](T* left, T* right) { return *left < *right; };
typedef std::set<mail*, decltype(ptr_cmp<int>)> maillist; typedef std::set<mail*, decltype(ptr_cmp<mail*>)> maillist;
class user { class user {
public: public:
user(fs::path user_data); user(fs::path user_data_json);
user(std::string name, fs::path user_dir); user(std::string name, fs::path user_dir);
~user(); ~user();
void addMail(mail mail); void addMail(mail* mail);
void sendMail(mail* mail, std::vector<std::string> recipients);
mail* getMail(u_int id);
maillist getMails() { return this->mails; }; maillist getMails() { return this->mails; };
private: private:
const std::string name; json user_data;
std::string name;
maillist mails; maillist mails;
}; };

View File

@ -25,6 +25,7 @@ public:
~user_handler(); ~user_handler();
void setSpoolDir(fs::path p) { this->spool_dir = p; }; void setSpoolDir(fs::path p) { this->spool_dir = p; };
user* getUser(std::string name) { return this->users[name]; };
private: private: