Skip to content

Commit 48c86d6

Browse files
authored
Merge pull request #46 from LaOuede/frank_invite
Frank invite règle l'issue #45
2 parents 88ac25f + 54cc10d commit 48c86d6

File tree

5 files changed

+117
-3
lines changed

5 files changed

+117
-3
lines changed

CommandHandler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void CommandHandler::initializeCommandCaller() {
4343
this->_command_caller.insert(pair<string, ACommand *>("PASS", new Pass));
4444
this->_command_caller.insert(pair<string, ACommand *>("JOIN", new Join));
4545
this->_command_caller.insert(pair<string, ACommand *>("KICK", new Kick));
46+
this->_command_caller.insert(pair<string, ACommand *>("INVITE", new Invite));
4647
}
4748

4849
void CommandHandler::commandTokenizer(Server *server) {

CommandHandler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "User.hpp"
2020
#include "Pass.hpp"
2121
#include "Kick.hpp"
22+
#include "Invite.hpp"
2223

2324
using std::cout;
2425
using std::endl;

Invite.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# include "Invite.hpp"
2+
# include "Server.hpp"
3+
# include "CommandHandler.hpp"
4+
5+
/* ************************************************************************** */
6+
/* Defines */
7+
/* ************************************************************************** */
8+
#define ERR_WELCOMED "462 PRIVMSG :You are not authenticated\r\n"
9+
#define ERR_NEEDMOREPARAMS(nickname) "461 PRVMSG " + nickname + " INVITE :Not enough parameters\r\n"
10+
#define ERR_NOSUCHCHANNEL(channel) "403 " + channel + " :No such channel\r\n"
11+
#define ERR_NOTONCHANNEL(nickname, channel) "442 " + nickname + " " + channel + " :You're not on that channel\r\n"
12+
#define ERR_CHANOPRIVSNEEDED(nickname, channel) "482 " + nickname + " " + channel + " :You're not channel operator\r\n"
13+
#define ERR_USERNOTEXIST(user) "401 " + user + " :No such user in the database\r\n"
14+
#define ERR_CANTINVITESELF "437 :You can't invite yourself\r\n"
15+
#define RPL_INVITING(nickname, nickname_invited, channel) ":" + nickname + " INVITE " + nickname_invited + " " + channel + "\r\n"
16+
/* ************************************************************************** */
17+
/* Constructors and Destructors */
18+
/* ************************************************************************** */
19+
Invite::Invite() : ACommand("INVITE") {}
20+
21+
Invite::~Invite() {}
22+
23+
24+
/* ************************************************************************** */
25+
/* Functions */
26+
/* ************************************************************************** */
27+
string Invite::executeCommand(Server *server) {
28+
int &fd = server->getFds()[server->getClientIndex()].fd;
29+
list<string> &tokens = server->getCommandHandler().getCommandTokens();
30+
string &nickname_invited = *tokens.begin();
31+
string &channel_token = *++tokens.begin();
32+
clientInfo &user_info = server->getUserDB()[fd];
33+
string &nickname = user_info._nickname;
34+
35+
string error = parseFirstPart(server, tokens, channel_token);
36+
if (!error.empty())
37+
return error;
38+
39+
Channel *channel = server->getChannel(channel_token);
40+
map<int, int> &user_list = channel->getUserList();
41+
if (user_list.find(fd) == user_list.end())
42+
return ERR_NOTONCHANNEL(nickname, channel_token);
43+
44+
if (user_list[fd] != OPERATOR) //TODO ajuster quand il y aura des channels private avec les MOD?
45+
return ERR_CHANOPRIVSNEEDED(nickname, channel_token);
46+
int fd_invited = findClientToInvite(server, nickname_invited);
47+
if (fd_invited == fd)
48+
return ERR_CANTINVITESELF;
49+
else if (fd_invited == 0)
50+
return ERR_USERNOTEXIST(nickname_invited);
51+
52+
channel->addUserToChannel(server, nickname_invited, fd_invited, 0);
53+
string message = RPL_INVITING(nickname, nickname_invited, channel_token);
54+
channel->broadcastToAll(message);
55+
return "";
56+
}
57+
58+
int Invite::findClientToInvite(Server *server, const string &nickname_invited) {
59+
map<int, clientInfo> &user_db = server->getUserDB();
60+
for (map<int, clientInfo>::const_iterator it = user_db.begin(); it != user_db.end(); ++it) {
61+
if (it->second._nickname == nickname_invited) {
62+
return it->first;
63+
}
64+
}
65+
return 0;
66+
}
67+
68+
string Invite::parseFirstPart(Server *server, const list<string> &tokens, const string &channel_token) {
69+
int &fd = server->getFds()[server->getClientIndex()].fd;
70+
clientInfo &user_info = server->getUserDB()[fd];
71+
string &nickname = user_info._nickname;
72+
73+
if (!user_info._welcomed)
74+
return ERR_WELCOMED;
75+
if (tokens.size() < 2)
76+
return ERR_NEEDMOREPARAMS(nickname);
77+
if (!server->getChannel(channel_token))
78+
return ERR_NOSUCHCHANNEL(channel_token);
79+
return "";
80+
}

Invite.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef INVITE_HPP
2+
#define INVITE_HPP
3+
4+
#pragma once
5+
6+
# include "ACommand.hpp"
7+
# include <iostream>
8+
# include <string>
9+
# include <list>
10+
11+
using std::cout;
12+
using std::endl;
13+
using std::string;
14+
using std::list;
15+
16+
class Server;
17+
18+
class Invite : public ACommand {
19+
public:
20+
// Construtors & Destructors
21+
Invite();
22+
virtual ~Invite();
23+
24+
// Methods
25+
string executeCommand(Server *server);
26+
int findClientToInvite(Server *server, const string &nickname_invited);
27+
string parseFirstPart(Server *server, const list<string> &tokens, const string &channel_token);
28+
29+
private:
30+
// Attributes
31+
string _name;
32+
};
33+
34+
#endif

Kick.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Kick::~Kick() {}
2828
/* Functions */
2929
/* ************************************************************************** */
3030
string Kick::executeCommand(Server *server) {
31-
string &hostname = server->getHostname();
3231
int &fd = server->getFds()[server->getClientIndex()].fd;
3332
list<string> &tokens = server->getCommandHandler().getCommandTokens();
3433
string &channel_token = *tokens.begin();
@@ -61,13 +60,12 @@ string Kick::executeCommand(Server *server) {
6160
channel->removeUserFromChannel(server, fd_kicked);
6261
string message = KICK(nickname, channel_token, user_kicked, comment);
6362
channel->broadcastToAll(message);
64-
return message;
63+
return "";
6564
}
6665

6766
string Kick::parseFirstPart(Server *server, const list<string> &tokens, const string &channel_token) {
6867
int &fd = server->getFds()[server->getClientIndex()].fd;
6968
clientInfo &user_info = server->getUserDB()[fd];
70-
clientInfo &user_info = server->getUserDB()[fd];
7169
string &nickname = user_info._nickname;
7270

7371
if (!user_info._welcomed)

0 commit comments

Comments
 (0)