cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / common / ignorelistmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 <QDebug>
24 #include <QStringList>
25
26 int IgnoreListManager::indexOf(const QString& ignore) const
27 {
28     for (int i = 0; i < _ignoreList.count(); i++) {
29         if (_ignoreList[i].contents() == ignore)
30             return i;
31     }
32     return -1;
33 }
34
35 QVariantMap IgnoreListManager::initIgnoreList() const
36 {
37     QVariantMap ignoreListMap;
38     QVariantList ignoreTypeList;
39     QStringList ignoreRuleList;
40     QStringList scopeRuleList;
41     QVariantList isRegExList;
42     QVariantList scopeList;
43     QVariantList strictnessList;
44     QVariantList isActiveList;
45
46     for (int i = 0; i < _ignoreList.count(); i++) {
47         ignoreTypeList << _ignoreList[i].type();
48         ignoreRuleList << _ignoreList[i].contents();
49         scopeRuleList << _ignoreList[i].scopeRule();
50         isRegExList << _ignoreList[i].isRegEx();
51         scopeList << _ignoreList[i].scope();
52         strictnessList << _ignoreList[i].strictness();
53         isActiveList << _ignoreList[i].isEnabled();
54     }
55
56     ignoreListMap["ignoreType"] = ignoreTypeList;
57     ignoreListMap["ignoreRule"] = ignoreRuleList;
58     ignoreListMap["scopeRule"] = scopeRuleList;
59     ignoreListMap["isRegEx"] = isRegExList;
60     ignoreListMap["scope"] = scopeList;
61     ignoreListMap["strictness"] = strictnessList;
62     ignoreListMap["isActive"] = isActiveList;
63     return ignoreListMap;
64 }
65
66 void IgnoreListManager::initSetIgnoreList(const QVariantMap& ignoreList)
67 {
68     QVariantList ignoreType = ignoreList["ignoreType"].toList();
69     QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
70     QStringList scopeRule = ignoreList["scopeRule"].toStringList();
71     QVariantList isRegEx = ignoreList["isRegEx"].toList();
72     QVariantList scope = ignoreList["scope"].toList();
73     QVariantList strictness = ignoreList["strictness"].toList();
74     QVariantList isActive = ignoreList["isActive"].toList();
75
76     int count = ignoreRule.count();
77     if (count != scopeRule.count() || count != isRegEx.count() || count != scope.count() || count != strictness.count()
78         || count != ignoreType.count() || count != isActive.count()) {
79         qWarning() << "Corrupted IgnoreList settings! (Count mismatch)";
80         return;
81     }
82
83     _ignoreList.clear();
84     for (int i = 0; i < ignoreRule.count(); i++) {
85         _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()),
86                                       ignoreRule[i],
87                                       isRegEx[i].toBool(),
88                                       static_cast<StrictnessType>(strictness[i].toInt()),
89                                       static_cast<ScopeType>(scope[i].toInt()),
90                                       scopeRule[i],
91                                       isActive[i].toBool());
92     }
93 }
94
95 /* since overloaded methods aren't syncable (yet?) we can't use that anymore
96 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
97   addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(), item.scope(), item.scopeRule(), item.isEnabled());
98 }
99 */
100 void IgnoreListManager::addIgnoreListItem(
101     int type, const QString& ignoreRule, bool isRegEx, int strictness, int scope, const QString& scopeRule, bool isActive)
102 {
103     if (contains(ignoreRule)) {
104         return;
105     }
106
107     IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type),
108                                             ignoreRule,
109                                             isRegEx,
110                                             static_cast<StrictnessType>(strictness),
111                                             static_cast<ScopeType>(scope),
112                                             scopeRule,
113                                             isActive);
114     _ignoreList << newItem;
115
116     SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
117 }
118
119 IgnoreListManager::StrictnessType IgnoreListManager::_match(
120     const QString& msgContents, const QString& msgSender, Message::Type msgType, const QString& network, const QString& bufferName)
121 {
122     // We method don't rely on a proper Message object to make this method more versatile.
123     // This allows us to use it in the core with unprocessed Messages or in the Client
124     // with properly preprocessed Messages.
125     if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
126         return UnmatchedStrictness;
127
128     foreach (IgnoreListItem item, _ignoreList) {
129         if (!item.isEnabled() || item.type() == CtcpIgnore)
130             continue;
131         if (item.scope() == GlobalScope || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
132             || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
133             QString str;
134             if (item.type() == MessageIgnore)
135                 str = msgContents;
136             else
137                 str = msgSender;
138
139             //      qDebug() << "IgnoreListManager::match: ";
140             //      qDebug() << "string: " << str;
141             //      qDebug() << "pattern: " << ruleRx.pattern();
142             //      qDebug() << "scopeRule: " << item.scopeRule;
143             //      qDebug() << "now testing";
144             if (item.contentsMatcher().match(str)) {
145                 return item.strictness();
146             }
147         }
148     }
149     return UnmatchedStrictness;
150 }
151
152 void IgnoreListManager::removeIgnoreListItem(const QString& ignoreRule)
153 {
154     removeAt(indexOf(ignoreRule));
155     SYNC(ARG(ignoreRule))
156 }
157
158 void IgnoreListManager::toggleIgnoreRule(const QString& ignoreRule)
159 {
160     int idx = indexOf(ignoreRule);
161     if (idx == -1)
162         return;
163     _ignoreList[idx].setIsEnabled(!_ignoreList[idx].isEnabled());
164     SYNC(ARG(ignoreRule))
165 }
166
167 bool IgnoreListManager::ctcpMatch(const QString sender, const QString& network, const QString& type)
168 {
169     foreach (IgnoreListItem item, _ignoreList) {
170         if (!item.isEnabled())
171             continue;
172         if (item.scope() == GlobalScope || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))) {
173             // For CTCP ignore rules, use ctcpSender
174             if (item.senderCTCPMatcher().match(sender)) {
175                 // Sender matches, check types
176                 if (item.ctcpTypes().isEmpty() || item.ctcpTypes().contains(type, Qt::CaseInsensitive)) {
177                     // Either all types are blocked, or type matches
178                     return true;
179                 }
180             }
181         }
182     }
183     return false;
184 }
185
186 /**************************************************************************
187  * IgnoreListItem
188  *************************************************************************/
189 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem& other) const
190 {
191     return (_type != other._type || _contents != other._contents || _isRegEx != other._isRegEx || _strictness != other._strictness
192             || _scope != other._scope || _scopeRule != other._scopeRule || _isEnabled != other._isEnabled);
193     // Don't compare ExpressionMatch objects as they are created as needed from the above
194 }
195
196 void IgnoreListManager::IgnoreListItem::determineExpressions() const
197 {
198     // Don't update if not needed
199     if (!_cacheInvalid) {
200         return;
201     }
202
203     // Set up matching rules
204     // Message is either wildcard or regex
205     ExpressionMatch::MatchMode contentsMode = _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : ExpressionMatch::MatchMode::MatchWildcard;
206
207     // Ignore rules are always case-insensitive
208     // Scope matching is always wildcard
209     // TODO: Expand upon ignore rule handling with next protocol break
210
211     if (_type == CtcpIgnore) {
212         // Set up CTCP sender
213         _contentsMatch = {};
214         _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
215     }
216     else {
217         // Set up message contents
218         _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
219         _ctcpSenderMatch = {};
220     }
221     // Scope rules are always multiple wildcard entries
222     // (Adding a regex option would be awesome, but requires a backwards-compatible protocol change)
223     _scopeRuleMatch = ExpressionMatch(_scopeRule, ExpressionMatch::MatchMode::MatchMultiWildcard, false);
224
225     _cacheInvalid = false;
226 }