Skip to content

Commit

Permalink
Merge pull request #17 from lithiumox-codam/epoll
Browse files Browse the repository at this point in the history
Epoll and remove the unused bool return values for commands
  • Loading branch information
LithiumOx authored Nov 3, 2024
2 parents 3523a7f + 3b4ec68 commit fc595cf
Show file tree
Hide file tree
Showing 43 changed files with 1,283 additions and 589 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
.cache
compile_commands.json
ircserv
ircbot
.env

# Subject File
subject.pdf
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "bot/include/openai-cpp"]
path = bot/include/openai-cpp
url = https://github.com/olrea/openai-cpp.git
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LIB_DIR := lib
SRC_DIR := src
OBJ_DIR := build

BONUS_DIR := bot

# Compiler flags
CC := c++
CFLAGS := -Wall -Werror -Wextra -std=c++20 -I./$(HDR_DIR) -I $(LIB_DIR)
Expand Down Expand Up @@ -44,14 +46,20 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(HDR)
open: $(NAME)
@ ./$(NAME) 8080

bonus:
@ make run -C $(BONUS_DIR)


clean:
@ echo "$(RED)$(BOLD)Cleaning $(NICKNAME)...$(RESET)"
@ rm -rf $(OBJ_DIR)
@ make clean -C $(BONUS_DIR)

fclean:
@ echo "$(RED)$(BOLD)Fully cleaning $(NICKNAME)...$(RESET)"
@ rm -rf $(OBJ_DIR)
@ rm -rf ${NAME}
@ make fclean -C $(BONUS_DIR)

re: fclean ${NAME}

Expand All @@ -64,4 +72,4 @@ run: all
bear: fclean
@ bear -- make

.PHONY: all clean fclean re open
.PHONY: all clean fclean re open format run bear bonus
70 changes: 70 additions & 0 deletions bot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
NAME := ircbot
NICKNAME := BOT

# Directories
HDR_DIR := include
LIB_DIR := lib
SRC_DIR := src
OBJ_DIR := build

# Compiler flags
CC := c++
CFLAGS := -Wall -Werror -Wextra -std=c++20 -I./$(HDR_DIR) -I $(LIB_DIR)

ifdef DEBUG
CFLAGS += -g -fsanitize=address
endif

# Linker flags
LDFLAGS := -lcurl

# Includes
HDR := $(shell find $(HDR_DIR) -name '*.hpp')

# Files
SRC := $(shell find $(SRC_DIR) -name '*.cpp')
OBJ := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC))

# Colours
GREEN := \033[32;1m
YELLOW := \033[33;1m
RED := \033[31;1m
BOLD := \033[1m
RESET := \033[0m

# Rules
all: ${NAME}

$(NAME): $(OBJ)
@ printf "%b%s%b" "$(YELLOW)$(BOLD)" "Compiling $(NICKNAME)..." "$(RESET)"
@ $(CC) $(OBJ) $(LDFLAGS) -o $(NAME)
@ printf "\t\t%b%s%b\n" "$(GREEN)$(BOLD)" "[OK]" "$(RESET)"

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(HDR)
@ mkdir -p $(dir $@)
@ $(CC) $(CFLAGS) -c $< -o $@ -I $(HDR_DIR)

open: $(NAME)
@ ./$(NAME) 8080

clean:
@ echo "$(RED)$(BOLD)Cleaning $(NICKNAME)...$(RESET)"
@ rm -rf $(OBJ_DIR)

fclean:
@ echo "$(RED)$(BOLD)Fully cleaning $(NICKNAME)...$(RESET)"
@ rm -rf $(OBJ_DIR)
@ rm -rf ${NAME}

re: fclean ${NAME}

format:
@ clang-format -i $(SRC) $(HDR)

run: all
@ ./$(NAME) localhost 6667

bear: fclean
@ bear -- make

.PHONY: all clean fclean re open format run bear
29 changes: 29 additions & 0 deletions bot/include/Bot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <iostream>
#include <string>

using namespace std;

class Bot {
private:
string in_buffer;
string out_buffer;

public:
Bot();
Bot &operator=(const Bot &other);
Bot(const Bot &other);
~Bot();

// Server functions
void readFromServer(void);
void sendToServer(void);
void addToBuffer(const string &data);
void join(void);

// Bot functions
void parse(void);
} ;

string getGPTResponse(const string &nick, const string &message);
29 changes: 29 additions & 0 deletions bot/include/Epoll.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <inttypes.h>
#include <sys/epoll.h>
#include <array>

using namespace std;

#define MAXEVENTS 10

class EpollClass {
private:
int fd;

public:
EpollClass();
EpollClass &operator=(const EpollClass &other);
EpollClass(const EpollClass &other);
~EpollClass();

// Event list
array<struct epoll_event, MAXEVENTS> events;

// Functions
void add(int socket_fd) const;
void change(int socket_fd, uint32_t events);
void remove(int socket_fd) const;
void wait();
} ;
1 change: 1 addition & 0 deletions bot/include/openai-cpp
Submodule openai-cpp added at 9554a4
Loading

0 comments on commit fc595cf

Please sign in to comment.