Add spam filter.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2021-01-18 08:19:22 +01:00
parent 62f6cfdaac
commit c564e79dbc
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 28 additions and 0 deletions

View File

@ -35,6 +35,7 @@
#include <iostream>
#include <iterator>
#include <map>
#include <regex>
#include <sstream>
#include <stdexcept>
#include <string>
@ -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

View File

@ -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