Compare commits

...

7 Commits

5 changed files with 346 additions and 77 deletions

View File

@ -1,34 +1,26 @@
cmake_minimum_required (VERSION 3.2)
cmake_minimum_required (VERSION 3.9...3.16)
project(feiertagebot
LANGUAGES CXX
)
LANGUAGES CXX)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
include(GNUInstallDirs)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
pkg_check_modules(CURLPP REQUIRED curlpp)
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
pkg_check_modules(libxdg-basedir REQUIRED IMPORTED_TARGET libxdg-basedir)
pkg_check_modules(curlpp REQUIRED IMPORTED_TARGET curlpp)
pkg_check_modules(jsoncpp REQUIRED IMPORTED_TARGET jsoncpp)
find_package(mastodonpp 0.5.4 REQUIRED CONFIG)
find_package(Filesystem REQUIRED)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} -Wpedantic -Wall -Wextra -ftrapv -fsanitize=undefined -g -Og -fno-omit-frame-pointer")
include_directories(${PROJECT_BINARY_DIR})
include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
include_directories(${CURL_INCLUDE_DIRS})
include_directories(${CURLPP_INCLUDE_DIRS})
include_directories(${JSONCPP_INCLUDE_DIRS})
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
link_directories(${CURL_LIBRARY_DIRS})
link_directories(${CURLPP_LIBRARY_DIRS})
link_directories(${JSONCPP_LIBRARY_DIRS})
include(debug_flags)
add_executable(${CMAKE_PROJECT_NAME} feiertagebot.cpp xdgjson/src/xdgjson.cpp)
target_link_libraries(${CMAKE_PROJECT_NAME}
"${LIBXDG_BASEDIR_LDFLAGS} ${CURLPP_LDFLAGS} ${JSONCPP_LDFLAGS}"
"-lstdc++fs -lmastodon-cpp")
PRIVATE
PkgConfig::libxdg-basedir PkgConfig::curlpp PkgConfig::jsoncpp
mastodonpp::mastodonpp std::filesystem)
install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@ -28,11 +28,11 @@ git submodule update
==== Installiere abhängigkeiten
* C++ compiler
* https://cmake.org/[cmake] (mindestens 3.2)
* https://cmake.org/[cmake] (mindestens: 3.2)
* http://repo.or.cz/w/libxdg-basedir.git[libxdg-basedir] (getested: 1.2)
* http://www.curlpp.org/[curlpp] (getested: 0.8)
* https://github.com/open-source-parsers/jsoncpp[jsoncpp] (getested: 1.8)
* https://schlomp.space/tastytea/mastodon-cpp[mastodon-cpp] (mindestens 0.30.1)
* https://schlomp.space/tastytea/mastodonpp[mastodonpp] (mindestens: 0.5.4)
==== Kompilieren

227
cmake/FindFilesystem.cmake Normal file
View File

