From c564e79dbce6f9362ce605cbc3ffd77c430de0ff Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 18 Jan 2021 08:19:22 +0100 Subject: [PATCH] Add spam filter. --- src/cgi.cpp | 26 ++++++++++++++++++++++++++ src/cgi.hpp | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/cgi.cpp b/src/cgi.cpp index 581708a..255e096 100644 --- a/src/cgi.cpp +++ b/src/cgi.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -83,6 +84,11 @@ entry_type parse_formdata() entry.description = cgi("description"); entry.report_time = time::to_string(system_clock::now()); + if (is_spam(entry)) + { + throw SpamException{}; + } + std::uint8_t screenshot_counter{1}; for (const auto &screenshot : cgi.getFiles()) { @@ -201,6 +207,26 @@ bool captcha_valid(std::uint8_t id, const string_view answer) return false; } +bool is_spam(const entry_type &entry) +{ + using std::regex; + using std::regex_search; + + std::ifstream file(files::get_datadir() / "spamfilter.lst"); + if (file.good()) + { + string line; + while (getline(file, line)) + { + if (regex_search(entry.description, regex{line})) + { + return true; + } + } + } + return false; +} + } // namespace FediBlock::cgi const char *SpamException::what() const noexcept diff --git a/src/cgi.hpp b/src/cgi.hpp index 2151bb2..8f30302 100644 --- a/src/cgi.hpp +++ b/src/cgi.hpp @@ -56,6 +56,8 @@ string text2html(string text); // Check if the answer matches the solution we have for the id. bool captcha_valid(std::uint8_t id, string_view answer); +bool is_spam(const entry_type &entry); + } // namespace FediBlock::cgi class SpamException : public std::exception