libravatarserv/src/libravatarserv.cpp

155 lines
4.9 KiB
C++
Raw Normal View History

2018-11-24 11:00:07 +01:00
/* 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"
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'
2018-11-27 09:59:02 +01:00
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")
{
try
2018-11-27 09:59:02 +01:00
{
2018-12-27 00:18:24 +01:00
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);
2018-12-27 06:55:16 +01:00
img.magick("PNG");
image::Image image({ 0, img });
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;
}