From 3c04fc5998c77d45235c5e48ea6d0d71894f82d8 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Mon, 16 Oct 2023 14:53:17 +0200 Subject: [PATCH] added send/add mail functions --- user.cpp | 45 +++++++++++++++++++++++++++++++++++++++++---- user.h | 17 ++++++++++++----- user_handler.h | 1 + 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/user.cpp b/user.cpp index 73f6657..0f1e543 100644 --- a/user.cpp +++ b/user.cpp @@ -1,24 +1,61 @@ #include "user.h" +#include "user_handler.h" #include #include +#include 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) : name(name) { json user; - user["mails"] = json::array(); + user["mails"] = json::object(); user["name"] = name; std::ofstream ofs(user_dir/(name+".json")); ofs << user; + this->user_data = user; } -user::~user() {} \ No newline at end of file +user::~user() { + +} + +void user::addMail(mail* mail) +{ + mail->id = this->mails.size(); + this->mails.insert(mail); +} + +void user::sendMail(mail* mail, std::vector recipients) +{ + user_handler* user_handler = user_handler::getInstance(); + std::vector 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; +} \ No newline at end of file diff --git a/user.h b/user.h index 308f949..2f1f1b9 100644 --- a/user.h +++ b/user.h @@ -6,28 +6,35 @@ #include #include +#include namespace fs = std::filesystem; +using json = nlohmann::json; -template +template static const bool ptr_cmp = [](T* left, T* right) { return *left < *right; }; -typedef std::set)> maillist; +typedef std::set)> maillist; class user { public: - user(fs::path user_data); + user(fs::path user_data_json); user(std::string name, fs::path user_dir); ~user(); - void addMail(mail mail); + void addMail(mail* mail); + void sendMail(mail* mail, std::vector recipients); + + mail* getMail(u_int id); maillist getMails() { return this->mails; }; private: - const std::string name; + json user_data; + + std::string name; maillist mails; }; \ No newline at end of file diff --git a/user_handler.h b/user_handler.h index 4624c8f..be5a5f8 100644 --- a/user_handler.h +++ b/user_handler.h @@ -25,6 +25,7 @@ public: ~user_handler(); void setSpoolDir(fs::path p) { this->spool_dir = p; }; + user* getUser(std::string name) { return this->users[name]; }; private: