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