forked from domi91c/Submitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cpp
More file actions
90 lines (87 loc) · 2 KB
/
Copy pathUser.cpp
File metadata and controls
90 lines (87 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <fstream>
#include <iostream>
#include "debug.h"
#include "colors.h"
#include "Command.h"
#include "User.h"
#define WHO_COMMAND_NAME "mwho"
using namespace std;
namespace sict {
User::User() {
m_multipleIp = false;
#ifdef SICT_DEBUG
m_userid.assign("debug_id");
m_uid = 1234;
#else
uid_t uid = getuid();
m_uid = (unsigned int)uid;
struct passwd *pw = getpwuid(uid);
if (pw) {
m_userid.assign(pw->pw_name);
}
else {
m_userid.assign("noid");
m_uid = 0;
}
#endif // SICT_DEBUG
}
bool User::multipleLogins()const {
return m_multipleIp;
}
const string& User::ip()const {
return m_ip;
}
void User::getIP() {
int logins = 0;
string token;
cout << col_yellow << "Please wait, loading user location information...." << endl;
Command C(WHO_COMMAND_NAME);
C += " > who.txt";
C.run();
ifstream who("who.txt");
while (who >> token) {
if (token == m_userid) {
who >> token >> token >> token >> token;
remParentheses(token);
if (logins) {
if (token != m_ip) {
m_multipleIp = true;
m_ip = token;
}
}
else {
m_ip = token;
}
logins++;
}
}
who.clear();
who.close();
cout << "Done!" << endl;
Command("rm who.txt").run();
}
void User::remParentheses(std::string& ip)const {
ip = ip.substr(1, ip.size() - 2);
}
bool User::operator==(unsigned int uid)const {
return uid == m_uid;
}
bool User::operator==(string userid)const {
return m_userid == userid;
}
bool User::operator==(const char* userid)const {
return m_userid == string(userid);
}
bool User::operator==(const User& U)const {
return m_userid == U.m_userid;
}
bool User::valid()const {
return m_uid != 0;
}
std::ostream& User::print(std::ostream& os) const {
return os << m_userid << ": " << m_uid;
}
std::ostream& operator<<(std::ostream& os, const User& U) {
return U.print(os);
}
}