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>
26 IgnoreListManager& IgnoreListManager::operator=(const IgnoreListManager& other)
31 SyncableObject::operator=(other);
32 _ignoreList = other._ignoreList;
36 int IgnoreListManager::indexOf(const QString& ignore) const
38 for (int i = 0; i < _ignoreList.count(); i++) {
39 if (_ignoreList[i].contents() == ignore)
45 QVariantMap IgnoreListManager::initIgnoreList() const
47 QVariantMap ignoreListMap;
48 QVariantList ignoreTypeList;
49 QStringList ignoreRuleList;
50 QStringList scopeRuleList;
51 QVariantList isRegExList;
52 QVariantList scopeList;
53 QVariantList strictnessList;
54 QVariantList isActiveList;
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();
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;
76 void IgnoreListManager::initSetIgnoreList(const QVariantMap& ignoreList)
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();
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)";
94 for (int i = 0; i < ignoreRule.count(); i++) {
95 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()),
98 static_cast<StrictnessType>(strictness[i].toInt()),
99 static_cast<ScopeType>(scope[i].toInt()),
101 isActive[i].toBool());
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());
110 void IgnoreListManager::addIgnoreListItem(
111 int type, const QString& ignoreRule, bool isRegEx, int strictness, int scope, const QString& scopeRule, bool isActive)
113 if (contains(ignoreRule)) {
117 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type),
120 static_cast<StrictnessType>(strictness),
121 static_cast<ScopeType>(scope),
124 _ignoreList << newItem;
126 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
129 IgnoreListManager::StrictnessType IgnoreListManager::_match(
130 const QString& msgContents, const QString& msgSender, Message::Type msgType, const QString& network, const QString& bufferName)
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;
138 foreach (IgnoreListItem item, _ignoreList) {
139 if (!item.isEnabled() || item.type() == CtcpIgnore)
141 if (item.scope() == GlobalScope || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
142 || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
144 if (item.type() == MessageIgnore)
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();
159 return UnmatchedStrictness;
162 void IgnoreListManager::removeIgnoreListItem(const QString& ignoreRule)
164 removeAt(indexOf(ignoreRule));
165 SYNC(ARG(ignoreRule))
168 void IgnoreListManager::toggleIgnoreRule(const QString& ignoreRule)
170 int idx = indexOf(ignoreRule);
173 _ignoreList[idx].setIsEnabled(!_ignoreList[idx].isEnabled());
174 SYNC(ARG(ignoreRule))
177 bool IgnoreListManager::ctcpMatch(const QString sender, const QString& network, const QString& type)
179 foreach (IgnoreListItem item, _ignoreList) {
180 if (!item.isEnabled())
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
196 /**************************************************************************
198 *************************************************************************/
199 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem& other) const
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
206 void IgnoreListManager::IgnoreListItem::determineExpressions() const
208 // Don't update if not needed
209 if (!_cacheInvalid) {
213 // Set up matching rules
214 // Message is either wildcard or regex
215 ExpressionMatch::MatchMode contentsMode = _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : ExpressionMatch::MatchMode::MatchWildcard;
217 // Ignore rules are always case-insensitive
218 // Scope matching is always wildcard
219 // TODO: Expand upon ignore rule handling with next protocol break
221 if (_type == CtcpIgnore) {
222 // Set up CTCP sender
224 _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
227 // Set up message contents
228 _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
229 _ctcpSenderMatch = {};
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);
235 _cacheInvalid = false;