@ -0,0 +1,227 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindFilesystem
##############
This module supports the C++17 standard library's filesystem utilities. Use the
:imp-target:`std::filesystem` imported target to
Options
*******
The ``COMPONENTS`` argument to this module supports the following values:
.. find-component:: Experimental
:name: fs.Experimental
Allows the module to find the "experimental" Filesystem TS version of the
Filesystem library. This is the library that should be used with the
``std::experimental::filesystem`` namespace.
.. find-component:: Final
:name: fs.Final
Finds the final C++17 standard version of the filesystem library.
If no components are provided, behaves as if the
:find-component:`fs.Final` component was specified.
If both :find-component:`fs.Experimental` and :find-component:`fs.Final` are
provided, first looks for ``Final``, and falls back to ``Experimental`` in case
of failure. If ``Final`` is found, :imp-target:`std::filesystem` and all
:ref:`variables <fs.variables>` will refer to the ``Final`` version.
Imported Targets
****************
.. imp-target:: std::filesystem
The ``std::filesystem`` imported target is defined when any requested
version of the C++ filesystem library has been found, whether it is
*Experimental* or *Final*.
If no version of the filesystem library is available, this target will not
be defined.
.. note::
This target has ``cxx_std_17`` as an ``INTERFACE``
:ref:`compile language standard feature <req-lang-standards>`. Linking
to this target will automatically enable C++17 if no later standard
version is already required on the linking target.
.. _fs.variables:
Variables
*********
.. variable:: CXX_FILESYSTEM_IS_EXPERIMENTAL
Set to ``TRUE`` when the :find-component:`fs.Experimental` version of C++
filesystem library was found, otherwise ``FALSE``.
.. variable:: CXX_FILESYSTEM_HAVE_FS
Set to ``TRUE`` when a filesystem header was found.
.. variable:: CXX_FILESYSTEM_HEADER
Set to either ``filesystem`` or ``experimental/filesystem`` depending on
whether :find-component:`fs.Final` or :find-component:`fs.Experimental` was
found.
.. variable:: CXX_FILESYSTEM_NAMESPACE
Set to either ``std::filesystem`` or ``std::experimental::filesystem``
depending on whether :find-component:`fs.Final` or
:find-component:`fs.Experimental` was found.
Examples
********
Using `find_package(Filesystem)` with no component arguments:
.. code-block:: cmake
find_package(Filesystem REQUIRED)
add_executable(my-program main.cpp)
target_link_libraries(my-program PRIVATE std::filesystem)
#]=======================================================================]
if(TARGET std::filesystem)
# This module has already been processed. Don't do it again.
return()
endif()
include(CMakePushCheckState)
include(CheckIncludeFileCXX)
include(CheckCXXSourceCompiles)
cmake_push_check_state()
set(CMAKE_REQUIRED_QUIET ${Filesystem_FIND_QUIETLY})
# All of our tests required C++17 or later
set(CMAKE_CXX_STANDARD 17)
# Normalize and check the component list we were given
set(want_components ${Filesystem_FIND_COMPONENTS})
if(Filesystem_FIND_COMPONENTS STREQUAL "")
set(want_components Final)
endif()
# Warn on any unrecognized components
set(extra_components ${want_components})
list(REMOVE_ITEM extra_components Final Experimental)
foreach(component IN LISTS extra_components)
message(WARNING "Extraneous find_package component for Filesystem: ${component}")
endforeach()
# Detect which of Experimental and Final we should look for
set(find_experimental TRUE)
set(find_final TRUE)
if(NOT "Final" IN_LIST want_components)
set(find_final FALSE)
endif()
if(NOT "Experimental" IN_LIST want_components)
set(find_experimental FALSE)
endif()
if(find_final)
check_include_file_cxx("filesystem" _CXX_FILESYSTEM_HAVE_HEADER)
mark_as_advanced(_CXX_FILESYSTEM_HAVE_HEADER)
if(_CXX_FILESYSTEM_HAVE_HEADER)
# We found the non-experimental header. Don't bother looking for the
# experimental one.
set(find_experimental FALSE)
endif()
else()
set(_CXX_FILESYSTEM_HAVE_HEADER FALSE)
endif()
if(find_experimental)
check_include_file_cxx("experimental/filesystem" _CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER)
mark_as_advanced(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER)
else()
set(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER FALSE)
endif()
if(_CXX_FILESYSTEM_HAVE_HEADER)
set(_have_fs TRUE)
set(_fs_header filesystem)
set(_fs_namespace std::filesystem)
elseif(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER)
set(_have_fs TRUE)
set(_fs_header experimental/filesystem)
set(_fs_namespace std::experimental::filesystem)
else()
set(_have_fs FALSE)
endif()
set(CXX_FILESYSTEM_HAVE_FS ${_have_fs} CACHE BOOL "TRUE if we have the C++ filesystem headers")
set(CXX_FILESYSTEM_HEADER ${_fs_header} CACHE STRING "The header that should be included to obtain the filesystem APIs")
set(CXX_FILESYSTEM_NAMESPACE ${_fs_namespace} CACHE STRING "The C++ namespace that contains the filesystem APIs")
set(_found FALSE)
if(CXX_FILESYSTEM_HAVE_FS)
# We have some filesystem library available. Do link checks
string(CONFIGURE [[
#include <@CXX_FILESYSTEM_HEADER@>
int main() {
auto cwd = @CXX_FILESYSTEM_NAMESPACE@::current_path();
return static_cast<int>(cwd.string().size());
}
]] code @ONLY)
# Try to compile a simple filesystem program without any linker flags
check_cxx_source_compiles("${code}" CXX_FILESYSTEM_NO_LINK_NEEDED)
set(can_link ${CXX_FILESYSTEM_NO_LINK_NEEDED})
if(NOT CXX_FILESYSTEM_NO_LINK_NEEDED)
set(prev_libraries ${CMAKE_REQUIRED_LIBRARIES})
# Add the libstdc++ flag
set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lstdc++fs)
check_cxx_source_compiles("${code}" CXX_FILESYSTEM_STDCPPFS_NEEDED)
set(can_link ${CXX_FILESYSTEM_STDCPPFS_NEEDED})
if(NOT CXX_FILESYSTEM_STDCPPFS_NEEDED)
# Try the libc++ flag
set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lc++fs)
check_cxx_source_compiles("${code}" CXX_FILESYSTEM_CPPFS_NEEDED)
set(can_link ${CXX_FILESYSTEM_CPPFS_NEEDED})
endif()
endif()
if(can_link)
add_library(std::filesystem INTERFACE IMPORTED)
target_compile_features(std::filesystem INTERFACE cxx_std_17)
set(_found TRUE)
if(CXX_FILESYSTEM_NO_LINK_NEEDED)
# Nothing to add...
elseif(CXX_FILESYSTEM_STDCPPFS_NEEDED)
target_link_libraries(std::filesystem INTERFACE -lstdc++fs)
elseif(CXX_FILESYSTEM_CPPFS_NEEDED)
target_link_libraries(std::filesystem INTERFACE -lc++fs)
endif()
endif()
endif()
cmake_pop_check_state()
set(Filesystem_FOUND ${_found} CACHE BOOL "TRUE if we can compile and link a program using std::filesystem" FORCE)
if(Filesystem_FIND_REQUIRED AND NOT Filesystem_FOUND)
message(FATAL_ERROR "Cannot Compile simple program using std::filesystem")
endif()

