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 #ifndef IGNORELISTMANAGER_H
22 #define IGNORELISTMANAGER_H
26 #include <QStringList>
29 #include "expressionmatch.h"
31 #include "syncableobject.h"
33 class IgnoreListManager : public SyncableObject
38 inline IgnoreListManager(QObject *parent = 0) : SyncableObject(parent) { setAllowClientUpdates(true); }
39 IgnoreListManager &operator=(const IgnoreListManager &other);
48 UnmatchedStrictness = 0,
60 * Individual ignore list rule
62 class IgnoreListItem {
65 * Construct an empty ignore rule
70 * Construct an ignore rule with the given parameters
72 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
73 * Duplicate entries are not allowed.
75 * @param type Type of ignore rule
76 * @param contents String representing a message contents expression to match
77 * @param isRegEx True if regular expression, otherwise false
78 * @param strictness Strictness of ignore rule
79 * @param scope What to match scope rule against
80 * @param scopeRule String representing a scope rule expression to match
81 * @param isEnabled True if enabled, otherwise false
83 IgnoreListItem(IgnoreType type, const QString &contents, bool isRegEx,
84 StrictnessType strictness, ScopeType scope, const QString &scopeRule,
86 : _contents(contents), _isRegEx(isRegEx), _strictness(strictness),
87 _scope(scope), _scopeRule(scopeRule), _isEnabled(isEnabled)
89 // Allow passing empty "contents" as they can happen when editing an ignore rule
91 // Handle CTCP ignores
95 // Cache expression matches on construction
97 // This provides immediate feedback on errors when loading the rule. If profiling shows
98 // this as a performance bottleneck, this can be removed in deference to caching on
101 // Inversely, if needed for validity checks, caching can be done on every update below
102 // instead of on first use.
103 determineExpressions();
107 * Gets the type of this ignore rule
109 * @return IgnoreType of the rule
111 inline IgnoreType type() const {
115 * Sets the type of this ignore rule
117 * @param type IgnoreType of the rule
119 inline void setType(IgnoreType type) {
120 // Handle CTCP ignores
121 if (type == CtcpIgnore) {
122 // This is not performance-intensive; sticking with QRegExp for Qt 4 is fine
123 // Split based on whitespace characters
124 QStringList split(contents().split(QRegExp("\\s+"), QString::SkipEmptyParts));
125 // Match on the first item, handling empty rules/matches
126 if (!split.isEmpty()) {
127 // Take the first item as the sender
128 _cacheCtcpSender = split.takeFirst();
129 // Track the rest as CTCP types to ignore
130 _cacheCtcpTypes = split;
133 // No match found - this can happen if a pure whitespace CTCP ignore rule is
134 // created. Fall back to matching all senders.
136 // RegEx match everything
137 _cacheCtcpSender = ".*";
140 // Wildcard match everything
141 _cacheCtcpSender = "*";
143 // Clear the types (split is already empty)
144 _cacheCtcpTypes = split;
151 * Gets the message contents this rule matches
153 * NOTE: Use IgnoreListItem::contentsMatcher() for performing matches
155 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
156 * Duplicate entries are not allowed.
158 * @return String representing a phrase or expression to match
160 inline QString contents() const {
164 * Sets the message contents this rule matches
166 * @param contents String representing a phrase or expression to match
168 inline void setContents(const QString &contents) {
169 // Allow passing empty "contents" as they can happen when editing an ignore rule
170 _contents = contents;
171 _cacheInvalid = true;
175 * Gets if this is a regular expression rule
177 * @return True if regular expression, otherwise false
179 inline bool isRegEx() const {
183 * Sets if this rule is a regular expression rule
185 * @param isRegEx True if regular expression, otherwise false
187 inline void setIsRegEx(bool isRegEx) {
189 _cacheInvalid = true;
193 * Gets the strictness of this ignore rule
195 * @return StrictnessType of the rule
197 inline StrictnessType strictness() const {
201 * Sets the strictness of this ignore rule
203 * @param strictness StrictnessType of the rule
205 inline void setStrictness(StrictnessType strictness) {
206 _strictness = strictness;
210 * Gets what to match scope rule against
212 * @return ScopeType of the rule
214 inline ScopeType scope() const {
218 * Sets what to match scope rule against
220 * @param type ScopeType of the rule
222 inline void setScope(ScopeType scope) {
227 * Gets the scope rule this rule matches
229 * NOTE: Use IgnoreListItem::scopeRuleMatcher() for performing matches
231 * @return String representing a phrase or expression to match
233 inline QString scopeRule() const {
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) {
242 _scopeRule = scopeRule;
243 _cacheInvalid = true;
247 * Gets if this rule is enabled and active
249 * @return True if enabled, otherwise false
251 inline bool isEnabled() const {
255 * Sets if this rule is enabled and active
257 * @param isEnabled True if enabled, otherwise false
259 inline void setIsEnabled(bool isEnabled) {
260 _isEnabled = isEnabled;
264 * Gets the ignored CTCP types for CTCP ignores
266 * @return List of CTCP types to ignore, or empty for all
268 inline QStringList ctcpTypes() const {
269 return _cacheCtcpTypes;
273 * Gets the expression matcher for the message contents, caching if needed
275 * @return Expression matcher to compare with message contents
277 inline ExpressionMatch contentsMatcher() const {
279 determineExpressions();
281 return _contentsMatch;
285 * Gets the expression matcher for the scope, caching if needed
287 * @return Expression matcher to compare with scope
289 inline ExpressionMatch scopeRuleMatcher() const {
291 determineExpressions();
293 return _scopeRuleMatch;
297 * Gets the expression matcher for the message contents, caching if needed
299 * @return Expression matcher to compare with message contents
301 inline ExpressionMatch senderCTCPMatcher() const {
303 determineExpressions();
305 return _ctcpSenderMatch;
308 bool operator!=(const IgnoreListItem &other) const;
312 * Update internal cache of expression matching if needed
314 void determineExpressions() const;
316 IgnoreType _type = {};
317 QString _contents = {};
318 bool _isRegEx = false;
319 StrictnessType _strictness = {};
320 ScopeType _scope = {};
321 QString _scopeRule = {};
322 bool _isEnabled = true;
324 QString _cacheCtcpSender = {}; ///< For CTCP rules, precalculate sender
325 QStringList _cacheCtcpTypes = {}; ///< For CTCP rules, precalculate types
327 // These represent internal cache and should be safe to mutate in 'const' functions
328 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
329 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
330 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message
331 mutable ExpressionMatch _scopeRuleMatch = {}; ///< Expression match cache for scope rule
332 mutable ExpressionMatch _ctcpSenderMatch = {}; ///< Expression match cache for CTCP nick
335 typedef QList<IgnoreListItem> IgnoreList;
337 int indexOf(const QString &ignore) const;
338 inline bool contains(const QString &ignore) const { return indexOf(ignore) != -1; }
339 inline bool isEmpty() const { return _ignoreList.isEmpty(); }
340 inline int count() const { return _ignoreList.count(); }
341 inline void removeAt(int index) { _ignoreList.removeAt(index); }
342 inline IgnoreListItem &operator[](int i) { return _ignoreList[i]; }
343 inline const IgnoreListItem &operator[](int i) const { return _ignoreList.at(i); }
344 inline const IgnoreList &ignoreList() const { return _ignoreList; }
346 //! Check if a message matches the IgnoreRule
347 /** This method checks if a message matches the users ignorelist.
348 * \param msg The Message that should be checked
349 * \param network The networkname the message belongs to
350 * \return UnmatchedStrictness, HardStrictness or SoftStrictness representing the match type
352 inline StrictnessType match(const Message &msg, const QString &network = QString()) { return _match(msg.contents(), msg.sender(), msg.type(), network, msg.bufferInfo().bufferName()); }
354 bool ctcpMatch(const QString sender, const QString &network, const QString &type = QString());
356 // virtual void addIgnoreListItem(const IgnoreListItem &item);
359 virtual QVariantMap initIgnoreList() const;
360 virtual void initSetIgnoreList(const QVariantMap &ignoreList);
362 //! Request removal of an ignore rule based on the rule itself.
363 /** Use this method if you want to remove a single ignore rule
364 * and get that synced with the core immediately.
365 * \param ignoreRule A valid ignore rule
367 virtual inline void requestRemoveIgnoreListItem(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
368 virtual void removeIgnoreListItem(const QString &ignoreRule);
370 //! Request toggling of "isActive" flag of a given ignore rule.
371 /** Use this method if you want to toggle the "isActive" flag of a single ignore rule
372 * and get that synced with the core immediately.
373 * \param ignoreRule A valid ignore rule
375 virtual inline void requestToggleIgnoreRule(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
376 virtual void toggleIgnoreRule(const QString &ignoreRule);
378 //! Request an IgnoreListItem to be added to the ignore list
379 /** Items added to the list with this method, get immediately synced with the core
380 * \param type The IgnoreType of the new rule
381 * \param ignoreRule The rule itself
382 * \param isRegEx Signals if the rule should be interpreted as a regular expression
383 * \param strictness Th StrictnessType that should be applied
384 * \param scope The ScopeType that should be set
385 * \param scopeRule A string of semi-colon separated network- or channelnames
386 * \param isActive Signals if the rule is enabled or not
388 virtual inline void requestAddIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
389 int scope, const QString &scopeRule, bool isActive)
391 REQUEST(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
395 virtual void addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
396 int scope, const QString &scopeRule, bool isActive);
399 void setIgnoreList(const QList<IgnoreListItem> &ignoreList) { _ignoreList = ignoreList; }
401 StrictnessType _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName);
404 void ignoreAdded(IgnoreType type, const QString &ignoreRule, bool isRegex, StrictnessType strictness, ScopeType scope, const QVariant &scopeRule, bool isActive);
407 IgnoreList _ignoreList;
411 #endif // IGNORELISTMANAGER_H