common: Add inverted scope match rules to ignores
[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 INIT_SYNCABLE_OBJECT(IgnoreListManager)
28 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other)
29 {
30     if (this == &other)
31         return *this;
32
33     SyncableObject::operator=(other);
34     _ignoreList = other._ignoreList;
35     return *this;
36 }
37
38
39 int IgnoreListManager::indexOf(const QString &ignore) const
40 {
41     for (int i = 0; i < _ignoreList.count(); i++) {
42         if (_ignoreList[i].ignoreRule == ignore)
43             return i;
44     }
45     return -1;
46 }
47
48
49 QVariantMap IgnoreListManager::initIgnoreList() const
50 {
51     QVariantMap ignoreListMap;
52     QVariantList ignoreTypeList;
53     QStringList ignoreRuleList;
54     QStringList scopeRuleList;
55     QVariantList isRegExList;
56     QVariantList scopeList;
57     QVariantList strictnessList;
58     QVariantList isActiveList;
59
60     for (int i = 0; i < _ignoreList.count(); i++) {
61         ignoreTypeList << _ignoreList[i].type;
62         ignoreRuleList << _ignoreList[i].ignoreRule;
63         scopeRuleList << _ignoreList[i].scopeRule;
64         isRegExList << _ignoreList[i].isRegEx;
65         scopeList << _ignoreList[i].scope;
66         strictnessList << _ignoreList[i].strictness;
67         isActiveList << _ignoreList[i].isActive;
68     }
69
70     ignoreListMap["ignoreType"] = ignoreTypeList;
71     ignoreListMap["ignoreRule"] = ignoreRuleList;
72     ignoreListMap["scopeRule"] = scopeRuleList;
73     ignoreListMap["isRegEx"] = isRegExList;
74     ignoreListMap["scope"] = scopeList;
75     ignoreListMap["strictness"] = strictnessList;
76     ignoreListMap["isActive"] = isActiveList;
77     return ignoreListMap;
78 }
79
80
81 void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList)
82 {
83     QVariantList ignoreType = ignoreList["ignoreType"].toList();
84     QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
85     QStringList scopeRule = ignoreList["scopeRule"].toStringList();
86     QVariantList isRegEx = ignoreList["isRegEx"].toList();
87     QVariantList scope = ignoreList["scope"].toList();
88     QVariantList strictness = ignoreList["strictness"].toList();
89     QVariantList isActive = ignoreList["isActive"].toList();
90
91     int count = ignoreRule.count();
92     if (count != scopeRule.count() || count != isRegEx.count() ||
93         count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
94         qWarning() << "Corrupted IgnoreList settings! (Count mismatch)";
95         return;
96     }
97
98     _ignoreList.clear();
99     for (int i = 0; i < ignoreRule.count(); i++) {
100         _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
101             static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
102             scopeRule[i], isActive[i].toBool());
103     }
104 }
105
106
107 /* since overloaded methods aren't syncable (yet?) we can't use that anymore
108 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
109   addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule, item.isActive);
110 }
111 */
112 void IgnoreListManager::addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
113     int scope, const QString &scopeRule, bool isActive)
114 {
115     if (contains(ignoreRule)) {
116         return;
117     }
118
119     IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
120         static_cast<ScopeType>(scope), scopeRule, isActive);
121     _ignoreList << newItem;
122
123     SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
124 }
125
126
127 IgnoreListManager::StrictnessType IgnoreListManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName)
128 {
129     // We method don't rely on a proper Message object to make this method more versatile.
130     // This allows us to use it in the core with unprocessed Messages or in the Client
131     // with properly preprocessed Messages.
132     if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
133         return UnmatchedStrictness;
134
135     foreach(IgnoreListItem item, _ignoreList) {
136         if (!item.isActive || item.type == CtcpIgnore)
137             continue;
138         if (item.scope == GlobalScope
139             || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))
140             || (item.scope == ChannelScope && scopeMatch(item.scopeRule, bufferName))) {
141             QString str;
142             if (item.type == MessageIgnore)
143                 str = msgContents;
144             else
145                 str = msgSender;
146
147 //      qDebug() << "IgnoreListManager::match: ";
148 //      qDebug() << "string: " << str;
149 //      qDebug() << "pattern: " << ruleRx.pattern();
150 //      qDebug() << "scopeRule: " << item.scopeRule;
151 //      qDebug() << "now testing";
152             if ((!item.isRegEx && item.regEx.exactMatch(str)) ||
153                 (item.isRegEx && item.regEx.indexIn(str) != -1)) {
154 //        qDebug() << "MATCHED!";
155                 return item.strictness;
156             }
157         }
158     }
159     return UnmatchedStrictness;
160 }
161
162
163 bool IgnoreListManager::scopeMatch(const QString &scopeRule, const QString &string) const
164 {
165     // A match happens when the string does NOT match ANY inverted rules and matches AT LEAST one
166     // normal rule, unless no normal rules exist (implicit wildcard match).  This gives inverted
167     // rules higher priority regardless of ordering.
168     //
169     // TODO: After switching to Qt 5, use of this should be split into two parts, one part that
170     // would generate compiled QRegularExpressions for match/inverted match, regenerating it on any
171     // rule changes, and another part that would check each message against these compiled rules.
172
173     // Keep track if any matches are found
174     bool matches = false;
175     // Keep track if normal rules and inverted rules are found, allowing for implicit wildcard
176     bool normalRuleFound = false, invertedRuleFound = false;
177
178     // Split each scope rule by separator, ignoring empty parts
179     foreach(QString rule, scopeRule.split(";", QString::SkipEmptyParts)) {
180         // Trim whitespace from the start/end of the rule
181         rule = rule.trimmed();
182         // Ignore empty rules
183         if (rule.isEmpty())
184             continue;
185
186         // Check if this is an inverted rule (starts with '!')
187         if (rule.startsWith("!")) {
188             // Inverted rule found
189             invertedRuleFound = true;
190
191             // Take the reminder of the string
192             QRegExp ruleRx(rule.mid(1), Qt::CaseInsensitive);
193             ruleRx.setPatternSyntax(QRegExp::Wildcard);
194             if (ruleRx.exactMatch(string)) {
195                 // Matches an inverted rule, full rule cannot match
196                 return false;
197             }
198         } else {
199             // Normal rule found
200             normalRuleFound = true;
201
202             QRegExp ruleRx(rule, Qt::CaseInsensitive);
203             ruleRx.setPatternSyntax(QRegExp::Wildcard);
204             if (ruleRx.exactMatch(string)) {
205                 // Matches a normal rule, full rule might match
206                 matches = true;
207                 // Continue checking in case other inverted rules negate this
208             }
209         }
210     }
211     // No inverted rules matched, okay to match normally
212     // Return true if...
213     // ...we found a normal match
214     // ...implicit wildcard: we had inverted rules (that didn't match) and no normal rules
215     return matches || (invertedRuleFound && !normalRuleFound);
216 }
217
218
219 void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule)
220 {
221     removeAt(indexOf(ignoreRule));
222     SYNC(ARG(ignoreRule))
223 }
224
225
226 void IgnoreListManager::toggleIgnoreRule(const QString &ignoreRule)
227 {
228     int idx = indexOf(ignoreRule);
229     if (idx == -1)
230         return;
231     _ignoreList[idx].isActive = !_ignoreList[idx].isActive;
232     SYNC(ARG(ignoreRule))
233 }
234
235
236 bool IgnoreListManager::ctcpMatch(const QString sender, const QString &network, const QString &type)
237 {
238     foreach(IgnoreListItem item, _ignoreList) {
239         if (!item.isActive)
240             continue;
241         if (item.scope == GlobalScope || (item.scope == NetworkScope && scopeMatch(item.scopeRule, network))) {
242             QString sender_;
243             QStringList types = item.ignoreRule.split(QRegExp("\\s+"), QString::SkipEmptyParts);
244
245             sender_ = types.takeAt(0);
246
247             QRegExp ruleRx = QRegExp(sender_);
248             ruleRx.setCaseSensitivity(Qt::CaseInsensitive);
249             if (!item.isRegEx)
250                 ruleRx.setPatternSyntax(QRegExp::Wildcard);
251             if ((!item.isRegEx && ruleRx.exactMatch(sender)) ||
252                 (item.isRegEx && ruleRx.indexIn(sender) != -1)) {
253                 if (types.isEmpty() || types.contains(type, Qt::CaseInsensitive))
254                     return true;
255             }
256         }
257     }
258     return false;
259 }