Compare commits
46 Commits
78aa891d38
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0582a52aad | |||
|
|
1497a3c979 | ||
| fe094406d0 | |||
|
|
fa65a4a2a7 | ||
| 1ad176b41d | |||
|
|
248da65e44 | ||
| b5ae2778fc | |||
|
|
2ae69c33a8 | ||
| 396891122c | |||
|
|
6b87fe8840 | ||
|
|
dec6930a9a | ||
|
|
e0d977d6b5 | ||
| e60ea7aab6 | |||
|
|
91533c0b9f | ||
|
|
e5e08d4cd0 | ||
| dec5a64118 | |||
| b325076d01 | |||
|
|
065f6ca10e | ||
|
|
bed4006e90 | ||
| edd4019f4e | |||
|
|
dd4de38e5f | ||
|
|
85d6bae03a | ||
| 982ca33116 | |||
|
|
fba8c95097 | ||
| 619b0360b2 | |||
|
|
66bd57bfde | ||
| 3de8940560 | |||
|
|
a21bac6e47 | ||
| 9a2d7919fb | |||
|
|
e95cd82211 | ||
| 3bce66fdd0 | |||
|
|
b7487c193d | ||
| f3400b6151 | |||
| 2b44730e1a | |||
| b1f2d3ad1f | |||
|
|
99e4e5cdb3 | ||
| dd019163e3 | |||
|
|
61fae2d6a8 | ||
| 81d71ea2ec | |||
|
|
18f813ed37 | ||
| d438bb828a | |||
| 86d1386b8e | |||
| f1f742959c | |||
| 300d489365 | |||
| 9093ac3511 | |||
|
|
d408d08249 |
@@ -33,6 +33,9 @@ Channel& Channel::operator=(const Channel &other)
|
|||||||
name_ = other.name_;
|
name_ = other.name_;
|
||||||
members_ = other.members_;
|
members_ = other.members_;
|
||||||
operators_ = other.operators_;
|
operators_ = other.operators_;
|
||||||
|
topic_ = other.topic_;
|
||||||
|
isInviteOnly_ = other.isInviteOnly_;
|
||||||
|
invitedMembers_ = other.invitedMembers_;
|
||||||
}
|
}
|
||||||
// std::cout << "Channel copy assignment operator called" << std::endl;
|
// std::cout << "Channel copy assignment operator called" << std::endl;
|
||||||
return (*this);
|
return (*this);
|
||||||
@@ -41,11 +44,24 @@ Channel& Channel::operator=(const Channel &other)
|
|||||||
|
|
||||||
// Constructor with name
|
// Constructor with name
|
||||||
|
|
||||||
Channel::Channel(std::string name) : name_(name) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
|
Channel::Channel(std::string &name) : name_(name), isInviteOnly_(false) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
|
||||||
|
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
std::string Channel::getName() const { return (this->name_); }
|
||||||
|
|
||||||
|
const std::set<int> &Channel::getMembers() const
|
||||||
|
{
|
||||||
|
return (members_);
|
||||||
|
}
|
||||||
|
|
||||||
// Members
|
// Members
|
||||||
void Channel::addMember(int fd) { this->members_.insert(fd); }
|
void Channel::addMember(int fd)
|
||||||
|
{
|
||||||
|
invitedMembers_.erase(fd);
|
||||||
|
this->members_.insert(fd);
|
||||||
|
}
|
||||||
|
|
||||||
void Channel::removeMember(int fd) { this->members_.erase(fd); }
|
void Channel::removeMember(int fd) { this->members_.erase(fd); }
|
||||||
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
|
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
|
||||||
|
|
||||||
@@ -53,3 +69,49 @@ bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0);
|
|||||||
void Channel::addOperator(int fd) { this->operators_.insert(fd); }
|
void Channel::addOperator(int fd) { this->operators_.insert(fd); }
|
||||||
void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
|
void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
|
||||||
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); }
|
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); }
|
||||||
|
|
||||||
|
void Channel::broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd)
|
||||||
|
{
|
||||||
|
for (std::set<int>::iterator member = members_.begin(); member != members_.end(); member++)
|
||||||
|
{
|
||||||
|
if ((*member) == excludedFd) continue;
|
||||||
|
std::map<int, User>::const_iterator user = clients.find(*member);
|
||||||
|
|
||||||
|
if (user != clients.end())
|
||||||
|
user->second.send(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Topic
|
||||||
|
|
||||||
|
void Channel::setTopic(std::string content) { this->topic_ = content; }
|
||||||
|
std::string Channel::getTopic() { return (this->topic_); }
|
||||||
|
|
||||||
|
|
||||||
|
// Mode
|
||||||
|
|
||||||
|
void Channel::setMode(std::string& mode, std::string& args)
|
||||||
|
{
|
||||||
|
bool value;
|
||||||
|
if (mode[0] == '+')
|
||||||
|
value = true;
|
||||||
|
else
|
||||||
|
value = false;
|
||||||
|
|
||||||
|
// very simple to test
|
||||||
|
if (mode[1] == 'i')
|
||||||
|
{
|
||||||
|
isInviteOnly_ = value;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) args;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::inviteMember(const User& client)
|
||||||
|
{
|
||||||
|
invitedMembers_.insert(client.getFd());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::isInviteOnly() const { return isInviteOnly_; }
|
||||||
|
bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; }
|
||||||
|
|||||||
@@ -16,31 +16,57 @@
|
|||||||
|
|
||||||
# include <iostream>
|
# include <iostream>
|
||||||
# include <set>
|
# include <set>
|
||||||
|
# include <map>
|
||||||
|
|
||||||
|
# include "../User/User.hpp"
|
||||||
|
|
||||||
class Channel
|
class Channel
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::string name_;
|
std::string name_;
|
||||||
std::set<int> members_;
|
|
||||||
std::set<int> operators_;
|
std::set<int> members_;
|
||||||
|
std::set<int> operators_;
|
||||||
|
|
||||||
|
std::string topic_;
|
||||||
|
|
||||||
|
bool isInviteOnly_;
|
||||||
|
std::set<int> invitedMembers_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Channel();
|
Channel();
|
||||||
Channel(std::string name);
|
Channel(std::string &name);
|
||||||
Channel(const Channel &other);
|
Channel(const Channel &other);
|
||||||
Channel& operator=(const Channel &other);
|
Channel& operator=(const Channel &other);
|
||||||
~Channel();
|
~Channel();
|
||||||
|
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
std::string getName() const;
|
||||||
|
const std::set<int> &getMembers() const;
|
||||||
|
|
||||||
|
// Topic
|
||||||
|
void setTopic(std::string content);
|
||||||
|
std::string getTopic();
|
||||||
|
|
||||||
// Users
|
// Users
|
||||||
void addMember(int fd);
|
void addMember(int fd);
|
||||||
void removeMember(int fd);
|
void removeMember(int fd);
|
||||||
bool hasMember(int fd) const;
|
bool hasMember(int fd) const;
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
void addOperator(int fd);
|
void addOperator(int fd);
|
||||||
void removeOperator(int fd);
|
void removeOperator(int fd);
|
||||||
bool hasOperator(int fd) const;
|
bool hasOperator(int fd) const;
|
||||||
|
|
||||||
|
void broadcast(const std::string &msg, const std::map<int, User> &clients, int excludedFd);
|
||||||
|
|
||||||
|
// modes
|
||||||
|
void setMode(std::string& mode, std::string& args);
|
||||||
|
|
||||||
|
void inviteMember(const User& client);
|
||||||
|
bool isInviteOnly() const;
|
||||||
|
bool isInvited(int fd) const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
7
Makefile
7
Makefile
@@ -1,6 +1,11 @@
|
|||||||
NAME = ircserv
|
NAME = ircserv
|
||||||
|
|
||||||
SRC = main.cpp Server/Server.cpp User/User.cpp
|
SRC = main.cpp Server/Server.cpp User/User.cpp \
|
||||||
|
Channel/Channel.cpp \
|
||||||
|
cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \
|
||||||
|
cmds/join.cpp cmds/privmsg.cpp cmds/quit.cpp \
|
||||||
|
cmds/mode.cpp cmds/invite.cpp cmds/kick.cpp \
|
||||||
|
cmds/topic.cpp
|
||||||
|
|
||||||
HEADERS = Server/Server.hpp User/User.hpp
|
HEADERS = Server/Server.hpp User/User.hpp
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* Server.cpp :+: :+: :+: */
|
/* Server.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/12 19:25:29 by iherman- ### ########.fr */
|
/* Updated: 2026/05/25 10:15:18 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -35,14 +35,6 @@
|
|||||||
|
|
||||||
const int Server::kConnectionQueueLimit = 10;
|
const int Server::kConnectionQueueLimit = 10;
|
||||||
|
|
||||||
// TEMPORARY TESTING FUNCTION
|
|
||||||
void echo(User& client, std::istringstream& input)
|
|
||||||
{
|
|
||||||
std::string message = "Server received: " + input.str();
|
|
||||||
send(client.getFd(), message.c_str(), message.size(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Server::Server() :
|
Server::Server() :
|
||||||
port_(PORT_DEFAULT),
|
port_(PORT_DEFAULT),
|
||||||
password_("password")
|
password_("password")
|
||||||
@@ -93,6 +85,13 @@ Server::Server(int port, const std::string& password) :
|
|||||||
if (serverSocket_ < 0)
|
if (serverSocket_ < 0)
|
||||||
throw std::runtime_error("Failed to create socket");
|
throw std::runtime_error("Failed to create socket");
|
||||||
|
|
||||||
|
int opt = 1;
|
||||||
|
if (setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
|
||||||
|
{
|
||||||
|
close(serverSocket_);
|
||||||
|
throw std::runtime_error("setsockopt failed");
|
||||||
|
}
|
||||||
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
std::memset(&addr, 0, sizeof(addr));
|
std::memset(&addr, 0, sizeof(addr));
|
||||||
|
|
||||||
@@ -101,13 +100,28 @@ Server::Server(int port, const std::string& password) :
|
|||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
if (bind(serverSocket_, (struct sockaddr*)&addr, sizeof(addr)))
|
if (bind(serverSocket_, (struct sockaddr*)&addr, sizeof(addr)))
|
||||||
|
{
|
||||||
|
close(serverSocket_);
|
||||||
throw std::runtime_error("Failed to bind");
|
throw std::runtime_error("Failed to bind");
|
||||||
|
}
|
||||||
|
|
||||||
if (listen(serverSocket_, kConnectionQueueLimit))
|
if (listen(serverSocket_, kConnectionQueueLimit))
|
||||||
|
{
|
||||||
|
close(serverSocket_); // maybe make an fd class that cleans up automatically using the destructor
|
||||||
throw std::runtime_error("Failed to listen");
|
throw std::runtime_error("Failed to listen");
|
||||||
|
}
|
||||||
|
|
||||||
// Add all new commands to commands_ here
|
// Add all new commands to commands_ here
|
||||||
commands_["echo"] = &echo;
|
commands_["PASS"] = &Server::pass_cmd;
|
||||||
|
commands_["NICK"] = &Server::nick_cmd;
|
||||||
|
commands_["USER"] = &Server::user_cmd;
|
||||||
|
commands_["JOIN"] = &Server::join_cmd;
|
||||||
|
commands_["QUIT"] = &Server::quit_cmd;
|
||||||
|
commands_["PRIVMSG"] = &Server::privmsg_cmd;
|
||||||
|
commands_["MODE"] = & Server::mode_cmd;
|
||||||
|
commands_["INVITE"] = &Server::invite_cmd;
|
||||||
|
commands_["KICK"] = &Server::kick_cmd;
|
||||||
|
commands_["TOPIC"] = &Server::topic_cmd;
|
||||||
|
|
||||||
std::cout << "Server port: " << port_
|
std::cout << "Server port: " << port_
|
||||||
<< "\nServer Password: " << password_
|
<< "\nServer Password: " << password_
|
||||||
@@ -142,7 +156,13 @@ void Server::parseCommand(User& client)
|
|||||||
|
|
||||||
args >> command;
|
args >> command;
|
||||||
|
|
||||||
std::map<std::string, void (*)(User&, std::istringstream&)>::iterator it = commands_.find(command);
|
if (command.empty())
|
||||||
|
{
|
||||||
|
client.clearBuffer();
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string, void (Server::*)(User&, std::istringstream&)>::iterator it = commands_.find(command);
|
||||||
if (it == commands_.end())
|
if (it == commands_.end())
|
||||||
{
|
{
|
||||||
std::string message = "Error: command not found!\n";
|
std::string message = "Error: command not found!\n";
|
||||||
@@ -150,7 +170,7 @@ void Server::parseCommand(User& client)
|
|||||||
client.clearBuffer();
|
client.clearBuffer();
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
it->second(client, args);
|
(this->*(it->second))(client, args);
|
||||||
client.clearBuffer();
|
client.clearBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +191,12 @@ bool Server::handleClient(User& client)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.appendBuffer(std::string(buffer, recv_amount));
|
std::string data(buffer, recv_amount);
|
||||||
|
size_t pos = data.find('\r');
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
data.erase(pos, 1);
|
||||||
|
|
||||||
|
client.appendBuffer(data);
|
||||||
|
|
||||||
if (client.getBuffer().find('\n') != std::string::npos)
|
if (client.getBuffer().find('\n') != std::string::npos)
|
||||||
parseCommand(client);
|
parseCommand(client);
|
||||||
@@ -212,6 +237,8 @@ std::vector<struct pollfd>::iterator Server::removeClient(std::vector<struct pol
|
|||||||
|
|
||||||
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
|
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
|
||||||
|
|
||||||
|
// should also remove client from all channels its in
|
||||||
|
|
||||||
clients_.erase(client->fd);
|
clients_.erase(client->fd);
|
||||||
return (sockets_.erase(client));
|
return (sockets_.erase(client));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* Server.hpp :+: :+: :+: */
|
/* Server.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/12 19:57:32 by iherman- ### ########.fr */
|
/* Updated: 2026/05/25 10:15:08 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -22,8 +22,16 @@
|
|||||||
# include <vector>
|
# include <vector>
|
||||||
# include <map>
|
# include <map>
|
||||||
|
|
||||||
# include "../User/User.hpp"
|
# include <cctype>
|
||||||
|
# include <sstream>
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
# include "../User/User.hpp"
|
||||||
|
# include "../Channel/Channel.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
# define SERVER_NAME "irc.server"
|
||||||
# define PORT_DEFAULT 9898
|
# define PORT_DEFAULT 9898
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
@@ -36,10 +44,11 @@ class Server
|
|||||||
|
|
||||||
std::string password_;
|
std::string password_;
|
||||||
|
|
||||||
std::vector<struct pollfd> sockets_;
|
std::vector<struct pollfd> sockets_;
|
||||||
std::map<int, User> clients_;
|
std::map<int, User> clients_;
|
||||||
|
std::map<std::string, Channel> channels_;
|
||||||
|
|
||||||
std::map<std::string, void (*)(User&, std::istringstream&)> commands_;
|
std::map<std::string, void (Server::*)(User&, std::istringstream&)> commands_;
|
||||||
|
|
||||||
bool handleClient(User& client);
|
bool handleClient(User& client);
|
||||||
|
|
||||||
@@ -51,6 +60,18 @@ class Server
|
|||||||
Server(const Server& other);
|
Server(const Server& other);
|
||||||
Server &operator=(const Server& other);
|
Server &operator=(const Server& other);
|
||||||
|
|
||||||
|
// Commands
|
||||||
|
void pass_cmd(User &client, std::istringstream &ss);
|
||||||
|
void nick_cmd(User &client, std::istringstream &ss);
|
||||||
|
void user_cmd(User &client, std::istringstream &ss);
|
||||||
|
void join_cmd(User &client, std::istringstream &ss);
|
||||||
|
void privmsg_cmd(User &client, std::istringstream &ss);
|
||||||
|
void quit_cmd(User &client, std::istringstream &ss);
|
||||||
|
void mode_cmd(User &client, std::istringstream &ss);
|
||||||
|
void invite_cmd(User &client, std::istringstream &ss);
|
||||||
|
void kick_cmd(User &client, std::istringstream &ss);
|
||||||
|
void topic_cmd(User &client, std::istringstream &ss);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server();
|
Server();
|
||||||
Server(int port, const std::string& password);
|
Server(int port, const std::string& password);
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ User& User::operator=(const User &other)
|
|||||||
buffer = other.buffer;
|
buffer = other.buffer;
|
||||||
authenticated = other.authenticated;
|
authenticated = other.authenticated;
|
||||||
registered = other.registered;
|
registered = other.registered;
|
||||||
|
channels_ = other.channels_;
|
||||||
}
|
}
|
||||||
// std::cout << "User copy assignment operator called" << std::endl;
|
// std::cout << "User copy assignment operator called" << std::endl;
|
||||||
return (*this);
|
return (*this);
|
||||||
@@ -53,6 +54,33 @@ User::~User()
|
|||||||
// std::cout << "User destructor called" << std::endl;
|
// std::cout << "User destructor called" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void User::send(const std::string &msg) const
|
||||||
|
{
|
||||||
|
::send(fd, msg.c_str(), msg.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Channels
|
||||||
|
void User::joinChannel(const std::string &channel)
|
||||||
|
{
|
||||||
|
channels_.insert(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
void User::leaveChannel(const std::string &channel)
|
||||||
|
{
|
||||||
|
channels_.erase(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool User::isInChannel(const std::string &channel) const
|
||||||
|
{
|
||||||
|
return (channels_.count(channel) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::set<std::string> &User::getChannels() const
|
||||||
|
{
|
||||||
|
return (channels_);
|
||||||
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
int User::getFd() const { return (this->fd); }
|
int User::getFd() const { return (this->fd); }
|
||||||
|
|||||||
@@ -15,17 +15,21 @@
|
|||||||
# define USER_HPP
|
# define USER_HPP
|
||||||
|
|
||||||
# include <iostream>
|
# include <iostream>
|
||||||
|
# include <sys/socket.h>
|
||||||
|
# include <set>
|
||||||
|
|
||||||
class User
|
class User
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int fd;
|
int fd;
|
||||||
std::string nick;
|
std::string nick;
|
||||||
std::string username;
|
std::string username;
|
||||||
std::string realname;
|
std::string realname;
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
bool authenticated;
|
bool authenticated;
|
||||||
bool registered;
|
bool registered;
|
||||||
|
|
||||||
|
std::set<std::string> channels_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
User();
|
User();
|
||||||
@@ -34,28 +38,35 @@ class User
|
|||||||
User& operator=(const User &other);
|
User& operator=(const User &other);
|
||||||
~User();
|
~User();
|
||||||
|
|
||||||
|
void send(const std::string &msg) const;
|
||||||
|
|
||||||
|
// Channels
|
||||||
|
void joinChannel(const std::string &channel);
|
||||||
|
void leaveChannel( const std::string &channel);
|
||||||
|
bool isInChannel(const std::string &channel) const;
|
||||||
|
const std::set<std::string> &getChannels() const;
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
int getFd() const;
|
int getFd() const;
|
||||||
std::string getNick() const;
|
std::string getNick() const;
|
||||||
std::string getUsername() const;
|
std::string getUsername() const;
|
||||||
std::string getRealname() const;
|
std::string getRealname() const;
|
||||||
std::string &getBuffer();
|
std::string &getBuffer();
|
||||||
bool isAuthenticated() const;
|
bool isAuthenticated() const;
|
||||||
bool isRegistered() const;
|
bool isRegistered() const;
|
||||||
|
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
void setNick(std::string nick);
|
void setNick(std::string nick);
|
||||||
void setUsername(std::string username);
|
void setUsername(std::string username);
|
||||||
void setRealname(std::string realname);
|
void setRealname(std::string realname);
|
||||||
void appendBuffer(std::string buff);
|
void appendBuffer(std::string buff);
|
||||||
void clearBuffer();
|
void clearBuffer();
|
||||||
|
|
||||||
void setAuthenticated(bool value);
|
void setAuthenticated(bool value);
|
||||||
void setRegistered(bool value);
|
void setRegistered(bool value);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
72
cmds/invite.cpp
Normal file
72
cmds/invite.cpp
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* invclient_ite.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/23 20:22:46 by iherman- #+# #+# */
|
||||||
|
/* Updated: 2026/05/23 21:14:14 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::invite_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string target;
|
||||||
|
std::string channel;
|
||||||
|
|
||||||
|
ss >> target >> channel;
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||||
|
|
||||||
|
if (target.empty() || channel.empty())
|
||||||
|
{
|
||||||
|
return client.send(":" SERVER_NAME " 461 * INVITE :Not enough parameters\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify target exists
|
||||||
|
std::map<int, User>::iterator client_it = clients_.begin();
|
||||||
|
for (; client_it != clients_.end(); ++client_it)
|
||||||
|
{
|
||||||
|
if (client_it->second.getNick() == target)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (client_it == clients_.end())
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 401 " + target + " :No such nick\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify channel exist & user is on channel
|
||||||
|
std::map<std::string, Channel>::iterator channel_it = channels_.find(channel);
|
||||||
|
if (channel_it == channels_.end())
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 401 " + target + " :No such channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!channel_it->second.hasMember(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 442 " + channel + " :You are not on that channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel_it->second.hasMember(client_it->second.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 443 " + channel + " :is already on channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel_it->second.isInviteOnly() && !channel_it->second.hasOperator(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 482 " + channel + ":You're not channel operator\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
client_it->second.send(":" + client.getNick() + "!" + client.getUsername() + "@localhost INVITE " + client_it->second.getNick() + " :" + channel_it->second.getName());
|
||||||
|
client.send(std::string(":") + SERVER_NAME " 341 " + client.getNick() + " " + client_it->second.getNick() + " " + channel_it->second.getName());
|
||||||
|
channel_it->second.inviteMember(client_it->second);
|
||||||
|
}
|
||||||
|
|
||||||
88
cmds/join.cpp
Normal file
88
cmds/join.cpp
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* join.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/23 18:32:37 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::join_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string args;
|
||||||
|
|
||||||
|
ss >> args;
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||||
|
if (args.empty()) return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " JOIN :Not enough parameters\r\n"));
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator it = channels_.find(args);
|
||||||
|
|
||||||
|
// creates channel
|
||||||
|
if (it == channels_.end())
|
||||||
|
{
|
||||||
|
channels_[args] = Channel(args);
|
||||||
|
channels_[args].addMember(client.getFd());
|
||||||
|
channels_[args].addOperator(client.getFd());
|
||||||
|
client.joinChannel(args);
|
||||||
|
|
||||||
|
std::string joinMsg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost JOIN " + args + "\r\n";
|
||||||
|
channels_[args].broadcast(joinMsg, clients_, -1);
|
||||||
|
|
||||||
|
std::string namesList = ":" SERVER_NAME " 353 " + client.getNick() + " = " + args + " :";
|
||||||
|
const std::set<int> &members = channels_[args].getMembers();
|
||||||
|
for (std::set<int>::const_iterator m = members.begin(); m != members.end(); m++)
|
||||||
|
{
|
||||||
|
std::map<int, User>::iterator u = clients_.find(*m);
|
||||||
|
if (u != clients_.end())
|
||||||
|
{
|
||||||
|
if (channels_[args].hasOperator(*m))
|
||||||
|
namesList += "@";
|
||||||
|
namesList += u->second.getNick() + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client.send(namesList + "\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (it->second.hasMember(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 443 " + client.getNick() + " " + args + " :is already on channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (it->second.isInviteOnly() && !it->second.isInvited(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 473 " + client.getNick() + " " + args + " :Cannot join channel (+i)\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
it->second.addMember(client.getFd());
|
||||||
|
|
||||||
|
client.joinChannel(it->first);
|
||||||
|
|
||||||
|
std::string joinMsg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost JOIN " + args + "\r\n";
|
||||||
|
channels_[args].broadcast(joinMsg, clients_, -1);
|
||||||
|
|
||||||
|
std::string namesList = ":" SERVER_NAME " 353 " + client.getNick() + " = " + args + " :";
|
||||||
|
const std::set<int> &members = channels_[args].getMembers();
|
||||||
|
for (std::set<int>::const_iterator m = members.begin(); m != members.end(); m++)
|
||||||
|
{
|
||||||
|
std::map<int, User>::iterator u = clients_.find(*m);
|
||||||
|
if (u != clients_.end())
|
||||||
|
{
|
||||||
|
if (channels_[args].hasOperator(*m))
|
||||||
|
namesList += "@";
|
||||||
|
namesList += u->second.getNick() + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client.send(namesList + "\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 366 " + client.getNick() + " " + args + " :End of /NAMES list\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
58
cmds/kick.cpp
Normal file
58
cmds/kick.cpp
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* kick.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/25 09:27:35 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/25 09:52:27 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::kick_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string channel;
|
||||||
|
std::string user;
|
||||||
|
std::string reason;
|
||||||
|
|
||||||
|
ss >> channel >> user;
|
||||||
|
getline(ss, reason);
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
|
||||||
|
|
||||||
|
if (channel.empty() || user.empty())
|
||||||
|
return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " KICK :not enough parameters\r\n"));
|
||||||
|
if (!reason.empty() && reason[0] == ' ')
|
||||||
|
reason = reason.substr(1);
|
||||||
|
if (!reason.empty() && reason[0] == ':')
|
||||||
|
reason = reason.substr(1);
|
||||||
|
if (reason.empty()) reason = "Kicked";
|
||||||
|
|
||||||
|
if (channel[0] != '#') return (client.send(""));
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator it_channels = channels_.find(channel);
|
||||||
|
|
||||||
|
if (it_channels == channels_.end())
|
||||||
|
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
|
||||||
|
if (!it_channels->second.hasOperator(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 482 " + client.getNick() + " " + channel + " :You're not channel operator\r\n"));
|
||||||
|
|
||||||
|
int userId = -1;
|
||||||
|
for (std::map<int, User>::iterator it_clients = clients_.begin(); it_clients != clients_.end(); it_clients++)
|
||||||
|
{
|
||||||
|
if (it_clients->second.getNick() == user)
|
||||||
|
userId = it_clients->second.getFd();
|
||||||
|
}
|
||||||
|
if (userId == -1)
|
||||||
|
return (client.send(":" SERVER_NAME " 401 " + client.getNick() + " " + user + " :No such nick\r\n"));
|
||||||
|
if (!it_channels->second.hasMember(userId))
|
||||||
|
return (client.send(":" SERVER_NAME " 441 " + client.getNick() + " " + user + " " + channel + " :They aren't on that channel\r\n"));
|
||||||
|
|
||||||
|
it_channels->second.broadcast(":" + client.getNick() + "!" + client.getUsername() + "@localhost KICK " + channel + " " + user + " :" + reason + "\r\n", clients_, -1);
|
||||||
|
clients_[userId].leaveChannel(channel);
|
||||||
|
it_channels->second.removeMember(userId);
|
||||||
|
|
||||||
|
}
|
||||||
41
cmds/mode.cpp
Normal file
41
cmds/mode.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* mode.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/23 17:15:27 by iherman- #+# #+# */
|
||||||
|
/* Updated: 2026/05/23 20:25:02 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::mode_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string target;
|
||||||
|
std::string mode;
|
||||||
|
std::string args;
|
||||||
|
|
||||||
|
ss >> target >> mode;
|
||||||
|
std::getline(ss, args); // might include space in channel name, need to fix
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||||
|
if (target.empty() || mode.empty()) return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " MODE :Not enough parameters\r\n"));
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator channel = channels_.find(target);
|
||||||
|
if (channel == channels_.end())
|
||||||
|
{
|
||||||
|
client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!channel->second.hasOperator(client.getFd()))
|
||||||
|
{
|
||||||
|
client.send(std::string(":") + SERVER_NAME + " 482 " + client.getNick() + " " + channel->second.getName() + " :You're not channel operator\r\n");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
channel->second.setMode(mode, args); // args should prob be a stringstream :(
|
||||||
|
}
|
||||||
68
cmds/nick.cpp
Normal file
68
cmds/nick.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* nick.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/16 11:12:12 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
static bool isValidNick(const std::string &nick)
|
||||||
|
{
|
||||||
|
const std::string special = "[]\\`_^{|}";
|
||||||
|
|
||||||
|
if (!isalpha(nick[0]) && special.find(nick[0]) == std::string::npos)
|
||||||
|
return (false);
|
||||||
|
for (size_t i = 1; i < nick.size(); i++)
|
||||||
|
{
|
||||||
|
if (!isalnum(nick[i]) && special.find(nick[i]) == std::string::npos && nick[i] != '-')
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
return (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::nick_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string args;
|
||||||
|
|
||||||
|
ss >> args;
|
||||||
|
if (!client.isAuthenticated()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||||
|
if (args.empty()) return (client.send(":" SERVER_NAME " 431 * :Not nickname given\r\n"));
|
||||||
|
if (!isValidNick(args)) return (client.send(":" SERVER_NAME " 432 * " + args + " :Erroneous nickname\r\n"));
|
||||||
|
for (std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++)
|
||||||
|
{
|
||||||
|
if (it->second.getNick() == args)
|
||||||
|
return (client.send(":" SERVER_NAME " 433 * " + args + " :Nickname is already in use\r\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string oldNick = client.getNick();
|
||||||
|
client.setNick(args);
|
||||||
|
|
||||||
|
if (client.isRegistered())
|
||||||
|
{
|
||||||
|
std::string msg = ":" + oldNick + " NICK " + args + "\r\n";
|
||||||
|
const std::set<std::string> &userChannels = client.getChannels();
|
||||||
|
for (std::set<std::string>::const_iterator it = userChannels.begin(); it != userChannels.end(); it++)
|
||||||
|
{
|
||||||
|
std::map<std::string, Channel>::iterator ch = channels_.find(*it);
|
||||||
|
if (ch != channels_.end())
|
||||||
|
ch->second.broadcast(msg, clients_, -1);
|
||||||
|
}
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!client.getUsername().empty())
|
||||||
|
{
|
||||||
|
client.setRegistered(true);
|
||||||
|
client.send(":" SERVER_NAME " 001 " + args + " :Welcome to the IRC Network " + args + "\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 002 " + args + " :Your host is " SERVER_NAME ", running version 1.0\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 003 " + args + " :This server was created May 2026\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 004 " + args + " :" SERVER_NAME " 1.0\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
|
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
|
||||||
/* Updated: 2026/05/10 22:45:31 by aortigos ### ########.fr */
|
/* Updated: 2026/05/15 22:08:49 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -17,18 +17,11 @@ void Server::pass_cmd(User &client, std::istringstream &ss)
|
|||||||
std::string args;
|
std::string args;
|
||||||
|
|
||||||
ss >> args;
|
ss >> args;
|
||||||
if (client.isAuthenticated())
|
if (args.empty()) return (client.send(":" SERVER_NAME " 461 * PASS :Not enough parameters\r\n"));
|
||||||
{
|
if (client.isAuthenticated()) return (client.send(":" SERVER_NAME " 462 " + client.getNick() + " :Unauthorized command (already registered)\r\n"));
|
||||||
// Still need to find what server should reply to different errors
|
|
||||||
std::string res = "You are already authenticated";
|
|
||||||
send(client.getFd(), res.c_str(), res.size(), 0);
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
if (this->password_ == args)
|
if (this->password_ == args)
|
||||||
client->authenticated = true;
|
client.setAuthenticated(true);
|
||||||
else {
|
else
|
||||||
// Still need to find what server should reply to different errors
|
client.send(":" SERVER_NAME " 464 * :Password incorrect\r\n");
|
||||||
std::string res = "Invalid password";
|
|
||||||
send(client.getFd(), res.c_str(), res.size(), 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
52
cmds/privmsg.cpp
Normal file
52
cmds/privmsg.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* privmsg.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/25 10:02:13 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::privmsg_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string channel;
|
||||||
|
std::string message;
|
||||||
|
|
||||||
|
ss >> channel;
|
||||||
|
getline(ss, message);
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
|
||||||
|
|
||||||
|
if (channel.empty())
|
||||||
|
return (client.send(":" SERVER_NAME " 411 " + client.getNick() + " :No recipient given (PRIVMSG)\r\n"));
|
||||||
|
|
||||||
|
if (message.empty()) return (client.send(":" SERVER_NAME " 412 " + client.getNick() + " :No text to send\r\n"));
|
||||||
|
|
||||||
|
if (message[0] == ' ')
|
||||||
|
message = message.substr(1);
|
||||||
|
|
||||||
|
if (channel[0] == '#')
|
||||||
|
{
|
||||||
|
std::map<std::string, Channel>::iterator it = channels_.find(channel);
|
||||||
|
|
||||||
|
if (it == channels_.end())
|
||||||
|
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
|
||||||
|
if (!it->second.hasMember(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 404 " + client.getNick() + " " + channel + " :Cannot send to channel\r\n"));
|
||||||
|
|
||||||
|
std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n";
|
||||||
|
it->second.broadcast(msg, clients_, client.getFd());
|
||||||
|
} else {
|
||||||
|
for(std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); it++)
|
||||||
|
{
|
||||||
|
if (it->second.getNick() == channel)
|
||||||
|
return (it->second.send(":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n"));
|
||||||
|
}
|
||||||
|
client.send(":" SERVER_NAME " 401 " + client.getNick() + " " + channel + " :No such nick\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
42
cmds/quit.cpp
Normal file
42
cmds/quit.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* quit.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/16 11:46:45 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::quit_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string reason;
|
||||||
|
|
||||||
|
std::getline(ss, reason);
|
||||||
|
|
||||||
|
if (!reason.empty() && reason[0] == ' ')
|
||||||
|
reason = reason.substr(1);
|
||||||
|
if (!reason.empty() && reason[0] == ':')
|
||||||
|
reason = reason.substr(1);
|
||||||
|
if (reason.empty())
|
||||||
|
reason = "Leaving";
|
||||||
|
|
||||||
|
std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost QUIT :" + reason + "\r\n";
|
||||||
|
|
||||||
|
const std::set<std::string> channels = client.getChannels();
|
||||||
|
for (std::set<std::string>::const_iterator it = channels.begin(); it != channels.end(); it++)
|
||||||
|
{
|
||||||
|
std::map<std::string, Channel>::iterator ch = channels_.find(*it);
|
||||||
|
if (ch != channels_.end())
|
||||||
|
{
|
||||||
|
ch->second.broadcast(msg, clients_, client.getFd());
|
||||||
|
ch->second.removeMember(client.getFd());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(client.getFd());
|
||||||
|
}
|
||||||
51
cmds/topic.cpp
Normal file
51
cmds/topic.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* topic.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/25 10:04:16 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/25 10:19:53 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::topic_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string channel;
|
||||||
|
std::string message;
|
||||||
|
|
||||||
|
ss >> channel;
|
||||||
|
getline(ss, message);
|
||||||
|
|
||||||
|
if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 " + client.getNick() + " :You have not registered\r\n"));
|
||||||
|
|
||||||
|
|
||||||
|
std::map<std::string, Channel>::iterator it = channels_.find(channel);
|
||||||
|
|
||||||
|
if (it == channels_.end())
|
||||||
|
return (client.send(":" SERVER_NAME " 403 " + client.getNick() + " " + channel + " :No such channel\r\n"));
|
||||||
|
|
||||||
|
if (!it->second.hasMember(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 404 " + client.getNick() + " " + channel + " :Cannot send to channel\r\n"));
|
||||||
|
|
||||||
|
if (message.empty())
|
||||||
|
{
|
||||||
|
std::string res = it->second.getTopic();
|
||||||
|
if (res.empty())
|
||||||
|
return (client.send(":" SERVER_NAME " 331 " + client.getNick() + " " + channel + " :No topic is set\r\n"));
|
||||||
|
client.send(":" SERVER_NAME " 332 " + client.getNick() + " " + channel + " :" + res + "\r\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!message.empty() && message[0] == ' ')
|
||||||
|
message = message.substr(1);
|
||||||
|
if (!it->second.hasOperator(client.getFd()))
|
||||||
|
return (client.send(":" SERVER_NAME " 482 " + client.getNick() + " " + channel + " :You're not channel operator\r\n"));
|
||||||
|
it->second.setTopic(message);
|
||||||
|
it->second.broadcast(":" + client.getNick() + "!" + client.getUsername() + "@localhost TOPIC " + channel + " :" + message + "\r\n", clients_, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
48
cmds/user.cpp
Normal file
48
cmds/user.cpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* user.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/15 22:08:59 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::user_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string username;
|
||||||
|
std::string hostname;
|
||||||
|
std::string servername;
|
||||||
|
std::string realname;
|
||||||
|
|
||||||
|
ss >> username >> hostname >> servername;
|
||||||
|
std::getline(ss, realname);
|
||||||
|
|
||||||
|
if (!client.isAuthenticated()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n"));
|
||||||
|
if (client.isRegistered()) return (client.send(":" SERVER_NAME " 462 " + client.getNick() + " :Unauthorized command (already registered)\r\n"));
|
||||||
|
|
||||||
|
if (!realname.empty() && realname[0] == ' ')
|
||||||
|
realname = realname.substr(1);
|
||||||
|
if (realname.empty() || realname[0] != ':')
|
||||||
|
return (client.send(":" SERVER_NAME " 461 * USER :Not enough parameters\r\n"));
|
||||||
|
if (!realname.empty() && realname[0] == ':')
|
||||||
|
realname = realname.substr(1);
|
||||||
|
|
||||||
|
if (username.empty() || hostname.empty() || servername.empty() || realname.empty())
|
||||||
|
return (client.send(":" SERVER_NAME " 461 * USER :Not enough parameters\r\n"));
|
||||||
|
client.setUsername(username);
|
||||||
|
client.setRealname(realname);
|
||||||
|
|
||||||
|
if (!client.getNick().empty())
|
||||||
|
{
|
||||||
|
client.setRegistered(true);
|
||||||
|
client.send(":" SERVER_NAME " 001 " + client.getNick() + " :Welcome to the IRC Network " + client.getNick() + "\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 002 " + client.getNick() + " :Your host is " SERVER_NAME ", running version 1.0\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 003 " + client.getNick() + " :This server was created May 2026\r\n");
|
||||||
|
client.send(":" SERVER_NAME " 004 " + client.getNick() + " :" SERVER_NAME " 1.0\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
25
tasks.md
25
tasks.md
@@ -37,19 +37,28 @@ Represents a connected IRC client.
|
|||||||
### Second stage
|
### Second stage
|
||||||
|
|
||||||
- [✓] Server can handle multiple clients simultaneously
|
- [✓] Server can handle multiple clients simultaneously
|
||||||
- [ ] Manage SO_REUSEADDR (restarting server fails to bind same port)
|
- [✓] Manage SO_REUSEADDR (restarting server fails to bind same port)
|
||||||
|
|
||||||
### Third stage
|
### Third stage
|
||||||
|
|
||||||
- [ ] Client has nickname and username
|
- [✓] Dispatcher (select function for each command)
|
||||||
- [ ] Implement generic parser (extract command and pass args to command function)
|
- [✓] Implement generic parser (extract command and pass args to command function)
|
||||||
- [ ] Dispatcher (select function for each command)
|
- [✓] Client has nickname and username
|
||||||
- [ ] PASS command for authenticate
|
- [✓] PASS command for authenticate
|
||||||
|
|
||||||
### Fourth stage
|
### Fourth stage
|
||||||
|
|
||||||
- [ ] Client can create/connect to channels
|
- [✓] Client can create/connect to channels
|
||||||
- [ ] JOIN command (user can create channel)
|
- [✓] JOIN command (user can create channel)
|
||||||
- [ ] PRIVMSG command (send message to a channel)
|
- [✓] PRIVMSG command (send message to a channel)
|
||||||
|
- [✓] QUIT command
|
||||||
|
- [✓] PRIVMSG difference between channels (#) and users
|
||||||
|
|
||||||
|
### Fifth stage
|
||||||
|
|
||||||
|
- [ ] PART command (user leaves a channel)
|
||||||
|
- [✓] KICK command (operator removes a user from channel)
|
||||||
|
- [ ] INVITE command (operator invites a user to channel)
|
||||||
|
- [ ] TOPIC command (view or set channel topic)
|
||||||
|
|
||||||
*It will continue...*
|
*It will continue...*
|
||||||
|
|||||||
Reference in New Issue
Block a user