diff options
51 files changed, 713 insertions, 4916 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index df634b5..808d9e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,35 +15,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE # ## Look for external libraries # -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") -include(FindEXPAT) -find_package(EXPAT REQUIRED) -find_package(ICONV REQUIRED) -find_package(LIBUUID REQUIRED) - -if(WITH_LIBIDN) - find_package(LIBIDN REQUIRED) -elseif(NOT WITHOUT_LIBIDN) - find_package(LIBIDN) -endif() - -if(WITH_SYSTEMD) - find_package(SYSTEMD REQUIRED) -elseif(NOT WITHOUT_SYSTEMD) - find_package(SYSTEMD) -endif() - -if(WITH_BOTAN) - find_package(BOTAN REQUIRED) -elseif(NOT WITHOUT_BOTAN) - find_package(BOTAN) -endif() - -if(WITH_CARES) - find_package(CARES REQUIRED) -elseif(NOT WITHOUT_CARES) - find_package(CARES) -endif() +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/") # ## Get the software version @@ -68,42 +40,12 @@ if(${PROJECT_NAME}_VERSION_SUFFIX MATCHES "^~dev$") endif() endif() -set(BIBOUMI_VERSION +set(SOFTWARE_VERSION ${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}${${PROJECT_NAME}_VERSION_SUFFIX}) # To be able to include the config.h file generated by cmake include_directories("${CMAKE_CURRENT_BINARY_DIR}/src/") include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/") -include_directories(${EXPAT_INCLUDE_DIRS}) -include_directories(${ICONV_INCLUDE_DIRS}) -include_directories(${LIBUUID_INCLUDE_DIRS}) - -if(LIBIDN_FOUND) - include_directories(${LIBIDN_INCLUDE_DIRS}) -endif() - -if(SYSTEMD_FOUND) - include_directories(${SYSTEMD_INCLUDE_DIRS}) -endif() - -if(BOTAN_FOUND) - include_directories(SYSTEM ${BOTAN_INCLUDE_DIRS}) -endif() - -if(CARES_FOUND) - include_directories(${CARES_INCLUDE_DIRS}) -endif() - -set(POLLER_DOCSTRING "Choose the poller between POLL and EPOLL (Linux-only)") -if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") - set(POLLER "EPOLL" CACHE STRING ${POLLER_DOCSTRING}) -else() - set(POLLER "POLL" CACHE STRING ${POLLER_DOCSTRING}) -endif() -if((NOT ${POLLER} MATCHES "POLL") AND - (NOT ${POLLER} MATCHES "EPOLL")) - message(FATAL_ERROR "POLLER must be either POLL or EPOLL") -endif() # ## Documentation @@ -121,43 +63,21 @@ if(RONN_EXECUTABLE) add_custom_target(doc DEPENDS ${MAN_PAGE}) endif() -# -## utils -# -file(GLOB source_utils - src/utils/*.[hc]pp) -add_library(utils STATIC ${source_utils}) -target_link_libraries(utils ${ICONV_LIBRARIES}) - -# -## config -# -file(GLOB source_config - src/config/*.[hc]pp) -add_library(config STATIC ${source_config}) -target_link_libraries(config utils) +if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/louloulibs") + message(FATAL_ERROR "louloulibs/ directory not found. Make sure you fetched the submodules with 'git submodule init && git submodule update'") +endif() +add_subdirectory("louloulibs") +include_directories("louloulibs") -# -## logger -# -file(GLOB source_logger - src/logger/*.[hc]pp) -add_library(logger STATIC ${source_logger}) -target_link_libraries(logger config) +include_directories(${EXPAT_INCLUDE_DIRS}) +include_directories(${ICONV_INCLUDE_DIRS}) +include_directories(${LIBUUID_INCLUDE_DIRS}) -# -## network -# -file(GLOB source_network - src/network/*.[hc]pp) -add_library(network STATIC ${source_network}) -target_link_libraries(network logger) -if(BOTAN_FOUND) - target_link_libraries(network ${BOTAN_LIBRARIES}) -endif() -if(CARES_FOUND) - target_link_libraries(network ${CARES_LIBRARIES}) -endif() +# If they are found in louloulibs CMakeLists.txt, we inherite these values +include_directories(${LIBIDN_INCLUDE_DIRS}) +include_directories(${SYSTEMD_INCLUDE_DIRS}) +include_directories(SYSTEM ${BOTAN_INCLUDE_DIRS}) +include_directories(${CARES_INCLUDE_DIRS}) # ## irclib @@ -168,17 +88,12 @@ add_library(irc STATIC ${source_irc}) target_link_libraries(irc network utils logger) # -## xmpplib +## xmpp # file(GLOB source_xmpp src/xmpp/*.[hc]pp) add_library(xmpp STATIC ${source_xmpp}) -target_link_libraries(xmpp bridge network utils logger - ${EXPAT_LIBRARIES} - ${LIBUUID_LIBRARIES}) -if(LIBIDN_FOUND) - target_link_libraries(xmpp ${LIBIDN_LIBRARIES}) -endif() +target_link_libraries(xmpp xmpplib bridge network utils logger) # ## bridge @@ -209,6 +124,7 @@ endif() add_executable(test_suite EXCLUDE_FROM_ALL src/test.cpp) target_link_libraries(test_suite + xmpplib xmpp irc bridge @@ -216,9 +132,6 @@ target_link_libraries(test_suite config logger) - -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/src/config.h) - # ## Install target # diff --git a/cmake/Modules/FindBOTAN.cmake b/cmake/Modules/FindBOTAN.cmake deleted file mode 100644 index a12bd35..0000000 --- a/cmake/Modules/FindBOTAN.cmake +++ /dev/null @@ -1,35 +0,0 @@ -# - Find botan -# Find the botan cryptographic library -# -# This module defines the following variables: -# BOTAN_FOUND - True if library and include directory are found -# If set to TRUE, the following are also defined: -# BOTAN_INCLUDE_DIRS - The directory where to find the header file -# BOTAN_LIBRARIES - Where to find the library file -# -# For conveniance, these variables are also set. They have the same values -# than the variables above. The user can thus choose his/her prefered way -# to write them. -# BOTAN_LIBRARY -# BOTAN_INCLUDE_DIR -# -# This file is in the public domain - -find_path(BOTAN_INCLUDE_DIRS NAMES botan/botan.h - PATH_SUFFIXES botan-1.11 - DOC "The botan include directory") - -find_library(BOTAN_LIBRARIES NAMES botan botan-1.11 - DOC "The botan library") - -# Use some standard module to handle the QUIETLY and REQUIRED arguments, and -# set BOTAN_FOUND to TRUE if these two variables are set. -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(BOTAN REQUIRED_VARS BOTAN_LIBRARIES BOTAN_INCLUDE_DIRS) - -if(BOTAN_FOUND) - set(BOTAN_LIBRARY ${BOTAN_LIBRARIES}) - set(BOTAN_INCLUDE_DIR ${BOTAN_INCLUDE_DIRS}) -endif() - -mark_as_advanced(BOTAN_INCLUDE_DIRS BOTAN_LIBRARIES) diff --git a/cmake/Modules/FindCARES.cmake b/cmake/Modules/FindCARES.cmake deleted file mode 100644 index c4c757a..0000000 --- a/cmake/Modules/FindCARES.cmake +++ /dev/null @@ -1,37 +0,0 @@ -# - Find c-ares -# Find the c-ares library, and more particularly the stringprep header. -# -# This module defines the following variables: -# CARES_FOUND - True if library and include directory are found -# If set to TRUE, the following are also defined: -# CARES_INCLUDE_DIRS - The directory where to find the header file -# CARES_LIBRARIES - Where to find the library file -# -# For conveniance, these variables are also set. They have the same values -# than the variables above. The user can thus choose his/her prefered way -# to write them. -# CARES_INCLUDE_DIR -# CARES_LIBRARY -# -# This file is in the public domain - -if(NOT CARES_FOUND) - find_path(CARES_INCLUDE_DIRS NAMES ares.h - DOC "The c-ares include directory") - - find_library(CARES_LIBRARIES NAMES cares - DOC "The c-ares library") - - # Use some standard module to handle the QUIETLY and REQUIRED arguments, and - # set CARES_FOUND to TRUE if these two variables are set. - include(FindPackageHandleStandardArgs) - find_package_handle_standard_args(CARES REQUIRED_VARS CARES_LIBRARIES CARES_INCLUDE_DIRS) - - # Compatibility for all the ways of writing these variables - if(CARES_FOUND) - set(CARES_INCLUDE_DIR ${CARES_INCLUDE_DIRS}) - set(CARES_LIBRARY ${CARES_LIBRARIES}) - endif() -endif() - -mark_as_advanced(CARES_INCLUDE_DIRS CARES_LIBRARIES) diff --git a/cmake/Modules/FindICONV.cmake b/cmake/Modules/FindICONV.cmake deleted file mode 100644 index 7ca173f..0000000 --- a/cmake/Modules/FindICONV.cmake +++ /dev/null @@ -1,60 +0,0 @@ -# - Find iconv -# Find the iconv (character set conversion) library -# -# This module defines the following variables: -# ICONV_FOUND - True if library and include directory are found -# If set to TRUE, the following are also defined: -# ICONV_INCLUDE_DIRS - The directory where to find the header file -# ICONV_LIBRARIES - Where to find the library file -# ICONV_SECOND_ARGUMENT_IS_CONST - The second argument for iconv() is const -# -# For conveniance, these variables are also set. They have the same values -# than the variables above. The user can thus choose his/her prefered way -# to write them. -# ICONV_LIBRARY -# ICONV_INCLUDE_DIR -# -# This file is in the public domain - -find_path(ICONV_INCLUDE_DIRS NAMES iconv.h - DOC "The iconv include directory") - -find_library(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c - DOC "The iconv library") - -# Use some standard module to handle the QUIETLY and REQUIRED arguments, and -# set ICONV_FOUND to TRUE if these two variables are set. -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(Iconv REQUIRED_VARS ICONV_LIBRARIES ICONV_INCLUDE_DIRS) - -# Check if the prototype is -# size_t iconv(iconv_t cd, char** inbuf, size_t* inbytesleft, -# char** outbuf, size_t* outbytesleft); -# or -# size_t iconv (iconv_t cd, const char** inbuf, size_t* inbytesleft, -# char** outbuf, size_t* outbytesleft); -if(ICONV_FOUND) - include(CheckCXXSourceCompiles) - - # Set the parameters needed to compile the following code. - set(CMAKE_REQUIRED_INCLUDES ${ICONV_INCLUDE_DIRS}) - set(CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARIES}) - - check_cxx_source_compiles(" - #include <iconv.h> - int main(){ - iconv_t conv = 0; - const char* in = 0; - size_t ilen = 0; - char* out = 0; - size_t olen = 0; - iconv(conv, &in, &ilen, &out, &olen); - return 0;}" - ICONV_SECOND_ARGUMENT_IS_CONST) - -# Compatibility for all the ways of writing these variables - set(ICONV_LIBRARY ${ICONV_LIBRARIES}) - set(ICONV_INCLUDE_DIR ${ICONV_INCLUDE_DIRS}) -endif() - -mark_as_advanced(ICONV_INCLUDE_DIRS ICONV_LIBRARIES ICONV_SECOND_ARGUMENT_IS_CONST)
\ No newline at end of file diff --git a/cmake/Modules/FindLIBIDN.cmake b/cmake/Modules/FindLIBIDN.cmake deleted file mode 100644 index 611a6a8..0000000 --- a/cmake/Modules/FindLIBIDN.cmake +++ /dev/null @@ -1,41 +0,0 @@ -# - Find libidn -# Find the libidn library, and more particularly the stringprep header. -# -# This module defines the following variables: -# LIBIDN_FOUND - True if library and include directory are found -# If set to TRUE, the following are also defined: -# LIBIDN_INCLUDE_DIRS - The directory where to find the header file -# LIBIDN_LIBRARIES - Where to find the library file -# -# For conveniance, these variables are also set. They have the same values -# than the variables above. The user can thus choose his/her prefered way -# to write them. -# LIBIDN_INCLUDE_DIR -# LIBIDN_LIBRARY -# -# This file is in the public domain - -include(FindPkgConfig) -pkg_check_modules(LIBIDN libidn) - -if(NOT LIBIDN_FOUND) - find_path(LIBIDN_INCLUDE_DIRS NAMES stringprep.h - DOC "The libidn include directory") - - # The library containing the stringprep module is libidn - find_library(LIBIDN_LIBRARIES NAMES idn - DOC "The libidn library") - - # Use some standard module to handle the QUIETLY and REQUIRED arguments, and - # set LIBIDN_FOUND to TRUE if these two variables are set. - include(FindPackageHandleStandardArgs) - find_package_handle_standard_args(LIBIDN REQUIRED_VARS LIBIDN_LIBRARIES LIBIDN_INCLUDE_DIRS) - - # Compatibility for all the ways of writing these variables - if(LIBIDN_FOUND) - set(LIBIDN_INCLUDE_DIR ${LIBIDN_INCLUDE_DIRS}) - set(LIBIDN_LIBRARY ${LIBIDN_LIBRARIES}) - endif() -endif() - -mark_as_advanced(LIBIDN_INCLUDE_DIRS LIBIDN_LIBRARIES)
\ No newline at end of file diff --git a/cmake/Modules/FindLIBUUID.cmake b/cmake/Modules/FindLIBUUID.cmake deleted file mode 100644 index 17d3c42..0000000 --- a/cmake/Modules/FindLIBUUID.cmake +++ /dev/null @@ -1,41 +0,0 @@ -# - Find libuuid -# Find the libuuid library -# -# This module defines the following variables: -# LIBUUID_FOUND - True if library and include directory are found -# If set to TRUE, the following are also defined: -# LIBUUID_INCLUDE_DIRS - The directory where to find the header file -# LIBUUID_LIBRARIES - Where to find the library file -# -# For conveniance, these variables are also set. They have the same values -# than the variables above. The user can thus choose his/her prefered way -# to write them. -# LIBUUID_INCLUDE_DIR -# LIBUUID_LIBRARY -# -# This file is in the public domain - -include(FindPkgConfig) -pkg_check_modules(LIBUUID uuid) - -if(NOT LIBUUID_FOUND) - find_path(LIBUUID_INCLUDE_DIRS NAMES uuid.h - PATH_SUFFIXES uuid - DOC "The libuuid include directory") - - find_library(LIBUUID_LIBRARIES NAMES uuid - DOC "The libuuid library") - - # Use some standard module to handle the QUIETLY and REQUIRED arguments, and - # set LIBUUID_FOUND to TRUE if these two variables are set. - include(FindPackageHandleStandardArgs) - find_package_handle_standard_args(LIBUUID REQUIRED_VARS LIBUUID_LIBRARIES LIBUUID_INCLUDE_DIRS) - - # Compatibility for all the ways of writing these variables - if(LIBUUID_FOUND) - set(LIBUUID_INCLUDE_DIR ${LIBUUID_INCLUDE_DIRS}) - set(LIBUUID_LIBRARY ${LIBUUID_LIBRARIES}) - endif() -endif() - -mark_as_advanced(LIBUUID_INCLUDE_DIRS LIBUUID_LIBRARIES) diff --git a/cmake/Modules/FindSYSTEMD.cmake b/cmake/Modules/FindSYSTEMD.cmake deleted file mode 100644 index c7decde..0000000 --- a/cmake/Modules/FindSYSTEMD.cmake +++ /dev/null @@ -1,39 +0,0 @@ -# - Find SystemdDaemon -# Find the systemd daemon library -# -# This module defines the following variables: -# SYSTEMD_FOUND - True if library and include directory are found -# If set to TRUE, the following are also defined: -# SYSTEMD_INCLUDE_DIRS - The directory where to find the header file -# SYSTEMD_LIBRARIES - Where to find the library file -# -# For conveniance, these variables are also set. They have the same values -# than the variables above. The user can thus choose his/her prefered way -# to write them. -# SYSTEMD_LIBRARY -# SYSTEMD_INCLUDE_DIR -# -# This file is in the public domain - -include(FindPkgConfig) -pkg_check_modules(SYSTEMD libsystemd) - -if(NOT SYSTEMD_FOUND) - find_path(SYSTEMD_INCLUDE_DIRS NAMES systemd/sd-daemon.h - DOC "The Systemd include directory") - - find_library(SYSTEMD_LIBRARIES NAMES systemd - DOC "The Systemd library") - - # Use some standard module to handle the QUIETLY and REQUIRED arguments, and - # set SYSTEMD_FOUND to TRUE if these two variables are set. - include(FindPackageHandleStandardArgs) - find_package_handle_standard_args(SYSTEMD REQUIRED_VARS SYSTEMD_LIBRARIES SYSTEMD_INCLUDE_DIRS) - - if(SYSTEMD_FOUND) - set(SYSTEMD_LIBRARY ${SYSTEMD_LIBRARIES}) - set(SYSTEMD_INCLUDE_DIR ${SYSTEMD_INCLUDE_DIRS}) - endif() -endif() - -mark_as_advanced(SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES)
\ No newline at end of file diff --git a/src/bridge/bridge.cpp b/src/bridge/bridge.cpp index 1a205bd..85049b9 100644 --- a/src/bridge/bridge.cpp +++ b/src/bridge/bridge.cpp @@ -1,7 +1,7 @@ #include <bridge/bridge.hpp> #include <bridge/colors.hpp> #include <bridge/list_element.hpp> -#include <xmpp/xmpp_component.hpp> +#include <xmpp/biboumi_component.hpp> #include <xmpp/xmpp_stanza.hpp> #include <irc/irc_message.hpp> #include <network/poller.hpp> @@ -20,7 +20,7 @@ using namespace std::string_literals; static const char* action_prefix = "\01ACTION "; -Bridge::Bridge(const std::string& user_jid, XmppComponent* xmpp, std::shared_ptr<Poller> poller): +Bridge::Bridge(const std::string& user_jid, BiboumiComponent* xmpp, std::shared_ptr<Poller> poller): user_jid(user_jid), xmpp(xmpp), poller(poller) diff --git a/src/bridge/bridge.hpp b/src/bridge/bridge.hpp index 8f71846..c50b7ab 100644 --- a/src/bridge/bridge.hpp +++ b/src/bridge/bridge.hpp @@ -12,7 +12,7 @@ #include <string> #include <memory> -class XmppComponent; +class BiboumiComponent; class Poller; /** @@ -32,7 +32,7 @@ using irc_responder_callback_t = std::function<bool(const std::string& irc_hostn class Bridge { public: - explicit Bridge(const std::string& user_jid, XmppComponent* xmpp, std::shared_ptr<Poller> poller); + explicit Bridge(const std::string& user_jid, BiboumiComponent* xmpp, std::shared_ptr<Poller> poller); ~Bridge(); /** * QUIT all connected IRC servers. @@ -211,7 +211,7 @@ private: * but we still need to communicate with it, when sending messages from * IRC to XMPP. */ - XmppComponent* xmpp; + BiboumiComponent* xmpp; /** * Poller, to give it the IrcClients that we spawn, to make it manage * their sockets. diff --git a/src/config.h.cmake b/src/config.h.cmake deleted file mode 100644 index 18d546f..0000000 --- a/src/config.h.cmake +++ /dev/null @@ -1,8 +0,0 @@ -#define SYSTEM_NAME "${CMAKE_SYSTEM}" -#cmakedefine ICONV_SECOND_ARGUMENT_IS_CONST -#cmakedefine LIBIDN_FOUND -#cmakedefine SYSTEMD_FOUND -#cmakedefine POLLER ${POLLER} -#cmakedefine BOTAN_FOUND -#cmakedefine CARES_FOUND -#cmakedefine BIBOUMI_VERSION "${BIBOUMI_VERSION}" diff --git a/src/config/config.cpp b/src/config/config.cpp deleted file mode 100644 index b870339..0000000 --- a/src/config/config.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include <config/config.hpp> - -#include <iostream> -#include <sstream> - -#include <stdlib.h> - -std::string Config::filename = "./biboumi.cfg"; -bool Config::file_must_exist = false; - -std::string Config::get(const std::string& option, const std::string& def) -{ - Config* self = Config::instance().get(); - auto it = self->values.find(option); - - if (it == self->values.end()) - return def; - return it->second; -} - -int Config::get_int(const std::string& option, const int& def) -{ - Config* self = Config::instance().get(); - std::string res = self->get(option, ""); - if (!res.empty()) - return atoi(res.c_str()); - else - return def; -} - -void Config::set(const std::string& option, const std::string& value, bool save) -{ - Config* self = Config::instance().get(); - self->values[option] = value; - if (save) - { - self->save_to_file(); - self->trigger_configuration_change(); - } -} - -void Config::connect(t_config_changed_callback callback) -{ - Config* self = Config::instance().get(); - self->callbacks.push_back(callback); -} - -void Config::close() -{ - Config* self = Config::instance().get(); - self->values.clear(); - Config::instance().reset(); -} - -/** - * Private methods - */ - -void Config::trigger_configuration_change() -{ - std::vector<t_config_changed_callback>::iterator it; - for (it = this->callbacks.begin(); it < this->callbacks.end(); ++it) - (*it)(); -} - -std::unique_ptr<Config>& Config::instance() -{ - static std::unique_ptr<Config> instance; - - if (!instance) - { - instance = std::make_unique<Config>(); - instance->read_conf(); - } - return instance; -} - -bool Config::read_conf() -{ - std::ifstream file; - file.open(filename.data()); - if (!file.is_open()) - { - if (Config::file_must_exist) - { - perror(("Error while opening file " + filename + " for reading.").c_str()); - file.exceptions(std::ifstream::failbit); - } - return false; - } - - std::string line; - size_t pos; - std::string option; - std::string value; - while (file.good()) - { - std::getline(file, line); - if (line == "" || line[0] == '#') - continue ; - pos = line.find('='); - if (pos == std::string::npos) - continue ; - option = line.substr(0, pos); - value = line.substr(pos+1); - this->values[option] = value; - } - return true; -} - -void Config::save_to_file() const -{ - std::ofstream file(this->filename.data()); - if (file.fail()) - { - std::cerr << "Could not save config file." << std::endl; - return ; - } - for (auto& it: this->values) - file << it.first << "=" << it.second << std::endl; - file.close(); -} diff --git a/src/config/config.hpp b/src/config/config.hpp deleted file mode 100644 index e070816..0000000 --- a/src/config/config.hpp +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Read the config file and save all the values in a map. - * Also, a singleton. - * - * Use Config::filename = "bla" to set the filename you want to use. - * - * If you want to exit if the file does not exist when it is open for - * reading, set Config::file_must_exist = true. - * - * Config::get() can the be used to access the values in the conf. - * - * Use Config::close() when you're done getting/setting value. This will - * save the config into the file. - */ - -#ifndef CONFIG_INCLUDED -# define CONFIG_INCLUDED - -#include <functional> -#include <fstream> -#include <memory> -#include <vector> -#include <string> -#include <map> - -typedef std::function<void()> t_config_changed_callback; - -class Config -{ -public: - Config(){}; - ~Config(){}; - /** - * returns a value from the config. If it doesn’t exist, use - * the second argument as the default. - * @param option The option we want - * @param def The default value in case the option does not exist - */ - static std::string get(const std::string&, const std::string&); - /** - * returns a value from the config. If it doesn’t exist, use - * the second argument as the default. - * @param option The option we want - * @param def The default value in case the option does not exist - */ - static int get_int(const std::string&, const int&); - /** - * Set a value for the given option. And write all the config - * in the file from which it was read if boolean is set. - * @param option The option to set - * @param value The value to use - * @param save if true, save the config file - */ - static void set(const std::string&, const std::string&, bool save = false); - /** - * Adds a function to a list. This function will be called whenever a - * configuration change occurs. - */ - static void connect(t_config_changed_callback); - /** - * Close the config file, saving it to the file is save == true. - */ - static void close(); - - /** - * Set the value of the filename to use, before calling any method. - */ - static std::string filename; - /** - * Set to true if you want an exception to be raised if the file does not - * exist when reading it. - */ - static bool file_must_exist; - -private: - /** - * Get the singleton instance - */ - static std::unique_ptr<Config>& instance(); - /** - * Read the configuration file at the given path. - */ - bool read_conf(); - /** - * Write all the config values into the configuration file - */ - void save_to_file() const; - /** - * Call all the callbacks previously registered using connect(). - * This is used to notify any class that a configuration change occured. - */ - void trigger_configuration_change(); - - std::map<std::string, std::string> values; - std::vector<t_config_changed_callback> callbacks; - - Config(const Config&) = delete; - Config& operator=(const Config&) = delete; - Config(Config&&) = delete; - Config& operator=(Config&&) = delete; -}; - -#endif // CONFIG_INCLUDED diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index dedb5de..cbbd4ea 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -14,7 +14,7 @@ #include <chrono> #include <string> -#include "config.h" +#include "louloulibs.h" using namespace std::string_literals; using namespace std::chrono_literals; diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp deleted file mode 100644 index 7336579..0000000 --- a/src/logger/logger.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include <logger/logger.hpp> -#include <config/config.hpp> - -Logger::Logger(const int log_level): - log_level(log_level), - stream(std::cout.rdbuf()) -{ -} - -Logger::Logger(const int log_level, const std::string& log_file): - log_level(log_level), - ofstream(log_file.data(), std::ios_base::app), - stream(ofstream.rdbuf()) -{ -} - -std::unique_ptr<Logger>& Logger::instance() -{ - static std::unique_ptr<Logger> instance; - - if (!instance) - { - const std::string log_file = Config::get("log_file", ""); - const int log_level = Config::get_int("log_level", 0); - if (log_file.empty()) - instance = std::make_unique<Logger>(log_level); - else - instance = std::make_unique<Logger>(log_level, log_file); - } - return instance; -} - -std::ostream& Logger::get_stream(const int lvl) -{ - if (lvl >= this->log_level) - return this->stream; - return this->null_stream; -} diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp deleted file mode 100644 index b1ae20d..0000000 --- a/src/logger/logger.hpp +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef LOGGER_INCLUDED -# define LOGGER_INCLUDED - -/** - * Singleton used in logger macros to write into files or stdout, with - * various levels of severity. - * Only the macros should be used. - * @class Logger - */ - -#include <memory> -#include <iostream> -#include <fstream> - -#define debug_lvl 0 -#define info_lvl 1 -#define warning_lvl 2 -#define error_lvl 3 - -#include "config.h" - -#ifdef SYSTEMD_FOUND -# include <systemd/sd-daemon.h> -#else -# define SD_DEBUG "[DEBUG]: " -# define SD_INFO "[INFO]: " -# define SD_WARNING "[WARNING]: " -# define SD_ERR "[ERROR]: " -#endif - -// Macro defined to get the filename instead of the full path. But if it is -// not properly defined by the build system, we fallback to __FILE__ -#ifndef __FILENAME__ -# define __FILENAME__ __FILE__ -#endif - -#define WHERE\ - __FILENAME__ << ":" << __LINE__ - -#define log_debug(text)\ - Logger::instance()->get_stream(debug_lvl) << SD_DEBUG << WHERE << ":\t" << text << std::endl; - -#define log_info(text)\ - Logger::instance()->get_stream(info_lvl) << SD_INFO << WHERE << ":\t" << text << std::endl; - -#define log_warning(text)\ - Logger::instance()->get_stream(warning_lvl) << SD_WARNING << WHERE << ":\t" << text << std::endl; - -#define log_error(text)\ - Logger::instance()->get_stream(error_lvl) << SD_ERR << WHERE << ":\t" << text << std::endl; - -/** - * Juste a structure representing a stream doing nothing with its input. - */ -class nullstream: public std::ostream -{ -public: - nullstream(): - std::ostream(0) - { } -}; - -class Logger -{ -public: - static std::unique_ptr<Logger>& instance(); - std::ostream& get_stream(const int); - Logger(const int log_level, const std::string& log_file); - Logger(const int log_level); - -private: - Logger(const Logger&); - Logger& operator=(const Logger&); - - const int log_level; - std::ofstream ofstream; - nullstream null_stream; - std::ostream stream; -}; - -#endif // LOGGER_INCLUDED diff --git a/src/main.cpp b/src/main.cpp index 148412e..9042a57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include <xmpp/xmpp_component.hpp> +#include <xmpp/biboumi_component.hpp> #include <utils/timed_events.hpp> #include <network/poller.hpp> #include <config/config.hpp> @@ -72,7 +72,7 @@ int main(int ac, char** av) return config_help("hostname"); auto p = std::make_shared<Poller>(); - auto xmpp_component = std::make_shared<XmppComponent>(p, + auto xmpp_component = std::make_shared<BiboumiComponent>(p, hostname, password); diff --git a/src/network/dns_handler.cpp b/src/network/dns_handler.cpp deleted file mode 100644 index 45bf626..0000000 --- a/src/network/dns_handler.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#include <config.h> -#ifdef CARES_FOUND - -#include <network/dns_socket_handler.hpp> -#include <network/tcp_socket_handler.hpp> -#include <network/dns_handler.hpp> -#include <network/poller.hpp> - -#include <algorithm> -#include <stdexcept> - -DNSHandler DNSHandler::instance; - -using namespace std::string_literals; - -void on_hostname4_resolved(void* arg, int status, int, struct hostent* hostent) -{ - TCPSocketHandler* socket_handler = static_cast<TCPSocketHandler*>(arg); - socket_handler->on_hostname4_resolved(status, hostent); -} - -void on_hostname6_resolved(void* arg, int status, int, struct hostent* hostent) -{ - TCPSocketHandler* socket_handler = static_cast<TCPSocketHandler*>(arg); - socket_handler->on_hostname6_resolved(status, hostent); -} - -DNSHandler::DNSHandler() -{ - int ares_error; - if ((ares_error = ::ares_library_init(ARES_LIB_INIT_ALL)) != 0) - throw std::runtime_error("Failed to initialize c-ares lib: "s + ares_strerror(ares_error)); - if ((ares_error = ::ares_init(&this->channel)) != ARES_SUCCESS) - throw std::runtime_error("Failed to initialize c-ares channel: "s + ares_strerror(ares_error)); -} - -ares_channel& DNSHandler::get_channel() -{ - return this->channel; -} - -void DNSHandler::destroy() -{ - this->socket_handlers.clear(); - ::ares_destroy(this->channel); - ::ares_library_cleanup(); -} - -void DNSHandler::gethostbyname(const std::string& name, - TCPSocketHandler* socket_handler, int family) -{ - socket_handler->free_cares_addrinfo(); - if (family == AF_INET) - ::ares_gethostbyname(this->channel, name.data(), family, - &::on_hostname4_resolved, socket_handler); - else - ::ares_gethostbyname(this->channel, name.data(), family, - &::on_hostname6_resolved, socket_handler); -} - -void DNSHandler::watch_dns_sockets(std::shared_ptr<Poller>& poller) -{ - fd_set readers; - fd_set writers; - - FD_ZERO(&readers); - FD_ZERO(&writers); - - int ndfs = ::ares_fds(this->channel, &readers, &writers); - // For each existing DNS socket, see if we are still supposed to watch it, - // if not then erase it - this->socket_handlers.erase( - std::remove_if(this->socket_handlers.begin(), this->socket_handlers.end(), - [&readers](const auto& dns_socket) - { - return !FD_ISSET(dns_socket->get_socket(), &readers); - }), - this->socket_handlers.end()); - - for (auto i = 0; i < ndfs; ++i) - { - bool read = FD_ISSET(i, &readers); - bool write = FD_ISSET(i, &writers); - // Look for the DNSSocketHandler with this fd - auto it = std::find_if(this->socket_handlers.begin(), - this->socket_handlers.end(), - [i](const auto& socket_handler) - { - return i == socket_handler->get_socket(); - }); - if (!read && !write) // No need to read or write to it - { // If found, erase it and stop watching it because it is not - // needed anymore - if (it != this->socket_handlers.end()) - // The s |