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_handler.h"
#include <fstream>
#include <string>
#include <vector>
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() {}
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;
}

15
user.h
View File

@ -6,28 +6,35 @@
#include <set>
#include <nlohmann/json.hpp>
#include <vector>
namespace fs = std::filesystem;
using json = nlohmann::json;
template <typename T>
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 {
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<std::string> recipients);
mail* getMail(u_int id);
maillist getMails() { return this->mails; };
private:
const std::string name;
json user_data;
std::string name;
maillist mails;
};

View File

@ -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: