libravatarserv/src/libravatarserv.cpp

157 lines
4.8 KiB
C++
Raw Normal View History

2018-11-24 11:00:07 +01:00
/* This file is part of libravatarserv.
2019-02-28 19:49:46 +01:00
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
2018-11-24 11:00:07 +01:00
*
* 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>
#include <Magick++/Geometry.h>
#include <identiconpp.hpp>
#include "version.hpp"
2018-11-25 06:00:45 +01:00
#include "libravatarserv.hpp"
2018-11-24 11:00:07 +01:00
using std::cout;
2018-11-25 06:00:45 +01:00
using std::cerr;
using std::endl;
2018-11-24 11:00:07 +01:00
2018-11-25 06:00:45 +01:00
// Global variables
std::map<const string, const string> hash::table;
fs::path settings::avatar_dir;
settings::Settings settings::settings;
2018-11-25 06:00:45 +01:00
int main()
2018-11-24 11:00:07 +01:00
{
2018-11-26 05:00:57 +01:00
cout << "Server: libravatarserv/" << global::version << endl;
const char *request = std::getenv("REQUEST_URI");
if (request == nullptr)
2018-11-25 06:00:45 +01:00
{
cout << "Status: 400 Bad Request\n\n";
2018-11-25 06:00:45 +01:00
cerr << "Error: ${REQUEST_URI} is empty.\n";
return 1;
}
2018-11-28 17:29:08 +01:00
http::Request avatar = http::parse_request(request);
2018-11-30 06:48:45 +01:00
if (!hash::is_valid(avatar.digest))
{
cout << "Status: 400 Bad Request\n\n";
cerr << "Error: Hash is invalid\n";
return 1;
}
2018-11-25 06:00:45 +01:00
if (!settings::find_avatar_dir())
2018-11-25 06:00:45 +01:00
{
cout << "Status: 503 Service Unavailable\n\n";
2018-11-25 06:00:45 +01:00
cerr << "Error: No avatars found.\n";
return 3;
}
hash::fill_table();
settings::read_settings();
2018-11-24 12:40:49 +01:00
image::Image image = image::get(avatar.digest, avatar.size);
if (image.error == 0)
2018-11-25 06:00:45 +01:00
{
image::write(image);
2018-11-25 06:00:45 +01:00
}
else
{
2018-12-26 05:47:06 +01:00
cerr << "Warning " << std::to_string(image.error)
2018-11-28 16:13:42 +01:00
<< ": Could not open file.\n";
if (settings::settings.redirect_nouser)
{
http::send_redirect(avatar);
return 0;
}
if (avatar.fallback.empty())
{
avatar.fallback = settings::settings.default_fallback;
}
if (avatar.fallback.substr(0, 3) == "404")
{
cout << "Status: 404 Not Found\n\n";
}
else if (avatar.fallback.substr(0, 4) == "http")
{
cout << "Status: 307 Temporary Redirect\n";
cout << "Location: " << avatar.fallback << endl << endl;
}
else if (avatar.fallback.substr(0, 2) == "mp" ||
avatar.fallback.substr(0, 2) == "mm")
{
// MD5 hash of 'mp'
image = image::get("1f2dfa567dcf95833eddf7aec167fec7", avatar.size);
if (image.error == 0)
{
image::write(image);
}
else
{
2018-11-27 09:59:02 +01:00
cerr << "Error: Mystery person not found.\n";
// Jump to last else
goto not_implemented;
2018-11-27 09:59:02 +01:00
}
}
else if (avatar.fallback.substr(0, 9) == "identicon" ||
avatar.fallback.substr(0, 5) == "retro")
2018-11-27 09:59:02 +01:00
{
try
2018-11-27 09:59:02 +01:00
{
uint8_t padwidth = avatar.size / 10;
if (avatar.size < 60)
{
padwidth = 0;
}
else if (padwidth < 10)
{
padwidth = 10;
}
Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil,
"fefefeff",
{ // The same colors ivatar uses.
"2d4fffff", // Blue
"feb42cff", // Yellow
"e279eaff", // Bright pink
"1eb3fdff", // Cyan
"E84D41ff", // Red
"31CB73ff", // Green
"8D45AAff" // Dark pink
},
{ padwidth, padwidth });
2019-02-20 03:03:09 +01:00
image.image = identicon.generate(avatar.digest, avatar.size);
image.image.magick("PNG");
2018-11-27 09:59:02 +01:00
image::write(image);
}
catch (const std::exception &e)
2018-11-27 09:59:02 +01:00
{
cout << "Status: 500 Internal Server Error\n\n";
cerr << "Error: Couldn't generate identicon ("
<< e.what() << ").\n";
}
}
else
{
not_implemented:
if (settings::settings.redirect_nofallback)
{
http::send_redirect(avatar);
}
else
{
cout << "Status: 501 Not Implemented\n\n";
}
}
2018-11-25 06:00:45 +01:00
}
2018-11-24 12:40:49 +01:00
2018-11-24 11:00:07 +01:00
return 0;
}