1 /***************************************************************************
2 * Copyright (C) 2005-2019 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>
27 IgnoreListManager& IgnoreListManager::operator=(const IgnoreListManager& other)
32 SyncableObject::operator=(other);
33 _ignoreList = other._ignoreList;
37 int IgnoreListManager::indexOf(const QString& ignore) const
39 for (int i = 0; i < _ignoreList.count(); i++) {
40 if (_ignoreList[i].contents() == ignore)
46 QVariantMap IgnoreListManager::initIgnoreList() const
48 QVariantMap ignoreListMap;
49 QVariantList ignoreTypeList;
50 QStringList ignoreRuleList;
51 QStringList scopeRuleList;
52 QVariantList isRegExList;
53 QVariantList scopeList;
54 QVariantList strictnessList;
55 QVariantList isActiveList;
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();
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;
77 void IgnoreListManager::initSetIgnoreList(const QVariantMap& ignoreList)
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();
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)";
95 for (int i = 0; i < ignoreRule.count(); i++) {
96 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()),
99 static_cast<StrictnessType>(strictness[i].toInt()),
100 static_cast<ScopeType>(scope[i].toInt()),
102 isActive[i].toBool());
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());
111 void IgnoreListManager::addIgnoreListItem(
112 int type, const QString& ignoreRule, bool isRegEx, int strictness, int scope, const QString& scopeRule, bool isActive)
114 if (contains(ignoreRule)) {
118 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type),
121 static_cast<StrictnessType>(strictness),
122 static_cast<ScopeType>(scope),
125 _ignoreList << newItem;
127 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
130 IgnoreListManager::StrictnessType IgnoreListManager::_match(
131 const QString& msgContents, const QString& msgSender, Message::Type msgType, const QString& network, const QString& bufferName)
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;
139 foreach (IgnoreListItem item, _ignoreList) {
140 if (!item.isEnabled() || item.type() == CtcpIgnore)
142 if (item.scope() == GlobalScope || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
143 || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
145 if (item.type() == MessageIgnore)
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();
160 return UnmatchedStrictness;
163 void IgnoreListManager::removeIgnoreListItem(const QString& ignoreRule)
165 removeAt(indexOf(ignoreRule));
166 SYNC(ARG(ignoreRule))
169 void IgnoreListManager::toggleIgnoreRule(const QString& ignoreRule)
171 int idx = indexOf(ignoreRule);
174 _ignoreList[idx].setIsEnabled(!_ignoreList[idx].isEnabled());
175 SYNC(ARG(ignoreRule))
178 bool IgnoreListManager::ctcpMatch(const QString sender, const QString& network, const QString& type)
180 foreach (IgnoreListItem item, _ignoreList) {
181 if (!item.isEnabled())
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
197 /**************************************************************************
199 *************************************************************************/
200 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem& other) const
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
207 void IgnoreListManager::IgnoreListItem::determineExpressions() const
209 // Don't update if not needed
210 if (!_cacheInvalid) {
214 // Set up matching rules
215 // Message is either wildcard or regex
216 ExpressionMatch::MatchMode contentsMode = _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : ExpressionMatch::MatchMode::MatchWildcard;
218 // Ignore rules are always case-insensitive
219 // Scope matching is always wildcard
220 // TODO: Expand upon ignore rule handling with next protocol break
222 if (_type == CtcpIgnore) {
223 // Set up CTCP sender
225 _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
228 // Set up message contents
229 _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
230 _ctcpSenderMatch = {};
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);
236 _cacheInvalid = false;