-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserIRC.cpp
140 lines (125 loc) · 3.15 KB
/
UserIRC.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "UserIRC.hpp"
string UserIRC::getModes(void) const {
string modes;
for (string::const_iterator it = MODES::USER_ALL.begin(); it != MODES::USER_ALL.end(); it++) {
if (this->getMode(*it)) { modes += *it; }
}
if (modes.size() > 0)
return "+" + modes;
return "";
}
bool UserIRC::getMode(char mode) const {
switch (mode) {
case MODES::AWAY:
return this->modes.away;
case MODES::INVISIBLE:
return this->modes.invisible;
case MODES::WALLOPS:
return this->modes.wallops;
case MODES::RESTRICTED:
return this->modes.restricted;
case MODES::OPERATOR:
return this->modes.isOperator;
case MODES::LOCAL_OPERATOR:
return this->modes.isLocalOperator;
case MODES::SERVER_NOTICES:
return this->modes.serverNotices;
default:
throw UserModes::UnknownMode();
}
}
void UserIRC::setMode(char mode, bool value) {
switch (mode) {
case MODES::AWAY:
this->modes.away = value;
break;
case MODES::INVISIBLE:
this->modes.invisible = value;
break;
case MODES::WALLOPS:
this->modes.wallops = value;
break;
case MODES::RESTRICTED:
this->modes.restricted = value;
break;
case MODES::OPERATOR:
this->modes.isOperator = value;
break;
case MODES::LOCAL_OPERATOR:
this->modes.isLocalOperator = value;
break;
case MODES::SERVER_NOTICES:
this->modes.serverNotices = value;
break;
default:
throw UserModes::UnknownMode();
}
}
const char* UserModes::UnknownMode::what(void) const throw() {
return "Unknown mode";
}
void UserList::addUser(UserIRC& user)
{
listC.push_back(user);
}
void UserList::removeUser(int& fd)
{
for (list<UserIRC>::iterator it = listC.begin(); it != listC.end(); ++it)
if ((*it).fdSocket == fd)
{
listC.erase(it);
return;
}
}
//accept and fill a newuser that's added to the list (need to fill furthermore the client after)(return the socket)
UserIRC* UserList::acceptNew(const int& endpoint)
{
UserIRC newOne;
newOne.allowed = false;
newOne.idle = time(0);
newOne.fdSocket = accept(endpoint, (sockaddr*)&newOne.addr, &newOne.sockLen);
newOne.ip = getIPAddress(&newOne);
listC.push_back(newOne);
return &listC.back();
}
UserIRC* UserList::findByUsername(const string& value)
{
for (list<UserIRC>::iterator it = listC.begin(); it != listC.end(); ++it)
if ((*it).username == value)
return &(*it);
return 0;
}
UserIRC* UserList::findByNickname(const string& value)
{
for (list<UserIRC>::iterator it = listC.begin(); it != listC.end(); ++it)
if ((*it).nickname == value)
return &(*it);
return 0;
}
UserIRC* UserList::findBySocket(const int& value)
{
for (list<UserIRC>::iterator it = listC.begin(); it != listC.end(); ++it)
if ((*it).fdSocket == value)
return &(*it);
return 0;
}
size_t UserList::size() const
{
return listC.size();
}
// //return the first user that need filling or nothing
// UserIRC* UserList::findFirstUnfilled()
// {
// for (list<UserIRC>::iterator it = listC.begin(); it != listC.end(); ++it)
// if ((*it).needFill)
// return &(*it);
// return 0;
// }
string getIPAddress(const UserIRC* user)
{
if (!user)
return "";
char buffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &user->addr.sin_addr.s_addr, buffer, INET_ADDRSTRLEN);
return buffer;
}