Compare commits

..

66 Commits

Author SHA1 Message Date
982ca33116 Merge pull request 'join cmd and channel fixes' (#30) from channel-fixes into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/30
2026-05-17 11:45:47 +00:00
aortigos
fba8c95097 join cmd and channel fixes 2026-05-17 13:44:55 +02:00
619b0360b2 Merge pull request 'Improving commands and added quit' (#29) from more-commands into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/29
2026-05-16 09:48:21 +00:00
aortigos
66bd57bfde Improving commands and added quit 2026-05-16 11:47:45 +02:00
3de8940560 Merge pull request 'now broadcast send message to every member of channel, excluding the author of message' (#28) from implement-broadcast-function into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/28
2026-05-15 21:12:07 +00:00
aortigos
a21bac6e47 now broadcast send message to every member of channel, excluding the author of message 2026-05-15 23:11:02 +02:00
9a2d7919fb Merge pull request 'less lines, same code' (#27) from re-factoring-code into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/27
2026-05-15 20:09:40 +00:00
aortigos
e95cd82211 less lines, same code 2026-05-15 22:09:14 +02:00
3bce66fdd0 Merge pull request 'Doing join command' (#26) from join-cmd into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/26
2026-05-15 20:07:35 +00:00
aortigos
b7487c193d Doing join command 2026-05-15 22:07:05 +02:00
f3400b6151 Merge pull request 'Channel changes and init JOIN command' (#25) from fixes into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/25
2026-05-15 13:36:24 +00:00
2b44730e1a Channel changes and init JOIN command 2026-05-15 15:35:58 +02:00
b1f2d3ad1f Merge pull request 'user command has been init' (#24) from user-command into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/24
2026-05-15 12:25:10 +00:00
aortigos
99e4e5cdb3 user command has been init 2026-05-15 14:24:46 +02:00
dd019163e3 Merge pull request 'Nick command now do all the checks' (#23) from nick-command into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/23
2026-05-15 09:52:10 +00:00
aortigos
61fae2d6a8 Nick command now do all the checks 2026-05-15 11:46:36 +02:00
81d71ea2ec Merge pull request 'pass has been refactorized and nick command init' (#22) from init-nick-command into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/22
2026-05-15 09:26:35 +00:00
aortigos
18f813ed37 pass has been refactorized and nick command init 2026-05-14 20:40:26 +02:00
d438bb828a Merge pull request 'send function inside client' (#21) from client-send-function into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/21
2026-05-14 15:27:48 +00:00
86d1386b8e send function inside client 2026-05-14 17:27:26 +02:00
f1f742959c Merge pull request 'Parser accept server functions, and first stage for implementing client.send function' (#20) from implement-pass-command into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/20
2026-05-14 15:24:47 +00:00
300d489365 Parser accept server functions, and first stage for implementing client.send function 2026-05-14 17:24:23 +02:00
9093ac3511 Merge pull request 'Added close() calls to all Server constructor fails, also added SO_REUSEADDR' (#19) from constructor-improvement into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/19
2026-05-12 18:39:04 +00:00
iherman-
d408d08249 Added close() calls to all Server constructor fails, also added SO_REUSEADDR 2026-05-12 20:38:33 +02:00
78aa891d38 Merge pull request 'Made the commands_ map be a Server attribute' (#18) from parsing-update into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/18
2026-05-12 18:03:47 +00:00
iherman-
3578bd5b5c Made the commands_ map be a Server attribute 2026-05-12 20:02:32 +02:00
644dbe92a8 Merge pull request 'Added a basic command parser with a temporary echo command. Can be expanded by just adding command functions to the std::map commands, Should maybe move the commands map to the Server class later' (#17) from command-parser into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/17
2026-05-11 18:40:05 +00:00
iherman-
226a271a41 Added a basic command parser with a temporary echo command. Can be expanded by just adding command functions to the std::map commands, Should maybe move the commands map to the Server class later 2026-05-11 20:37:34 +02:00
292fe11f15 Merge pull request 'pass-command' (#16) from pass-command into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/16
2026-05-10 20:46:08 +00:00
aortigos
7f40babccc comments for pass.cpp 2026-05-10 22:45:40 +02:00
aortigos
2f995fe856 cmds folder created and first version of pass cmd 2026-05-10 22:44:27 +02:00
iherman-
c61e0826c6 Merged changes 2026-05-10 20:24:20 +02:00
iherman-
d6652af02d Added missing header to main 2026-05-10 19:04:55 +02:00
iherman-
bdc24594a4 Added functional multiple client handling 2026-05-09 21:52:16 +02:00
b201de3796 Merge pull request 'Channel class init' (#14) from init-channel-class into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/14
2026-05-08 15:24:58 +00:00
1c6c9be6ec Channel class init 2026-05-08 17:24:06 +02:00
d1577cdb08 Merge pull request 'User constructor with fd' (#13) from user-constructor into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/13
2026-05-08 14:40:22 +00:00
86e3034a0a User constructor with fd 2026-05-08 16:37:51 +02:00
3614237b8b Merge pull request 'Fixed syntax and operator=' (#12) from fix-user-syntax into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/12
2026-05-08 14:27:39 +00:00
ffd650da20 Fixed syntax and operator= 2026-05-08 16:27:00 +02:00
63ebb5b9ba Merge pull request 'SRC points to User/User.cpp, instead of Client/Client.cpp' (#11) from fix-makefile into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/11
2026-05-08 14:21:40 +00:00
b688d04fde SRC points to User/User.cpp, instead of Client/Client.cpp 2026-05-08 16:20:57 +02:00
cd4eb82af2 Merge pull request 'echo-server-implementation' (#10) from echo-server-implementation into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/10
2026-05-07 12:22:12 +00:00
iherman-
752eb89e72 Improved Server::run to now properly handle multiple packets 2026-05-07 14:18:52 +02:00
iherman-
4beabead01 Added a basic Server::run function which listens for one connection and sends the data back, added listen call to server constructor 2026-05-07 13:52:42 +02:00
iherman-
0c29c50e23 Created a functional basic constructor for Server class 2026-05-07 13:01:57 +02:00
3a43f9d2bd Merge pull request 'client-class-init' (#9) from client-class-init into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/9
2026-05-07 08:42:26 +00:00
5051c6cfcd Info about user added 2026-05-07 10:41:28 +02:00
f70a46dde0 Client.cpp added to makefile 2026-05-07 10:41:03 +02:00
def1787cb3 User class created 2026-05-07 10:40:05 +02:00
38823adb9d Merge pull request 'Fixed a really stupid mistake wow thats crazy' (#8) from fixing-idiot-mistake into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/8
2026-05-06 16:27:50 +00:00
iherman-
fd7a24de3d Fixed a really stupid mistake wow thats crazy 2026-05-06 18:27:10 +02:00
635335831c Merge pull request 'Removed executable and added a check to the Server class to check whether the port is in a valid range' (#7) from remove-executable into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/7
2026-05-06 16:20:25 +00:00
iherman-
cbc6c7fc41 Removed executable and added a check to the Server class to check whether the port is in a valid range 2026-05-06 18:19:17 +02:00
f2bbec366b Merge pull request 'Made a basic arg parser and shell implementation of the Server class' (#6) from simple-main into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/6
2026-05-06 16:09:48 +00:00
iherman-
b47c7dc9b8 Made a basic arg parser and shell implementation of the Server class 2026-05-06 18:07:17 +02:00
d2e02440b3 Merge pull request 'Finished makefile' (#5) from Improving-Makefile into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/5
2026-05-06 15:09:10 +00:00
Igor Herman Perik
e08ef37928 Finished makefile 2026-05-05 20:51:49 +02:00
bbeaae5923 Merge pull request 'tasks-file' (#4) from tasks-file into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/4
2026-05-05 17:54:02 +00:00
Angel Ortigosa Perez
c31c69c0fa Tasks will continue... 2026-05-05 19:52:58 +02:00
Angel Ortigosa Perez
206ff02eb4 Stages that project must follow 2026-05-05 19:50:33 +02:00
ddfe1202fd Merge pull request 'Created makefile' (#3) from Makefile-creation into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/3
2026-05-05 17:42:40 +00:00
Igor Herman Perik
77e3166f15 Created makefile 2026-05-05 19:41:11 +02:00
Angel Ortigosa Perez
c7bc6f90ca First line in italic 2026-05-05 19:40:10 +02:00
279a71aa31 Merge pull request 'Added thanks at the end' (#2) from readme-improving into main
Reviewed-on: http://gitea.hadi.es/aortigos/ft_irc/pulls/2
2026-05-05 17:37:56 +00:00
Angel Ortigosa Perez
b520ef7c34 Added thanks at the end 2026-05-05 19:36:53 +02:00
16 changed files with 1148 additions and 1 deletions

75
Channel/Channel.cpp Normal file
View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Channel.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/08 16:41:40 by aortigos #+# #+# */
/* Updated: 2026/05/08 16:41:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Channel.hpp"
//////////////////
// Constructors //
//////////////////
Channel::Channel() { /* std::cout << "Channel default constructor called" << std::endl; */}
Channel::~Channel() { /* std::cout << "Channel destructor called" << std::endl; */}
Channel::Channel(const Channel &other)
{
*this = other;
// std::cout << "Channel copy constructor called" << std::endl;
}
Channel& Channel::operator=(const Channel &other)
{
if (this != &other)
{
name_ = other.name_;
members_ = other.members_;
operators_ = other.operators_;
}
// std::cout << "Channel copy assignment operator called" << std::endl;
return (*this);
}
// Constructor with name
Channel::Channel(std::string &name) : name_(name) { /* 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
void Channel::addMember(int fd) { this->members_.insert(fd); }
void Channel::removeMember(int fd) { this->members_.erase(fd); }
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
// Operators
void Channel::addOperator(int fd) { this->operators_.insert(fd); }
void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); }
void Channel::broadcast(const std::string &msg, 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>::iterator user = clients.find(*member);
if (user != clients.end())
user->second.send(msg);
}
}

58
Channel/Channel.hpp Normal file
View File

@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Channel.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/08 16:41:40 by aortigos #+# #+# */
/* Updated: 2026/05/08 16:41:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHANNEL_HPP
# define CHANNEL_HPP
# include <iostream>
# include <set>
# include <map>
# include "../User/User.hpp"
class Channel
{
private:
std::string name_;
std::set<int> members_;
std::set<int> operators_;
public:
Channel();
Channel(std::string &name);
Channel(const Channel &other);
Channel& operator=(const Channel &other);
~Channel();
// Getters
std::string getName() const;
const std::set<int> &getMembers() const;
// Users
void addMember(int fd);
void removeMember(int fd);
bool hasMember(int fd) const;
// Operators
void addOperator(int fd);
void removeOperator(int fd);
bool hasOperator(int fd) const;
void broadcast(const std::string &msg, std::map<int, User> &clients, int excludedFd);
};
#endif

47
Makefile Normal file
View File

@@ -0,0 +1,47 @@
NAME = ircserv
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 \
HEADERS = Server/Server.hpp User/User.hpp
OBJ = $(SRC:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
TOTAL := $(words $(SRC))
COUNT = 0
all: $(NAME)
$(NAME): $(OBJ) $(HEADERS)
@rm -f .build_start
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
%.o: %.cpp
@if [ ! -f .build_start ]; then printf "$(GREEN)Compiling objects...\n$(RESET)"; touch .build_start; fi
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@printf "$(RED)Removing objects...\n$(RESET)"
@rm -f $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

View File

@@ -1,4 +1,4 @@
**This project has been created as part of the 42 curriculum by iherman- and aortigos** *This project has been created as part of the 42 curriculum by iherman- and aortigos*
# Description # Description
@@ -8,3 +8,5 @@ The goal of this project is to do an IRC Server
# Resources # Resources
Thanks for reading :)

298
Server/Server.cpp Normal file
View File

@@ -0,0 +1,298 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
/* Updated: 2026/05/16 11:31:25 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <poll.h>
#include <cerrno>
#include <sstream>
const int Server::kConnectionQueueLimit = 10;
Server::Server() :
port_(PORT_DEFAULT),
password_("password")
{
serverSocket_ = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket_ < 0)
throw std::runtime_error("Failed to create socket");
struct sockaddr_in addr;
std::memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(serverSocket_, (struct sockaddr*)&addr, sizeof(addr)))
throw std::runtime_error("Failed to bind");
if (listen(serverSocket_, kConnectionQueueLimit))
throw std::runtime_error("Failed to listen");
std::cout << "Server port: " << port_
<< "\nServer Password: " << password_
<< std::endl;
}
Server::Server(const Server& other)
{
port_ = other.port_;
serverSocket_ = other.serverSocket_;
password_ = other.password_;
sockets_ = other.sockets_;
}
Server::Server(int port, const std::string& password) :
port_(port),
password_(password)
{
if (port_ < 1 || port_ > 65535)
throw std::runtime_error("Invalid port");
if (password_.empty())
throw std::runtime_error("Empty password");
serverSocket_ = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket_ < 0)
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;
std::memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(serverSocket_, (struct sockaddr*)&addr, sizeof(addr)))
{
close(serverSocket_);
throw std::runtime_error("Failed to bind");
}
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");
}
// Add all new commands to commands_ here
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;
std::cout << "Server port: " << port_
<< "\nServer Password: " << password_
<< std::endl;
}
Server::~Server()
{
for (std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); ++it)
close(it->first);
close(serverSocket_);
}
Server &Server::operator=(const Server& other)
{
if (this != &other)
{
port_ = other.port_;
serverSocket_ = other.serverSocket_;
password_ = other.password_;
sockets_ = other.sockets_;
}
return *this;
}
void Server::parseCommand(User& client)
{
std::istringstream args(client.getBuffer());
std::string command;
args >> 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())
{
std::string message = "Error: command not found!\n";
send(client.getFd(), message.c_str(), message.size(), 0);
client.clearBuffer();
return ;
}
(this->*(it->second))(client, args);
client.clearBuffer();
}
bool Server::handleClient(User& client)
{
const std::size_t kBufferSize = 1024;
char buffer[kBufferSize] = {0};
int recv_amount = recv(client.getFd(), buffer, kBufferSize, 0);
if (recv_amount == 0)
return true;
if (recv_amount < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
return false;
return true;
}
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)
parseCommand(client);
return false;
}
static struct pollfd newPollfd(int fd)
{
pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
pfd.revents = 0;
return pfd;
}
void Server::addClient()
{
int newClientSocket = accept(serverSocket_, NULL, NULL);
if (newClientSocket == -1)
{
// Not sure if program should terminate
std::cerr << "Problem occured accepting client" << std::endl;
return ;
}
fcntl(newClientSocket, F_SETFL, O_NONBLOCK);
sockets_.push_back(newPollfd(newClientSocket));
clients_[newClientSocket] = User(newClientSocket);
std::cout << "Client with fd: " << newClientSocket << " connected" << std::endl;
}
std::vector<struct pollfd>::iterator Server::removeClient(std::vector<struct pollfd>::iterator& client)
{
close(client->fd);
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
clients_.erase(client->fd);
return (sockets_.erase(client));
}
void Server::run()
{
// add serverSocket_ to sockets_
sockets_.push_back(newPollfd(serverSocket_));
while (true)
{
int ret = poll(sockets_.data(), sockets_.size(), -1);
if (ret <= 0)
{
// not sure if program should terminate
std::cerr << "Poll failed" << std::endl;
continue ;
}
for (std::vector<struct pollfd>::iterator it = sockets_.begin(); it != sockets_.end(); )
{
if (it->revents & (POLLERR | POLLHUP | POLLNVAL))
{
it = removeClient(it);
continue ;
}
if (!(it->revents & POLLIN))
{
it++;
continue ;
}
std::cout << "Checking client with fd: " << it->fd << std::endl;
// check if listening port is open, if so add client
if (it->fd == serverSocket_)
{
addClient();
it++;
break ;
}
// call handleClient for each other socket
std::map<int, User>::iterator clientIt = clients_.find(it->fd);
if (clientIt == clients_.end())
{
std::cerr << "Unknown fd in poll list: " << it->fd << std::endl;
close(it->fd);
it = sockets_.erase(it);
continue;
}
bool disconnected = handleClient(clientIt->second);
if (disconnected)
it = removeClient(it);
else
it++;
}
}
}

80
Server/Server.hpp Normal file
View File

@@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
/* Updated: 2026/05/16 11:45:11 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SERVER_HPP
# define SERVER_HPP
// C++ lib functions
# include <iostream>
# include <string>
# include <cstring>
# include <vector>
# include <map>
# include <cctype>
# include <sstream>
# include <unistd.h>
# include "../User/User.hpp"
# include "../Channel/Channel.hpp"
# define SERVER_NAME "irc.server"
# define PORT_DEFAULT 9898
class Server
{
private:
static const int kConnectionQueueLimit;
int port_;
int serverSocket_;
std::string password_;
std::vector<struct pollfd> sockets_;
std::map<int, User> clients_;
std::map<std::string, Channel> channels_;
std::map<std::string, void (Server::*)(User&, std::istringstream&)> commands_;
bool handleClient(User& client);
void addClient();
std::vector<struct pollfd>::iterator removeClient(std::vector<struct pollfd>::iterator& client);
void parseCommand(User& client);
Server(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);
public:
Server();
Server(int port, const std::string& password);
~Server();
void run();
};
#endif // SERVER_HPP

105
User/User.cpp Normal file
View File

@@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* User.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/07 10:22:52 by aortigos #+# #+# */
/* Updated: 2026/05/07 10:22:52 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "User.hpp"
//////////////////
// Constructors //
//////////////////
User::User() : fd(-1), nick(""), username(""), realname(""), buffer(""), authenticated(false), registered(false)
{
// std::cout << "User default constructor called" << std::endl;
}
User::User(int fd) : fd(fd), nick(""), username(""), realname(""), buffer(""), authenticated(false), registered(false)
{
// std::cout << "User with fd constructor called" << std::endl;
}
User::User(const User &other)
{
*this = other;
// std::cout << "User copy constructor called" << std::endl;
}
User& User::operator=(const User &other)
{
if (this != &other)
{
fd = other.fd;
nick = other.nick;
username = other.username;
realname = other.realname;
buffer = other.buffer;
authenticated = other.authenticated;
registered = other.registered;
channels_ = other.channels_;
}
// std::cout << "User copy assignment operator called" << std::endl;
return (*this);
}
User::~User()
{
// std::cout << "User destructor called" << std::endl;
}
void User::send(const std::string &msg)
{
::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
int User::getFd() const { return (this->fd); }
std::string User::getNick() const { return (this->nick); }
std::string User::getUsername() const { return (this->username); }
std::string User::getRealname() const { return (this->realname); }
std::string &User::getBuffer() { return (this->buffer); }
bool User::isAuthenticated() const { return (this->authenticated); }
bool User::isRegistered() const { return (this->registered); }
// Setters
void User::appendBuffer(std::string buff) { this->buffer.append(buff); }
void User::clearBuffer() { this->buffer.clear(); }
void User::setNick(std::string nick) { this->nick = nick; }
void User::setUsername(std::string username) { this->username = username; }
void User::setRealname(std::string realname) { this->realname = realname; }
void User::setAuthenticated(bool value) { this->authenticated = value; }
void User::setRegistered(bool value) { this->registered = value; }

72
User/User.hpp Normal file
View File

@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* User.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/07 10:22:52 by aortigos #+# #+# */
/* Updated: 2026/05/07 10:22:52 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef USER_HPP
# define USER_HPP
# include <iostream>
# include <sys/socket.h>
# include <set>
class User
{
private:
int fd;
std::string nick;
std::string username;
std::string realname;
std::string buffer;
bool authenticated;
bool registered;
std::set<std::string> channels_;
public:
User();
User(int fd);
User(const User &other);
User& operator=(const User &other);
~User();
void send(const std::string &msg);
// 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
int getFd() const;
std::string getNick() const;
std::string getUsername() const;
std::string getRealname() const;
std::string &getBuffer();
bool isAuthenticated() const;
bool isRegistered() const;
// Setters
void setNick(std::string nick);
void setUsername(std::string username);
void setRealname(std::string realname);
void appendBuffer(std::string buff);
void clearBuffer();
void setAuthenticated(bool value);
void setRegistered(bool value);
};
#endif

79
cmds/join.cpp Normal file
View File

@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* join.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/16 17:04:29 by aortigos ### ########.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);
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");
else
{
client.joinChannel(it->first);
it->second.addMember(client.getFd());
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");
}
}
}

68
cmds/nick.cpp Normal file
View 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");
}
}

27
cmds/pass.cpp Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pass.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
/* Updated: 2026/05/15 22:08:49 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::pass_cmd(User &client, std::istringstream &ss)
{
std::string args;
ss >> args;
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"));
if (this->password_ == args)
client.setAuthenticated(true);
else
client.send(":" SERVER_NAME " 464 * :Password incorrect\r\n");
}

41
cmds/privmsg.cpp Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* privmsg.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/15 15:35:16 by aortigos #+# #+# */
/* Updated: 2026/05/16 11:28:47 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
// Test function, need to change all the error messages
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"));
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()) return (client.send(":" SERVER_NAME " 412 " + client.getNick() + " :No text to send\r\n"));
if (message[0] == ' ')
message = message.substr(1);
std::string msg = ":" + client.getNick() + "!" + client.getUsername() + "@localhost PRIVMSG " + channel + " :" + message + "\r\n";
return (it->second.broadcast(msg, clients_, client.getFd()));
}

42
cmds/quit.cpp Normal file
View 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());
}

48
cmds/user.cpp Normal file
View 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");
}
}

50
main.cpp Normal file
View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/06 17:57:53 by iherman- #+# #+# */
/* Updated: 2026/05/10 19:02:29 by iherman- ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server/Server.hpp"
#include <sstream>
#include <cstdio>
int get_port(const char* arg)
{
std::stringstream input(arg);
int port;
if (!(input >> port))
throw std::runtime_error("Invalid port");
if (input.peek() != EOF)
throw std::runtime_error("Malformed port");
return port;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
std::cerr << "Usage: ./ircserv <port> <password>" << std::endl;
return 1;
}
try
{
int port = get_port(argv[1]);
Server server(port, argv[2]);
server.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
}

55
tasks.md Normal file
View File

@@ -0,0 +1,55 @@
# Tasks for IRC
### Useful info
You can try connect IRC server with
```bash
nc localhost <port>
```
---
### User
Represents a connected IRC client.
| Attribute | Type | Description |
|-----------------|----------|------------------------------------------------|
| `fd` | `int` | TCP s-ocket file descriptor |
| `nick` | `string` | IRC nickname |
| `username` | `string` | Username |
| `realname` | `string` | Real name |
| `buffer` | `string` | Incoming data buffer, accumulates until `\r\n` |
| `authenticated` | `bool` | `true` after correct `PASS` |
| `registered` | `bool` | `true` after `NICK` + `USER` received |
**Registration flow:** `PASS` → authenticated → `NICK` + `USER` → registered → client ready
---
### First stage
- [✓] Server starts
- [✓] It accepts two params, ./ircserv <port> <password>
- [✓] The server accepts one user, user can send messages and we can see from server
- [✓] Only 1 poll()
### Second stage
- [✓] Server can handle multiple clients simultaneously
- [✓] Manage SO_REUSEADDR (restarting server fails to bind same port)
### Third stage
- [✓] Dispatcher (select function for each command)
- [ ] Implement generic parser (extract command and pass args to command function)
- [ ] Client has nickname and username
- [ ] PASS command for authenticate
### Fourth stage
- [ ] Client can create/connect to channels
- [ ] JOIN command (user can create channel)
- [ ] PRIVMSG command (send message to a channel)
*It will continue...*