Create and implemented userManager class
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,2 +1,2 @@
|
|||||||
all:
|
all:
|
||||||
g++ -std=c++17 src/main.cpp -o ncsambawatcher
|
g++ -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher
|
||||||
8
src/locations.h
Normal file
8
src/locations.h
Normal file
@@ -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
|
||||||
18
src/main.cpp
18
src/main.cpp
@@ -1,4 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <array>
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
@@ -7,10 +10,13 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "pipedata.h"
|
#include "pipedata.h"
|
||||||
|
#include "locations.h"
|
||||||
|
#include "usermanager.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
|
||||||
|
|
||||||
int p1[2];
|
int p1[2];
|
||||||
pipe(p1);
|
pipe(p1);
|
||||||
|
|
||||||
@@ -21,7 +27,13 @@ int main()
|
|||||||
{
|
{
|
||||||
close(p1[0]); // read
|
close(p1[0]); // read
|
||||||
|
|
||||||
|
FILE* logpipe = popen(LOGFILE, "r");
|
||||||
|
std::array<char, 256> buffer;
|
||||||
|
|
||||||
|
while(fgets(buffer.data(), buffer.size(), logpipe) != nullptr)
|
||||||
|
{
|
||||||
|
std::string line(buffer.data());
|
||||||
|
}
|
||||||
|
|
||||||
close(p1[1]); // write
|
close(p1[1]); // write
|
||||||
}
|
}
|
||||||
@@ -33,6 +45,8 @@ int main()
|
|||||||
|
|
||||||
close(p1[0]); // read
|
close(p1[0]); // read
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closelog();
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,8 @@
|
|||||||
|
|
||||||
struct pipestruct
|
struct pipestruct
|
||||||
{
|
{
|
||||||
std::string s;
|
std::string user;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _PIPDATA_H
|
#endif // _PIPDATA_H
|
||||||
14
src/usermanager.cpp
Normal file
14
src/usermanager.cpp
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "usermanager.h"
|
||||||
|
|
||||||
|
std::vector<std::string> splitLogFile(const std::string& input, char delimiter = '|')
|
||||||
|
{
|
||||||
|
std::vector<std::string> ret;
|
||||||
|
std::stringstream ss(input);
|
||||||
|
std::string token;
|
||||||
|
|
||||||
|
while (std::getline(ss, token, delimiter)) {
|
||||||
|
ret.push_back(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
94
src/usermanager.h
Normal file
94
src/usermanager.h
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
#ifndef _USERMAN_H
|
||||||
|
#define _USERMAN_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <sstream>
|
||||||
|
#include "locations.h"
|
||||||
|
|
||||||
|
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
|
||||||
|
|
||||||
|
class userManager
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::map<std::string, bool> 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<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
|
{
|
||||||
|
it->second = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> getUsers()
|
||||||
|
{
|
||||||
|
std::vector<std::string> ret;
|
||||||
|
|
||||||
|
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
|
{
|
||||||
|
ret.push_back(it->first);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> getFlaggedUsers()
|
||||||
|
{
|
||||||
|
std::vector<std::string> ret;
|
||||||
|
|
||||||
|
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->second)
|
||||||
|
{
|
||||||
|
ret.push_back(it->first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _USERMAN_H
|
||||||
Reference in New Issue
Block a user