1 /***************************************************************************
2 * Copyright (C) 2005-2022 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>
28 int IgnoreListManager::indexOf(const QString& ignore) const
30 for (int i = 0; i < _ignoreList.count(); i++) {
31 if (_ignoreList[i].contents() == ignore)
37 QVariantMap IgnoreListManager::initIgnoreList() const
39 QVariantMap ignoreListMap;
40 QVariantList ignoreTypeList;
41 QStringList ignoreRuleList;
42 QStringList scopeRuleList;
43 QVariantList isRegExList;
44 QVariantList scopeList;
45 QVariantList strictnessList;
46 QVariantList isActiveList;
48 for (int i = 0; i < _ignoreList.count(); i++) {
49 ignoreTypeList << _ignoreList[i].type();
50 ignoreRuleList << _ignoreList[i].contents();
51 scopeRuleList << _ignoreList[i].scopeRule();
52 isRegExList << _ignoreList[i].isRegEx();
53 scopeList << _ignoreList[i].scope();
54 strictnessList << _ignoreList[i].strictness();
55 isActiveList << _ignoreList[i].isEnabled();
58 ignoreListMap["ignoreType"] = ignoreTypeList;
59 ignoreListMap["ignoreRule"] = ignoreRuleList;
60 ignoreListMap["scopeRule"] = scopeRuleList;
61 ignoreListMap["isRegEx"] = isRegExList;
62 ignoreListMap["scope"] = scopeList;
63 ignoreListMap["strictness"] = strictnessList;
64 ignoreListMap["isActive"] = isActiveList;
68 void IgnoreListManager::initSetIgnoreList(const QVariantMap& ignoreList)
70 QVariantList ignoreType = ignoreList["ignoreType"].toList();
71 QStringList ignoreRule = ignoreList["ignoreRule"].toStringList();
72 QStringList scopeRule = ignoreList["scopeRule"].toStringList();
73 QVariantList isRegEx = ignoreList["isRegEx"].toList();
74 QVariantList scope = ignoreList["scope"].toList();
75 QVariantList strictness = ignoreList["strictness"].toList();
76 QVariantList isActive = ignoreList["isActive"].toList();
78 int count = ignoreRule.count();
79 if (count != scopeRule.count() || count != isRegEx.count() || count != scope.count() || count != strictness.count()
80 || count != ignoreType.count() || count != isActive.count()) {
81 qWarning() << "Corrupted IgnoreList settings! (Count mismatch)";
86 for (int i = 0; i < ignoreRule.count(); i++) {
87 _ignoreList << IgnoreListItem(static_cast<IgnoreType>(ignoreType[i].toInt()),
90 static_cast<StrictnessType>(strictness[i].toInt()),
91 static_cast<ScopeType>(scope[i].toInt()),
93 isActive[i].toBool());
97 /* since overloaded methods aren't syncable (yet?) we can't use that anymore
98 void IgnoreListManager::addIgnoreListItem(const IgnoreListItem &item) {
99 addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(), item.scope(), item.scopeRule(), item.isEnabled());
102 void IgnoreListManager::addIgnoreListItem(
103 int type, const QString& ignoreRule, bool isRegEx, int strictness, int scope, const QString& scopeRule, bool isActive)
105 if (contains(ignoreRule)) {
109 IgnoreListItem newItem = IgnoreListItem(static_cast<IgnoreType>(type),
112 static_cast<StrictnessType>(strictness),
113 static_cast<ScopeType>(scope),
116 _ignoreList << newItem;
118 SYNC(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
121 IgnoreListManager::StrictnessType IgnoreListManager::_match(
122 const QString& msgContents, const QString& msgSender, Message::Type msgType, const QString& network, const QString& bufferName)
124 // We method don't rely on a proper Message object to make this method more versatile.
125 // This allows us to use it in the core with unprocessed Messages or in the Client
126 // with properly preprocessed Messages.
127 if (!(msgType & (Message::Plain | Message::Notice | Message::Action)))
128 return UnmatchedStrictness;
130 foreach (IgnoreListItem item, _ignoreList) {
131 if (!item.isEnabled() || item.type() == CtcpIgnore)
133 if (item.scope() == GlobalScope || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))
134 || (item.scope() == ChannelScope && item.scopeRuleMatcher().match(bufferName))) {
136 if (item.type() == MessageIgnore) {
137 // TODO: Make this configurable? Pre-0.14, format codes were not removed
138 str = stripFormatCodes(msgContents);
143 // qDebug() << "IgnoreListManager::match: ";
144 // qDebug() << "string: " << str;
145 // qDebug() << "pattern: " << ruleRx.pattern();
146 // qDebug() << "scopeRule: " << item.scopeRule;
147 // qDebug() << "now testing";
148 if (item.contentsMatcher().match(str)) {
149 return item.strictness();
153 return UnmatchedStrictness;
156 void IgnoreListManager::removeIgnoreListItem(const QString& ignoreRule)
158 removeAt(indexOf(ignoreRule));
159 SYNC(ARG(ignoreRule))
162 void IgnoreListManager::toggleIgnoreRule(const QString& ignoreRule)
164 int idx = indexOf(ignoreRule);
167 _ignoreList[idx].setIsEnabled(!_ignoreList[idx].isEnabled());
168 SYNC(ARG(ignoreRule))
171 bool IgnoreListManager::ctcpMatch(const QString sender, const QString& network, const QString& type)
173 foreach (IgnoreListItem item, _ignoreList) {
174 if (!item.isEnabled())
176 if (item.scope() == GlobalScope || (item.scope() == NetworkScope && item.scopeRuleMatcher().match(network))) {
177 // For CTCP ignore rules, use ctcpSender
178 if (item.senderCTCPMatcher().match(sender)) {
179 // Sender matches, check types
180 if (item.ctcpTypes().isEmpty() || item.ctcpTypes().contains(type, Qt::CaseInsensitive)) {
181 // Either all types are blocked, or type matches
190 /**************************************************************************
192 *************************************************************************/
193 bool IgnoreListManager::IgnoreListItem::operator!=(const IgnoreListItem& other) const
195 return (_type != other._type || _contents != other._contents || _isRegEx != other._isRegEx || _strictness != other._strictness
196 || _scope != other._scope || _scopeRule != other._scopeRule || _isEnabled != other._isEnabled);
197 // Don't compare ExpressionMatch objects as they are created as needed from the above
200 void IgnoreListManager::IgnoreListItem::determineExpressions() const
202 // Don't update if not needed
203 if (!_cacheInvalid) {
207 // Set up matching rules
208 // Message is either wildcard or regex
209 ExpressionMatch::MatchMode contentsMode = _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : ExpressionMatch::MatchMode::MatchWildcard;
211 // Ignore rules are always case-insensitive
212 // Scope matching is always wildcard
213 // TODO: Expand upon ignore rule handling with next protocol break
215 if (_type == CtcpIgnore) {
216 // Set up CTCP sender
218 _ctcpSenderMatch = ExpressionMatch(_cacheCtcpSender, contentsMode, false);
221 // Set up message contents
222 _contentsMatch = ExpressionMatch(_contents, contentsMode, false);
223 _ctcpSenderMatch = {};
225 // Scope rules are always multiple wildcard entries
226 // (Adding a regex option would be awesome, but requires a backwards-compatible protocol change)
227 _scopeRuleMatch = ExpressionMatch(_scopeRule, ExpressionMatch::MatchMode::MatchMultiWildcard, false);
229 _cacheInvalid = false;