diff --git a/includes/server.hpp b/includes/server.hpp index cf7e3bc..a0191cc 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -29,7 +29,7 @@ namespace irc inline void closeMainSocket() { - if (_main_socket > NULL_SOCKET) + if(_main_socket > NULL_SOCKET) close(_main_socket); _main_socket = NULL_SOCKET; _active = false; diff --git a/includes/unstd/array.hpp b/includes/unstd/array.hpp index ea0c5bd..1ed49a8 100644 --- a/includes/unstd/array.hpp +++ b/includes/unstd/array.hpp @@ -25,13 +25,13 @@ namespace unstd TArray() {} TArray(const TArray& rhs) { - for (std::size_t i = 0; i < N; i++) + for(std::size_t i = 0; i < N; i++) _data[i] = rhs._data[i]; } inline TArray& operator=(const TArray& rhs) { - for (std::size_t i = 0; i < N; i++) + for(std::size_t i = 0; i < N; i++) _data[i] = rhs._data[i]; return *this; } diff --git a/includes/unstd/shared_ptr.ipp b/includes/unstd/shared_ptr.ipp index 2222aa0..32c65ac 100644 --- a/includes/unstd/shared_ptr.ipp +++ b/includes/unstd/shared_ptr.ipp @@ -21,14 +21,14 @@ namespace unstd template SharedPtr::SharedPtr(const SharedPtr& rhs) : _ptr(rhs._ptr), _ref(rhs._ref) { - if (_ptr && _ref) + if(_ptr && _ref) ++_ref->shared; } template SharedPtr& SharedPtr::operator=(const SharedPtr& rhs) { - if (_ptr == rhs._ptr) + if(_ptr == rhs._ptr) return *this; safeRelease(); @@ -96,10 +96,10 @@ namespace unstd template void SharedPtr::safeRelease() { - if (_ref == NULL) - return ; + if(_ref == NULL) + return; _ref->shared--; - if (_ref->shared <= 0) + if(_ref->shared <= 0) { delete _ptr; _ptr = NULL; diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 2538574..03a47c8 100644 --- a/srcs/channel.cpp +++ b/srcs/channel.cpp @@ -26,14 +26,14 @@ namespace irc void Channel::addClient(unstd::SharedPtr client, bool op) { - if (!_clients.insert(client).second) + if(!_clients.insert(client).second) { client->sendCode(ERR_USERONCHANNEL, "You are already in the channel"); - return ; + return; } - if (op) + if(op) _operators.insert(client); - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(client->getNickName(), "JOIN", _name); sendWho(client); } @@ -41,52 +41,52 @@ namespace irc void Channel::modOperator(unstd::SharedPtr client, const std::string& clientname, bool mode) { client_it it; - if (mode) + if(mode) { - for (it = _clients.begin(); it != _clients.end(); ++it) + for(it = _clients.begin(); it != _clients.end(); ++it) { - if (const_cast&>(*it)->getNickName() == clientname) + if(const_cast&>(*it)->getNickName() == clientname) { - if (isOp(*it)) - return ; + if(isOp(*it)) + return; showModesModify(client, mode, 'o', clientname); _operators.insert(const_cast&>(*it)); break; } } - if (it == _clients.end()) + if(it == _clients.end()) client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel"); - return ; + return; } - for (it = _operators.begin(); it != _operators.end(); ++it) + for(it = _operators.begin(); it != _operators.end(); ++it) { - if (const_cast&>(*it)->getNickName() == clientname) + if(const_cast&>(*it)->getNickName() == clientname) { showModesModify(client, mode, 'o', clientname); _operators.erase(it); break; } } - if (it == _operators.end()) + if(it == _operators.end()) client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel"); } bool Channel::removeClient(unstd::SharedPtr client, const std::string& reason, bool quit) { - if (!_clients.erase(client)) + if(!_clients.erase(client)) return (false); - if (isOp(client)) + if(isOp(client)) _operators.erase(client); - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_it it = _clients.begin(); it != _clients.end(); ++it) { - if (!quit) + if(!quit) const_cast&>(*it)->sendMsg(client->getNickName(), "PART", _name + ' ' + reason); else const_cast&>(*it)->sendMsg(client->getNickName(), "QUIT", reason); } - if (!quit) + if(!quit) client->sendMsg(client->getNickName(), "PART", _name + ' ' + reason); return true; } @@ -95,9 +95,9 @@ namespace irc { std::string clientlist(":YipIRC " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :"); - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_it it = _clients.begin(); it != _clients.end(); ++it) { - if (isOp(const_cast&>(*it))) + if(isOp(const_cast&>(*it))) clientlist += '@'; clientlist += const_cast&>(*it)->getNickName() + ' '; } @@ -109,9 +109,9 @@ namespace irc void Channel::handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice) const { const std::string sender = client ? client->getNickName() : ""; - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_it it = _clients.begin(); it != _clients.end(); ++it) { - if (client == *it) + if(client == *it) continue; const_cast&>(*it)->sendMsg(sender, (notice ? "NOTICE " : "PRIVMSG ") + _name, msg); } @@ -125,14 +125,14 @@ namespace irc modes += (modevalue) ? '+' : '-'; modes += flag; - if (flag == 'k') + if(flag == 'k') modes += " " + _password; - if (flag == 'l') + if(flag == 'l') modes += " " + unstd::toString(_channel_size);; - if (flag == 'o') + if(flag == 'o') modes += " " + op; out += getName(); - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(client->getNickName(), out, modes); } @@ -140,18 +140,18 @@ namespace irc { std::string modes = _name + " +"; - if (_invite_only) + if(_invite_only) modes += 'i'; - if (_topic_op_restrict) + if(_topic_op_restrict) modes += 't'; - if (_password.size() > 0) + if(_password.size() > 0) modes += 'k'; - if (_channel_size != -1) + if(_channel_size != -1) modes += 'l'; - if (modes.size() <= 2) - return ; - if (modes.size() == _name.size() + 2) - return ; + if(modes.size() <= 2) + return; + if(modes.size() == _name.size() + 2) + return; client->sendCode(RPL_CHANNELMODEIS, modes); } @@ -162,48 +162,48 @@ namespace irc unsigned long arg_nb = 3; int arg_index = 2; - if (flags.find_first_not_of("itkol+-") != std::string::npos || flags.find_last_of("+-") != 0) + if(flags.find_first_not_of("itkol+-") != std::string::npos || flags.find_last_of("+-") != 0) { client->sendCode(ERR_UNKNOWNMODE, "MODE : Unknown mode"); - return ; + return; } - for (std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it) + for(std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it) { - if (std::distance(flags.begin(), it) != static_cast(flags.find_first_of(*it))) + if(std::distance(flags.begin(), it) != static_cast(flags.find_first_of(*it))) it = flags.erase(it) - 1; } - if (flags.find('o') != std::string::npos) + if(flags.find('o') != std::string::npos) arg_nb++; - if (flags.find('k') != std::string::npos && modevalue) + if(flags.find('k') != std::string::npos && modevalue) arg_nb++; - if (flags.find('l') != std::string::npos && modevalue) + if(flags.find('l') != std::string::npos && modevalue) arg_nb++; - if (msg.getTokens().size() < arg_nb) + if(msg.getTokens().size() < arg_nb) { client->sendCode(ERR_NEEDMOREPARAMS, "MODE : Need more params"); - return ; + return; } - for (std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it) + for(std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it) { switch(*it) { case 'i': - if (_invite_only == modevalue) + if(_invite_only == modevalue) break; _invite_only = modevalue; showModesModify(client, modevalue, 'i'); break; case 't': - if (_topic_op_restrict == modevalue) + if(_topic_op_restrict == modevalue) break; _topic_op_restrict = modevalue; showModesModify(client, modevalue, 't'); break; case 'k': - if (modevalue) + if(modevalue) { - if (msg.getArgs()[arg_index] == _password) + if(msg.getArgs()[arg_index] == _password) { arg_index++; break; @@ -214,8 +214,8 @@ namespace irc } else { - if (_password.empty()) - return ; + if(_password.empty()) + return; _password.clear(); logs::report(log_message, "password removed on %s", _name.c_str()); showModesModify(client, modevalue, 'k'); @@ -225,29 +225,29 @@ namespace irc modOperator(client, msg.getArgs()[arg_index++], modevalue); break; case 'l': - if (!modevalue && _channel_size == -1) - return ; - if (!modevalue) + if(!modevalue && _channel_size == -1) + return; + if(!modevalue) { _channel_size = -1; showModesModify(client, modevalue, 'l'); break; } - if (modevalue) + if(modevalue) { char* end; long tmp = std::strtol(msg.getArgs()[arg_index++].c_str(), &end, 10); - if (errno == ERANGE || *end != 0 || tmp < 0) + if(errno == ERANGE || *end != 0 || tmp < 0) { client->sendCode(ERR_UNKNOWNMODE, "MODE : Invalid channel size"); logs::report(log_error, "invalid channel size"); - return ; + return; } - if (tmp == _channel_size) - return ; + if(tmp == _channel_size) + return; else { - if (tmp == _channel_size) + if(tmp == _channel_size) break; _channel_size = tmp; showModesModify(client, modevalue, 'l'); @@ -260,9 +260,9 @@ namespace irc bool Channel::hasClient(std::string client) const { - for (client_const_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_const_it it = _clients.begin(); it != _clients.end(); ++it) { - if (const_cast&>(*it)->getNickName() == client) + if(const_cast&>(*it)->getNickName() == client) return true; } return false; @@ -270,10 +270,10 @@ namespace irc void Channel::setTopic(unstd::SharedPtr client, const std::string& new_topic) { - if (_topic_op_restrict && !isOp(client)) + if(_topic_op_restrict && !isOp(client)) { client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges"); - return ; + return; } _topic = new_topic; relayTopic(client, true); @@ -281,26 +281,26 @@ namespace irc void Channel::relayTopic(unstd::SharedPtr client, bool broadcast) { - if (!hasClient(client)) - return ; - if (_topic.empty()) + if(!hasClient(client)) + return; + if(_topic.empty()) { - if (!broadcast) + if(!broadcast) return client->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set"); - for (client_it it = _clients.begin(); it != _clients.end(); ++ it) + for(client_it it = _clients.begin(); it != _clients.end(); ++ it) const_cast&>(*it)->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set"); } - if (!broadcast) + if(!broadcast) return client->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic); - for (client_it it = _clients.begin(); it != _clients.end(); ++ it) + for(client_it it = _clients.begin(); it != _clients.end(); ++ it) const_cast&>(*it)->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic); } bool Channel::isOp(unstd::SharedPtr client) const { - for (client_const_it it = _operators.begin(); it != _operators.end(); ++it) + for(client_const_it it = _operators.begin(); it != _operators.end(); ++it) { - if (const_cast&>(*it)->getNickName() == client->getNickName()) + if(const_cast&>(*it)->getNickName() == client->getNickName()) return true; } return false; @@ -308,24 +308,24 @@ namespace irc bool Channel::kick(unstd::SharedPtr op, unstd::SharedPtr target, const std::string& reason) { - if (!hasClient(op)) + if(!hasClient(op)) { op->sendCode(ERR_NOTONCHANNEL, _name + " you're not on that channel"); return false; } - if (!hasClient(target)) + if(!hasClient(target)) { op->sendCodeInChannel(ERR_USERNOTINCHANNEL, *this, "they aren't on that channel"); return false; } - if (!isOp(op)) + if(!isOp(op)) { op->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *this, "you're not channel operator"); return false; } - for (client_it it = _clients.begin(); it != _clients.end(); ++it) + for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(op->getNickName(), "KICK " + _name + ' ' + target->getNickName(), reason); - if (isOp(target)) + if(isOp(target)) _operators.erase(target); _clients.erase(target); return true; diff --git a/srcs/client.cpp b/srcs/client.cpp index 418f930..7eb3787 100644 --- a/srcs/client.cpp +++ b/srcs/client.cpp @@ -35,7 +35,7 @@ namespace irc #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif - if (send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) + if(send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str()); } @@ -45,7 +45,7 @@ namespace irc #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif - if (send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) + if(send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str()); } @@ -55,7 +55,7 @@ namespace irc #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif - if (send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) + if(send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str()); } @@ -64,7 +64,7 @@ namespace irc #ifdef DEBUG logs::report(log_message,"sending '%s'", str.c_str()); #endif - if (send(_fd, str.c_str(), str.length(), 0) != static_cast(str.length())) + if(send(_fd, str.c_str(), str.length(), 0) != static_cast(str.length())) logs::report(log_error, "server failed to send a message to '%s' (:sadge:)", _username.c_str()); } @@ -74,7 +74,7 @@ namespace irc #ifdef DEBUG logs::report(log_message,"sending '%s'", out.c_str()); #endif - if (send(_fd, out.c_str(), out.length(), 0) != static_cast(out.length())) + if(send(_fd, out.c_str(), out.length(), 0) != static_cast(out.length())) logs::report(log_error, "server failed to send a message from '%s' to '%s' (:sadge:)", sender.c_str(), _username.c_str()); } @@ -90,7 +90,7 @@ namespace irc int len = vsnprintf(NULL, 0, message.c_str(), al); - if (len > 0) + if(len > 0) { std::vector tmp(len + 1); vsnprintf(&tmp[0], tmp.size(), message.c_str(), al_copy); @@ -103,17 +103,17 @@ namespace irc #ifdef DEBUG logs::report(log_message,"sending '%s'", buffer.c_str()); #endif - if (send(_fd, buffer.c_str(), buffer.length(), 0) < static_cast(buffer.length())) + if(send(_fd, buffer.c_str(), buffer.length(), 0) < static_cast(buffer.length())) logs::report(log_error, "server failed to send a message to '%s'", _nickname.c_str()); } void Client::printUserHeader() const { std::cout << AnsiColor::green << "[User " << _id; - if (!_realname.empty()) + if(!_realname.empty()) std::cout << " {realname " << _realname << '}'; - if (!_username.empty()) + if(!_username.empty()) std::cout << " {username " << _username << "}"; - if (!_nickname.empty()) + if(!_nickname.empty()) std::cout << " {nickname " << _nickname << "}"; std::cout << "] : " << AnsiColor::reset; } @@ -121,14 +121,14 @@ namespace irc std::string Client::getNextMsg() { std::size_t finder = _msg_in_flight.find("\r\n"); - if (finder != std::string::npos) + if(finder != std::string::npos) { std::string msg = _msg_in_flight.substr(0, finder); _msg_in_flight = _msg_in_flight.substr(finder + 2); return msg; } finder = _msg_in_flight.find("\n"); - if (finder != std::string::npos) + if(finder != std::string::npos) { std::string msg = _msg_in_flight.substr(0, finder); _msg_in_flight = _msg_in_flight.substr(finder + 1); @@ -147,8 +147,8 @@ namespace irc std::string tosend = "Your host is "; getsockopt(_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &size); protoent *proto = getprotobynumber(protocol); - if (!isLogged() || !isRegistered() || isWelcomed() || _nickname.empty()) - return ; + if(!isLogged() || !isRegistered() || isWelcomed() || _nickname.empty()) + return; _welcomed = true; gethostname(hostname, 1023); tosend += hostname; diff --git a/srcs/main.cpp b/srcs/main.cpp index c10760b..9bc7c31 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -23,8 +23,8 @@ static irc::Server* serv_ptr = NULL; void signalsHandler(int foo) { (void)foo; - if (!serv_ptr) - return ; + if(!serv_ptr) + return; serv_ptr->closeMainSocket(); std::cout << "\b\b \b\b" << std::flush; irc::logs::report(irc::log_message, "Shutting down..."); @@ -32,17 +32,17 @@ void signalsHandler(int foo) int main(int ac, char** av) { - if (ac != 3) + if(ac != 3) { irc::logs::report(irc::log_message, "usage './ircserv ', try again dumbass"); return 0; } - if (av[1] == NULL || av[2] == NULL) + if(av[1] == NULL || av[2] == NULL) irc::logs::report(irc::log_fatal_error, "invalid argv, argv[1] or argv[2] is NULL (wtf)"); char* end; int port = std::strtol(av[1], &end, 10); - if (errno == ERANGE || *end != 0 || port <= 0 || port > 0xFFFF || std::strlen(av[1]) == 0) + if(errno == ERANGE || *end != 0 || port <= 0 || port > 0xFFFF || std::strlen(av[1]) == 0) irc::logs::report(irc::log_fatal_error, "invalid port"); irc::Server serv(port, av[2]); diff --git a/srcs/message.cpp b/srcs/message.cpp index e49e5ad..afea4e4 100644 --- a/srcs/message.cpp +++ b/srcs/message.cpp @@ -22,13 +22,13 @@ namespace irc void split(const std::string& s, std::vector& elems) { std::string token; - for (std::string::const_iterator it = s.begin(); it != s.end();) + for(std::string::const_iterator it = s.begin(); it != s.end();) { - if (std::isspace(*it)) + if(std::isspace(*it)) { elems.push_back(token); token.clear(); - while (std::isspace(*it) && it != s.end()) + while(std::isspace(*it) && it != s.end()) it++; } else @@ -38,18 +38,18 @@ namespace irc } } elems.push_back(token); - for (std::vector::iterator vit = elems.begin(); vit != elems.end();) + for(std::vector::iterator vit = elems.begin(); vit != elems.end();) { bool isempty = true; - for (std::string::const_iterator it = vit->begin(); it != vit->end(); ++it) + for(std::string::const_iterator it = vit->begin(); it != vit->end(); ++it) { - if (!std::isspace(*it)) + if(!std::isspace(*it)) { isempty = false; break; } } - if (isempty) + if(isempty) vit = elems.erase(vit); else vit++; @@ -67,23 +67,23 @@ namespace irc Message::Message(unstd::SharedPtr client, const std::string& msg) : _raw_msg(msg), _client(client) { std::vector tokens = details::split(msg); - if (tokens.empty()) - return ; + if(tokens.empty()) + return; _command = tokens[0]; _tokens = tokens; bool reason = false; - for (std::vector::iterator it = tokens.begin() + 1; it < tokens.end(); ++it) + for(std::vector::iterator it = tokens.begin() + 1; it < tokens.end(); ++it) { - if ((*it)[0] == ':') + if((*it)[0] == ':') reason = true; - if (!reason) + if(!reason) _args.push_back(*it); else { - if ((*it)[0] == ':') + if((*it)[0] == ':') it->erase(it->begin()); _reason.append(*it); - if (it != tokens.end() - 1) + if(it != tokens.end() - 1) _reason.push_back(' '); } } diff --git a/srcs/server.cpp b/srcs/server.cpp index 51c1ddb..a7cdf0a 100644 --- a/srcs/server.cpp +++ b/srcs/server.cpp @@ -35,10 +35,10 @@ namespace irc struct tm tstruct = *localtime(<ime); char buf[100]; - if (password.empty() || password.find_first_of(" \t\r\v") != std::string::npos) + if(password.empty() || password.find_first_of(" \t\r\v") != std::string::npos) { logs::report(log_error, "Password is invalid !"); - return ; + return; } std::memset(&_s_data, 0, sizeof(sockaddr)); initSocket(); @@ -52,7 +52,7 @@ namespace irc _s_data.sin_addr.s_addr = INADDR_ANY; _s_data.sin_port = htons(_port); _main_socket = socket(AF_INET, SOCK_STREAM, 0); // AF_INET == IPv4, SOCK_STREAM == TCP - if (_main_socket < 0) + if(_main_socket < 0) logs::report(log_fatal_error, "socket error"); logs::report(log_message, "socket succesfully started"); } @@ -62,14 +62,14 @@ namespace irc int opt = 1; initSocketData(); - if (setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends + if(setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends logs::report(log_fatal_error, "setsockopt() error (tout a pete)"); - if (bind(_main_socket, reinterpret_cast(&_s_data), sizeof(_s_data)) != 0) // Bind _main_socket to localhost + if(bind(_main_socket, reinterpret_cast(&_s_data), sizeof(_s_data)) != 0) // Bind _main_socket to localhost logs::report(log_fatal_error, "bind error"); logs::report(log_message, "bind successful, starting listen loop"); - if (listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS + if(listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS logs::report(log_fatal_error, "listen error"); logs::report(log_message, "listen queue created successfully"); @@ -80,27 +80,27 @@ namespace irc { char buffer[INPUT_SIZE] = { 0 }; ssize_t recv_size; - for (std::vector >::iterator it = _client.begin(); it != _client.end(); ++it) + for(std::vector >::iterator it = _client.begin(); it != _client.end(); ++it) { - if (!FD_ISSET((*it)->getFD(), &_fd_set)) + if(!FD_ISSET((*it)->getFD(), &_fd_set)) continue; - while ((recv_size = recv((*it)->getFD(), buffer, INPUT_SIZE, 0)) > 0) // read() but forsocket fd + while((recv_size = recv((*it)->getFD(), buffer, INPUT_SIZE, 0)) > 0) // read() but forsocket fd { (*it)->newMsgInFlight(buffer); #ifdef DEBUG logs::report(log_message,"processing '%s'", buffer); #endif - while (handleMessage(*it)); + while(handleMessage(*it)); std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining } - if (recv_size == 0 || (*it)->disconnectRequired()) // recv return 0 ifan user disconnect + if(recv_size == 0 || (*it)->disconnectRequired()) // recv return 0 ifan user disconnect { logs::report(log_message, "User %d disconnected", (*it)->getID()); close((*it)->getFD()); - for (channel_it cit = _channels.begin(); cit != _channels.end();) + for(channel_it cit = _channels.begin(); cit != _channels.end();) { cit->removeClient(*it, "", true); - if (cit->getNumberOfClients() == 0) + if(cit->getNumberOfClients() == 0) { logs::report(log_message, "channel '%s' has been destroyed", cit->getName().c_str()); cit = _channels.erase(cit); @@ -121,27 +121,27 @@ namespace irc int i = 0; socklen_t len = sizeof(sockaddr_in); - if (_main_socket == NULL_SOCKET) - return ; - while (_active) + if(_main_socket == NULL_SOCKET) + return; + while(_active) { FD_ZERO(&_fd_set); FD_SET(_main_socket, &_fd_set); - for (std::vector >::iterator it = _client.begin(); it != _client.end(); ++it) + for(std::vector >::iterator it = _client.begin(); it != _client.end(); ++it) FD_SET((*it)->getFD(), &_fd_set); tmp = select(MAX_USERS, &_fd_set, NULL, NULL, NULL); // SELECT blocks till a connection or message is received, and let only those in _fd_set - if (tmp < 0 && _main_socket != NULL_SOCKET) + if(tmp < 0 && _main_socket != NULL_SOCKET) logs::report(log_fatal_error, "select fd error"); - if (FD_ISSET(_main_socket, &_fd_set)) // ifit's a new connection + if(FD_ISSET(_main_socket, &_fd_set)) // ifit's a new connection { sockaddr_in cli_sock; fd = accept(_main_socket, reinterpret_cast(&cli_sock), &len); // adds the new connection - if (fd < 0) + if(fd < 0) logs::report(log_fatal_error, "accept() error"); - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) + if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0) logs::report(log_fatal_error, "fcntl() error"); unstd::SharedPtr new_client(new Client(fd, cli_sock, i++)); @@ -155,41 +155,41 @@ namespace irc bool Server::handleMessage(unstd::SharedPtr client) { - if (client->getMsgInFlight().empty()) // ifthere are no commands just return + if(client->getMsgInFlight().empty()) // ifthere are no commands just return return false; const Message msg(client, client->getNextMsg()); - if (msg.getCmd() == "NICK") + if(msg.getCmd() == "NICK") handleNick(client, msg, *this); - else if (msg.getCmd() == "USER") + else if(msg.getCmd() == "USER") handleUser(client, msg, *this); - else if (msg.getCmd() == "PASS") + else if(msg.getCmd() == "PASS") handlePass(client, msg, *this); - else if (!client->isWelcomed()) + else if(!client->isWelcomed()) return true; - else if (msg.getCmd() == "QUIT") + else if(msg.getCmd() == "QUIT") handleQuit(client, msg); - else if (msg.getCmd() == "WHO") + else if(msg.getCmd() == "WHO") handleWho(client, msg); - else if (msg.getCmd() == "PART") + else if(msg.getCmd() == "PART") handlePart(client, msg); - else if (msg.getCmd() == "JOIN") + else if(msg.getCmd() == "JOIN") handleJoin(client, msg); - else if (msg.getCmd() == "INVITE") + else if(msg.getCmd() == "INVITE") handleInvite(client, msg); - else if (msg.getCmd() == "PRIVMSG") + else if(msg.getCmd() == "PRIVMSG") handlePrivMsg(client, msg); - else if (msg.getCmd() == "NOTICE") + else if(msg.getCmd() == "NOTICE") handleNotice(client, msg); - else if (msg.getCmd() == "KICK") + else if(msg.getCmd() == "KICK") handleKick(client, msg); - else if (msg.getCmd() == "TOPIC") + else if(msg.getCmd() == "TOPIC") handleTopic(client, msg); - else if (msg.getCmd() == "PING") + else if(msg.getCmd() == "PING") handlePing(client, msg); - else if (msg.getCmd() == "MODE") + else if(msg.getCmd() == "MODE") handleMode(client, msg); - else if (msg.getCmd() == "imfeelinglucky") + else if(msg.getCmd() == "imfeelinglucky") handleRussian(client); else client->sendCode(ERR_UNKNOWNCOMMAND, "No such command"); @@ -198,9 +198,9 @@ namespace irc bool Server::isUserKnown(const std::string& user) const { - for (client_const_it it = _client.begin(); it < _client.end(); ++it) + for(client_const_it it = _client.begin(); it < _client.end(); ++it) { - if (const_cast&>(*it)->getNickName() == user) + if(const_cast&>(*it)->getNickName() == user) return true; } return false; @@ -208,9 +208,9 @@ namespace irc bool Server::isChannelKnown(const std::string& channel) const { - for (channel_const_it it = _channels.begin(); it < _channels.end(); ++it) + for(channel_const_it it = _channels.begin(); it < _channels.end(); ++it) { - if (it->getName() == channel) + if(it->getName() == channel) return true; } return false; @@ -218,9 +218,9 @@ namespace irc Channel* Server::getChannelByName(const std::string& name) { - for (channel_it it = _channels.begin(); it < _channels.end(); ++it) + for(channel_it it = _channels.begin(); it < _channels.end(); ++it) { - if (it->getName() == name) + if(it->getName() == name) return &*it; } return NULL; @@ -228,9 +228,9 @@ namespace irc unstd::SharedPtr Server::getClientByName(const std::string& name) { - for (client_it it = _client.begin(); it < _client.end(); ++it) + for(client_it it = _client.begin(); it < _client.end(); ++it) { - if ((*it)->getNickName() == name) + if((*it)->getNickName() == name) return *it; } return unstd::SharedPtr(NULL); @@ -238,13 +238,13 @@ namespace irc Server::~Server() { - for (client_it it = _client.begin(); it != _client.end(); ++it) + for(client_it it = _client.begin(); it != _client.end(); ++it) { - if ((*it)->isWelcomed()) + if((*it)->isWelcomed()) (*it)->kill("Server shutting down"); } closeMainSocket(); - for (int i = 3; i <= FD_MAX; ++i) + for(int i = 3; i <= FD_MAX; ++i) close(i); } } diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 39998ef..a6ba720 100644 --- a/srcs/server_functions.cpp +++ b/srcs/server_functions.cpp @@ -36,33 +36,33 @@ namespace irc void Server::handleNick(unstd::SharedPtr client, const Message& msg, const Server& server) { - if (msg.getTokens().size() < 2) + if(msg.getTokens().size() < 2) { client->sendCode(ERR_NONICKNAMEGIVEN, " NICK : No nickname given"); - return ; + return; } - if (msg.getTokens().size() >= 3) - return ; + if(msg.getTokens().size() >= 3) + return; const std::string& nickname = msg.getTokens()[1]; - for (client_it it = _client.begin(); it != _client.end(); ++it) + for(client_it it = _client.begin(); it != _client.end(); ++it) { - if ((*it)->getNickName() == nickname) + if((*it)->getNickName() == nickname) { - if (client->getNickName().empty()) + if(client->getNickName().empty()) { client->sendCode(ERR_NICKCOLLISION, " NICK : Nickname is used"); client->kill(" NICK : Nickname already used"); client->requireDisconnect(); - return ; + return; } client->sendCode(ERR_NICKCOLLISION, "NICK : Nickname is used"); - return ; + return; } } std::string oldNick = (client->getNickName().size() > 0) ? client->getNickName() : msg.getTokens()[1]; // get nickname before /nick execution inside server (oldNick) client->printUserHeader(); client->setNewNickName(msg.getTokens()[1]); - for (client_it it = _client.begin(); it != _client.end(); ++it) + for(client_it it = _client.begin(); it != _client.end(); ++it) (*it)->sendMsg(oldNick, "NICK", msg.getTokens()[1]); client->welcome(server); client->printUserHeader(); @@ -71,18 +71,18 @@ namespace irc void Server::handleUser(unstd::SharedPtr client, const Message& msg, const Server& server) { - if (msg.getTokens().size() < 5) + if(msg.getTokens().size() < 5) { client->sendCode(ERR_NEEDMOREPARAMS, "USER : Need more parameters"); - return ; + return; } - if (client->isRegistered()) + if(client->isRegistered()) { client->sendCode(ERR_ALREADYREGISTRED, "USER : You are already registered"); - return ; + return; } - if (msg.getTokens()[4][0] != ':') - return ; + if(msg.getTokens()[4][0] != ':') + return; client->printUserHeader(); client->setNewUserName(msg.getTokens()[1]); std::cout << "new username, " << client->getUserName() << std::endl; @@ -90,7 +90,7 @@ namespace irc client->printUserHeader(); std::string realname; - for (std::vector::const_iterator it = msg.getTokens().begin() + 4; it != msg.getTokens().end(); ++it) + for(std::vector::const_iterator it = msg.getTokens().begin() + 4; it != msg.getTokens().end(); ++it) { realname.append(*it); realname.append(" "); @@ -105,12 +105,12 @@ namespace irc void Server::handlePass(unstd::SharedPtr client, const Message& msg, const Server& server) { - if (client->isLogged()) + if(client->isLogged()) { client->sendCode(ERR_ALREADYREGISTRED, "PASS : You are already registered"); - return ; + return; } - if (msg.getTokens()[1] == _password) + if(msg.getTokens()[1] == _password) { client->login(); client->welcome(server); @@ -124,7 +124,7 @@ namespace irc void Server::handleQuit(unstd::SharedPtr client, const Message& msg) { - for (channel_it it = _channels.begin(); it != _channels.end(); ++it) + for(channel_it it = _channels.begin(); it != _channels.end(); ++it) it->removeClient(client, (msg.getReason().empty() ? "Leaving" : msg.getReason()), true); client->printUserHeader(); std::cout << "quit" << std::endl; @@ -132,34 +132,34 @@ namespace irc void Server::handlePart(unstd::SharedPtr client, const Message& msg) { - if (msg.getTokens().size() < 2 && msg.getTokens().size() > 3) + if(msg.getTokens().size() < 2 && msg.getTokens().size() > 3) { client->sendCode(ERR_NEEDMOREPARAMS, "PART : Parameters amount invalid"); - return ; + return; } - if (msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&') - return ; - if (!isChannelKnown(msg.getArgs()[0])) + if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&') + return; + if(!isChannelKnown(msg.getArgs()[0])) { client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel"); - return ; + return; } Channel* channel = getChannelByName(msg.getArgs()[0]); - if (channel == NULL) + if(channel == NULL) logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->getName().c_str()); - if (!channel->removeClient(client, (msg.getReason().empty() ? "" : msg.getReason()))) + if(!channel->removeClient(client, (msg.getReason().empty() ? "" : msg.getReason()))) { client->sendCode(ERR_NOTONCHANNEL, "PART : Not on channel"); - return ; + return; } client->printUserHeader(); std::cout << "leaving channel, " << msg.getArgs()[0] << std::endl; - if (channel->getNumberOfClients() == 0) + if(channel->getNumberOfClients() == 0) { channel_it it; - for (it = _channels.begin(); it < _channels.end(); ++it) + for(it = _channels.begin(); it < _channels.end(); ++it) { - if (it->getName() == msg.getArgs()[0]) + if(it->getName() == msg.getArgs()[0]) break; } _channels.erase(it); @@ -169,95 +169,95 @@ namespace irc void Server::handleJoin(unstd::SharedPtr client, const Message& msg) { - if (msg.getArgs().empty()) + if(msg.getArgs().empty()) { client->sendCode(ERR_NEEDMOREPARAMS, "JOIN : Need more params"); - return ; + return; } - if (msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&') + if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&') { client->sendCode(ERR_NOSUCHCHANNEL, ": " + msg.getTokens()[1], "No such channel"); - return ; + return; } channel_it it; - for (it = _channels.begin(); it != _channels.end(); ++it) + for(it = _channels.begin(); it != _channels.end(); ++it) { - if (msg.getTokens()[1] == it->getName()) + if(msg.getTokens()[1] == it->getName()) break; } - if (it == _channels.end()) + if(it == _channels.end()) { _channels.push_back(Channel(msg.getTokens()[1])); _channels.back().addClient(client, true); logs::report(log_message, "channel '%s' has been created", msg.getTokens()[1].c_str()); - return ; + return; } - if ((msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) || (msg.getTokens().size() == 2 && it->getPassword().size() > 0)) + if((msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) || (msg.getTokens().size() == 2 && it->getPassword().size() > 0)) client->sendCode(ERR_BADCHANNELKEY, "JOIN : Invalid password"); - else if (it->getChannelSize() != -1 && it->getChannelSize() <= static_cast(it->getNumberOfClients())) + else if(it->getChannelSize() != -1 && it->getChannelSize() <= static_cast(it->getNumberOfClients())) client->sendCode(ERR_CHANNELISFULL, "JOIN : Channel is full"); - else if (it->isInviteOnly() && !client->hasBeenInvitedTo(it->getName())) + else if(it->isInviteOnly() && !client->hasBeenInvitedTo(it->getName())) client->sendCode(ERR_INVITEONLYCHAN, it->getName()); - else if (it->getPassword().size() == 0) + else if(it->getPassword().size() == 0) it->addClient(client); - else if (msg.getTokens().size() == 3 && it->getPassword().size() > 0 && msg.getTokens()[2] == it->getPassword()) + else if(msg.getTokens().size() == 3 && it->getPassword().size() > 0 && msg.getTokens()[2] == it->getPassword()) it->addClient(client); } void Server::handlePrivMsg(unstd::SharedPtr client, const Message& msg) { - if (msg.getArgs().empty()) + if(msg.getArgs().empty()) { client->sendCode(ERR_NORECIPIENT, "PRIVMSG : No recipient given"); - return ; + return; } - if (msg.getTokens().size() < 3) + if(msg.getTokens().size() < 3) { client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send"); - return ; + return; } - if (msg.getReason().empty()) + if(msg.getReason().empty()) { client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send"); - return ; + return; } - if (msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#') + if(msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#') { - for (client_it itc = _client.begin(); itc != _client.end(); ++itc) + for(client_it itc = _client.begin(); itc != _client.end(); ++itc) { - if ((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName()) + if((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName()) { std::string complete_msg; - if (msg.getTokens().size() > 2) + if(msg.getTokens().size() > 2) { - for (std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) + for(std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) { complete_msg.append(*tit); complete_msg.append(" "); } complete_msg.erase(complete_msg.begin()); } - if (complete_msg.empty()) + if(complete_msg.empty()) client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send"); (*itc)->sendMsg(client->getNickName(), "PRIVMSG " + (*itc)->getNickName(), complete_msg); break; } } - return ; + return; } - for (channel_it it = _channels.begin(); it != _channels.end(); ++it) + for(channel_it it = _channels.begin(); it != _channels.end(); ++it) { - if (msg.getTokens()[1] == it->getName()) + if(msg.getTokens()[1] == it->getName()) { - if (!it->hasClient(client)) + if(!it->hasClient(client)) { client->sendCode(ERR_CANNOTSENDTOCHAN, "PRIVMSG : You are not in the channel"); - return ; + return; } std::string complete_msg; - if (msg.getTokens().size() > 2) + if(msg.getTokens().size() > 2) { - for (std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) + for(std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) { complete_msg.append(*tit); complete_msg.append(" "); @@ -272,18 +272,18 @@ namespace irc void Server::handleNotice(unstd::SharedPtr client, const Message& msg) { - if (msg.getArgs().empty()) - return ; - if (msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#') + if(msg.getArgs().empty()) + return; + if(msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#') { - for (client_it itc = _client.begin(); itc != _client.end(); ++itc) + for(client_it itc = _client.begin(); itc != _client.end(); ++itc) { - if ((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName()) + if((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName()) { std::string complete_msg; - if (msg.getTokens().size() > 2) + if(msg.getTokens().size() > 2) { - for (std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) + for(std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) { complete_msg.append(*tit); complete_msg.append(" "); @@ -294,16 +294,16 @@ namespace irc break; } } - return ; + return; } - for (channel_it it = _channels.begin(); it != _channels.end(); ++it) + for(channel_it it = _channels.begin(); it != _channels.end(); ++it) { - if (msg.getTokens()[1] == it->getName()) + if(msg.getTokens()[1] == it->getName()) { std::string complete_msg; - if (msg.getTokens().size() > 2) + if(msg.getTokens().size() > 2) { - for (std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) + for(std::vector::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit) { complete_msg.append(*tit); complete_msg.append(" "); @@ -318,47 +318,47 @@ namespace irc void Server::handleInvite(unstd::SharedPtr client, const Message& msg) { - if (msg.getArgs().empty() || msg.getArgs().size() != 2) + if(msg.getArgs().empty() || msg.getArgs().size() != 2) { client->sendCode(ERR_NEEDMOREPARAMS, "INVITE : Invalid parameters"); - return ; + return; } - if (!isUserKnown(msg.getArgs()[0])) + if(!isUserKnown(msg.getArgs()[0])) { client->sendCode(ERR_NOSUCHNICK, const_cast(msg.getArgs()[0]) + " no such nick"); - return ; + return; } - if (!isChannelKnown(msg.getArgs()[1])) + if(!isChannelKnown(msg.getArgs()[1])) { client->sendCode(ERR_NOSUCHCHANNEL, const_cast(msg.getArgs()[1]) + " no such channel"); - return ; + return; } Channel* channel_target = getChannelByName(msg.getArgs()[1]); - if (channel_target == NULL) + if(channel_target == NULL) logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str()); unstd::SharedPtr client_target = getClientByName(msg.getArgs()[0]); - if (client_target.get() == NULL) + if(client_target.get() == NULL) logs::report(log_fatal_error, "(INVITE), cannot get client '%s' by name; panic !", msg.getArgs()[0].c_str()); - if (!channel_target->hasClient(client)) + if(!channel_target->hasClient(client)) { logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str()); - return ; + return; } - if (channel_target->hasClient(client_target->getNickName())) + if(channel_target->hasClient(client_target->getNickName())) { client->sendCode(ERR_USERONCHANNEL, msg.getArgs()[0] + ' ' + msg.getArgs()[1], "is already on channel"); - return ; + return; } - if (channel_target->isInviteOnly() && !channel_target->isOp(client)) + if(channel_target->isInviteOnly() && !channel_target->isOp(client)) { client->sendCode(ERR_CHANOPRIVSNEEDED, msg.getArgs()[1], "INVITE : you're not channel operator"); - return ; + return; } client_target->inviteToChannel(channel_target->getName()); @@ -368,52 +368,52 @@ namespace irc void Server::handleKick(unstd::SharedPtr client, const Message& msg) { - if (msg.getArgs().empty()) + if(msg.getArgs().empty()) { client->sendCode(ERR_NONICKNAMEGIVEN, "KICK : No nickname given"); - return ; + return; } typedef std::vector::iterator name_it; std::vector channels = unstd::split(msg.getArgs()[0], ','); std::vector users = unstd::split(msg.getArgs()[1], ','); - if (users.size() != channels.size()) + if(users.size() != channels.size()) { client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters"); - return ; + return; } - for (name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel) + for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel) { - if (!isUserKnown(*user)) + if(!isUserKnown(*user)) { client->sendCode(ERR_NOSUCHNICK, const_cast(*user) + " : no such nick"); continue; } - if (!isChannelKnown(*channel)) + if(!isChannelKnown(*channel)) { client->sendCode(ERR_NOSUCHCHANNEL, const_cast(*channel) + " : no such channel"); continue; } Channel* channel_target = getChannelByName(*channel); - if (channel_target == NULL) + if(channel_target == NULL) logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->c_str()); unstd::SharedPtr client_target = getClientByName(*user); - if (client_target.get() == NULL) + if(client_target.get() == NULL) logs::report(log_fatal_error, "(KICK), cannot get client '%s' by name; panic !", user->c_str()); - if (!channel_target->kick(client, client_target, msg.getReason())) + if(!channel_target->kick(client, client_target, msg.getReason())) continue; client->printUserHeader(); std::cout << "kicked " << *user << " from " << *channel << std::endl; - if (channel_target->getNumberOfClients() == 0) + if(channel_target->getNumberOfClients() == 0) { channel_it it; - for (it = _channels.begin(); it < _channels.end(); ++it) + for(it = _channels.begin(); it < _channels.end(); ++it) { - if (it->getName() == msg.getArgs()[0]) + if(it->getName() == msg.getArgs()[0]) break; } _channels.erase(it); @@ -426,37 +426,37 @@ namespace irc { irc::Channel *chan; - if (msg.getTokens().size() != 2) - return ; - if ((chan = getChannelByName(msg.getTokens()[1])) == NULL) + if(msg.getTokens().size() != 2) + return; + if((chan = getChannelByName(msg.getTokens()[1])) == NULL) { client->sendCode(ERR_NOSUCHCHANNEL, "WHO : No such channel"); - return ; + return; } chan->sendWho(client); } void Server::handleTopic(unstd::SharedPtr client, const Message& msg) { - if (msg.getArgs().empty()) + if(msg.getArgs().empty()) { client->sendCode(ERR_NEEDMOREPARAMS, "TOPIC : Need more parameters"); - return ; + return; } - if (!isChannelKnown(msg.getArgs()[0])) + if(!isChannelKnown(msg.getArgs()[0])) { client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel"); - return ; + return; } Channel* channel = getChannelByName(msg.getArgs()[0]); - if (channel == NULL) + if(channel == NULL) logs::report(log_fatal_error, "(TOPIC), cannot get channel '%s' by name; panic !", channel->getName().c_str()); - if (!channel->hasClient(client)) + if(!channel->hasClient(client)) { client->sendCode(ERR_NOTONCHANNEL, msg.getArgs()[0] + " you're not on that channel"); - return ; + return; } - if (!msg.getReason().empty()) + if(!msg.getReason().empty()) { channel->setTopic(client, msg.getReason()); client->printUserHeader(); @@ -468,10 +468,10 @@ namespace irc void Server::handlePing(unstd::SharedPtr client, const Message& msg) { - if (msg.getTokens().size() == 1) - return ; + if(msg.getTokens().size() == 1) + return; std::string out = "PONG"; - for (std::vector::const_iterator it = msg.getTokens().begin() + 1; it < msg.getTokens().end(); ++it) + for(std::vector::const_iterator it = msg.getTokens().begin() + 1; it < msg.getTokens().end(); ++it) out += ' ' + *it; out += "\r\n"; client->sendPlainText(out); @@ -482,35 +482,35 @@ namespace irc void Server::handleMode(unstd::SharedPtr client, const Message& msg) { irc::Channel *chan; - if (msg.getTokens().size() < 2) + if(msg.getTokens().size() < 2) { client->sendCode(ERR_NEEDMOREPARAMS, "MODE : Need more parameters"); - return ; + return; } - if (msg.getTokens().size() == 2 && (msg.getTokens()[1][0] == '#' || msg.getTokens()[1][0] == '&')) + if(msg.getTokens().size() == 2 && (msg.getTokens()[1][0] == '#' || msg.getTokens()[1][0] == '&')) { chan = getChannelByName(msg.getTokens()[1]); - if (chan == NULL) + if(chan == NULL) client->sendCode(ERR_NOSUCHCHANNEL, "MODE : No such channel"); else chan->showModes(client); - return ; + return; } chan = getChannelByName(msg.getTokens()[1]); - if (chan == NULL) + if(chan == NULL) { client->sendCode(ERR_NOSUCHCHANNEL, "MODE : No such channel"); - return ; + return; } - if (!chan->isOp(client)) + if(!chan->isOp(client)) { client->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *chan, "MODE : You need operator privileges to execute this command"); - return ; + return; } - if (msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+') + if(msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+') { client->sendCode(ERR_UNKNOWNMODE, "MODE : Invalid flags (missing +/-)"); - return ; + return; } chan->changeMode(client, msg); } @@ -518,7 +518,7 @@ namespace irc { srand(time(NULL)); - if (rand() % 6 == 0) + if(rand() % 6 == 0) { client->kill("Pew Pew"); usleep(100); diff --git a/srcs/unstd/string.cpp b/srcs/unstd/string.cpp index 1ef52e7..5386f24 100644 --- a/srcs/unstd/string.cpp +++ b/srcs/unstd/string.cpp @@ -18,13 +18,13 @@ namespace unstd { std::vector elems; std::string token; - for (std::string::const_iterator it = s.begin(); it != s.end();) + for(std::string::const_iterator it = s.begin(); it != s.end();) { - if (*it == delim) + if(*it == delim) { elems.push_back(token); token.clear(); - while (*it == delim && it != s.end()) + while(*it == delim && it != s.end()) it++; } else diff --git a/srcs_bonus/bot.cpp b/srcs_bonus/bot.cpp index 3d543e7..a6cdc33 100644 --- a/srcs_bonus/bot.cpp +++ b/srcs_bonus/bot.cpp @@ -28,7 +28,7 @@ Bot::Bot() : _channel_created(false), _logged(false), _fd(-1) Bot::~Bot() { - if (_fd >= 0) + if(_fd >= 0) close(_fd); } @@ -42,29 +42,29 @@ void signalsHandler(int foo) bool Bot::init(const std::string &ip, const std::string &port, const std::string &password) { - if (ip.empty() || port.empty() || password.empty()) + if(ip.empty() || port.empty() || password.empty()) irc::logs::report(irc::log_fatal_error, "An argument is empty"); char* end; int portval = std::strtol(port.c_str(), &end, 10); - if (errno == ERANGE || *end != 0 || portval < 0 || portval > 0xFFFF) + if(errno == ERANGE || *end != 0 || portval < 0 || portval > 0xFFFF) irc::logs::report(irc::log_fatal_error, "invalid port"); _connect_commands.push_back("PASS " + password + "\r\n"); _connect_commands.push_back("NICK greg\r\n"); _connect_commands.push_back("USER greg_Bot 0 * :Botrealname\r\n"); _connect_commands.push_back("JOIN #greg\r\n"); _fd = socket(AF_INET, SOCK_STREAM, 0); - if (_fd == -1) + if(_fd == -1) irc::logs::report(irc::log_fatal_error, "FD error"); _serv_addr.sin_family = AF_INET; _serv_addr.sin_port = htons(portval); _serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); - if (connect(_fd, (struct sockaddr*)&_serv_addr, sizeof(_serv_addr)) < 0) + if(connect(_fd, (struct sockaddr*)&_serv_addr, sizeof(_serv_addr)) < 0) { irc::logs::report(irc::log_error, "connect error"); return (false); } - if (fcntl(_fd, F_SETFL, O_NONBLOCK) < 0) + if(fcntl(_fd, F_SETFL, O_NONBLOCK) < 0) { irc::logs::report(irc::log_error, "fcntl error"); return (false); @@ -76,48 +76,48 @@ bool Bot::init(const std::string &ip, const std::string &port, const std::string void Bot::send_message(const std::string &content) { - if (send(_fd, content.c_str(), content.length(), 0) < 0) + if(send(_fd, content.c_str(), content.length(), 0) < 0) irc::logs::report(irc::log_fatal_error, "send error"); } void Bot::handle_response(std::string buffer) { - if (!_logged && buffer == ":YipIRC 001 greg :Welcome to YipIRC 😀, your nickname is : greg\r\n") + if(!_logged && buffer == ":YipIRC 001 greg :Welcome to YipIRC 😀, your nickname is : greg\r\n") { _logged = true; irc::logs::report(irc::log_message, "Logged in succesfully"); } - else if (!_logged) - return ; - if (!_channel_created && buffer == ":greg JOIN :#greg\r\n") + else if(!_logged) + return; + if(!_channel_created && buffer == ":greg JOIN :#greg\r\n") { _channel_created = true; irc::logs::report(irc::log_message, "Created the channel succesfully"); } - else if (!_channel_created) - return ; - if (buffer.find("KICK #greg greg :") != std::string::npos || buffer.find("explose") != std::string::npos) + else if(!_channel_created) + return; + if(buffer.find("KICK #greg greg :") != std::string::npos || buffer.find("explose") != std::string::npos) { send_message("QUIT: Explose\r\n"); std::exit(0); } - if (buffer.find("quoi") != std::string::npos) + if(buffer.find("quoi") != std::string::npos) send_message("PRIVMSG #greg :feur\r\n"); } void Bot::connect_to_server() { char buffer[1024]; - for (std::vector::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it) + for(std::vector::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it) { - if (send(_fd, (*it).c_str(), (*it).size(), 0) < 0) + if(send(_fd, (*it).c_str(), (*it).size(), 0) < 0) irc::logs::report(irc::log_fatal_error, "send error"); - if (recv(_fd, buffer, 1024, 0) > 0) + if(recv(_fd, buffer, 1024, 0) > 0) handle_response(buffer); } - while (active) + while(active) { - if (recv(_fd, buffer, 1024, 0) > 0) + if(recv(_fd, buffer, 1024, 0) > 0) handle_response(buffer); std::memset(buffer, 0, sizeof(buffer)); } diff --git a/srcs_bonus/main.cpp b/srcs_bonus/main.cpp index 1f71ad9..fff1d7b 100644 --- a/srcs_bonus/main.cpp +++ b/srcs_bonus/main.cpp @@ -15,11 +15,11 @@ int main(int argc, char **argv) { - if (argc != 4) + if(argc != 4) irc::logs::report(irc::log_fatal_error, "./greg "); Bot greg; - if (!greg.init(argv[1], argv[2], argv[3])) + if(!greg.init(argv[1], argv[2], argv[3])) return 1; greg.connect_to_server(); return 0;