This repository has been archived on 2021-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
backend/src/generators/rss.cpp

121 lines
3.0 KiB
C++

/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "rss.hpp"
#include "time.hpp"
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <string>
#include <string_view>
#include <vector>
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 << "</" << endtag << ">\n";
}
void write_rss(ostream &out, const vector<entry_type> &entries,
const vector<string> &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"(<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
)";
out << R"( <atom:link href=")" + selfurl
<< R"(" rel="self" type="application/rss+xml"/>)" << '\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: <item>s.
out << " </channel>\n</rss>\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<string_view> args(argv, argv + argc);
write_rss(cout, {}, {});
return 0;
}