gitea2rss/src/main.cpp

95 lines
2.5 KiB
C++
Raw Normal View History

2019-04-17 00:17:26 +02:00
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
2019-04-17 01:00:33 +02:00
#include <string>
2019-04-17 05:52:10 +02:00
#include <cstdlib>
2019-04-17 01:00:33 +02:00
#include <curlpp/cURLpp.hpp>
#include "version.hpp"
2019-04-20 04:47:53 +02:00
#include "gitea2rss.hpp"
2019-04-17 00:17:26 +02:00
using std::cout;
using std::cerr;
2019-04-17 01:00:33 +02:00
using std::string;
2019-04-17 02:51:40 +02:00
using std::chrono::system_clock;
2019-04-17 01:00:33 +02:00
bool cgi = false;
2019-04-17 00:17:26 +02:00
int main(int argc, char *argv[])
{
2019-04-17 05:52:10 +02:00
const char *envquery = std::getenv("QUERY_STRING");
string url;
2019-04-20 04:47:53 +02:00
curlpp::initialize();
2019-04-17 05:52:10 +02:00
if (envquery != nullptr)
{
const string query = envquery;
const char *envbaseurl = std::getenv("GITEA2RSS_BASEURL");
if (envbaseurl == nullptr)
{
cout << "Status: 500 Internal Server Error\n\n";
2019-04-17 07:12:08 +02:00
cerr << "Error: GITEA2RSS_BASEURL not set\n";
2019-04-17 05:52:10 +02:00
return 1;
}
cgi = true;
2019-04-17 07:12:08 +02:00
const size_t pos = query.find("repo=");
if (pos == std::string::npos)
{
cout << "Status: 400 Bad Request\n\n";
return 1;
}
2019-04-17 06:34:48 +02:00
url = string(envbaseurl) + "/" + query.substr(query.find('=', pos) + 1);
cout << "Content-Type: application/rss+xml\n";
2019-04-17 05:52:10 +02:00
}
else if (argc < 2)
2019-04-17 00:17:26 +02:00
{
cerr << "usage: " << argv[0] << " URL of Gitea project\n";
return 1;
}
2019-04-17 05:52:10 +02:00
else
{
url = argv[1];
}
2019-04-17 01:00:33 +02:00
2019-04-17 02:51:40 +02:00
const string now = strtime(system_clock::now());
cout <<
"<rss version=\"2.0\">\n"
" <channel>\n"
" <title>" << get_project(url) << " releases</title>\n"
2019-04-17 02:51:40 +02:00
" <link>" << url << "</link>\n"
" <description>Releases of " << get_repo(url) << "</description>\n"
2019-04-17 02:51:40 +02:00
" <generator>gitea2rss " << global::version << "</generator>\n"
" <lastBuildDate>" << now << "</lastBuildDate>\n";
uint8_t ret = releases(url);
if (ret != 0)
2019-04-17 01:23:23 +02:00
{
return ret;
2019-04-17 01:23:23 +02:00
}
2019-04-17 01:00:33 +02:00
2019-04-17 02:51:40 +02:00
cout <<
" </channel>\n"
"</rss>\n";
2019-04-20 04:47:53 +02:00
curlpp::terminate();
2019-04-17 00:17:26 +02:00
return 0;
}