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