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