From 8325da36244063b3a6acfc98cd4faaa9098f69ad Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 5 Jul 2020 09:06:45 +0200 Subject: [PATCH] [WIP] RSS support. --- src/generators/CMakeLists.txt | 11 +++- src/generators/rss.cpp | 120 ++++++++++++++++++++++++++++++++++ src/generators/rss.hpp | 64 ++++++++++++++++++ 3 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 src/generators/rss.cpp create mode 100644 src/generators/rss.hpp diff --git a/src/generators/CMakeLists.txt b/src/generators/CMakeLists.txt index 561c230..3e45521 100644 --- a/src/generators/CMakeLists.txt +++ b/src/generators/CMakeLists.txt @@ -1,6 +1,6 @@ include(GNUInstallDirs) -add_executable(gen_html html.cpp) +add_executable(gen_html "html.cpp") target_link_libraries(gen_html PRIVATE fediblock) @@ -8,3 +8,12 @@ target_link_libraries(gen_html install(TARGETS gen_html RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" RENAME ${PROJECT_NAME}-gen_html) + +add_executable(gen_rss "rss.cpp") + +target_link_libraries(gen_rss + PRIVATE fediblock) + +install(TARGETS gen_rss + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + RENAME ${PROJECT_NAME}-gen_rss) diff --git a/src/generators/rss.cpp b/src/generators/rss.cpp new file mode 100644 index 0000000..e13fd26 --- /dev/null +++ b/src/generators/rss.cpp @@ -0,0 +1,120 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * 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, version 3. + * + * 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 . + */ + +#include "rss.hpp" +#include "time.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace FediBlock::rss +{ + +using std::getenv; +using std::ostream; +using std::string; +using std::string_view; +using std::uint8_t; +using std::chrono::system_clock; + +void write_line(ostream &out, const uint8_t spaces, const string_view rsstag, + const string_view value) +{ + string endtag; + // If there is a space in the rsstag, use only the part up until the space + // for the ending rsstag. + const size_t pos = rsstag.find(' '); + if (pos == string_view::npos) + { + endtag = rsstag; + } + else + { + endtag = rsstag.substr(0, pos); + } + + out << string(spaces, ' '); + out << '<' << rsstag << '>' << value << "\n"; +} + +void write_rss(ostream &out, const vector &entries, + const vector &tags) +{ + string selfurl; + char *env{getenv("HTTPS")}; + if (env == nullptr || string(env) != "on") + { + selfurl = "http://"; + } + else + { + selfurl = "https://"; + } + env = getenv("SERVER_NAME"); + if (env != nullptr) + { + selfurl += env; + } + env = getenv("REQUEST_URI"); + if (env != nullptr) + { + selfurl += env; + } + + constexpr string_view blocklist_url{ + "https://fediblock-test.tastytea.de/blocklist/"}; + constexpr string_view rss_time_format{"%a, %d %b %Y %T %z"}; + + out << R"( + +)"; + out << R"( )" << '\n'; + write_line(out, 4, "title", "FediBlock: newest entries"); + write_line(out, 4, "link", blocklist_url); + write_line(out, 4, "description", "FediBlock: Newest entries."); + write_line(out, + 4, + "lastBuildDate", + time::to_string(system_clock::now(), rss_time_format)); + + // TODO: s. + + out << " \n\n"; +} + +} // namespace FediBlock::rss + +using std::cout; +using std::string_view; +using std::vector; + +using namespace FediBlock::rss; + +int main(int argc, char *argv[]) +{ + const vector args(argv, argv + argc); + + write_rss(cout, {}, {}); + + return 0; +} diff --git a/src/generators/rss.hpp b/src/generators/rss.hpp new file mode 100644 index 0000000..532dbc2 --- /dev/null +++ b/src/generators/rss.hpp @@ -0,0 +1,64 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * 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, version 3. + * + * 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 . + */ + +#ifndef FEDIBLOCK_BACKEND_RSS_HPP +#define FEDIBLOCK_BACKEND_RSS_HPP + +#include "types.hpp" + +#include +#include +#include +#include +#include + +namespace FediBlock::rss +{ + +using std::ostream; +using std::string; +using std::string_view; +using std::uint8_t; +using std::vector; + +/*! + * @brief Write a line of RSS. + * + * @param out The ostream to use for output. + * @param spaces Preceding spaces. + * @param rsstag RSS tag. + * @param value Content of the tag. + * + * @since 0.2.0 + */ +void write_line(ostream &out, uint8_t spaces, string_view rsstag, + string_view value); + +/*! + * @brief Convert entries to RSS. + * + * @param out The ostream to use for output. + * @param entries The entries to write. + * @param tags Only write entries with these tags (logical or). + * + * @since 0.2.0 + */ +void write_rss(ostream &out, const vector &entries, + const vector &tags); + +} // namespace FediBlock::rss + +#endif // FEDIBLOCK_BACKEND_RSS_HPP