added userhandler (WIP)

This commit is contained in:
Benedikt Galbavy 2023-10-16 00:08:59 +02:00
parent 9ef789caf3
commit ddb1c277a3
6 changed files with 74 additions and 4 deletions

4
.gitignore vendored
View File

@ -1,5 +1,9 @@
twmailer-server
twmailer-client
spool
.cache
compile_commands.json
# Prerequisites
*.d

View File

@ -1,4 +1,5 @@
#include "user.h"
#include "user_handler.h"
#include <cstddef>
#include <cstdio>
@ -34,6 +35,8 @@ inline bool isInteger(const std::string & s);
void *clientCommunication(void *data);
void signalHandler(int sig);
user_handler* user_handler::instancePtr = nullptr;
int main (int argc, char* argv[])
{
if (argc < 3 ||
@ -44,6 +47,12 @@ int main (int argc, char* argv[])
return EXIT_FAILURE;
}
fs::path spool_dir;
user_handler::getInstance()->setSpoolDir(spool_dir = fs::path(argv[2]));
fs::create_directory(spool_dir/"users");
fs::create_directory(spool_dir/"messages");
char* p;
u_long PORT = strtoul(argv[1], &p, 10);

View File

@ -1,3 +1,8 @@
#include "user.h"
user::user(fs::path)
{
// json stuff go here
}
user::~user() {}

12
user.h
View File

@ -5,6 +5,9 @@
#include <string>
#include <set>
#include <nlohmann/json.hpp>
namespace fs = std::filesystem;
template <typename T>
static const bool ptr_cmp = [](T* left, T* right) { return *left < *right; };
@ -14,13 +17,14 @@ typedef std::set<mail*, decltype(ptr_cmp<int>)> maillist;
class user {
public:
user(std::string name, maillist mails)
: name(name), mails(mails)
{};
user(fs::path);
user(std::string name)
: name(name) {};
~user();
void addMail(mail mail);
std::set<mail*> getMails();
maillist getMails() { return this->mails; };
private:

12
user_handler.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "user_handler.h"
#include <string>
user_handler::user_handler()
{
for (const auto& entry : fs::directory_iterator()) {
if (entry.path().extension() == ".json") {
this->users.insert(std::pair<std::string, user*>(fs::path(entry.path()).replace_extension(), new user(entry)));
}
}
}

36
user_handler.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include "user.h"
#include <filesystem>
#include <map>
#include <string>
namespace fs = std::filesystem;
/* singleton for handling users */
class user_handler {
public:
user_handler(const user_handler& obj) = delete;
static user_handler* getInstance() {
if (instancePtr == nullptr) {
instancePtr = new user_handler();
return instancePtr;
} else {
return instancePtr;
}
};
~user_handler();
void setSpoolDir(fs::path p) { this->spool_dir = p; };
private:
static user_handler* instancePtr;
user_handler();
fs::path spool_dir;
std::map<std::string, user*> users;
};