Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ TEST_DIR = ./tests
INCLUDES = -I$(INC_DIR) \
-I$(INC_DIR)/core \
-I$(INC_DIR)/network \
-I$(INC_DIR)/commands \
-I$(INC_DIR)/commands
# -I$(INC_DIR)/bot

FLAGS = -Wall -Wextra -Werror -std=c++98 $(INCLUDES)

Expand Down Expand Up @@ -48,7 +49,8 @@ SRCS = $(SRCS_DIR)/main.cpp \
$(SRCS_DIR)/modes/OperatorMode.cpp \
$(SRCS_DIR)/modes/KeyMode.cpp \
$(SRCS_DIR)/modes/UserLimitMode.cpp \
$(SRCS_DIR)/protocol/IrcUtils.cpp
$(SRCS_DIR)/protocol/IrcUtils.cpp
# $(SRCS_DIR)/bot/BotClient.cpp

OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o)

Expand Down
62 changes: 62 additions & 0 deletions incs/bot/BotClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include "IClient.hpp"
#include "IServer.hpp"
#include "IChannel.hpp"

class IBot;
class BotMessageBuffer;
class BotClient : public IClient
{
private:
static int s_nextBotId; // Starts at -1, decrements
int m_id; // Negative FD
std::string m_nickname;
std::string m_username;
std::string m_realname;
std::string m_hostname;
IServer& m_server;
std::set<IChannel*> m_channels;
BotMessageBuffer m_buffer;
bool _passwordProvided;

std::time_t m_lastActivity;
std::time_t m_lastPingSent;

public:
BotClient(const std::string& nick, IServer& server);
~BotClient();

int getFd() const; // Returns negative m_id
const std::string& getNickname() const;
const std::string& getUsername() const;
const std::string& getRealname() const;
const std::string& getHostname() const; // Returns "internal"
IServer* getServer() const;
std::string getPrefix() const; // "nick!bot@internal"

bool isRegistered() const; // Always returns true
bool isPasswordProvided() const; // Always returns true

void setNickname(const std::string& nick);
void setUsername(const std::string& user);
void setRealname(const std::string& real);
void setPasswordProvided(bool provided);
void attemptRegistration();

void joinChannel(IChannel* channel);
void leaveChannel(IChannel* channel);
bool isInChannel(const std::string& channel) const;
const std::set< IChannel* >& getChannels() const;
// Buffer access

std::time_t getLastActivity() const;
void updateLastActivity();
std::time_t getLastPingSent() const;
void setLastPingSent(std::time_t last_ping);

IMessageBuffer& getBuffer();
const IMessageBuffer& getBuffer() const;
void setBot(IBot* bot); // Links to bot handler

};
2 changes: 1 addition & 1 deletion incs/core/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Client : public IClient
bool _passwordProvided;

MessageBuffer _buffer;
IServer* _server;
IServer& _server;
std::set< IChannel* > _channels;

public:
Expand Down
162 changes: 162 additions & 0 deletions srcs/bot/BotClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "bot/BotClient.hpp"
#include "IChannel.hpp"
#include "core/IServer.hpp"

int BotClient::s_nextBotId = -1;

BotClient::BotClient(const std::string& nick, IServer& server)
: m_id(s_nextBotId--), m_nickname(nick), m_username(""), m_realname(""),
m_hostname("internal"), m_server(server), _passwordProvided(true),
m_lastActivity(std::time(NULL)), m_lastPingSent(0)
{
}

BotClient::~BotClient()
{
}

IServer* BotClient::getServer() const
{
return &m_server;
}

int BotClient::getFd() const
{
return m_id;
}

const std::string& BotClient::getNickname() const
{
return (m_nickname);
}

const std::string& BotClient::getUsername() const
{
return (m_username);
}

const std::string& BotClient::getRealname() const
{
return (m_realname);
}

const std::string& BotClient::getHostname() const
{
return (m_hostname);
}

bool BotClient::isPasswordProvided() const
{
return true;
}

bool BotClient::isRegistered() const
{
return true;
}

void BotClient::setNickname(const std::string& nick)
{
m_nickname = nick;
attemptRegistration();
}

void BotClient::setUsername(const std::string& user)
{
m_username = user;
attemptRegistration();
}

void BotClient::setRealname(const std::string& real)
{
m_realname = real;
}

void BotClient::setPasswordProvided(bool provided)
{
_passwordProvided = provided;
attemptRegistration();
}

void BotClient::joinChannel(IChannel* channel)
{
m_channels.insert(channel);
}

void BotClient::leaveChannel(IChannel* channel)
{
m_channels.erase(channel);
}

bool BotClient::isInChannel(const std::string& channelName) const
{
for (std::set< IChannel* >::const_iterator it = m_channels.begin(); it != m_channels.end(); ++it)
{
if ((*it)->getName() == channelName)
return true;
}
return false;
}

const std::set< IChannel* >& BotClient::getChannels() const
{
return m_channels;
}

void BotClient::attemptRegistration()
{
if (m_nickname.empty())
return;
if (m_username.empty())
return;
if (m_server.requiresPassword() && !_passwordProvided)
return;
m_server.registerClient(m_nickname, this);
}

IMessageBuffer& BotClient::getBuffer()
{
return m_buffer;
}

const IMessageBuffer& BotClient::getBuffer() const
{
return m_buffer;
}

std::time_t BotClient::getLastActivity() const
{
return m_lastActivity;
}

void BotClient::updateLastActivity()
{
m_lastActivity = std::time(NULL);
m_lastPingSent = 0;
}

std::time_t BotClient::getLastPingSent() const
{
return m_lastPingSent;
}

void BotClient::setLastPingSent(std::time_t last_ping)
{
m_lastPingSent = last_ping;
}

std::string BotClient::getPrefix() const
{
if (m_nickname.empty())
return m_hostname;

std::string prefix = m_nickname;
if (!m_username.empty())
prefix += "!" + m_username + "@" + m_hostname;
return prefix;
}

void BotClient::setBot(IBot* bot)
{
(void) bot;
}
9 changes: 5 additions & 4 deletions srcs/core/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Client::Client(int fd, const std::string& hostname, IServer& server)
: _fd(fd), _nickname(""), _username(""), _realname(""), _hostname(hostname),
m_lastActivity(std::time(NULL)), m_lastPingSent(0), _state(HANDSHAKE),
_passwordProvided(false), _server(&server)
_passwordProvided(false), _server(server)
{
}

Expand All @@ -15,7 +15,7 @@ Client::~Client()

IServer* Client::getServer() const
{
return _server;
return &_server;
}

int Client::getFd() const
Expand Down Expand Up @@ -109,10 +109,10 @@ void Client::attemptRegistration()
return;
if (_username.empty())
return;
if (_server->requiresPassword() && !_passwordProvided)
if (_server.requiresPassword() && !_passwordProvided)
return;
_state = REGISTERED;
_server->registerClient(_nickname, this);
_server.registerClient(_nickname, this);
}

IMessageBuffer& Client::getBuffer()
Expand Down Expand Up @@ -156,3 +156,4 @@ std::string Client::getPrefix() const
prefix += "!" + _username + "@" + _hostname;
return prefix;
}