1 /***************************************************************************
2 * Copyright (C) 2005-2020 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 ***************************************************************************/
23 #include "common-export.h"
29 #include <QStringList>
31 #include "expressionmatch.h"
33 #include "syncableobject.h"
35 class COMMON_EXPORT IgnoreListManager : public SyncableObject
41 inline IgnoreListManager(QObject* parent = nullptr)
42 : SyncableObject(parent)
44 setAllowClientUpdates(true);
56 UnmatchedStrictness = 0,
69 * Individual ignore list rule
71 class COMMON_EXPORT IgnoreListItem
75 * Construct an empty ignore rule
77 IgnoreListItem() = default;
80 * Construct an ignore rule with the given parameters
82 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
83 * Duplicate entries are not allowed.
85 * @param type Type of ignore rule
86 * @param contents String representing a message contents expression to match
87 * @param isRegEx True if regular expression, otherwise false
88 * @param strictness Strictness of ignore rule
89 * @param scope What to match scope rule against
90 * @param scopeRule String representing a scope rule expression to match
91 * @param isEnabled True if enabled, otherwise false
94 IgnoreType type, QString contents, bool isRegEx, StrictnessType strictness, ScopeType scope, QString scopeRule, bool isEnabled)
95 : _contents(std::move(contents))
97 , _strictness(strictness)
99 , _scopeRule(std::move(scopeRule))
100 , _isEnabled(isEnabled)
102 // Allow passing empty "contents" as they can happen when editing an ignore rule
104 // Handle CTCP ignores
107 _cacheInvalid = true;
108 // Cache expression matches on construction
110 // This provides immediate feedback on errors when loading the rule. If profiling shows
111 // this as a performance bottleneck, this can be removed in deference to caching on
114 // Inversely, if needed for validity checks, caching can be done on every update below
115 // instead of on first use.
116 determineExpressions();
120 * Gets the type of this ignore rule
122 * @return IgnoreType of the rule
124 inline IgnoreType type() const { return _type; }
126 * Sets the type of this ignore rule
128 * @param type IgnoreType of the rule
130 inline void setType(IgnoreType type)
132 // Handle CTCP ignores
133 if (type == CtcpIgnore) {
134 // This is not performance-intensive; sticking with QRegExp for Qt 4 is fine
135 // Split based on whitespace characters
136 QStringList split(contents().split(QRegExp("\\s+"), QString::SkipEmptyParts));
137 // Match on the first item, handling empty rules/matches
138 if (!split.isEmpty()) {
139 // Take the first item as the sender
140 _cacheCtcpSender = split.takeFirst();
141 // Track the rest as CTCP types to ignore
142 _cacheCtcpTypes = split;
145 // No match found - this can happen if a pure whitespace CTCP ignore rule is
146 // created. Fall back to matching all senders.
148 // RegEx match everything
149 _cacheCtcpSender = ".*";
152 // Wildcard match everything
153 _cacheCtcpSender = "*";
155 // Clear the types (split is already empty)
156 _cacheCtcpTypes = split;
163 * Gets the message contents this rule matches
165 * NOTE: Use IgnoreListItem::contentsMatcher() for performing matches
167 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
168 * Duplicate entries are not allowed.
170 * @return String representing a phrase or expression to match
172 inline QString contents() const { return _contents; }
174 * Sets the message contents this rule matches
176 * @param contents String representing a phrase or expression to match
178 inline void setContents(const QString& contents)
180 // Allow passing empty "contents" as they can happen when editing an ignore rule
181 _contents = contents;
182 _cacheInvalid = true;
186 * Gets if this is a regular expression rule
188 * @return True if regular expression, otherwise false
190 inline bool isRegEx() const { return _isRegEx; }
192 * Sets if this rule is a regular expression rule
194 * @param isRegEx True if regular expression, otherwise false
196 inline void setIsRegEx(bool isRegEx)
199 _cacheInvalid = true;
203 * Gets the strictness of this ignore rule
205 * @return StrictnessType of the rule
207 inline StrictnessType strictness() const { return _strictness; }
209 * Sets the strictness of this ignore rule
211 * @param strictness StrictnessType of the rule
213 inline void setStrictness(StrictnessType strictness) { _strictness = strictness; }
216 * Gets what to match scope rule against
218 * @return ScopeType of the rule
220 inline ScopeType scope() const { return _scope; }
222 * Sets what to match scope rule against
224 * @param type ScopeType of the rule
226 inline void setScope(ScopeType scope) { _scope = scope; }
229 * Gets the scope rule this rule matches
231 * NOTE: Use IgnoreListItem::scopeRuleMatcher() for performing matches
233 * @return String representing a phrase or expression to match
235 inline QString scopeRule() const { return _scopeRule; }
237 * Sets the scope rule this rule matches
239 * @param scopeRule String representing a phrase or expression to match
241 inline void setScopeRule(const QString& scopeRule)
243 _scopeRule = scopeRule;
244 _cacheInvalid = true;
248 * Gets if this rule is enabled and active
250 * @return True if enabled, otherwise false
252 inline bool isEnabled() const { return _isEnabled; }
254 * Sets if this rule is enabled and active
256 * @param isEnabled True if enabled, otherwise false
258 inline void setIsEnabled(bool isEnabled) { _isEnabled = isEnabled; }
261 * Gets the ignored CTCP types for CTCP ignores
263 * @return List of CTCP types to ignore, or empty for all
265 inline QStringList ctcpTypes() const { return _cacheCtcpTypes; }
268 * Gets the expression matcher for the message contents, caching if needed
270 * @return Expression matcher to compare with message contents
272 inline ExpressionMatch contentsMatcher() const
275 determineExpressions();
277 return _contentsMatch;
281 * Gets the expression matcher for the scope, caching if needed
283 * @return Expression matcher to compare with scope
285 inline ExpressionMatch scopeRuleMatcher() const
288 determineExpressions();
290 return _scopeRuleMatch;
294 * Gets the expression matcher for the message contents, caching if needed
296 * @return Expression matcher to compare with message contents
298 inline ExpressionMatch senderCTCPMatcher() const
301 determineExpressions();
303 return _ctcpSenderMatch;
306 bool operator!=(const IgnoreListItem& other) const;
310 * Update internal cache of expression matching if needed
312 void determineExpressions() const;
314 IgnoreType _type = {};
315 QString _contents = {};
316 bool _isRegEx = false;
317 StrictnessType _strictness = {};
318 ScopeType _scope = {};
319 QString _scopeRule = {};
320 bool _isEnabled = true;
322 QString _cacheCtcpSender = {}; ///< For CTCP rules, precalculate sender
323 QStringList _cacheCtcpTypes = {}; ///< For CTCP rules, precalculate types
325 // These represent internal cache and should be safe to mutate in 'const' functions
326 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
327 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
328 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message
329 mutable ExpressionMatch _scopeRuleMatch = {}; ///< Expression match cache for scope rule
330 mutable ExpressionMatch _ctcpSenderMatch = {}; ///< Expression match cache for CTCP nick
333 using IgnoreList = QList<IgnoreListItem>;
335 int indexOf(const QString& ignore) const;
336 inline bool contains(const QString& ignore) const { return indexOf(ignore) != -1; }
337 inline bool isEmpty() const { return _ignoreList.isEmpty(); }
338 inline int count() const { return _ignoreList.count(); }
339 inline void removeAt(int index) { _ignoreList.removeAt(index); }
340 inline IgnoreListItem& operator[](int i) { return _ignoreList[i]; }
341 inline const IgnoreListItem& operator[](int i) const { return _ignoreList.at(i); }
342 inline const IgnoreList& ignoreList() const { return _ignoreList; }
344 //! Check if a message matches the IgnoreRule
345 /** This method checks if a message matches the users ignorelist.
346 * \param msg The Message that should be checked
347 * \param network The networkname the message belongs to
348 * \return UnmatchedStrictness, HardStrictness or SoftStrictness representing the match type
350 inline StrictnessType match(const Message& msg, const QString& network = QString())
352 return _match(msg.contents(), msg.sender(), msg.type(), network, msg.bufferInfo().bufferName());
355 bool ctcpMatch(const QString sender, const QString& network, const QString& type = QString());
357 // virtual void addIgnoreListItem(const IgnoreListItem &item);
360 virtual QVariantMap initIgnoreList() const;
361 virtual void initSetIgnoreList(const QVariantMap& ignoreList);
363 //! Request removal of an ignore rule based on the rule itself.
364 /** Use this method if you want to remove a single ignore rule
365 * and get that synced with the core immediately.
366 * \param ignoreRule A valid ignore rule
368 virtual inline void requestRemoveIgnoreListItem(const QString& ignoreRule) { REQUEST(ARG(ignoreRule)) }
369 virtual void removeIgnoreListItem(const QString& ignoreRule);
371 //! Request toggling of "isActive" flag of a given ignore rule.
372 /** Use this method if you want to toggle the "isActive" flag of a single ignore rule
373 * and get that synced with the core immediately.
374 * \param ignoreRule A valid ignore rule
376 virtual inline void requestToggleIgnoreRule(const QString& ignoreRule) { REQUEST(ARG(ignoreRule)) }
377 virtual void toggleIgnoreRule(const QString& ignoreRule);
379 //! Request an IgnoreListItem to be added to the ignore list
380 /** Items added to the list with this method, get immediately synced with the core
381 * \param type The IgnoreType of the new rule
382 * \param ignoreRule The rule itself
383 * \param isRegEx Signals if the rule should be interpreted as a regular expression
384 * \param strictness Th StrictnessType that should be applied
385 * \param scope The ScopeType that should be set
386 * \param scopeRule A string of semi-colon separated network- or channelnames
387 * \param isActive Signals if the rule is enabled or not
389 virtual inline void requestAddIgnoreListItem(
390 int type, const QString& ignoreRule, bool isRegEx, int strictness, int scope, const QString& scopeRule, bool isActive)
392 REQUEST(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
395 virtual void addIgnoreListItem(
396 int type, const QString& ignoreRule, bool isRegEx, int strictness, int scope, const QString& scopeRule, bool isActive);
399 void setIgnoreList(const QList<IgnoreListItem>& ignoreList) { _ignoreList = ignoreList; }
401 StrictnessType _match(
402 const QString& msgContents, const QString& msgSender, Message::Type msgType, const QString& network, const QString& bufferName);
405 void ignoreAdded(IgnoreType type,
406 const QString& ignoreRule,
408 StrictnessType strictness,
410 const QVariant& scopeRule,
414 IgnoreList _ignoreList;