57
cmake/debug_flags.cmake Normal file
View File

@ -0,0 +1,57 @@
# Set compiler flags for Debug builds.
# Only has an effect on GCC/Clang >= 5.0.
set(DEBUG_CXXFLAGS "")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"
AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5")
list(APPEND DEBUG_CXXFLAGS
"-Wall"
"-Wextra"
"-Wpedantic"
"-Wuninitialized"
"-Wshadow"
"-Wnon-virtual-dtor"
"-Wconversion"
"-Wsign-conversion"
"-Wold-style-cast"
"-Wzero-as-null-pointer-constant"
"-Wmissing-declarations"
"-Wcast-align"
"-Wunused"
"-Woverloaded-virtual"
"-Wdouble-promotion"
"-Wformat=2"
"-ftrapv"
"-fsanitize=undefined"
"-Og"
"-fno-omit-frame-pointer")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
list(APPEND DEBUG_CXXFLAGS
"-Wlogical-op"
"-Wuseless-cast")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6")
list(APPEND DEBUG_CXXFLAGS
"-Wmisleading-indentation"
"-Wduplicated-cond"
"-Wnull-dereference")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7")
list(APPEND DEBUG_CXXFLAGS
"-Wduplicated-branches")
endif()
endif()
endif()
add_compile_options("$<$<CONFIG:Debug>:${DEBUG_CXXFLAGS}>")
set(DEBUG_LDFLAGS
"-fsanitize=undefined")
# add_link_options was introduced in version 3.13.
if(${CMAKE_VERSION} VERSION_LESS 3.13)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${DEBUG_LDFLAGS}")
else()
add_link_options("$<$<CONFIG:Debug>:${DEBUG_LDFLAGS}>")
endif()
else()
message(STATUS
"No additional compiler flags were set, "
"because your compiler was not anticipated.")
endif()

View File

