From 40d58ab011965925155b5a0a10721608a01211c5 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Thu, 22 May 2025 19:33:56 +0200 Subject: [PATCH] Create and implemented userManager class --- Makefile | 2 +- src/locations.h | 8 ++++ src/main.cpp | 18 ++++++++- src/pipedata.h | 3 +- src/usermanager.cpp | 14 +++++++ src/usermanager.h | 94 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 src/locations.h create mode 100644 src/usermanager.cpp create mode 100644 src/usermanager.h diff --git a/Makefile b/Makefile index 2e4c366..18f8ee9 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ all: - g++ -std=c++17 src/main.cpp -o ncsambawatcher \ No newline at end of file + g++ -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher \ No newline at end of file diff --git a/src/locations.h b/src/locations.h new file mode 100644 index 0000000..4b3bab0 --- /dev/null +++ b/src/locations.h @@ -0,0 +1,8 @@ +#ifndef _LOCATIONS_H +#define _LOCATIONS_H + +#define LOGFILE "journalctl -u smbd --since now -f" + +#define USER_LOG_LOCATION 3 + +#endif // _LOCATIONS_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8114e36..3eaa220 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #include #include #include @@ -7,10 +10,13 @@ #include #include #include "pipedata.h" - +#include "locations.h" +#include "usermanager.h" int main() { + openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER); + int p1[2]; pipe(p1); @@ -21,7 +27,13 @@ int main() { close(p1[0]); // read - + FILE* logpipe = popen(LOGFILE, "r"); + std::array buffer; + + while(fgets(buffer.data(), buffer.size(), logpipe) != nullptr) + { + std::string line(buffer.data()); + } close(p1[1]); // write } @@ -33,6 +45,8 @@ int main() close(p1[0]); // read } + + closelog(); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/src/pipedata.h b/src/pipedata.h index 6f0f668..34355d4 100644 --- a/src/pipedata.h +++ b/src/pipedata.h @@ -5,7 +5,8 @@ struct pipestruct { - std::string s; + std::string user; + }; #endif // _PIPDATA_H \ No newline at end of file diff --git a/src/usermanager.cpp b/src/usermanager.cpp new file mode 100644 index 0000000..8fcaaf4 --- /dev/null +++ b/src/usermanager.cpp @@ -0,0 +1,14 @@ +#include "usermanager.h" + +std::vector splitLogFile(const std::string& input, char delimiter = '|') +{ + std::vector ret; + std::stringstream ss(input); + std::string token; + + while (std::getline(ss, token, delimiter)) { + ret.push_back(token); + } + + return ret; +} \ No newline at end of file diff --git a/src/usermanager.h b/src/usermanager.h new file mode 100644 index 0000000..972d358 --- /dev/null +++ b/src/usermanager.h @@ -0,0 +1,94 @@ +#ifndef _USERMAN_H +#define _USERMAN_H + +#include +#include +#include +#include +#include "locations.h" + +std::vector splitLogFile(const std::string& input, char delimiter); + +class userManager +{ +private: + std::map users; + +public: + + void addUserFromLogLine(std::string &line) + { + addUser(splitLogFile(line, '|').at(USER_LOG_LOCATION)); + } + + void addUser(std::string &user) + { + if (users.count(user) == 0) + { + users[user] = false; + } + } + + void removeUser(std::string &user) + { + users.erase(user); + } + + bool isContains(std::string &user) + { + return users.count(user) == 1; + } + + void setUserFlagged(std::string &user) + { + if (users.count(user) == 1) + { + users[user] = true; + } + } + + void setUserUnflagged(std::string &user) + { + if (users.count(user) == 1) + { + users[user] = false; + } + } + + void unflagAllUsers() + { + for (std::map::iterator it = users.begin(); it != users.end(); ++it) + { + it->second = false; + } + } + + std::vector getUsers() + { + std::vector ret; + + for (std::map::iterator it = users.begin(); it != users.end(); ++it) + { + ret.push_back(it->first); + } + + return ret; + } + + std::vector getFlaggedUsers() + { + std::vector ret; + + for (std::map::iterator it = users.begin(); it != users.end(); ++it) + { + if (it->second) + { + ret.push_back(it->first); + } + } + + return ret; + } +}; + +#endif // _USERMAN_H \ No newline at end of file