Web and fun modules

parent 5adbf2ac
......@@ -15,6 +15,9 @@
include_directories("${CMAKE_SOURCE_DIR}/modules")
melanomodule(irc "Internet Relay Chat support" ON)
melanomodule(xonotic "Xonotic integration" ON)
melanomodule(posix "Extra features for POSIX systems" OFF)
#modules ID Desctiption Default
melanomodule(irc "Internet Relay Chat support" ON)
melanomodule(xonotic "Xonotic integration" ON)
melanomodule(posix "Extra features for POSIX systems" OFF)
melanomodule(web "Web stuff (HTTP and friends)" ON)
melanomodule(fun "Possibly useless yet silly stuff" ON)
# 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_fun)
file(GLOB_RECURSE SOURCES *.cpp)
add_library(${MODULE_NAME} ${SOURCES})
This diff is collapsed.
/**
* \file
* \brief This file contains definitions for handlers which are pretty useless
*
* \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/>.
*/
#ifndef FUN_HANDLERS_HPP
#define FUN_HANDLERS_HPP
#include <unordered_map>
#include <boost/filesystem.hpp>
#include "handler/handler.hpp"
#include "web/web-api.hpp"
#include "math.hpp"
namespace fun {
/**
* \brief Handler translating between morse and latin
*/
class Morse : public handler::SimpleAction
{
public:
Morse(const Settings& settings, Melanobot* bot)
: SimpleAction("morse",settings,bot)
{
synopsis += " text|morse";
help = "Converts between ASCII and Morse code";
}
protected:
bool on_handle(network::Message& msg) override;
private:
static std::unordered_map<char,std::string> morse;
};
/**
* \brief Turns ASCII characters upside-down
*/
class ReverseText : public handler::SimpleAction
{
public:
ReverseText(const Settings& settings, Melanobot* bot)
: SimpleAction("reverse",settings,bot)
{
synopsis += " text";
help = "Turns ASCII upside-down";
}
protected:
bool on_handle(network::Message& msg) override;
private:
static std::unordered_map<char,std::string> reverse_ascii;
};
/**
* \brief Searches for a Chuck Norris joke
*/
class ChuckNorris : public handler::SimpleJson
{
public:
ChuckNorris(const Settings& settings, Melanobot* bot)
: SimpleJson("norris",settings,bot)
{
synopsis += " [name]";
help = "Shows a Chuck Norris joke from http://icndb.com";
}
protected:
bool on_handle(network::Message& msg) override;
void json_success(const network::Message& msg, const Settings& parsed) override
{
/// \todo convert html entities
reply_to(msg,parsed.get("value.joke",""));
}
private:
std::string api_url = "http://api.icndb.com/jokes/random";
};
/**
* \brief Draws a pretty My Little Pony ASCII art
* \note Very useful to see how the bot handles flooding
* \note Even more useful to see pretty ponies ;-)
* \see https://github.com/mbasaglia/ASCII-Pony
*/
class RenderPony : public handler::SimpleAction
{
public:
RenderPony(const Settings& settings, Melanobot* bot)
: SimpleAction("render_pony",settings,bot)
{
synopsis += " pony";
help = "Draws a pretty pony /)^3^(\\";
pony_path = settings.get("path",pony_path);
}
protected:
bool on_handle(network::Message& msg) override;
private:
std::string pony_path;
};
/**
* \brief Answers direct questions
*/
class AnswerQuestions : public handler::Handler
{
public:
AnswerQuestions(const Settings& settings, Melanobot* bot)
: Handler(settings, bot)
{
direct = settings.get("direct",direct);
}
bool can_handle(const network::Message& msg) const override
{
return msg.direct && !msg.message.empty() && msg.message.back() == '?';
}
protected:
bool on_handle(network::Message& msg) override;
private:
bool direct = true;
/// Answers corresponding to yes or no
static std::vector<std::string> category_yesno;
/// Generic and unsatisfying answers
static std::vector<std::string> category_dunno;
/// Answers to some time in the past
static std::vector<std::string> category_when_did;
/// Generic answers to when
static std::vector<std::string> category_when;
/// Answers to some time in the future
static std::vector<std::string> category_when_will;
/**
* \brief Selects a random answer from a set of categories
*/
void random_answer(network::Message& msg,
const std::vector<std::vector<std::string>*>& categories) const;
};
} // namespace fun
#endif // FUN_HANDLERS_HPP
/**
* \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 "fun-handlers.hpp"
/**
* \brief Registers the fun handlers
*/
void melanomodule_fun()
{
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);
}
......@@ -33,7 +33,7 @@ void melanomodule_irc()
* \todo Extract more stuff from src/ into modules/
*/
REGISTER_CONNECTION(irc,&irc::IrcConnection::create);
REGISTER_LOG_TYPE(irc,color::dark_magenta);
REGISTER_LOG_TYPE("irc",color::dark_magenta);
REGISTER_FORMATTER<irc::FormatterIrc>();
REGISTER_HANDLER(irc::handler::CtcpVersion,CtcpVersion);
......
......@@ -151,6 +151,6 @@ private:
void melanomodule_posix()
{
REGISTER_CONNECTION(stdin,&StdinConnection::create);
REGISTER_LOG_TYPE(std,color::white);
REGISTER_LOG_TYPE("std",color::white);
}
#endif // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
# 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_web)
file(GLOB_RECURSE SOURCES *.cpp)
add_library(${MODULE_NAME} ${SOURCES})
# cURLpp
find_package (cURLpp REQUIRED)
target_link_libraries(${MODULE_NAME} ${cURLpp_LIBRARIES})
include_directories (${cURLpp_INCLUDE_DIRS})
......@@ -27,13 +27,10 @@
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include "../string/logger.hpp"
#include "string/logger.hpp"
#include "config.hpp"
REGISTER_LOG_TYPE(web,color::dark_blue);
REGISTER_SERVICE(network::http::HttpService,web);
namespace network {
namespace http {
......
......@@ -19,7 +19,7 @@
#ifndef HTTP_HPP
#define HTTP_HPP
#include "async_service.hpp"
#include "network/async_service.hpp"
#include <map>
......
......@@ -18,6 +18,9 @@
* 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 WEB_API_CONCRETE
#define WEB_API_CONCRETE
#include "web-api.hpp"
namespace handler {
......@@ -53,8 +56,6 @@ private:
std::string api_url;
std::string not_found_reply;
};
REGISTER_HANDLER(SearchVideoYoutube,SearchVideoYoutube);
/**
* \brief Handler searching images with Google
......@@ -80,7 +81,6 @@ protected:
reply_to(msg,parsed.get("responseData.results.0.unescapedUrl",not_found_reply));
}
};
REGISTER_HANDLER(SearchImageGoogle,SearchImageGoogle);
......@@ -110,7 +110,6 @@ protected:
reply_to(msg,result);
}
};
REGISTER_HANDLER(UrbanDictionary,UrbanDictionary);
/**
......@@ -156,7 +155,8 @@ private:
std::string api_url;
};
REGISTER_HANDLER(SearchWebSearx,SearchWebSearx);
} // namespace handler
#endif // WEB_API_CONCRETE
......@@ -19,8 +19,8 @@
#ifndef HANDLER_WEB_API_HPP
#define HANDLER_WEB_API_HPP
#include "handler.hpp"
#include "network/http.hpp"
#include "handler/handler.hpp"
#include "http.hpp"
#include "string/json.hpp"
namespace handler {
......
/**
* \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 "web-api-concrete.hpp"
#include "http.hpp"
/**
* \brief Initializes the web module
*/
void melanomodule_web()
{
REGISTER_LOG_TYPE("web",color::dark_blue);
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);
}
......@@ -26,5 +26,5 @@
void melanomodule_xonotic()
{
REGISTER_FORMATTER<xonotic::Formatter>();
REGISTER_LOG_TYPE(xon,color::dark_cyan);
REGISTER_LOG_TYPE("xon",color::dark_cyan);
}
......@@ -25,12 +25,6 @@ find_package (Boost COMPONENTS system chrono thread filesystem program_options R
target_link_libraries(${EXECUTABLE_NAME} ${Boost_LIBRARIES})
include_directories (${Boost_INCLUDE_DIRS})
# cURLpp
find_package (cURLpp REQUIRED)
target_link_libraries(${EXECUTABLE_NAME} ${cURLpp_LIBRARIES})
include_directories (${cURLpp_INCLUDE_DIRS})
if (MELANOMODULES)
target_link_libraries(${EXECUTABLE_NAME} ${MELANOMODULES})
endif()
This diff is collapsed.
......@@ -30,7 +30,7 @@ int main(int argc, char **argv)
MELANOMODULES_INIT
REGISTER_LOG_TYPE(sys,color::dark_red);
REGISTER_LOG_TYPE("sys",color::dark_red);
try {
Settings settings = Settings::initialize(argc,argv);
......
......@@ -32,14 +32,6 @@
#include "string.hpp"
#include "settings.hpp"
/**
* \brief Registers a log type
* \note Use in implementation files, not headers
* \param name Log type name as a C++ token
* \param color Color used to identify the log type (color::Color12 object)
*/
#define REGISTER_LOG_TYPE(name,color) static Logger::RegisterLogType RegisterLogType_##name(#name,color)
/**
* \brief Singleton class handling logs
* \see Log for a nicer interface
......@@ -47,16 +39,6 @@
class Logger
{
public:
/**
* \brief Dummy class which registers log types
*/
struct RegisterLogType
{
RegisterLogType(const std::string& name, color::Color12 color)
{
Logger::instance().register_log_type(name,color);
}
};
/**
* \brief Singleton instance
......@@ -137,6 +119,19 @@ private:
std::mutex mutex;
};
/**
* \brief Registers a log type
* \param name Log type name
* \param color Color used to identify the log type
* \todo Either remove complete or add a static version in Logger
*/
inline void REGISTER_LOG_TYPE(const std::string& name, color::Color12 color)
{
Logger::instance().register_log_type(name,color);
}
/**
* \brief Simple log stream-like interface
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment