-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
202 lines (176 loc) · 5.52 KB
/
Copy pathutils.cpp
File metadata and controls
202 lines (176 loc) · 5.52 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
195
196
197
198
199
200
201
202
#include "utils.hpp"
std::string Utils::removeCRLF(std::string const &str)
{
std::string out = str;
if (!out.empty() && (out[out.size() - 1] == '\n' || out[out.size() - 1] == '\r')) {
out.erase(out.size() - 1);
}
if (!out.empty() && (out[out.size() - 1] == '\n' || out[out.size() - 1] == '\r')) {
out.erase(out.size() - 1);
}
return(out);
}
std::string Utils::strToUpper(std::string const &str)
{
std::string out = str;
for (std::string::iterator it = out.begin(); it != out.end(); ++it)
{
*it = std::toupper(*it);
}
return(out);
}
std::string Utils::strToLower(std::string const &str)
{
std::string out = str;
for (std::string::iterator it = out.begin(); it != out.end(); ++it)
{
*it = std::tolower(*it);
}
return(out);
}
std::string Utils::getCurrentDate(void)
{
// Get the current time as a time_t object
std::time_t currentTime = std::time(NULL);
// Convert to local time representation
std::tm* localTime = std::localtime(¤tTime);
// Buffer to hold the formatted date string
char dateString[100];
// Format the date into a human-readable string
// Format specifiers:
// %Y - year with century
// %m - month (01-12)
// %d - day of the month (01-31)
// %H - hour (00-23)
// %M - minute (00-59)
// %S - second (00-60)
std::strftime(dateString, sizeof(dateString), "%Y-%m-%d %H:%M:%S", localTime);
return (dateString);
}
std::string Utils::getCurrentTimestamp()
{
std::time_t rawtime;
// Get the current time as a Unix timestamp
std::time(&rawtime);
return (Utils::itoa(rawtime));
}
bool Utils::isAllowedNickCharacter(char const c)
{
return (isalnum(c) || c == '[' || c == ']' || c == '{' || c == '}' || c == '\\' || c == '|' || c == '_' || c == '-');
}
std::string Utils::getUserPrefix(Server *server, Session *session)
{
std::string prefix;
prefix = ":" + session->getNickName() + "!" + session->getUserName() + "@" + server->getHostName() + " ";
return(prefix);
}
std::string Utils::getServerPrefix(Server *server, Session *session, std::string rep_code)
{
std::string prefix;
prefix = ":" + server->getHostName() + " " + rep_code + " " + session->getNickName() + " ";
return(prefix);
}
//Find the char c before # or the last & char
bool Utils::findFlag(const std::string& str, char c)
{
// Find the position of the first '#' character
size_t pos_hash = str.find('#');
// Find the position of the last '&' character
size_t pos_ampersand = str.rfind('&');
// Determine the position to search until
size_t pos_end = std::min(pos_hash, pos_ampersand);
// Search for the character 'c' within the substring before '#' or the last '&'
return str.substr(0, pos_end).find(c) != std::string::npos;
}
bool Utils::findChar(std::string &str, char &c)
{
return str.find(c) != std::string::npos;
}
void Utils::removeDuplicatesStr(std::vector<std::string>& vec)
{
// Sort the vector
std::sort(vec.begin(), vec.end());
// Remove adjacent duplicates
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}
//channel should contain the prefix ~#&..., send sender "" to send it to also the sender
void Utils::sendToChannel(Server *server, Channel *channel,std::string sender, std::string &msg, std::string &channelname_with_flags_from_msg)
{
bool is_op = Utils::findFlag(channelname_with_flags_from_msg, '@');
bool is_founder = Utils::findFlag(channelname_with_flags_from_msg, '~');
std::vector<std::string> lst_user;
if(is_founder == false && is_op == false)
lst_user = channel->get_users();
else
{
if(is_op)
{
std::vector<std::string> ops = channel->get_operators();
lst_user.insert(lst_user.end(), ops.begin(), ops.end());
}
if(is_founder)
{
lst_user.push_back(channel->get_founder());
}
}
Utils::removeDuplicatesStr(lst_user);
if(channel)
{
for(size_t i = 0; i < lst_user.size(); i++)
{
if(lst_user[i] != sender)
server->getSession(lst_user[i])->addSendBuffer(msg);
}
}
}
void Utils::sendBufferNow(Session *session)
{
if(!session->getSendBuffer().empty())
{
send(session->getFdSocket(), session->getSendBuffer().c_str(), session->getSendBuffer().length(), MSG_NOSIGNAL);
session->getSendBuffer().clear();
}
}
bool Utils::isValidChannelName(const std::string& str)
{
// Check if the string has at least 2 characters
if (str.length() < 2) {
return false;
}
// Check if contain # or &
if(str.find('#') == std::string::npos && str.find('&') == std::string::npos)
return false;
bool flag_op = Utils::findFlag(str, '@');
bool flag_founder = Utils::findFlag(str, '~');
int total_flag = flag_op + flag_founder;
if(str[total_flag] != '#' && str[total_flag] != '&')
return(false);
// Check if all other characters are alphanumeric
for (size_t i = 1; i < str.length(); ++i) {
if (!std::isalnum(static_cast<unsigned char>(str[i])) && str[i] != '@' && str[i] != '~' && str[i] != '&' && str[i] != '#')
return false;
}
return true;
}
std::vector<std::string> Utils::split(const std::string& str, char delimiter)
{
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
bool Utils::containsCRLF(const std::string &str)
{
// Iterate through the string
for (size_t i = 0; i < str.size() - 1; ++i) {
// Check for the sequence "\r\n"
if (str[i] == '\r' && str[i + 1] == '\n') {
return true;
}
}
return false;
}