Skip to content

Commit fcdfab2

Browse files
authored
Merge pull request jmtth#79 from jmkko/dev
Update welcome reply sequence and registration logic
2 parents 85e9cab + 083eac1 commit fcdfab2

15 files changed

Lines changed: 128 additions & 67 deletions

File tree

.clangd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ CompileFlags:
3737
- -Iincludes/clients
3838
- -Iincludes/commands
3939
- -Iincludes/server
40+
- -Itest/include
4041
InlayHints:
4142
Enabled: true

codes.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
000=CORRECT_FORMAT
88
001=RPL_WELCOME:Welcome to the IRC HazADoU& SerVerRrrr
99
002=RPL_YOURHOST:Your host is
10-
004=RPL_CREATED:This server was created
10+
003=RPL_CREATED:This server was created today
1111
004=RPL_MYINFO
1212
005=RPL_ISUPPORT
1313
#internal codes

includes/clients/Client.hpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010
class Server;
1111
class Channel;
1212

13-
//NOLINTNEXTLINE(performance-enum-size)
14-
enum ClientStatus {
15-
UNAUTHENTICATED, // before anything
16-
AUTHENTICATED, // after PASS
17-
REGISTERED, // after PASS + NICK + USER
18-
};
13+
// NOLINTNEXTLINE(performance-enum-size)
14+
// enum ClientStatus {
15+
// UNAUTHENTICATED, // before anything
16+
// AUTHENTICATED, // after PASS
17+
// REGISTERED, // after PASS + NICK + USER
18+
// };
19+
typedef unsigned short ClientStatus;
20+
21+
#define UNAUTHENTICATED 0b00000 // NOLINT
22+
#define AUTHENTICATED 0b00001 // NOLINT
23+
#define REGISTERED 0b00010 // NOLINT
1924

