-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
194 lines (169 loc) · 4.55 KB
/
Copy pathmain.cpp
File metadata and controls
194 lines (169 loc) · 4.55 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "ft_irc.hpp"
#include "./Server/Server.hpp"
#include "./Bot/BravyBot.hpp"
static bool mainServer(int argc, char *argv[]);
static bool mainBravyBot(int argc, char *argv[]);
int main(int argc, char *argv[])
{
srand(time(0));
if (IS_SERVER)
return mainServer(argc, argv);
else
{
return mainBravyBot(argc, argv);
}
return 0;
}
static bool mainServer(int argc, char *argv[])
{
if (argc != 3)
{
std::cerr << "Error: usage: " << argv[0] << " <port> <password>" << std::endl;
return 1;
}
if (std::string(argv[1]).find_first_not_of("0123456789") != std::string::npos
|| strToNum<int>(argv[1]) < 1024 || strToNum<int>(argv[1]) > 49151)
{
std::cerr << "Error: port must be an integer between 1024 and 49151" << std::endl;
return 1;
}
if (argv[2][0] == '\0')
{
std::cerr << "Error: password cannot be empty" << std::endl;
return 1;
}
Server server(strToNum<int>(argv[1]), argv[2]);
try
{
server.run();
server.listenClients();
}
catch (const Server::ServerException &e)
{
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
static bool mainBravyBot(int argc, char *argv[])
{
if (argc != 4)
{
std::cerr << "Error: usage: " << argv[0] << " <ip> <port> <password>" << std::endl;
return 1;
}
if (std::string(argv[2]).find_first_not_of("0123456789") != std::string::npos
|| strToNum<int>(argv[2]) < 1024 || strToNum<int>(argv[2]) > 49151)
{
std::cerr << "Error: port must be an integer between 1024 and 49151" << std::endl;
return 1;
}
if (argv[3][0] == '\0')
{
std::cerr << "Error: password cannot be empty" << std::endl;
return 1;
}
BravyBot bot(argv[1], strToNum<int>(argv[2]), argv[3]);
try
{
bot.run();
}
catch (const BravyBot::BravyBotException &e)
{
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
std::vector<std::string> split(const std::string &str, char delim)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(str);
while (std::getline(tokenStream, token, delim))
tokens.push_back(token);
return tokens;
}
int countWords(const std::string &str)
{
std::stringstream ss(str);
std::string word;
int count = 0;
while (ss >> word)
count++;
return count;
}
std::string getWord(const std::string &str, int n)
{
std::stringstream ss(str);
std::string word;
int count = 0;
while (ss >> word)
{
if (count == n)
return word;
count++;
}
return "";
}
std::string getDay(time_t t)
{
struct tm *timeinfo = localtime(&t);
char buffer[80];
strftime(buffer, 80, "%a", timeinfo);
return std::string(buffer);
}
std::string getDate(time_t t)
{
struct tm *timeinfo = localtime(&t);
char buffer[80];
strftime(buffer, 80, "%d %b %Y", timeinfo);
return std::string(buffer);
}
std::string getTime(time_t t)
{
struct tm *timeinfo = localtime(&t);
char buffer[80];
strftime(buffer, 80, "%H:%M:%S", timeinfo);
return std::string(buffer);
}
std::string getMoment(time_t t)
{
return getDay(t) + ", " + getDate(t) + " at " + getTime(t);
}
bool isFormattedLike(const std::string &str, const std::string &format)
{
std::string::const_iterator it1, it2;
for (it1 = str.begin(), it2 = format.begin(); it1 != str.end() && it2 != format.end(); it1++, it2++)
{
if (*it1 == ' ' && it1 + 1 != str.end() && *(it1 + 1) == ' ')
it1++;
if (*it2 != '%')
{
if (*it1 != *it2)
return false;
}
else
{
it2++;
if (it2 == format.end() && *it1 != '%')
return false;
if (*it2 == 'd')
{
std::string word = getWord(str.substr(it1 - str.begin()), 0);
if (word.empty() || word.find_first_not_of("0123456789") != std::string::npos)
return false;
it1 += word.length() - 1;
}
else if (*it2 == 's')
{
while (it1 != str.end() && *it1 != ' ' && *it1 != '!' && *it1 != '@')
it1++;
it1--;
}
}
}
if (it2 != format.end())
return false;
return true;
}