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/main.cpp

101 lines
2.9 KiB
C++

/* This file is part of FediBlock-backend.
* Copyright © 2020, 2021 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 "cgi.hpp"
#include "config.hpp"
#include "files.hpp"
#include "fs-compat.hpp"
#include "git.hpp"
#include "gitea.hpp"
#include "json.hpp"
#include "types.hpp"
#include <git2/global.h>
#include <cstdlib>
#include <exception>
#include <iostream>
using std::cerr;
using std::cout;
using std::exception;
using std::getenv;
using namespace FediBlock;
int main()
{
cout << "Content-Type: text/plain; charset=utf-8\r\n\r\n";
char *env{getenv("REQUEST_METHOD")};
if (env != nullptr && string(env) != "POST")
{
return 0;
}
git::init(true);
gitea::init();
try
{
const entry_type entry{cgi::parse_formdata()};
if (entry.instance.empty() || entry.tags.empty()
|| entry.receipts.empty() || entry.description.empty())
{
// Probably spam, since the input fields are marked as required.
cout << "One of the required fields (Instance, Tags, Receipts, "
"Description) was not filled.\r\n"
<< "Please use the back button, complete the form and submit "
"again.\r\n";
return 1;
}
git::update_cached_repo();
git::create_branch();
git::commit(entry);
git::push();
gitea::pull_request(git::get_branch_name(), entry);
cout << "Created pull request: <https://" << config::forge_domain << "/"
<< config::forge_org << "/" << config::forge_repo_data << "/pulls/"
<< gitea::get_last_pr_number() << ">.\r\n";
cout << "Your entry needs to be approved before it appears on the "
"website. You can subscribe to notifications or add comments "
"at the URL above.\r\n";
files::remove_tmpdir();
}
catch (const SpamException &e)
{
cerr << e.what() << '\n';
cout << e.what() << "\r\n";
}
catch (const exception &e)
{
cerr << e.what() << '\n';
cout << "An error happened: " << e.what() << "\r\n";
cout << "Please report it at <https://" << config::forge_domain << "/"
<< config::forge_org << "/" << config::forge_repo_backend
<< "/issues>.\r\n";
}
gitea::cleanup();
git::cleanup(true);
return 0;
}