2025
class Client
2126
{
2227
public:
2328
Client(Socket socket, sockaddr_in addr);
2429
virtual ~Client();
2530

26-
2731
Socket get_socket() const;
2832
const std::string& get_address() const;
2933
unsigned short get_port() const;
@@ -36,32 +40,32 @@ class Client
3640
std::string get_nickname() const;
3741
std::string get_user_name() const;
3842
std::string get_real_name() const;
39-
std::string get_userhost() const;
40-
std::string get_full_userhost() const;
43+
std::string get_userhost() const;
44+
std::string get_full_userhost() const;
4145
ClientStatus get_status() const;
4246

4347
bool is_registered() const;
44-
bool is_authenticated () const;
48+
bool is_authenticated() const;
4549
int get_nb_joined_channels() const;
4650

4751
void set_nickname(const std::string& nick);
4852
void set_user_name(const std::string& userName);
4953
void set_real_name(const std::string& realName);
5054
void set_status(ClientStatus status);
5155
void add_joined_channel(Channel& channel);
52-
void remove_joined_channel(Channel& channel);
56+
void remove_joined_channel(Channel& channel);
5357
void remove_from_all_channels();
5458
void set_send_buffer(const std::string& buffer);
5559

5660
void append_to_send_buffer(const std::string& msg);
5761
void append_to_read_buffer(const std::string& msg);
5862

59-
void broadcast_to_all_channels(Server& server, ReplyCode code, const std::string& params, const std::string& trailing = "");
60-
Channel* get_channel(const std::string& name);
63+
void broadcast_to_all_channels(Server& server, ReplyCode code, const std::string& params, const std::string& trailing = "");
64+
Channel* get_channel(const std::string& name);
6165

6266
private:
6367
Client(const Client& other);
64-
Client& operator=(const Client& other);
68+
Client& operator=(const Client& other);
6569
TcpSocket _socket;
6670
sockaddr_in _addr;
6771
std::string _addrStr;

srcs/clients/Client.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include "Client.hpp"
2-
31
#include "Channel.hpp"
2+
#include "Client.hpp"
43
#include "Config.hpp"
54
#include "LogManager.hpp"
65
#include "consts.hpp"
@@ -76,9 +75,9 @@ std::string& Client::get_read_buffer() { return _readBuffer; }
7675

7776
bool Client::has_data_to_send() const { return _sendBuffer.empty(); }
7877

79-
bool Client::is_registered() const { return _status == REGISTERED; }
78+
bool Client::is_registered() const { return _status & REGISTERED; }
8079

81-
bool Client::is_authenticated () const { return _status == AUTHENTICATED; }
80+
bool Client::is_authenticated () const { return _status & AUTHENTICATED; }
8281

8382
int Client::get_nb_joined_channels() const { return static_cast<int>(_joinedChannels.size()); }
8483

@@ -88,7 +87,7 @@ void Client::set_user_name(const std::string& userName) { _userNam
8887

8988
void Client::set_real_name(const std::string& realName) { _realName = realName; }
9089

91-
void Client::set_status(ClientStatus status) { _status = status; }
90+
void Client::set_status(ClientStatus status) { _status = _status | status; }
9291

9392
void Client::add_joined_channel(Channel& channel) { _joinedChannels[channel.get_name()] = &channel; }
9493

srcs/commands/CmdFactory.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include "CmdFactory.hpp"
2-
31
#include "Client.hpp"
2+
#include "CmdFactory.hpp"
43
#include "ICommand.hpp"
54
#include "Invite.hpp"
65
#include "Join.hpp"
@@ -43,9 +42,10 @@ CmdFactory::~CmdFactory(void) {}
4342

4443
bool CmdFactory::check_in(Client& client, std::string& command)
4544
{
46-
if (!client.is_registered() && command != "NICK" && command != "USER" && command != "PASS" && command != "QUIT") {
45+
if (!client.is_authenticated() && command != "PASS" && command != "QUIT")
46+
return false;
47+
if (!client.is_registered() && command != "USER" && command != "NICK" && command != "PASS" && command != "QUIT")
4748
return false;
48-
}
4949
return true;
5050
}
5151

srcs/commands/Nick.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include "Nick.hpp"
2-
31
#include "Client.hpp"
42
#include "Config.hpp"
53
#include "LogManager.hpp"
4+
#include "Nick.hpp"
65
#include "ReplyHandler.hpp"
76
#include "Server.hpp"
87
#include "reply_codes.hpp"
@@ -19,16 +18,26 @@ void Nick::execute(Server& server, Client& client)
1918
(void)server;
2019
std::string oldNickname = client.get_nickname();
2120
ReplyHandler& rh = ReplyHandler::get_instance(&server);
21+
LOG_DV_CMD(_nickname);
22+
2223
// welcome sequence complete for first time
23-
if (oldNickname.empty() && client.get_user_name().empty() && client.is_authenticated()) {
24-
LOG_dt_CMD("first change");
25-
// rh.process_response(client, RPL_WELCOME);
24+
if (oldNickname.empty() && !client.get_user_name().empty()) {
25+
LOG_dt_CMD("Nick after USER");
26+
client.set_status(REGISTERED);
27+
rh.process_response(client,
28+
RPL_WELCOME,
29+
"",
30+
NULL,
31+
ircCodes.trailing(RPL_WELCOME) + " " + _nickname + "!" + client.get_user_name() + "@localhost");
32+
rh.process_response(client, RPL_YOURHOST, "", NULL, ircCodes.trailing(RPL_YOURHOST) + " " + server.get_name());
33+
rh.process_response(client, RPL_CREATED);
34+
rh.process_response(client, RPL_MYINFO, "", NULL, server.get_name() + " 1.0 0 0");
2635
// normal success behavior
2736
} else if (!oldNickname.empty() && !client.get_user_name().empty() && client.is_registered()) {
2837
// send the message to every user in every channel that this client takes part in
2938
// NOT WORKING (client._joinedChannel is empty)
3039
client.broadcast_to_all_channels(server, TRANSFER_NICK, _nickname); // ! \\ ;
31-
rh.process_response(client, TRANSFER_NICK, _nickname);
40+
// rh.process_response(client, TRANSFER_NICK, _nickname);
3241
}
3342
client.set_nickname(_nickname);
3443
}

srcs/commands/User.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#include "User.hpp"
2-
31
#include "Client.hpp"
2+
#include "Config.hpp"
43
#include "LogManager.hpp"
54
#include "ReplyHandler.hpp"
65
#include "Server.hpp"
6+
#include "User.hpp"
77
#include "reply_codes.hpp"
88

99
#include <iostream>
@@ -32,9 +32,17 @@ void User::execute(Server& server, Client& client)
3232
client.set_real_name(_realname.substr(1));
3333
LOG_DV_CMD(_realname);
3434
ReplyHandler& rh = ReplyHandler::get_instance(&server);
35-
if (!client.get_nickname().empty() && client.get_status() == AUTHENTICATED) {
35+
if (!client.get_nickname().empty()) {
3636
client.set_status(REGISTERED);
37-
rh.process_response(client, RPL_WELCOME);
37+
rh.process_response(
38+
client,
39+
RPL_WELCOME,
40+
"",
41+
NULL,
42+
ircCodes.trailing(RPL_WELCOME) + " " + client.get_nickname() + "!" + client.get_user_name() + "@localhost");
43+
rh.process_response(client, RPL_YOURHOST, "", NULL, ircCodes.trailing(RPL_YOURHOST) + " " + server.get_name());
44+
rh.process_response(client, RPL_CREATED);
45+
rh.process_response(client, RPL_MYINFO, "", NULL, server.get_name() + " 1.0 0 0");
3846
} else {
3947
LOG_CMD.info("202 RPL_USER");
4048
}

srcs/server/Config.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "Config.hpp"
2-
32
#include "LogManager.hpp"
43
#include "consts.hpp"
54
#include "reply_codes.hpp"
@@ -67,7 +66,7 @@ bool Config::_parse_code_file(const std::string& fileName)
6766
if (posColon != std::string::npos) {
6867
// Format: 001=RPL_WELCOME: Welcome message
6968
std::string value = line.substr(posAssign + 1, posColon - posAssign - 1);
70-
std::string trailing = line.substr(posColon);
69+
std::string trailing = line.substr(posColon + 1);
7170
_codes[code] = value;
7271
_trailings[code] = trailing;
7372
} else {

srcs/server/ReplyHandler.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include "ReplyHandler.hpp"
2-
31
#include "Client.hpp"
42
#include "Config.hpp"
53
#include "LogManager.hpp"
4+
#include "ReplyHandler.hpp"
65
#include "Server.hpp"
76
#include "reply_codes.hpp"
87
#include "utils.hpp"
@@ -92,11 +91,11 @@ generate_code_response(Client& client, ReplyCode code, const std::string& parame
9291
std::string nick = client.get_nickname().empty() ? "*" : client.get_nickname();
9392
std::string numericPrefix = ":" + ircConfig.get_name() + " " + utils::code_to_str(code) + " " + nick + " ";
9493
if (parameters.empty() && trailing.empty())
95-
return (numericPrefix + ircCodes.trailing(code));
94+
return (numericPrefix + ":" + ircCodes.trailing(code));
9695
else if (parameters.empty() && !trailing.empty())
9796
return (numericPrefix + ":" + trailing);
9897
else if (trailing.empty())
99-
return (numericPrefix + parameters + " " + ircCodes.trailing(code));
98+
return (numericPrefix + parameters + " :" + ircCodes.trailing(code));
10099
else
101100
return (numericPrefix + parameters + " :" + trailing);
102101
}

test/.clangd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ CompileFlags:
4040
- -I../includes/commands
4141
- -I../includes/server
4242
InlayHints:
43-
Enabled: true
43+
Enabled: true

0 commit comments

Comments
 (0)