-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode.cpp
More file actions
175 lines (166 loc) · 4.09 KB
/
Copy pathmode.cpp
File metadata and controls
175 lines (166 loc) · 4.09 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "gen.hpp"
void handleModeI(char mode, Channel &channel, std::string &args, User &from)
{
(void)args;
(void)from;
std::cout << "\033[36mMode 'i' traité.\033[0m" << std::endl;
if (mode == '+')
{
channel.setInvitmode(true);
channel.addmode('i');
}
else if (mode == '-')
{
channel.setInvitmode(false);
channel.submode('i');
}
std::cout << "Channel invitmode: " << std::boolalpha << channel.getInvitmode() << std::endl;
}
void handleModeT(char mode, Channel &channel, std::string &args, User &from)
{
(void)from;
(void)args;
std::cout << "\033[36mMode 't' traité.\033[0m" << std::endl;
if (mode == '+')
{
channel.setTopadm(true);
channel.addmode('t');
}
else if (mode == '-')
{
channel.setTopadm(false);
channel.submode('t');
}
std::cout << "Channel Topic admin only: " << std::boolalpha << channel.getTopadm() << std::endl;
}
void handleModeK(char mode, Channel &channel, std::string &args, User &from)
{
(void)from;
std::cout << "\033[36mMode 'k' traité.\033[0m" << std::endl;
if (mode == '+')
{
std::istringstream iss(args);
std::string key, rest;
iss >> key;
iss >> rest;
channel.setPass(key);
channel.addmode('k');
args.erase(0, key.size() + 1);
}
else if (mode == '-')
{
channel.setPass("");
channel.submode('k');
}
std::cout << "Channel Pass:" << channel.getPass() << std::endl;
}
void handleModeO(char mode, Channel &channel, std::string &args, User &from)
{
(void)from;
std::cout << "\033[36mMode 'o' traité.\033[0m" << std::endl;
if (mode == '+')
{
std::istringstream iss(args);
std::string op;
iss >> op;
if (!channel.findOperator(channel.findUser(op)) && channel.findUser(op))
{
channel.operators.push_back(channel.findUser(op));
std::cout << "User:" << op << " become operator!" << std::endl;
}
args.erase(0, op.size() + 1);
}
else if (mode == '-')
{
std::istringstream iss(args);
std::string op;
iss >> op;
if (channel.findOperator(channel.findUser(op)))
{
for (std::vector<int>::iterator it = channel.operators.begin(); it != channel.operators.end();)
{
if (*it == channel.findUser(op))
channel.operators.erase(it);
else
++it;
}
}
args.erase(0, op.size() + 1);
}
}
// Fonction pour gérer le mode 'l'
void handleModeL(char mode, Channel &channel, std::string &args, User &from)
{
(void)from;
std::cout << "\033[36mMode 'l' traité.\033[0m" << mode << std::endl;
if (mode == '+')
{
std::istringstream iss(args);
int key;
iss >> key;
if ((iss.rdstate() & std::ifstream::failbit) != 0)
return;
if (key > 0)
{
channel.setMaxuser(key);
channel.addmode('l');
channel.setChannelsize(true);
}
while (key > 9)
{
args.erase(0, 1);
key = key / 10;
}
args.erase(0, 1);
}
else if (mode == '-')
{
channel.setChannelsize(false);
channel.setMaxuser(1);
channel.submode('l');
}
std::cout << "set max user: " << channel.getMaxUser() << std::endl;
}
typedef void (*ModeHandler)(char, Channel &, std::string &, User &);
void modeHandlersAttribution(std::map<char, ModeHandler> &modeHandlers)
{
modeHandlers['i'] = handleModeI;
modeHandlers['t'] = handleModeT;
modeHandlers['k'] = handleModeK;
modeHandlers['o'] = handleModeO;
modeHandlers['l'] = handleModeL;
}
void handleChannelModes(Channel &channel, const std::string &modestring, User from)
{
std::map<char, ModeHandler> modeHandlers;
std::map<char, ModeHandler>::iterator it;
std::istringstream iss(modestring);
std::string options, tmp;
ModeHandler handler;
char mode;
modeHandlersAttribution(modeHandlers);
iss >> mode;
iss >> options;
iss >> tmp;
if (tmp[0] == '-')
options += tmp;
size_t argsPos = modestring.find(options) + options.length() + 1;
std::string args = (argsPos < modestring.length()) ? modestring.substr(argsPos) : "";
for (size_t i = 0; options[i]; i++)
{
if (options[i] == '-')
{
mode = '-';
continue;
}
it = modeHandlers.find(options[i]);
if (it != modeHandlers.end())
{
handler = it->second;
handler(mode, channel, args, from);
}
else
break;
}
send(from.getSocket(), std::string(RPL_CHANNELMODEIS).c_str(), std::string(RPL_CHANNELMODEIS).length(), 0);
}