diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cc041c..4897436 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (expandurl-mastodon - VERSION 0.8.2 + VERSION 0.9.0 LANGUAGES CXX ) diff --git a/src/expandurl-mastodon.hpp b/src/expandurl-mastodon.hpp index 62bccc0..56c72af 100644 --- a/src/expandurl-mastodon.hpp +++ b/src/expandurl-mastodon.hpp @@ -63,6 +63,15 @@ const string expand(const string &url); */ const string strip(const string &url); +/*! + * @brief Initialize replacements for URLs + * + * If no replacements are found in the config file, a default list is + * inserted. + * + */ +const void init_replacements(); + class Listener { diff --git a/src/main.cpp b/src/main.cpp index 88b584c..d79486d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,13 +52,15 @@ void signal_handler(int signum) int main(int argc, char *argv[]) { + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + if (!configfile.read()) { syslog(LOG_WARNING, "Could not open %s.", configfile.get_filepath().c_str()); } + init_replacements(); - signal(SIGINT, signal_handler); - signal(SIGTERM, signal_handler); curlpp::initialize(); openlog("expandurl-mastodon", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_LOCAL1); syslog(LOG_NOTICE, "Program started by user %d", getuid()); diff --git a/src/url.cpp b/src/url.cpp index c353ad2..c1b9977 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -77,23 +77,39 @@ const string expand(const string &url) const string strip(const string &url) { - using replace_pair = std::pair; using namespace std::regex_constants; + Json::Value &config = configfile.get_json(); string newurl = url; - const std::array replace_array = - {{ - { std::regex("[\\?&]utm_[^&]+", icase), "" }, // Google - { std::regex("[\\?&]wtmc=[^&]+", icase), "" }, // Twitter? - { std::regex("[\\?&]__twitter_impression=[^&]+", icase), "" }, // Twitter? - { std::regex("[\\?&]wt_zmc=[^&]+", icase), "" }, // Twitter? - { std::regex("//amp\\.", icase), "//" } // AMP - }}; - - for (const replace_pair &pair : replace_array) + for (auto it = config["replace"].begin(); it != config["replace"].end(); + ++it) { - newurl = std::regex_replace(newurl, pair.first, pair.second); + newurl = std::regex_replace(newurl, + std::regex(it.name(), icase), + (*it).asString()); } return newurl; } + +const void init_replacements() +{ + using replace_pair = std::pair; + Json::Value &config = configfile.get_json(); + if (config["replace"].isNull()) + { + const std::array replace_array = + {{ + { "[\\?&]utm_[^&]+", "" }, // Google + { "[\\?&]wtmc=[^&]+", "" }, // Twitter? + { "[\\?&]__twitter_impression=[^&]+", "" }, // Twitter? + { "[\\?&]wt_zmc=[^&]+", "" }, // Twitter? + { "//amp\\.", "//" } // AMP + }}; + + for (const replace_pair &pair : replace_array) + { + config["replace"][pair.first] = pair.second; + } + } +}