libravatarserv/src/libravatarserv.cpp

134 lines
3.6 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>
2018-11-26 02:20:33 +01:00
#include <cstdlib>
2018-11-25 06:00:45 +01:00
#include <basedir.h>
#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;
using global::avatar_dir;
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 global::avatar_dir = "";
int main()
2018-11-24 11:00:07 +01:00
{
2018-11-26 05:00:57 +01:00
cout << "Server: libravatarserv/" << global::version << endl;
cout << "Cache-Control: max-age=86400\n";
cout << "Connection: close\n";
const char *request = std::getenv("REQUEST_URI");
if (request == nullptr)
2018-11-25 06:00:45 +01:00
{
cout << "Status: 404 Not Found\n\n";
cerr << "Error: ${REQUEST_URI} is empty.\n";
return 1;
}
if (!find_avatar_dir())
{
cout << "Status: 404 Not Found\n\n";
cerr << "Error: No avatars found.\n";
return 3;
}
hash::fill_table();
2018-11-24 12:40:49 +01:00
http::Request avatar = http::parse_request(request);
2018-11-25 06:00:45 +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
{
cerr << "Error " << std::to_string(image.error) << ": Could not open file.\n";
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
{
cout << "Status: 404 Not Found\n\n";
cerr << "Mystery person not found.\n";
}
}
else
{
// If fallback is empty or unknown
cout << "Status: 404 Not Found\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;
}
2018-11-25 06:00:45 +01:00
bool find_avatar_dir()
{
const char *envdir = std::getenv("LIBRAVATARSERV_DIR");
if (envdir == nullptr)
2018-11-25 06:00:45 +01:00
{
// TODO: Remove AVATAR_DIR in 1.0.0
// DEPRECATED because it is potentially used by another program
envdir = std::getenv("AVATAR_DIR");
}
if (envdir != nullptr && fs::is_directory(envdir))
{
avatar_dir = envdir;
2018-11-25 06:00:45 +01:00
}
else
{
xdgHandle xdg;
xdgInitHandle(&xdg);
auto data_dirs = xdgDataDirectories(&xdg);
const uint8_t size = sizeof(data_dirs) / sizeof(*data_dirs);
for (uint8_t index = 0; index <= size; ++index)
{
const string searchdir = data_dirs[index] + string("/libravatarserv");
if (fs::is_directory(searchdir))
{
avatar_dir = searchdir;
break;
}
}
xdgWipeHandle(&xdg);
if (avatar_dir.empty())
{
return false;
}
}
return true;
}