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 INIT_SYNCABLE_OBJECT(IgnoreListManager)
28 IgnoreListManager &IgnoreListManager::operator=(const IgnoreListManager &other)
33 SyncableObject::operator=(other);
34 _ignoreList = other._ignoreList;
39 int IgnoreListManager::indexOf(const QString &ignore) const
41 for (int i = 0; i < _ignoreList.count(); i++) {
42 if (_ignoreList[i].contents() == ignore)
49 QVariantMap IgnoreListManager::initIgnoreList() const
51 QVariantMap ignoreListMap;
52 QVariantList ignoreTypeList;
53 QStringList ignoreRuleList;
54 QStringList scopeRuleList;
55 QVariantList isRegExList;
56 QVariantList scopeList;
57 QVariantList strictnessList;
58 QVariantList isActiveList;
60 for (int i = 0; i < _ignoreList.count(); i++) {
61 ignoreTypeList << _ignoreList[i].type();
62 ignoreRuleList << _ignoreList[i].contents();
63 scopeRuleList << _ignoreList[i].scopeRule();
64 isRegExList << _ignoreList[i].isRegEx();
65 scopeList << _ignoreList[i].scope();
66 strictnessList << _ignoreList[i].strictness();
67 isActiveList << _ignoreList[i].isEnabled();
70 ignoreListMap["ignoreType"] = ignoreTypeList;
71 ignoreListMap["ignoreRule"] = ignoreRuleList;
72 ignoreListMap["scopeRule"] = scopeRuleList;
73 ignoreListMap["isRegEx"] = isRegExList;
74 ignoreListMap["scope"] = scopeList;
75 ignoreListMap["strictness"] = strictnessList;
76 ignoreListMap["isActive"] = isActiveList;
81 void IgnoreListManager::initSetIgnoreList(const QVariantMap &ignoreList)
83 QVariantList ignoreType = ignoreList["ignoreType"].toList();
84 QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
85 QStringList scopeRule = ignoreList["scopeRule"].toStringList();
86 QVariantList isRegEx = ignoreList["isRegEx"].toList();
87 QVariantList scope = ignoreList["scope"].toList();
88 QVariantList strictness = ignoreList["strictness"].toList();
89 QVariantList isActive = ignoreList["isActive"].toList();
91 int count = ignoreRule.count();
92 if (count != scopeRule.count() || count != isRegEx.count() ||
93 count != scope.count() || count != strictness.count() || count != ignoreType.count() || count != isActive.count()) {
94 qWarning() << "Corrupted IgnoreList settings! (Count mismatch)";
99 for (int i = 0; i < ignoreRule.count(); i++) {
100 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()), ignoreRule[i], isRegEx[i].toBool(),
101 static_cast<StrictnessType>(strictness[i].toInt()), static_cast<ScopeType>(scope[i].toInt()),
102 scopeRule[i], isActive[i].toBool());
107 /* since overloaded methods aren't syncable (yet?) we can't use that anymore
108 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
109 addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(), item.scope(), item.scopeRule(), item.isEnabled());
112 void IgnoreListManager::addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
113 int scope, const QString &scopeRule, bool isActive)
115 if (contains(ignoreRule)) {
119 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type), ignoreRule, isRegEx, static_cast<StrictnessType>(strictness),
120 static_cast<ScopeType>(scope), scopeRule, isActive);
121 _ignoreList << newItem;
123 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
127 IgnoreListManager::StrictnessType IgnoreListManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName)
129 // We method don't rely on a proper Message object to make this method more versatile.
130 // This allows us to use it in the core with unprocessed Messages or in the Client
131 // with properly preprocessed Messages.
132 if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
133 return UnmatchedStrictness;
135 foreach(IgnoreListItem item, _ignoreList) {
136 if (!item.isEnabled() || item.type() == CtcpIgnore)
138 if (item.scope() == GlobalScope
139 || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
140 || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
142 if (item.type() == MessageIgnore)
147 // qDebug() << "IgnoreListManager::match: ";
148 // qDebug() << "string: " << str;
149 // qDebug() << "pattern: " << ruleRx.pattern();
150 // qDebug() << "scopeRule: " << item.scopeRule;
151 // qDebug() << "now testing";
152 if (item.contentsMatcher().match(str)) {
153 return item.strictness();
157 return UnmatchedStrictness;
161 void IgnoreListManager::removeIgnoreListItem(const QString &ignoreRule)
163 removeAt(indexOf(ignoreRule));
164 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))
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
184 || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))) {
186 // For CTCP ignore rules, use ctcpSender
187 if (item.senderCTCPMatcher().match(sender)) {
188 // Sender matches, check types
189 if (item.ctcpTypes().isEmpty()
190 || item.ctcpTypes().contains(type, Qt::CaseInsensitive)) {
191 // Either all types are blocked, or type matches
201 /**************************************************************************
203 *************************************************************************/
204 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem &other) const
206 return (_type != other._type ||
207 _contents != other._contents ||
208 _isRegEx != other._isRegEx ||
209 _strictness != other._strictness ||
210 _scope != other._scope ||
211 _scopeRule != other._scopeRule ||
212 _isEnabled != other._isEnabled);
213 // Don't compare ExpressionMatch objects as they are created as needed from the above
217 void IgnoreListManager::IgnoreListItem::determineExpressions() const
219 // Don't update if not needed
220 if (!_cacheInvalid) {
224 // Set up matching rules
225 // Message is either wildcard or regex
226 ExpressionMatch::MatchMode contentsMode =
227 _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx :
228 ExpressionMatch::MatchMode::MatchWildcard;
230 // Ignore rules are always case-insensitive
231 // Scope matching is always wildcard
232 // TODO: Expand upon ignore rule handling with next protocol break
234 if (_type == CtcpIgnore) {
235 // Set up CTCP sender
237 _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
240 // Set up message contents
241 _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
242 _ctcpSenderMatch = {};
244 // Scope rules are always multiple wildcard entries
245 // (Adding a regex option would be awesome, but requires a backwards-compatible protocol change)
246 _scopeRuleMatch = ExpressionMatch(_scopeRule,
247 ExpressionMatch::MatchMode::MatchMultiWildcard, false);
249 _cacheInvalid = false;