1 /***************************************************************************
2 * Copyright (C) 2005-2018 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"
25 #include <QStringList>
27 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other)
32 SyncableObject::operator=(other);
33 _ignoreList = other._ignoreList;
38 int IgnoreListManager::indexOf(const QString &ignore) const
40 for (int i = 0; i < _ignoreList.count(); i++) {
41 if (_ignoreList[i].contents() == ignore)
48 QVariantMap IgnoreListManager::initIgnoreList() const
50 QVariantMap ignoreListMap;
51 QVariantList ignoreTypeList;
52 QStringList ignoreRuleList;
53 QStringList scopeRuleList;
54 QVariantList isRegExList;
55 QVariantList scopeList;
56 QVariantList strictnessList;
57 QVariantList isActiveList;
59 for (int i = 0; i < _ignoreList.count(); i++) {
60 ignoreTypeList << _ignoreList[i].type();
61 ignoreRuleList << _ignoreList[i].contents();
62 scopeRuleList << _ignoreList[i].scopeRule();
63 isRegExList << _ignoreList[i].isRegEx();
64 scopeList << _ignoreList[i].scope();
65 strictnessList << _ignoreList[i].strictness();
66 isActiveList << _ignoreList[i].isEnabled();
69 ignoreListMap["ignoreType"] = ignoreTypeList;
70 ignoreListMap["ignoreRule"] = ignoreRuleList;
71 ignoreListMap["scopeRule"] = scopeRuleList;
72 ignoreListMap["isRegEx"] = isRegExList;
73 ignoreListMap["scope"] = scopeList;
74 ignoreListMap["strictness"] = strictnessList;
75 ignoreListMap["isActive"] = isActiveList;
80 void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList)
82 QVariantList ignoreType = ignoreList["ignoreType"].toList();
83 QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
84 QStringList scopeRule = ignoreList["scopeRule"].toStringList();
85 QVariantList isRegEx = ignoreList["isRegEx"].toList();
86 QVariantList scope = ignoreList["scope"].toList();
87 QVariantList strictness = ignoreList["strictness"].toList();
88 QVariantList isActive = ignoreList["isActive"].toList();
90 int count = ignoreRule.count();
91 if (count != scopeRule.count() || count != isRegEx.count() ||
92 count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
93 qWarning() << "Corrupted IgnoreList settings! (Count mismatch)";
98 for (int i = 0; i < ignoreRule.count(); i++) {
99 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
100 static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
101 scopeRule[i], 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(int type, const QString &ignoreRule, bool isRegEx, int strictness,
112 int scope, const QString &scopeRule, bool isActive)
114 if (contains(ignoreRule)) {
118 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
119 static_cast<ScopeType>(scope), scopeRule, isActive);
120 _ignoreList << newItem;
122 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
126 IgnoreListManager::StrictnessType IgnoreListManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName)
128 // We method don't rely on a proper Message object to make this method more versatile.
129 // This allows us to use it in the core with unprocessed Messages or in the Client
130 // with properly preprocessed Messages.
131 if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
132 return UnmatchedStrictness;
134 foreach(IgnoreListItem item, _ignoreList) {
135 if (!item.isEnabled() || item.type() == CtcpIgnore)
137 if (item.scope() == GlobalScope
138 || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
139 || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
141 if (item.type() == MessageIgnore)
146 // qDebug() << "IgnoreListManager::match: ";
147 // qDebug() << "string: " << str;
148 // qDebug() << "pattern: " << ruleRx.pattern();
149 // qDebug() << "scopeRule: " << item.scopeRule;
150 // qDebug() << "now testing";
151 if (item.contentsMatcher().match(str)) {
152 return item.strictness();
156 return UnmatchedStrictness;
160 void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule)
162 removeAt(indexOf(ignoreRule));
163 SYNC(ARG(ignoreRule))
167 void IgnoreListManager::toggleIgnoreRule(const QString &ignoreRule)
169 int idx = indexOf(ignoreRule);
172 _ignoreList[idx].setIsEnabled(!_ignoreList[idx].isEnabled());
173 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
183 || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))) {
185 // For CTCP ignore rules, use ctcpSender
186 if (item.senderCTCPMatcher().match(sender)) {
187 // Sender matches, check types
188 if (item.ctcpTypes().isEmpty()
189 || item.ctcpTypes().contains(type, Qt::CaseInsensitive)) {
190 // Either all types are blocked, or type matches
200 /**************************************************************************
202 *************************************************************************/
203 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem &other) const
205 return (_type != other._type ||
206 _contents != other._contents ||
207 _isRegEx != other._isRegEx ||
208 _strictness != other._strictness ||
209 _scope != other._scope ||
210 _scopeRule != other._scopeRule ||
211 _isEnabled != other._isEnabled);
212 // Don't compare ExpressionMatch objects as they are created as needed from the above
216 void IgnoreListManager::IgnoreListItem::determineExpressions() const
218 // Don't update if not needed
219 if (!_cacheInvalid) {
223 // Set up matching rules
224 // Message is either wildcard or regex
225 ExpressionMatch::MatchMode contentsMode =
226 _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx :
227 ExpressionMatch::MatchMode::MatchWildcard;
229 // Ignore rules are always case-insensitive
230 // Scope matching is always wildcard
231 // TODO: Expand upon ignore rule handling with next protocol break
233 if (_type == CtcpIgnore) {
234 // Set up CTCP sender
236 _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
239 // Set up message contents
240 _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
241 _ctcpSenderMatch = {};
243 // Scope rules are always multiple wildcard entries
244 // (Adding a regex option would be awesome, but requires a backwards-compatible protocol change)
245 _scopeRuleMatch = ExpressionMatch(_scopeRule,
246 ExpressionMatch::MatchMode::MatchMultiWildcard, false);
248 _cacheInvalid = false;