Add spam filter.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
62f6cfdaac
commit
c564e79dbc
26
src/cgi.cpp
26
src/cgi.cpp
|
@ -35,6 +35,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <regex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -83,6 +84,11 @@ entry_type parse_formdata()
|
||||||
entry.description = cgi("description");
|
entry.description = cgi("description");
|
||||||
entry.report_time = time::to_string(system_clock::now());
|
entry.report_time = time::to_string(system_clock::now());
|
||||||
|
|
||||||
|
if (is_spam(entry))
|
||||||
|
{
|
||||||
|
throw SpamException{};
|
||||||
|
}
|
||||||
|
|
||||||
std::uint8_t screenshot_counter{1};
|
std::uint8_t screenshot_counter{1};
|
||||||
for (const auto &screenshot : cgi.getFiles())
|
for (const auto &screenshot : cgi.getFiles())
|
||||||
{
|
{
|
||||||
|
@ -201,6 +207,26 @@ bool captcha_valid(std::uint8_t id, const string_view answer)
|
||||||
return false;
|
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
|
} // namespace FediBlock::cgi
|
||||||
|
|
||||||
const char *SpamException::what() const noexcept
|
const char *SpamException::what() const noexcept
|
||||||
|
|
|
@ -56,6 +56,8 @@ string text2html(string text);
|
||||||
// Check if the answer matches the solution we have for the id.
|
// Check if the answer matches the solution we have for the id.
|
||||||
bool captcha_valid(std::uint8_t id, string_view answer);
|
bool captcha_valid(std::uint8_t id, string_view answer);
|
||||||
|
|
||||||
|
bool is_spam(const entry_type &entry);
|
||||||
|
|
||||||
} // namespace FediBlock::cgi
|
} // namespace FediBlock::cgi
|
||||||
|
|
||||||
class SpamException : public std::exception
|
class SpamException : public std::exception
|
||||||
|
|
Reference in New Issue