Adding join channels and privmsg

This commit is contained in:
2026-04-17 19:12:05 +02:00
parent b3c190293a
commit 1c8ad9ba68
5 changed files with 64 additions and 14 deletions

View File

@@ -3,7 +3,7 @@ NAME = ircserv
SRC = main.cpp Server/Server.cpp Client/Client.cpp \ SRC = main.cpp Server/Server.cpp Client/Client.cpp \
Channel/Channel.cpp \ Channel/Channel.cpp \
commands/pass.cpp commands/nick.cpp commands/user.cpp \ commands/pass.cpp commands/nick.cpp commands/user.cpp \
commands/join.cpp commands/join.cpp commands/privmsg.cpp
OBJ = $(SRC:.cpp=.o) OBJ = $(SRC:.cpp=.o)

View File

@@ -156,4 +156,8 @@ void Server::parseCommand(int fd, std::string line)
cmd_nick(fd, ss); cmd_nick(fd, ss);
else if (command == "USER") else if (command == "USER")
cmd_user(fd, ss); cmd_user(fd, ss);
else if (command == "JOIN")
cmd_join(fd, ss);
else if (command == "PRIVMSG")
cmd_privmsg(fd, ss);
} }

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/17 16:26:18 by aortigos #+# #+# */ /* Created: 2026/04/17 16:26:18 by aortigos #+# #+# */
/* Updated: 2026/04/17 18:44:02 by aortigos ### ########.fr */ /* Updated: 2026/04/17 18:54:46 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -20,15 +20,26 @@ void Server::cmd_join(int fd, std::istringstream &ss)
if (!clients[fd].isRegistered()) if (!clients[fd].isRegistered())
{ {
std::string msg = "You are not registered"; std::string msg = "You are not registered\r\n";
send(fd, msg.c_str(), msg.size(), 0); send(fd, msg.c_str(), msg.size(), 0);
return ;
} }
if (channels.find(channelName) == channels.end()) if (channels.find(args) == channels.end())
{ {
channels.insert(std::make_pair(args, Channel(args)));
channels[args].addMember(fd);
std::string msg = "Channel created\r\n";
send(fd, msg.c_str(), msg.size(), 0);
} else { } else {
channels[args].addMember(fd);
std::string msg = "Channel already exists\r\n";
send(fd, msg.c_str(), msg.size(), 0);
return ;
} }
} }

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/17 11:56:55 by aortigos #+# #+# */ /* Created: 2026/04/17 11:56:55 by aortigos #+# #+# */
/* Updated: 2026/04/17 14:19:29 by aortigos ### ########.fr */ /* Updated: 2026/04/17 18:59:47 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -47,15 +47,16 @@ void Server::cmd_nick(int fd, std::istringstream &ss)
} }
} }
if (!clients[fd].getNick().empty())
{
std::string msg = ":" std::string msg = ":"
+ clients[fd].getNick() + clients[fd].getNick()
+ " NICK " + " NICK "
+ args + args
+ "\r\n"; + "\r\n";
send(fd, msg.c_str(), msg.size(), 0);
}
clients[fd].setNick(args); clients[fd].setNick(args);
send(fd, msg.c_str(), msg.size(), 0);
} }

View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* privmsg.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/17 19:02:55 by aortigos #+# #+# */
/* Updated: 2026/04/17 19:11:54 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Server/Server.hpp"
void Server::cmd_privmsg(int fd, std::istringstream &ss)
{
std::string channel;
std::string message;
ss >> channel;
std::getline(ss, message);
message += "\r\n"
std::vector<int> members = channels[channel].getMembers();
// iterador para recorrer los miembros e ir enviandoles el mensaje
for (size_t i = 0; i < members.size(); i++)
{
if (members[i] != fd)
send(members[i], message.c_str(), message.size(), 0);
}
}