From 785cd7a35fb02af562a7ea4a7083260d78e942d3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 30 Nov 2018 07:00:37 +0100 Subject: [PATCH] Refactored settings-stuff into own file. --- src/hash.cpp | 4 +-- src/image.cpp | 5 ++- src/libravatarserv.cpp | 79 ++++------------------------------------ src/libravatarserv.hpp | 9 ++--- src/settings.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 81 deletions(-) create mode 100644 src/settings.cpp diff --git a/src/hash.cpp b/src/hash.cpp index a7e3fa9..828c513 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -22,7 +22,6 @@ #include #include "libravatarserv.hpp" -using global::avatar_dir; using namespace hash; const string hash::md5(const string &text) @@ -57,7 +56,8 @@ bool hash::fill_table() - for (const fs::path &path : fs::recursive_directory_iterator(avatar_dir)) + for (const fs::path &path : + fs::recursive_directory_iterator(settings::avatar_dir)) { if (fs::is_regular_file(path)) { diff --git a/src/image.cpp b/src/image.cpp index 1f96608..8ff5be1 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -23,7 +23,6 @@ using std::cout; using std::cerr; using std::endl; -using global::avatar_dir; using namespace image; const Image image::get(const string &digest, const uint16_t size) @@ -35,7 +34,7 @@ const Image image::get(const string &digest, const uint16_t size) const auto &entry = hash::table.find(digest); if (entry == hash::table.end()) { - filename = avatar_dir / "default"; + filename = settings::avatar_dir / "default"; if (!fs::is_regular_file(filename)) { cerr << "Error: User not found and no default image set.\n"; @@ -44,7 +43,7 @@ const Image image::get(const string &digest, const uint16_t size) } else { - filename = avatar_dir / entry->second; + filename = settings::avatar_dir / entry->second; } try diff --git a/src/libravatarserv.cpp b/src/libravatarserv.cpp index 53d3d61..6e26f3f 100644 --- a/src/libravatarserv.cpp +++ b/src/libravatarserv.cpp @@ -15,21 +15,17 @@ */ #include -#include -#include #include "version.hpp" #include "libravatarserv.hpp" using std::cout; using std::cerr; using std::endl; -using global::avatar_dir; -using global::settings; // Global variables std::map hash::table; -fs::path global::avatar_dir = ""; -global::Settings global::settings; +fs::path settings::avatar_dir = ""; +settings::Settings settings::settings; int main() { @@ -52,14 +48,14 @@ int main() return 1; } - if (!find_avatar_dir()) + if (!settings::find_avatar_dir()) { cout << "Status: 503 Service Unavailable\n\n"; cerr << "Error: No avatars found.\n"; return 3; } hash::fill_table(); - read_settings(); + settings::read_settings(); image::Image image = image::get(avatar.digest, avatar.size); if (image.error == 0) @@ -70,14 +66,14 @@ int main() { cerr << "Error " << std::to_string(image.error) << ": Could not open file.\n"; - if (global::settings.redirect_nouser) + if (settings::settings.redirect_nouser) { http::send_redirect(avatar); return 0; } if (avatar.fallback.empty()) { - avatar.fallback = settings.default_fallback; + avatar.fallback = settings::settings.default_fallback; } if (avatar.fallback.substr(0, 3) == "404") { @@ -120,7 +116,7 @@ int main() } else { - if (global::settings.redirect_nofallback) + if (settings::settings.redirect_nofallback) { http::send_redirect(avatar); } @@ -133,64 +129,3 @@ int main() return 0; } - -bool find_avatar_dir() -{ - const char *envdir = std::getenv("LIBRAVATARSERV_DIR"); - if (envdir == nullptr) - { - // 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; - } - 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; -} - -void read_settings() -{ - const char *env = std::getenv("LIBRAVATARSERV_DEFAULT_FALLBACK"); - if (env != nullptr) - { - global::settings.default_fallback = env; - } - - env = std::getenv("LIBRAVATARSERV_REDIRECT_NOFALLBACK"); - if (env != nullptr && env[0] == '1') - { - global::settings.redirect_nofallback = true; - } - - env = std::getenv("LIBRAVATARSERV_REDIRECT_NOUSER"); - if (env != nullptr && env[0] == '1') - { - global::settings.redirect_nouser = true; - } -} diff --git a/src/libravatarserv.hpp b/src/libravatarserv.hpp index 882fc1b..97eb227 100644 --- a/src/libravatarserv.hpp +++ b/src/libravatarserv.hpp @@ -36,11 +36,9 @@ using std::uint16_t; using std::uint8_t; int main(); -bool find_avatar_dir(); -void read_settings(); -namespace global -{ +namespace settings +{ // settings.cpp extern fs::path avatar_dir; extern struct Settings { @@ -48,6 +46,9 @@ namespace global bool redirect_nofallback = false; bool redirect_nouser = false; } settings; + + bool find_avatar_dir(); + void read_settings(); } namespace http // http.cpp diff --git a/src/settings.cpp b/src/settings.cpp new file mode 100644 index 0000000..794e888 --- /dev/null +++ b/src/settings.cpp @@ -0,0 +1,82 @@ +/* This file is part of libravatarserv. + * Copyright © 2018 tastytea + * + * 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 . + */ + +#include +#include +#include "libravatarserv.hpp" + +using namespace settings; + +bool settings::find_avatar_dir() +{ + const char *envdir = std::getenv("LIBRAVATARSERV_DIR"); + if (envdir == nullptr) + { + // 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; + } + 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; +} + +void settings::read_settings() +{ + const char *env = std::getenv("LIBRAVATARSERV_DEFAULT_FALLBACK"); + if (env != nullptr) + { + settings.default_fallback = env; + } + + env = std::getenv("LIBRAVATARSERV_REDIRECT_NOFALLBACK"); + if (env != nullptr && env[0] == '1') + { + settings.redirect_nofallback = true; + } + + env = std::getenv("LIBRAVATARSERV_REDIRECT_NOUSER"); + if (env != nullptr && env[0] == '1') + { + settings.redirect_nouser = true; + } +}