From 43232d802beaa83704038f2d7918c986f40163ec Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 27 Nov 2018 04:35:14 +0100 Subject: [PATCH] Added support for mystery person (aka mystery man) --- CMakeLists.txt | 2 +- README.md | 6 +++++- src/image.cpp | 8 ++++++++ src/libravatarserv.cpp | 26 ++++++++++++++++++++------ src/libravatarserv.hpp | 3 ++- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e584d7f..166c08e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(libravatarserv - VERSION 0.3.2 + VERSION 0.4.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 1026fcb..0adc00e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ images tied to email or OpenID addresses. * MD5 hashes * SHA256 hashes * Variable image size (`s` or `size`) -* Default actions (`d` or `default`): 404, URL +* Default actions (`d` or `default`): 404, URL, mp/mm The default behaviour for unknown users is to return a 404 error. If a default image is put in the avatar directory, that is returned instead. The default @@ -47,6 +47,10 @@ variable `${LIBRAVATARSERV_DIR}` (until 0.3.0 `${AVATAR_DIR}`). The image files are named like your email address, no file extension. If you want to deliver a default image for unknown email addresses, name it `default`. +If you want to support "Mystery persons" (mp/mm) as default avatars, place a +file named `mp` in the avatar dir. You can use the [default libravatar mystery +person](https://seccdn.libravatar.org/mm/512.png), for example. + Test your setup on https://www.libravatar.org/tools/check/. ### Example diff --git a/src/image.cpp b/src/image.cpp index f1df5be..7a9835a 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -17,6 +17,7 @@ #include #include "libravatarserv.hpp" +using std::cout; using std::cerr; using std::endl; using global::avatar_dir; @@ -61,3 +62,10 @@ const Image image::get(const string &digest, const uint16_t size) return { error, img }; } + +void image::write(Image &image) +{ + cout << "Content-type: image/png\n\n"; + cout.flush(); // We need to flush before we use /dev/stdout directly. + image.image.write("/dev/stdout"); +} diff --git a/src/libravatarserv.cpp b/src/libravatarserv.cpp index df09080..cacb873 100644 --- a/src/libravatarserv.cpp +++ b/src/libravatarserv.cpp @@ -54,16 +54,14 @@ int main() http::Request avatar = http::parse_request(request); - image::Image answer = image::get(avatar.digest, avatar.size); - if (answer.error == 0) + image::Image image = image::get(avatar.digest, avatar.size); + if (image.error == 0) { - cout << "Content-type: image/png\n\n"; - cout.flush(); // We need to flush before we use /dev/stdout directly. - answer.image.write("/dev/stdout"); + image::write(image); } else { - cerr << "Error " << std::to_string(answer.error) << ": Could not open file.\n"; + cerr << "Error " << std::to_string(image.error) << ": Could not open file.\n"; if (avatar.fallback.empty()) { cout << "Status: 404 Not Found\n\n"; @@ -75,6 +73,22 @@ int main() 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"; + } + } } } diff --git a/src/libravatarserv.hpp b/src/libravatarserv.hpp index 477f93e..abc5465 100644 --- a/src/libravatarserv.hpp +++ b/src/libravatarserv.hpp @@ -69,11 +69,12 @@ namespace image // image.cpp { struct Image { - const uint8_t error; + uint8_t error; Magick::Image image; }; const Image get(const string &digest, const uint16_t size); + void write(Image &image); } #endif // LIBRAVATARSERV_HPP