1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "ignorelistmanager.h"
24 #include <QStringList>
26 int IgnoreListManager::indexOf(const QString& ignore) const
28 for (int i = 0; i < _ignoreList.count(); i++) {
29 if (_ignoreList[i].contents() == ignore)
35 QVariantMap IgnoreListManager::initIgnoreList() const
37 QVariantMap ignoreListMap;
38 QVariantList ignoreTypeList;
39 QStringList ignoreRuleList;
40 QStringList scopeRuleList;
41 QVariantList isRegExList;
42 QVariantList scopeList;
43 QVariantList strictnessList;
44 QVariantList isActiveList;
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();
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;
66 void IgnoreListManager::initSetIgnoreList(const QVariantMap& ignoreList)
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();
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)";
84 for (int i = 0; i < ignoreRule.count(); i++) {
85 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()),
88 static_cast<StrictnessType>(strictness[i].toInt()),
89 static_cast<ScopeType>(scope[i].toInt()),
91 isActive[i].toBool());
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());
100 void IgnoreListManager::addIgnoreListItem(
101 int type, const QString& ignoreRule, bool isRegEx, int strictness, int scope, const QString& scopeRule, bool isActive)
103 if (contains(ignoreRule)) {
107 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type),
110 static_cast<StrictnessType>(strictness),
111 static_cast<ScopeType>(scope),
114 _ignoreList << newItem;
116 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
119 IgnoreListManager::StrictnessType IgnoreListManager::_match(
120 const QString& msgContents, const QString& msgSender, Message::Type msgType, const QString& network, const QString& bufferName)
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;
128 foreach (IgnoreListItem item, _ignoreList) {
129 if (!item.isEnabled() || item.type() == CtcpIgnore)
131 if (item.scope() == GlobalScope || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
132 || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
134 if (item.type() == MessageIgnore)
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();
149 return UnmatchedStrictness;
152 void IgnoreListManager::removeIgnoreListItem(const QString& ignoreRule)
154 removeAt(indexOf(ignoreRule));
155 SYNC(ARG(ignoreRule))
158 void IgnoreListManager::toggleIgnoreRule(const QString& ignoreRule)
160 int idx = indexOf(ignoreRule);
163 _ignoreList[idx].setIsEnabled(!_ignoreList[idx].isEnabled());
164 SYNC(ARG(ignoreRule))
167 bool IgnoreListManager::ctcpMatch(const QString sender, const QString& network, const QString& type)
169 foreach (IgnoreListItem item, _ignoreList) {
170 if (!item.isEnabled())
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
186 /**************************************************************************
188 *************************************************************************/
189 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem& other) const
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
196 void IgnoreListManager::IgnoreListItem::determineExpressions() const
198 // Don't update if not needed
199 if (!_cacheInvalid) {
203 // Set up matching rules
204 // Message is either wildcard or regex
205 ExpressionMatch::MatchMode contentsMode = _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : ExpressionMatch::MatchMode::MatchWildcard;
207 // Ignore rules are always case-insensitive
208 // Scope matching is always wildcard
209 // TODO: Expand upon ignore rule handling with next protocol break
211 if (_type == CtcpIgnore) {
212 // Set up CTCP sender
214 _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
217 // Set up message contents
218 _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
219 _ctcpSenderMatch = {};
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);
225 _cacheInvalid = false;