@ -1,7 +1,16 @@
// Author: tastytea <tastytea@tastytea.de>
// CC-0 / Public Domain
#include "xdgjson/src/xdgjson.hpp"
#include <mastodonpp/mastodonpp.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
#include <json/json.h>
#include <string>
#include <string_view>
#include <exception>
#include <iostream>
#include <sstream>
@ -11,12 +20,6 @@
#include <chrono>
#include <ctime>
#include <cstdint>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
#include <json/json.h>
#include <mastodon-cpp/easy/all.hpp>
#include "xdgjson/src/xdgjson.hpp"
namespace curlopts = curlpp::options;
using std::string;
@ -26,18 +29,18 @@ using std::endl;
using std::vector;
using std::chrono::system_clock;
using std::chrono::hours;
using std::uint_fast16_t;
using std::string_view;
const string version = "2019-04-18_1";
constexpr string_view version = "2020-05-16_1";
struct Holiday
struct holiday
{
string date;
string description;
vector<string> regions;
};
const string decode_state(const string &abbr)
string decode_state(const string &abbr)
{
const std::map<string, string> states =
{
@ -63,13 +66,11 @@ const string decode_state(const string &abbr)
{
return states.find(abbr)->second;
}
else
{
return abbr;
}
return abbr;
}
const string get_date(const system_clock::time_point &timepoint,
string get_date(const system_clock::time_point &timepoint,
const string &format)
{
std::time_t time = system_clock::to_time_t(timepoint);
@ -79,45 +80,38 @@ const string get_date(const system_clock::time_point &timepoint,
return string(buffer);
}
const string get_token(const string &instance)
string get_token(const string &instance)
{
Mastodon::API masto(instance, "");
uint_fast16_t ret = 0;
string client_id, client_secret, url;
string code, access_token;
ret = masto.register_app1("feiertagebot",
"urn:ietf:wg:oauth:2.0:oob",
"write:statuses",
"https://schlomp.space/tastytea/feiertagebot",
client_id,
client_secret,
url);
if (ret > 0)
mastodonpp::Instance server{instance, ""};
mastodonpp::Instance::ObtainToken token{server};
auto answer{token.step_1("feiertagebot",
"write:statuses",
"https://schlomp.space/tastytea/feiertagebot")};
if (!answer)
{
cerr << "Error " << std::to_string(ret) << endl;
cerr << "Error " << answer.error_message << endl;
return "";
}
cout << "Visit " << url << " to authorize this application.\n";
string code;
cout << "Visit " << answer << " to authorize this application.\n";
cout << "Insert code: ";
std::getline(std::cin, code);
ret = masto.register_app2(client_id,
client_secret,
"urn:ietf:wg:oauth:2.0:oob",
code,
access_token);
if (ret > 0)
answer = token.step_2(code);
if (!answer)
{
cerr << "Error " << std::to_string(ret) << endl;
cerr << "Error " << answer.error_message << endl;
return "";
}
return access_token;
return answer.body;
}
int main()
{
string instance, access_token;
string instance;
string access_token;
xdgjson config("feiertagebot.json");
config.read();
Json::Value &json = config.get_json();
@ -147,14 +141,14 @@ int main()
string year = get_date(system_clock::now(), "%Y");
string tomorrow = get_date(system_clock::now() + hours(24), "%Y-%m-%d");
string overmorrow = get_date(system_clock::now() + hours(48),
"%d.%m.%Y");
"%Y-%m-%d");
curlpp::Easy request;
std::stringstream ss;
request.setOpt<curlopts::Url>("https://feiertage-api.de/api/?jahr="
+ year);
request.setOpt<curlopts::UserAgent>("feiertagebot/" + version);
request.setOpt<curlopts::UserAgent>(string("feiertagebot/") += version);
request.setOpt<curlopts::FollowLocation>(true);
ss << request;
Json::Value root;
@ -164,7 +158,7 @@ int main()
for (const string &date : { tomorrow, overmorrow })
{
Holiday current_day;
holiday current_day;
for (Json::Value::const_iterator it_root = root.begin();
it_root != root.end(); ++it_root)
{
@ -189,7 +183,7 @@ int main()
current_day.regions.end(), "NATIONAL")
!= current_day.regions.end())
{
output += "Deutschland.\n";
output += "Deutschland.\n\n";
}
else
{
@ -206,22 +200,21 @@ int main()
}
output += decode_state(region);
}
output += ".\n";
output += ".\n\n";
}
}
if (!output.empty())
{
uint_fast16_t ret;
Mastodon::Easy masto(instance, access_token);
masto.exceptions(true);
Mastodon::Easy::Status status;
status.content(output + "\n#bot");
masto.send_post(status, ret);
if (ret > 0)
mastodonpp::Instance server{instance, access_token};
mastodonpp::Connection connection{server};
string status{output + "#bot"};
const auto answer{connection.post(mastodonpp::API::v1::statuses,
{{"status", output}})};
if (!answer)
{
cerr << "Error " << std::to_string(ret) << endl;
return ret;
cerr << "Error " << answer.error_message << endl;
return answer.curl_error_code;
}
}
}