Core module

parent a22f030e
......@@ -30,6 +30,9 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
# Flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -pedantic -Werror")
include_directories("${CMAKE_SOURCE_DIR}/src")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wno-vexing-parse")
endif()
# Check C++14
message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
......@@ -69,7 +72,9 @@ macro(melanomodule name description default)
add_subdirectory(${name})
set(MELANOMODULES_ ${MELANOMODULES_} "melanomodule_${name}")
set(MELANOMODULES ${MELANOMODULES_} PARENT_SCOPE)
set(MELANOMODULES_INIT_ "${MELANOMODULES_INIT_} void melanomodule_${name}()\; melanomodule_${name}()\;")
set(MELANOMODULES_INIT_ "${MELANOMODULES_INIT_} \\
Melanomodule melanomodule_${name}()\; \\
modules.push_back(melanomodule_${name}())\;")
set(MELANOMODULES_INIT ${MELANOMODULES_INIT_} PARENT_SCOPE)
message(STATUS "Enabled module ${name}: ${description}")
endif()
......
......@@ -16,6 +16,7 @@
include_directories("${CMAKE_SOURCE_DIR}/modules")
#modules ID Desctiption Default
melanomodule(core "Core module, should always be enabled" ON)
melanomodule(irc "Internet Relay Chat support" ON)
melanomodule(xonotic "Xonotic integration" ON)
melanomodule(posix "Extra features for POSIX systems" OFF)
......
# Copyright (C) 2015 Mattia Basaglia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(MODULE_NAME melanomodule_core)
file(GLOB_RECURSE SOURCES *.cpp)
add_library(${MODULE_NAME} ${SOURCES})
/**
* \file
* \author Mattia Basaglia
* \copyright Copyright 2015 Mattia Basaglia
* \section License
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "melanomodule.hpp"
#include "handler/admin.hpp"
#include "handler/admin.hpp"
#include "handler/bridge.hpp"
#include "handler/misc.hpp"
#include "handler/simple-group.hpp"
Melanomodule melanomodule_core()
{
Melanomodule module {"core", "Core module"};
module.register_log_type("sys",color::dark_red);
REGISTER_HANDLER(handler::AdminQuit,Quit);
REGISTER_HANDLER(handler::AdminGroup,AdminGroup);
REGISTER_HANDLER(handler::FilterGroup,FilterGroup);
REGISTER_HANDLER(handler::AdminReconnect,Reconnect);
REGISTER_HANDLER(handler::AdminConnect,Connect);
REGISTER_HANDLER(handler::AdminDisconnect,Disconnect);
REGISTER_HANDLER(handler::Chanhax,Chanhax);
REGISTER_HANDLER(handler::Bridge,Bridge);
REGISTER_HANDLER(handler::BridgeChat,BridgeChat);
REGISTER_HANDLER(handler::License,License);
REGISTER_HANDLER(handler::Help,Help);
REGISTER_HANDLER(handler::Echo,Echo);
REGISTER_HANDLER(handler::ServerHost,ServerHost);
REGISTER_HANDLER(handler::Cointoss,Cointoss);
REGISTER_HANDLER(handler::Reply,Reply);
REGISTER_HANDLER(handler::SimpleGroup, Group);
return module;
}
......@@ -18,7 +18,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "handler.hpp"
#include "simple-group.hpp"
#ifndef HANDLER_ADMIN_HPP
#define HANDLER_ADMIN_HPP
namespace handler {
......@@ -53,7 +56,6 @@ protected:
private:
std::string message = "Bye!";
};
REGISTER_HANDLER(AdminQuit,Quit);
/**
* \brief Manages a user group
......@@ -119,7 +121,6 @@ private:
std::string description; ///< Used as list_name property
std::string ignore; ///< Group to be ignored on add/remove
};
REGISTER_HANDLER(AdminGroup,AdminGroup);
/**
* \brief Discards messages coming from certain users
......@@ -140,7 +141,6 @@ private:
return true;
}
};
REGISTER_HANDLER(FilterGroup,FilterGroup);
/**
* \brief Makes the bot reconnect
......@@ -172,7 +172,6 @@ protected:
private:
std::string message = "Reconnecting...";
};
REGISTER_HANDLER(AdminReconnect,Reconnect);
/**
* \brief Makes the bot Connect
......@@ -193,7 +192,6 @@ protected:
return true;
}
};
REGISTER_HANDLER(AdminConnect,Connect);
/**
* \brief Makes the bot disconnect
......@@ -225,7 +223,6 @@ protected:
private:
std::string message = "Disconnecting...";
};
REGISTER_HANDLER(AdminDisconnect,Disconnect);
/**
* \brief Changes the channel of a message
......@@ -276,6 +273,6 @@ private:
std::string trigger;
std::regex regex_chanhax;
};
REGISTER_HANDLER(Chanhax,Chanhax);
} // namespace handler
#endif // HANDLER_ADMIN_HPP
/**
* \file
* \author Mattia Basaglia
* \copyright Copyright Mattia Basaglia
* \copyright Copyright 2015 Mattia Basaglia
* \section License
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
......@@ -16,28 +16,30 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HANDLER_BRIDGE_HPP
#define HANDLER_BRIDGE_HPP
#include "handler.hpp"
#include "bridge.hpp"
namespace handler {
/**
* \brief Acts as bridge across connections
* \todo attachable to other connections (maybe limiting the protocol)
*/
class Bridge : public SimpleGroup
Bridge::Bridge(const Settings& settings, Melanobot* bot)
: SimpleGroup(settings,bot)
{
public:
Bridge(const Settings& settings, Melanobot* bot);
protected:
bool on_handle(network::Message& msg) override;
std::string destination_name = settings.get("destination","");
if ( destination_name.empty() )
throw ConfigurationError();
network::Connection* destination = nullptr; ///< Message destination
boost::optional<std::string> dst_channel; ///< Message destination channel
};
destination = bot->connection(destination_name);
dst_channel = settings.get_optional<std::string>("dst_channel");
}
bool Bridge::on_handle(network::Message& msg)
{
network::Message& targeted = msg;
targeted.destination = destination;
if ( dst_channel )
targeted.dst_channel = dst_channel;
return SimpleGroup::on_handle(targeted);
}
} // namespace handler
#endif // HANDLER_BRIDGE_HPP
/**
* \file
* \author Mattia Basaglia
* \copyright Copyright 2015 Mattia Basaglia
* \copyright Copyright Mattia Basaglia
* \section License
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
......@@ -16,33 +16,28 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HANDLER_BRIDGE_HPP
#define HANDLER_BRIDGE_HPP
#include "bridge.hpp"
#include "simple-group.hpp"
namespace handler {
REGISTER_HANDLER(Bridge,Bridge);
Bridge::Bridge(const Settings& settings, Melanobot* bot)
: SimpleGroup(settings,bot)
/**
* \brief Acts as bridge across connections
* \todo attachable to other connections (maybe limiting the protocol)
*/
class Bridge : public SimpleGroup
{
public:
Bridge(const Settings& settings, Melanobot* bot);
std::string destination_name = settings.get("destination","");
if ( destination_name.empty() )
throw ConfigurationError();
destination = bot->connection(destination_name);
dst_channel = settings.get_optional<std::string>("dst_channel");
}
bool Bridge::on_handle(network::Message& msg)
{
network::Message& targeted = msg;
targeted.destination = destination;
if ( dst_channel )
targeted.dst_channel = dst_channel;
return SimpleGroup::on_handle(targeted);
}
protected:
bool on_handle(network::Message& msg) override;
network::Connection* destination = nullptr; ///< Message destination
boost::optional<std::string> dst_channel; ///< Message destination channel
};
/**
* \brief Simply echoes chat messages (to be used in a Bridge group)
......@@ -91,7 +86,6 @@ protected:
network::Duration timeout = network::Duration::zero();
bool ignore_self = true;
};
REGISTER_HANDLER(BridgeChat,BridgeChat);
} // namespace handler
#endif // HANDLER_BRIDGE_HPP
......@@ -19,7 +19,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "handler.hpp"
#ifndef HANDLER_MISC
#define HANDLER_MISC
#include "handler/handler.hpp"
#include "math.hpp"
namespace handler {
......@@ -48,7 +51,6 @@ protected:
private:
std::string sources_url;
};
REGISTER_HANDLER(License,License);
/**
* \brief Handler showing help on the available handlers
......@@ -213,7 +215,6 @@ private:
return {};
}
};
REGISTER_HANDLER(Help,Help);
/**
* \brief Just repeat what it has been told
......@@ -235,7 +236,6 @@ protected:
return true;
}
};
REGISTER_HANDLER(Echo,Echo);
/**
* \brief Shows the server the bot is connected to
......@@ -257,7 +257,6 @@ protected:
return true;
}
};
REGISTER_HANDLER(ServerHost,ServerHost);
/**
* \brief Shows one of the given items, at random
......@@ -308,8 +307,6 @@ private:
std::vector<std::string> default_items = { "Heads", "Tails" };
bool customizable = true;
};
REGISTER_HANDLER(Cointoss,Cointoss);
/**
* \brief Fixed reply
......@@ -382,6 +379,7 @@ private:
bool case_sensitive = true;
bool direct = true;
};
REGISTER_HANDLER(Reply,Reply);
} // namespace handler
#endif // HANDLER_MISC
......@@ -18,12 +18,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "handler.hpp"
#include "simple-group.hpp"
namespace handler {
REGISTER_HANDLER(SimpleGroup, Group);
/// \todo option to copy settings from another group
SimpleGroup::SimpleGroup(const Settings& settings, Melanobot* bot)
: SimpleAction("",settings,bot,true)
......
/**
* \file
* \author Mattia Basaglia
* \copyright Copyright 2015 Mattia Basaglia
* \license
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HANDLER_SIMPLE_GROUP_HPP
#define HANDLER_SIMPLE_GROUP_HPP
#include "handler/handler.hpp"
/**
* \brief Namespace for classes that handle connection messages
*
* \see Handler and SimpleAction for base classes.
*/
namespace handler {
/**
* \brief A simple group of actions which share settings
* \todo Add a simple way to create pre-defined group classes which will have
* a set of preset actions (eg: Q Whois system, Cup management, Xonotic integration)
*/
class SimpleGroup : public SimpleAction
{
public:
SimpleGroup(const Settings& settings, Melanobot* bot);
bool can_handle(const network::Message& msg) const override;
void populate_properties(const std::vector<std::string>& properties, PropertyTree& output) const override;
std::string get_property(const std::string& name) const override
{
if ( name == "name" )
return this->name;
else if ( name == "help_group" )
return help_group;
return SimpleAction::get_property(name);
}
protected:
bool on_handle(network::Message& msg) override;
std::vector<std::unique_ptr<Handler>> children; ///< Contained handlers
std::string channels; ///< Channel filter
/// \todo if posible, merge name and help_group
std::string name; ///< Name to show in help
std::string help_group; ///< Selects whether to be shown in help
network::Connection* source = nullptr; ///< Accepted connection (Null => all connections)
};
/**
* \brief Handles a list of elements (base class)
* \note Derived classes shall provide the property \c list_name
* which contains a human-readable name of the list,
* used for descriptions of the handler.
*/
class AbstractList : public SimpleGroup
{
public:
/**
* \param default_trigger Default trigger/group name
* \param clear Whether to allow clearing the list
* \param settings Handler settings
* \param bot Main bot
*/
AbstractList(const std::string& default_trigger, bool clear,
const Settings& settings, Melanobot* bot);
/**
* \brief Adds \c element to the list
* \return \b true on success
*/
virtual bool add(const std::string& element) = 0;
/**
* \brief Removes \c element from the list
* \return \b true on success
*/
virtual bool remove(const std::string& element) = 0;
/**
* \brief Removes all elements from the list
* \return \b true on success
*/
virtual bool clear() = 0;
/**
* \brief Returns a vector containing all the elements of the string
*/
virtual std::vector<std::string> elements() const = 0;
std::string get_property(const std::string& name) const override
{
if ( name == "help" )
return "Manages "+get_property("list_name");
return SimpleGroup::get_property(name);
}
protected:
bool on_handle(network::Message& msg) override;
};
} // namespace handler
#endif // HANDLER_SIMPLE_GROUP
......@@ -17,15 +17,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fun-handlers.hpp"
#include "melanomodule.hpp"
/**
* \brief Registers the fun handlers
*/
void melanomodule_fun()
Melanomodule melanomodule_fun()
{
Melanomodule module{"fun","Fun handlers"};
REGISTER_HANDLER(fun::AnswerQuestions,AnswerQuestions);
REGISTER_HANDLER(fun::RenderPony,RenderPony);
REGISTER_HANDLER(fun::ChuckNorris,ChuckNorris);
REGISTER_HANDLER(fun::ReverseText,ReverseText);
REGISTER_HANDLER(fun::Morse,Morse);
return module;
}
......@@ -22,19 +22,21 @@
#include "irc/handler/irc_actions.hpp"
#include "irc/handler/irc_admin.hpp"
#include "irc/handler/irc_whois.hpp"
#include "melanomodule.hpp"
/**
* \brief Initializes the IRC module
*/
void melanomodule_irc()
Melanomodule melanomodule_irc()
{
/**
* \todo Turn more REGISTER_ macros into templates
* \todo Extract more stuff from src/ into modules/
*/
REGISTER_CONNECTION<irc::IrcConnection>("irc");
REGISTER_LOG_TYPE("irc",color::dark_magenta);
REGISTER_FORMATTER<irc::FormatterIrc>();
Melanomodule module{"irc","IRC integration"};
module.register_connection<irc::IrcConnection>("irc");
module.register_log_type("irc",color::dark_magenta);
module.register_formatter<irc::FormatterIrc>();
REGISTER_HANDLER(irc::handler::CtcpVersion,CtcpVersion);
REGISTER_HANDLER(irc::handler::CtcpSource,CtcpSource);
......@@ -57,4 +59,6 @@ void melanomodule_irc()
REGISTER_HANDLER(irc::handler::QSendWhois,QSendWhois);
REGISTER_HANDLER(irc::handler::QGetWhois,QGetWhois);
REGISTER_HANDLER(irc::handler::WhoisCheckMe,WhoisCheckMe);
return module;
}
......@@ -18,7 +18,9 @@
*/
#include <boost/asio.hpp>
#ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
#ifndef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
# error "The posix module requires a POSIX-conformant system"
#endif
#include <cstdio>
#include <memory>
......@@ -145,12 +147,17 @@ private:
}
};
#include "melanomodule.hpp"
/**
* \brief POSIX module initialization
* \todo split file
*/
void melanomodule_posix()
Melanomodule melanomodule_posix()
{
REGISTER_CONNECTION<StdinConnection>("stdin");
REGISTER_LOG_TYPE("std",color::white);
Melanomodule module{"posix","POSIX extensions"};
module.register_connection<StdinConnection>("stdin");
module.register_log_type("std",color::white);
return module;
}
#endif // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
......@@ -18,17 +18,21 @@
*/
#include "web-api-concrete.hpp"
#include "http.hpp"
#include "melanomodule.hpp"
/**
* \brief Initializes the web module
*/
void melanomodule_web()
Melanomodule melanomodule_web()
{
REGISTER_LOG_TYPE("web",color::dark_blue);
REGISTER_SERVICE(network::http::HttpService,web);
Melanomodule module{"web","Web services"};
module.register_log_type("web",color::dark_blue);
module.register_service<network::http::HttpService>("web");
REGISTER_HANDLER(handler::SearchVideoYoutube,SearchVideoYoutube);
REGISTER_HANDLER(handler::SearchImageGoogle,SearchImageGoogle);
REGISTER_HANDLER(handler::UrbanDictionary,UrbanDictionary);
REGISTER_HANDLER(handler::SearchWebSearx,SearchWebSearx);
return module;
}
......@@ -18,13 +18,15 @@
*/
#include "formatter.hpp"
#include "string/logger.hpp"
#include "melanomodule.hpp"
/**
* \brief Initializes the Xonotic module
*/
void melanomodule_xonotic()
Melanomodule melanomodule_xonotic()
{
REGISTER_FORMATTER<xonotic::Formatter>();
REGISTER_LOG_TYPE("xon",color::dark_cyan);
Melanomodule module{"xonotic","Xonotic integration"};
module.register_formatter<xonotic::Formatter>();
module.register_log_type("xon",color::dark_cyan);
return module;
}
......@@ -2,7 +2,7 @@
* \file
* \author Mattia Basaglia
* \copyright Copyright 2015 Mattia Basaglia
* \license
* \section License
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
......@@ -32,11 +32,6 @@
#include "string/string_functions.hpp"
#include "melanobot.hpp"
/**
* \brief Namespace for classes that handle connection messages
*
* \see Handler and SimpleAction for base classes.
*/
namespace handler {
/**
......@@ -331,89 +326,6 @@ private:
}
};
/**
* \brief A simple group of actions which share settings
* \todo Add a simple way to create pre-defined group classes which will have
* a set of preset actions (eg: Q Whois system, Cup management, Xonotic integration)
*/
class SimpleGroup : public SimpleAction
{
public:
SimpleGroup(const Settings& settings, Melanobot* bot);
bool can_handle(const network::Message& msg) const override;
void populate_properties(const std::vector<std::string>& properties, PropertyTree& output) const override;
std::string get_property(const std::string& name) const override
{
if ( name == "name" )
return this->name;
else if ( name == "help_group" )
return help_group;
return SimpleAction::get_property(name);
}
protected:
bool on_handle(network::Message& msg) override;
std::vector<std::unique_ptr<Handler>> children; ///< Contained handlers
std::string channels; ///< Channel filter
/// \todo if posible, merge name and help_group
std::string name; ///< Name to show in help
std::string help_group; ///< Selects whether to be shown in help
network::Connection* source = nullptr; ///< Accepted connection (Null => all connections)
};
/**
* \brief Handles a list of elements (base class)
* \note Derived classes shall provide the property \c list_name
* which contains a human-readable name of the list,
* used for descriptions of the handler.
*/
class AbstractList : public SimpleGroup
{
public:
/**
* \param default_trigger Default trigger/group name
* \param clear Whether to allow clearing the list
* \param settings Handler settings
* \param bot Main bot
*/
AbstractList(const std::string& default_trigger, bool clear,
const Settings& settings, Melanobot* bot);
/**
* \brief Adds \c element to the list
* \return \b true on success
*/
virtual bool add(const std::string& element) = 0;
/**
* \brief Removes \c element from the list
* \return \b true on success
*/
virtual bool remove(const std::string& element) = 0;
/**
* \brief Removes all elements from the list
* \return \b true on success
*/
virtual bool clear() = 0;
/**
* \brief Returns a vector containing all the elements of the string
*/
virtual std::vector<std::string> elements() const = 0;
std::string get_property(const std::string& name) const override
{
if ( name == "help" )
return "Manages "+get_property("list_name");
return SimpleGroup::get_property(name);
}
protected:
bool on_handle(network::Message& msg) override;
};
/**
* \brief Registers a Handler to the HandlerFactory
......@@ -492,4 +404,4 @@ private:
};
} // namespace handler
#endif // HANDLER_GROUP_HPP
#endif // HANDLER_HPP
......@@ -20,6 +20,7 @@
#include "string/logger.hpp"
#include "settings.hpp"
#include "network/async_service.hpp"
#include "melanomodule.hpp"
#include "melanomodules.hpp"
int main(int argc, char **argv)
......@@ -28,9 +29,10 @@ int main(int argc, char **argv)