/* 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 "html.hpp" #include "cgi.hpp" #include "config.hpp" #include "files.hpp" #include "fs-compat.hpp" #include "git.hpp" #include "json.hpp" #include #include #include #include #include #include #include #include namespace FediBlock::html { using std::cerr; using std::cout; using std::exception; using std::ifstream; using std::runtime_error; using std::string_view; using std::stringstream; using std::vector; void write_html(ostream &out, const vector &entries) { out << R"( FediBlock – Blocklist

Blocklist

)"; for (const auto &entry : entries) { out << "\n"; out << "
\n"; out << " " << entry.report_time.substr(0, 10) << ": " << entry.instance << "\n"; out << R"( Hyperlink to this entry)" << '\n'; out << "

" << cgi::text2html(entry.description) << "

\n"; out << "

Tags: "; for (const auto &tag : entry.tags) { if (tag != *(entry.tags.begin())) { out << ", "; } out << tag; } out << "

\n"; out << " Receipts:\n
    \n"; for (const auto &receipt : entry.receipts) { out << "
  • " << receipt << "
  • \n"; } out << "
\n"; if (!entry.screenshot_filepaths.empty()) { out << " Screenshots:
\n" << "
    \n"; for (const auto &screenshot : entry.screenshot_filepaths) { out << "
  • " << screenshot << "
  • \n"; } out << "
\n"; } out << "
\n"; } out << R"( )"; } void copy_screenshots(const fs::path &directory) { const auto dir_iter{fs::directory_iterator(files::get_cachedir() / "repo")}; 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); } } } // namespace FediBlock::html int main(int argc, char *argv[]) { using namespace FediBlock; using namespace FediBlock::html; const vector args(argv, argv + argc); if (argc <= 1) { cerr << "usage: " << args[0] << " \n"; return 1; } const string_view target_dir{args[1]}; git::init(true); bool remove_lockfile{false}; try { 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)}; write_html(cout, entries); copy_screenshots(target_dir); } catch (const exception &e) { cerr << "Error: " << e.what() << '\n'; } git::cleanup(remove_lockfile); return 0; }