summaryrefslogtreecommitdiff
path: root/MessageFilterRules.cpp
blob: 6208d99811aafeb719e8c1b7946af0a63dc1647c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "MessageFilterRules.hpp"

#include "MessageFilter.hpp"

#include <QRegularExpression>
#include <QStringList>

namespace
{
  using SpecOp = SpecialOperatingActivity;

#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
  auto const SkipEmptyParts = Qt::SkipEmptyParts;
#else
  auto const SkipEmptyParts = QString::SkipEmptyParts;
#endif

  QString msk144FilterText(QString const& text, MessageFilterLogic::FilterContext const& ctx)
  {
    if (!ctx.filtersForWord2) return text;

    QStringList const words = text.mid(24).split(" ", SkipEmptyParts);
    if (words.size() < 2) return "___";
    if (words[1].length() == 2 && words[1].contains(QRegularExpression{"\\w\\w"})) {
      return words.size() > 2 ? words[2] : "___";
    }
    return words[1];
  }

  bool containsFilterMatch(QString const& filterText, QStringList const& keywords, bool startsWith)
  {
    return startsWith ? MessageFilter::startsWithAny(filterText, keywords)
                      : MessageFilter::containsAny(filterText, keywords);
  }
}

MessageFilterRules::Evaluation MessageFilterRules::evaluateMSK144Text(QString text, MessageFilterLogic::FilterContext const& ctx)
{
  Evaluation evaluation;
  text = text.replace("<", "").replace(">", "");

  bool const startsWith = ctx.filtersForWord2;
  QString const filterText = msk144FilterText(text, ctx);

  if (SpecOp::NONE == ctx.specOp && ctx.alwaysPass && containsFilterMatch(filterText, ctx.passKeywords, startsWith)) {
    return evaluation;
  }

  evaluation.filtersApplied = true;

  if (SpecOp::NONE == ctx.specOp && ctx.blacklisted && containsFilterMatch(filterText, ctx.blacklistKeywords, startsWith)) {
    if (!ctx.bypass) evaluation.result.filtered = true;
    if (!(ctx.filtersForWaitAndPounceOnly || ctx.bypass)) {
      evaluation.result.shouldReturn = true;
      return evaluation;
    }
    if (ctx.pounce && isScoringAutoRespondPolicy (ctx.respondPolicy)) {
      evaluation.result.resetPoints = true;
    }
  } else if (SpecOp::NONE == ctx.specOp && ctx.whitelisted && !containsFilterMatch(filterText, ctx.whitelistKeywords, startsWith)) {
    if (!ctx.bypass) evaluation.result.filtered = true;
    if (!(ctx.filtersForWaitAndPounceOnly || ctx.bypass)) {
      evaluation.result.shouldReturn = true;
      return evaluation;
    }
    if (ctx.pounce && isScoringAutoRespondPolicy (ctx.respondPolicy)) {
      evaluation.result.resetPoints = true;
    }
  }

  return evaluation;
}