forked from tastytea/libravatarserv
155 lines
4.9 KiB
C++
155 lines
4.9 KiB
C++
/* This file is part of libravatarserv.
|
|
* Copyright © 2018 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>
|
|
#include <Magick++/Geometry.h>
|
|
#include <identiconpp.hpp>
|
|
#include "version.hpp"
|
|
#include "libravatarserv.hpp"
|
|
|
|
using std::cout;
|
|
using std::cerr;
|
|
using std::endl;
|
|
|
|
// Global variables
|
|
std::map<const string, const string> hash::table;
|
|
fs::path settings::avatar_dir;
|
|
settings::Settings settings::settings;
|
|
|
|
int main()
|
|
{
|
|
cout << "Server: libravatarserv/" << global::version << endl;
|
|
|
|
const char *request = std::getenv("REQUEST_URI");
|
|
if (request == nullptr)
|
|
{
|
|
cout << "Status: 400 Bad Request\n\n";
|
|
cerr << "Error: ${REQUEST_URI} is empty.\n";
|
|
return 1;
|
|
}
|
|
http::Request avatar = http::parse_request(request);
|
|
if (!hash::is_valid(avatar.digest))
|
|
{
|
|
cout << "Status: 400 Bad Request\n\n";
|
|
cerr << "Error: Hash is invalid\n";
|
|
return 1;
|
|
}
|
|
|
|
if (!settings::find_avatar_dir())
|
|
{
|
|
cout << "Status: 503 Service Unavailable\n\n";
|
|
cerr << "Error: No avatars found.\n";
|
|
return 3;
|
|
}
|
|
hash::fill_table();
|
|
settings::read_settings();
|
|
|
|
image::Image image = image::get(avatar.digest, avatar.size);
|
|
if (image.error == 0)
|
|
{
|
|
image::write(image);
|
|
}
|
|
else
|
|
{
|
|
cerr << "Warning " << std::to_string(image.error)
|
|
<< ": 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
|
|
{
|
|
cerr << "Error: Mystery person not found.\n";
|
|
// Jump to last else
|
|
goto not_implemented;
|
|
}
|
|
}
|
|
else if (avatar.fallback.substr(0, 9) == "identicon")
|
|
{
|
|
try
|
|
{
|
|
Identiconpp identicon(4, 4, Identiconpp::algorithm::sigil,
|
|
"ffffffff",
|
|
{ // The 16 named colors specified in HTML 4.01
|
|
// minus white, silver and gray.
|
|
"000000ff", // Black
|
|
"ff0000ff", // Red
|
|
"800000ff", // Maroon
|
|
"ffff00ff", // Yellow
|
|
"808000ff", // Olive
|
|
"00ff00ff", // lime
|
|
"008000ff", // Green
|
|
"00ffffff", // Aqua
|
|
"008080ff", // Teal
|
|
"0000ffff", // Blue
|
|
"000080ff", // Navy
|
|
"ff00ffff", // Fuchsia
|
|
"800080ff", // Purple
|
|
});
|
|
Magick::Image img;
|
|
img = identicon.generate(avatar.digest, avatar.size);
|
|
img.magick("PNG");
|
|
image::Image image({ 0, img });
|
|
image::write(image);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
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";
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|