2020-07-02 08:09:58 +02:00
|
|
|
|
/* 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 "html.hpp"
|
2020-10-18 01:27:29 +02:00
|
|
|
|
#include "cgi.hpp"
|
2020-10-27 17:50:14 +01:00
|
|
|
|
#include "config.hpp"
|
2020-07-02 08:09:58 +02:00
|
|
|
|
#include "files.hpp"
|
|
|
|
|
#include "fs-compat.hpp"
|
|
|
|
|
#include "git.hpp"
|
|
|
|
|
#include "json.hpp"
|
|
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <stdexcept>
|
2020-07-02 09:47:56 +02:00
|
|
|
|
#include <string_view>
|
|
|
|
|
#include <vector>
|
2020-07-02 08:09:58 +02:00
|
|
|
|
|
2020-10-29 14:45:53 +01:00
|
|
|
|
namespace FediBlock::html
|
2020-07-02 08:09:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using std::cerr;
|
|
|
|
|
using std::cout;
|
|
|
|
|
using std::exception;
|
|
|
|
|
using std::ifstream;
|
2020-10-14 03:40:45 +02:00
|
|
|
|
using std::runtime_error;
|
2020-07-02 09:47:56 +02:00
|
|
|
|
using std::string_view;
|
2020-07-02 08:09:58 +02:00
|
|
|
|
using std::stringstream;
|
2020-07-02 09:47:56 +02:00
|
|
|
|
using std::vector;
|
2020-07-02 08:09:58 +02:00
|
|
|
|
|
2020-07-02 09:47:56 +02:00
|
|
|
|
void write_html(ostream &out, const vector<entry_type> &entries)
|
|
|
|
|
{
|
|
|
|
|
out << R"(<!doctype html>
|
2020-10-17 07:12:44 +02:00
|
|
|
|
<html lang="en">
|
2020-07-02 09:47:56 +02:00
|
|
|
|
<head>
|
2020-10-14 05:53:20 +02:00
|
|
|
|
<title>FediBlock – Blocklist</title>
|
2020-07-02 09:47:56 +02:00
|
|
|
|
<meta charset="utf-8">
|
2020-07-04 09:43:45 +02:00
|
|
|
|
<meta name="robots" content="noindex, nofollow">
|
2020-10-18 07:56:04 +02:00
|
|
|
|
<style>
|
2020-10-18 08:15:47 +02:00
|
|
|
|
.small
|
|
|
|
|
{
|
2020-10-19 16:15:53 +02:00
|
|
|
|
font-size: small;
|
2020-10-18 08:15:47 +02:00
|
|
|
|
}
|
|
|
|
|
img
|
|
|
|
|
{
|
2020-10-19 16:15:53 +02:00
|
|
|
|
max-height: 10em;
|
|
|
|
|
max-width: 40ch;
|
2020-10-18 08:15:47 +02:00
|
|
|
|
}
|
2020-10-19 05:41:43 +02:00
|
|
|
|
summary
|
|
|
|
|
{
|
|
|
|
|
font-size: large;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
2020-10-19 07:07:23 +02:00
|
|
|
|
details:nth-of-type(even)
|
|
|
|
|
{
|
2020-10-19 16:15:53 +02:00
|
|
|
|
background-color: #0001;
|
2020-10-19 07:07:23 +02:00
|
|
|
|
}
|
2020-10-18 07:56:04 +02:00
|
|
|
|
</style>
|
2020-10-19 11:47:47 +02:00
|
|
|
|
<link rel="alternate" type="application/rss+xml"
|
2020-10-19 16:15:53 +02:00
|
|
|
|
title="The newest FediBlock entries. All tags." href="/rss">
|
2020-07-02 09:47:56 +02:00
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>Blocklist</h1>
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
|
|
for (const auto &entry : entries)
|
|
|
|
|
{
|
|
|
|
|
out << "\n";
|
2020-10-18 07:56:04 +02:00
|
|
|
|
out << " <details id=\"" << entry.instance << "\">\n";
|
2020-10-19 07:21:24 +02:00
|
|
|
|
out << " <summary>" << entry.report_time.substr(0, 10) << ": "
|
|
|
|
|
<< entry.instance << "</summary>\n";
|
2020-10-18 07:56:04 +02:00
|
|
|
|
out << R"( <a href="#)" << entry.instance
|
|
|
|
|
<< R"(" class="small">Hyperlink to this entry</a>)" << '\n';
|
2020-10-18 01:27:29 +02:00
|
|
|
|
out << " <p>" << cgi::text2html(entry.description) << "</p>\n";
|
2020-07-02 09:47:56 +02:00
|
|
|
|
out << " <p><strong>Tags:</strong> ";
|
|
|
|
|
for (const auto &tag : entry.tags)
|
|
|
|
|
{
|
|
|
|
|
if (tag != *(entry.tags.begin()))
|
|
|
|
|
{
|
|
|
|
|
out << ", ";
|
|
|
|
|
}
|
|
|
|
|
out << tag;
|
|
|
|
|
}
|
|
|
|
|
out << "</p>\n";
|
2020-10-17 07:07:48 +02:00
|
|
|
|
out << " <strong>Receipts:</strong>\n <ul>\n";
|
2020-07-02 09:47:56 +02:00
|
|
|
|
for (const auto &receipt : entry.receipts)
|
|
|
|
|
{
|
2020-10-17 07:07:48 +02:00
|
|
|
|
out << " <li><a href=\"" << receipt << "\">" << receipt
|
2020-10-17 05:26:17 +02:00
|
|
|
|
<< "</a></li>\n";
|
2020-07-02 09:47:56 +02:00
|
|
|
|
}
|
2020-10-17 07:07:48 +02:00
|
|
|
|
out << " </ul>\n";
|
2020-07-02 09:47:56 +02:00
|
|
|
|
if (!entry.screenshot_filepath.empty())
|
|
|
|
|
{
|
2020-07-02 11:43:17 +02:00
|
|
|
|
out << " <p><strong>Screenshot:</strong><br>"
|
|
|
|
|
<< "<a href=\"" << entry.screenshot_filepath << "\"><img src=\""
|
2020-10-18 08:15:47 +02:00
|
|
|
|
<< entry.screenshot_filepath << R"("></a></p>)" << '\n';
|
2020-07-02 09:47:56 +02:00
|
|
|
|
}
|
|
|
|
|
out << " </details>\n";
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 03:27:36 +02:00
|
|
|
|
out << R"(
|
|
|
|
|
<p class="footer" style="text-align: right;">
|
2020-10-27 17:50:14 +01:00
|
|
|
|
<a href="https://)"
|
|
|
|
|
<< config::forge_domain << "/" << config::forge_org
|
|
|
|
|
<< R"(">Sourcecode</a> licensed under the
|
2020-10-17 03:27:36 +02:00
|
|
|
|
<a href="https://www.gnu.org/licenses/agpl-3.0.html">AGPL-3.0-only</a>.
|
|
|
|
|
</p>
|
2020-10-18 07:56:04 +02:00
|
|
|
|
|
2020-10-18 09:10:19 +02:00
|
|
|
|
<script>
|
2020-10-18 07:56:04 +02:00
|
|
|
|
// Open details when called with ID.
|
|
|
|
|
function open_details()
|
|
|
|
|
{
|
|
|
|
|
const hash = decodeURI(location.hash.substring(1));
|
|
|
|
|
if(hash)
|
|
|
|
|
{
|
|
|
|
|
const details = document.getElementById(hash);
|
|
|
|
|
if(details && details.tagName.toLowerCase() === 'details')
|
|
|
|
|
{
|
|
|
|
|
details.open = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
window.addEventListener('hashchange', open_details);
|
|
|
|
|
open_details();
|
|
|
|
|
</script>
|
2020-10-17 03:27:36 +02:00
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
)";
|
2020-07-02 09:47:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void copy_screenshots(const fs::path &directory)
|
|
|
|
|
{
|
2020-10-14 03:40:45 +02:00
|
|
|
|
const auto dir_iter{fs::directory_iterator(files::get_cachedir() / "repo")};
|
2020-07-02 09:47:56 +02:00
|
|
|
|
for (const fs::path &path : dir_iter)
|
|
|
|
|
{
|
|
|
|
|
if (path.filename().string()[0] == '.')
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (path.extension() == ".json")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs::copy_file(path, directory / path.filename(),
|
|
|
|
|
fs::copy_options::skip_existing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 14:45:53 +01:00
|
|
|
|
} // namespace FediBlock::html
|
|
|
|
|
|
|
|
|
|
using namespace FediBlock;
|
|
|
|
|
using namespace FediBlock::html;
|
2020-07-02 08:09:58 +02:00
|
|
|
|
|
2020-07-02 09:47:56 +02:00
|
|
|
|
int main(int argc, char *argv[])
|
2020-07-02 08:09:58 +02:00
|
|
|
|
{
|
2020-07-02 09:47:56 +02:00
|
|
|
|
const vector<string_view> args(argv, argv + argc);
|
|
|
|
|
if (argc <= 1)
|
|
|
|
|
{
|
|
|
|
|
cerr << "usage: " << args[0] << " <target directory>\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
const string_view target_dir{args[1]};
|
|
|
|
|
|
2020-10-14 03:40:45 +02:00
|
|
|
|
git::init(true);
|
2020-07-02 08:09:58 +02:00
|
|
|
|
|
2020-10-14 03:40:45 +02:00
|
|
|
|
bool remove_lockfile{false};
|
2020-07-02 08:09:58 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-10-14 03:40:45 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
git::update_cached_repo();
|
|
|
|
|
remove_lockfile = true;
|
|
|
|
|
}
|
|
|
|
|
catch (const runtime_error &)
|
|
|
|
|
{
|
|
|
|
|
// Ignore, use old version of repo.
|
|
|
|
|
}
|
|
|
|
|
const auto entries{files::read_json_files(true)};
|
2020-07-02 09:47:56 +02:00
|
|
|
|
write_html(cout, entries);
|
|
|
|
|
copy_screenshots(target_dir);
|
2020-07-02 08:09:58 +02:00
|
|
|
|
}
|
|
|
|
|
catch (const exception &e)
|
|
|
|
|
{
|
|
|
|
|
cerr << "Error: " << e.what() << '\n';
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 03:40:45 +02:00
|
|
|
|
git::cleanup(remove_lockfile);
|
2020-07-02 08:09:58 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|