tests: Convert ExpressionMatchTests into a GTest-based test case
[quassel.git] / src / common / ignorelistmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "ignorelistmanager.h"
22
23 #include <QtCore>
24 #include <QDebug>
25 #include <QStringList>
26
27 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other)
28 {
29     if (this == &other)
30         return *this;
31
32     SyncableObject::operator=(other);
33     _ignoreList = other._ignoreList;
34     return *this;
35 }
36
37
38 int IgnoreListManager::indexOf(const QString &ignore) const
39 {
40     for (int i = 0; i < _ignoreList.count(); i++) {
41         if (_ignoreList[i].contents() == ignore)
42             return i;
43     }
44     return -1;
45 }
46
47
48 QVariantMap IgnoreListManager::initIgnoreList() const
49 {
50     QVariantMap ignoreListMap;
51     QVariantList ignoreTypeList;
52     QStringList ignoreRuleList;
53     QStringList scopeRuleList;
54     QVariantList isRegExList;
55     QVariantList scopeList;
56     QVariantList strictnessList;
57     QVariantList isActiveList;
58
59     for (int i = 0; i < _ignoreList.count(); i++) {
60         ignoreTypeList << _ignoreList[i].type();
61         ignoreRuleList << _ignoreList[i].contents();
62         scopeRuleList << _ignoreList[i].scopeRule();
63         isRegExList << _ignoreList[i].isRegEx();
64         scopeList << _ignoreList[i].scope();
65         strictnessList << _ignoreList[i].strictness();
66         isActiveList << _ignoreList[i].isEnabled();
67     }
68
69     ignoreListMap["ignoreType"] = ignoreTypeList;
70     ignoreListMap["ignoreRule"] = ignoreRuleList;
71     ignoreListMap["scopeRule"] = scopeRuleList;
72     ignoreListMap["isRegEx"] = isRegExList;
73     ignoreListMap["scope"] = scopeList;
74     ignoreListMap["strictness"] = strictnessList;
75     ignoreListMap["isActive"] = isActiveList;
76     return ignoreListMap;
77 }
78
79
80 void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList)
81 {
82     QVariantList ignoreType = ignoreList["ignoreType"].toList();
83     QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
84     QStringList scopeRule = ignoreList["scopeRule"].toStringList();
85     QVariantList isRegEx = ignoreList["isRegEx"].toList();
86     QVariantList scope = ignoreList["scope"].toList();
87     QVariantList strictness = ignoreList["strictness"].toList();
88     QVariantList isActive = ignoreList["isActive"].toList();
89
90     int count = ignoreRule.count();
91     if (count != scopeRule.count() || count != isRegEx.count() ||
92         count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
93         qWarning() << "Corrupted IgnoreList settings! (Count mismatch)";
94         return;
95     }
96
97     _ignoreList.clear();
98     for (int i = 0; i < ignoreRule.count(); i++) {
99         _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
100             static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
101             scopeRule[i], isActive[i].toBool());
102     }
103 }
104
105
106 /* since overloaded methods aren't syncable (yet?) we can't use that anymore
107 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
108   addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(), item.scope(), item.scopeRule(), item.isEnabled());
109 }
110 */
111 void IgnoreListManager::addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
112     int scope, const QString &scopeRule, bool isActive)
113 {
114     if (contains(ignoreRule)) {
115         return;
116     }
117
118     IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
119         static_cast<ScopeType>(scope), scopeRule, isActive);
120     _ignoreList << newItem;
121
122     SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
123 }
124
125
126 IgnoreListManager::StrictnessType IgnoreListManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName)
127 {
128     // We method don't rely on a proper Message object to make this method more versatile.
129     // This allows us to use it in the core with unprocessed Messages or in the Client
130     // with properly preprocessed Messages.
131     if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
132         return UnmatchedStrictness;
133
134     foreach(IgnoreListItem item, _ignoreList) {
135         if (!item.isEnabled() || item.type() == CtcpIgnore)
136             continue;
137         if (item.scope() == GlobalScope
138             || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
139             || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
140             QString str;
141             if (item.type() == MessageIgnore)
142                 str = msgContents;
143             else
144                 str = msgSender;
145
146 //      qDebug() << "IgnoreListManager::match: ";
147 //      qDebug() << "string: " << str;
148 //      qDebug() << "pattern: " << ruleRx.pattern();
149 //      qDebug() << "scopeRule: " << item.scopeRule;
150 //      qDebug() << "now testing";
151             if (item.contentsMatcher().match(str)) {
152                 return item.strictness();
153             }
154         }
155     }
156     return UnmatchedStrictness;
157 }
158
159
160 void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule)
161 {
162     removeAt(indexOf(ignoreRule));
163     SYNC(ARG(ignoreRule))
164 }
165
166
167 void IgnoreListManager::toggleIgnoreRule(const QString &ignoreRule)
168 {
169     int idx = indexOf(ignoreRule);
170     if (idx == -1)
171         return;
172     _ignoreList[idx].setIsEnabled(!_ignoreList[idx].isEnabled());
173     SYNC(ARG(ignoreRule))
174 }
175
176
177 bool IgnoreListManager::ctcpMatch(const QString sender, const QString &network, const QString &type)
178 {
179     foreach(IgnoreListItem item, _ignoreList) {
180         if (!item.isEnabled())
181             continue;
182         if (item.scope() == GlobalScope
183                 || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))) {
184
185             // For CTCP ignore rules, use ctcpSender
186             if (item.senderCTCPMatcher().match(sender)) {
187                 // Sender matches, check types
188                 if (item.ctcpTypes().isEmpty()
189                         || item.ctcpTypes().contains(type, Qt::CaseInsensitive)) {
190                     // Either all types are blocked, or type matches
191                     return true;
192                 }
193             }
194         }
195     }
196     return false;
197 }
198
199
200 /**************************************************************************
201  * IgnoreListItem
202  *************************************************************************/
203 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem &other) const
204 {
205     return (_type != other._type ||
206             _contents != other._contents ||
207             _isRegEx != other._isRegEx ||
208             _strictness != other._strictness ||
209             _scope != other._scope ||
210             _scopeRule != other._scopeRule ||
211             _isEnabled != other._isEnabled);
212     // Don't compare ExpressionMatch objects as they are created as needed from the above
213 }
214
215
216 void IgnoreListManager::IgnoreListItem::determineExpressions() const
217 {
218     // Don't update if not needed
219     if (!_cacheInvalid) {
220         return;
221     }
222
223     // Set up matching rules
224     // Message is either wildcard or regex
225     ExpressionMatch::MatchMode contentsMode =
226             _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx :
227                        ExpressionMatch::MatchMode::MatchWildcard;
228
229     // Ignore rules are always case-insensitive
230     // Scope matching is always wildcard
231     // TODO: Expand upon ignore rule handling with next protocol break
232
233     if (_type == CtcpIgnore) {
234         // Set up CTCP sender
235         _contentsMatch = {};
236         _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
237     }
238     else {
239         // Set up message contents
240         _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
241         _ctcpSenderMatch = {};
242     }
243     // Scope rules are always multiple wildcard entries
244     // (Adding a regex option would be awesome, but requires a backwards-compatible protocol change)
245     _scopeRuleMatch = ExpressionMatch(_scopeRule,
246                                       ExpressionMatch::MatchMode::MatchMultiWildcard, false);
247
248     _cacheInvalid = false;
249 }