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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ OBJ = $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o)

BONUS_SRCS := $(SRCS_DIR)/bot/BotMessageBuffer.cpp \
$(SRCS_DIR)/bot/BotClient.cpp \
$(SRCS_DIR)/bot/SixSevenBot.cpp
$(SRCS_DIR)/bot/SixSevenBot.cpp \
$(SRCS_DIR)/bot/NielBot.cpp


OBJS := $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/%.o)
OBJS_BONUS := $(SRCS:$(SRCS_DIR)/%.cpp=$(OBJ_DIR)/bonus/%.o) \
Expand Down
1 change: 1 addition & 0 deletions incs/bot/BotMessageBuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include <iostream>
#include <cstddef>
#include <vector>
Expand Down
23 changes: 15 additions & 8 deletions incs/bot/NielBot.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
#pragma one
#pragma once

#include "IChannel.hpp"
#include "IClient.hpp"
#include "IServer.hpp"
#include "bot/BotClient.hpp"
#include "bot/IBot.hpp"
#include "core/IServer.hpp"
#include <fstream>

class NielBot : public IBot
{
private:
IServer* m_server;
IClient* m_client;
bool contains42(const std::string& msg)const;
void sendReply(IChannel* channel);
IServer& m_server;
BotClient* m_client;

void sendToChannel(IChannel* channel, const std::string& msg);

public:
NielBot(IServer* server, IClient* botClient);
virtual ~NielBot();
NielBot(IServer& server, const std::string& nick = "NielBot");
~NielBot();

void onPrivateMessage(IClient* sender, const std::string& msg);
void onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg);

IClient* getClient();

void joinChannel(const std::string& channelName);
void loadAscii(const std::string& path);
};
73 changes: 73 additions & 0 deletions srcs/bot/NielBot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "bot/NielBot.hpp"
#include "CommandType.hpp"
#include "Logger.hpp"
#include "bot/BotClient.hpp"
#include "bot/BotMessageBuffer.hpp"
#include "protocol/Message.hpp"
#include "protocol/MessageParser.hpp"


NielBot::NielBot(IServer& server, const std::string& nick) : m_server(server)
{
m_client = new BotClient(nick, server);
m_client->setBot(this);
BotMessageBuffer *bmb = dynamic_cast<BotMessageBuffer *>(&m_client->getBuffer());
bmb->setBot(this);
}

NielBot::~NielBot()
{
delete m_client;
}

void NielBot::onChannelMessage(IClient* sender, IChannel* channel, const std::string& msg)
{
if (sender == m_client)
return;

bool has42 = msg.find("42") != std::string::npos;
if (has42)
{
std::ifstream file("ascii/xavier.txt");
std::string line;
while (std::getline(file, line))
sendToChannel(channel, line);
}
}

void NielBot::onPrivateMessage(IClient* sender, const std::string& msg)
{
if (sender == m_client)
return;
(void)msg;
}

void NielBot::joinChannel(const std::string& channelName)
{
IChannel* channel = m_server.getChannel(channelName);
if (!channel)
channel = m_server.createChannel(channelName, m_client);
if (!channel->hasMember(m_client))
{
channel->addMember(m_client);
m_client->joinChannel(channel);
}
}

void NielBot::sendToChannel(IChannel* channel, const std::string& message)
{
Message msg;
msg.m_prefix = m_client->getPrefix();
msg.m_command = "PRIVMSG";
msg.m_command_type = irc::PRIVMSG;
msg.m_params.push_back(channel->getName());
msg.m_params.push_back(message);

std::string serialized = MessageParser::serialize(msg);
channel->broadcast(serialized, m_client);
}

IClient* NielBot::getClient()
{
return (m_client);
}
5 changes: 5 additions & 0 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Logger.hpp"
#include "bot/SixSevenBot.hpp"
#include "bot/NielBot.hpp"
#include "core/Server.hpp"
#include <csignal>
#include <cstdlib>
Expand Down Expand Up @@ -32,6 +33,10 @@ int main(int argc, char** argv)
SixSevenBot *sixSevenBot = new SixSevenBot(srv);
srv.registerBot(sixSevenBot);
sixSevenBot->joinChannel("#eighty-nine");

NielBot *nielBot = new NielBot(srv);
srv.registerBot(nielBot);
nielBot->joinChannel("#42");
#endif
srv.run();
return (0);
Expand Down