Added support for configurable regular expressions
This commit is contained in:
parent
a1ef65b71c
commit
f3db10dc0a
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required (VERSION 3.7)
|
||||
project (expandurl-mastodon
|
||||
VERSION 0.8.2
|
||||
VERSION 0.9.0
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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());
|
||||
|
|
40
src/url.cpp
40
src/url.cpp
|
@ -77,23 +77,39 @@ const string expand(const string &url)
|
|||
|
||||
const string strip(const string &url)
|
||||
{
|
||||
using replace_pair = std::pair<const std::regex, const std::string>;
|
||||
using namespace std::regex_constants;
|
||||
Json::Value &config = configfile.get_json();
|
||||
string newurl = url;
|
||||
|
||||
const std::array<const replace_pair, 5> 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<const std::string, const std::string>;
|
||||
Json::Value &config = configfile.get_json();
|
||||
if (config["replace"].isNull())
|
||||
{
|
||||
const std::array<const replace_pair, 5> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue