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
126 _cacheCtcpSender = split.takeFirst();
127 // Track the rest as CTCP types to ignore
128 _cacheCtcpTypes = split;
134 * Gets the message contents this rule matches
136 * NOTE: Use IgnoreListItem::contentsMatcher() for performing matches
138 * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
139 * Duplicate entries are not allowed.
141 * @return String representing a phrase or expression to match
143 inline QString contents() const {
147 * Sets the message contents this rule matches
149 * @param contents String representing a phrase or expression to match
151 inline void setContents(const QString &contents) {
152 // Allow passing empty "contents" as they can happen when editing an ignore rule
153 _contents = contents;
154 _cacheInvalid = true;
158 * Gets if this is a regular expression rule
160 * @return True if regular expression, otherwise false
162 inline bool isRegEx() const {
166 * Sets if this rule is a regular expression rule
168 * @param isRegEx True if regular expression, otherwise false
170 inline void setIsRegEx(bool isRegEx) {
172 _cacheInvalid = true;
176 * Gets the strictness of this ignore rule
178 * @return StrictnessType of the rule
180 inline StrictnessType strictness() const {
184 * Sets the strictness of this ignore rule
186 * @param strictness StrictnessType of the rule
188 inline void setStrictness(StrictnessType strictness) {
189 _strictness = strictness;
193 * Gets what to match scope rule against
195 * @return ScopeType of the rule
197 inline ScopeType scope() const {
201 * Sets what to match scope rule against
203 * @param type ScopeType of the rule
205 inline void setScope(ScopeType scope) {
210 * Gets the scope rule this rule matches
212 * NOTE: Use IgnoreListItem::scopeRuleMatcher() for performing matches
214 * @return String representing a phrase or expression to match
216 inline QString scopeRule() const {
220 * Sets the scope rule this rule matches
222 * @param scopeRule String representing a phrase or expression to match
224 inline void setScopeRule(const QString &scopeRule) {
225 _scopeRule = scopeRule;
226 _cacheInvalid = true;
230 * Gets if this rule is enabled and active
232 * @return True if enabled, otherwise false
234 inline bool isEnabled() const {
238 * Sets if this rule is enabled and active
240 * @param isEnabled True if enabled, otherwise false
242 inline void setIsEnabled(bool isEnabled) {
243 _isEnabled = isEnabled;
247 * Gets the ignored CTCP types for CTCP ignores
249 * @return List of CTCP types to ignore, or empty for all
251 inline QStringList ctcpTypes() const {
252 return _cacheCtcpTypes;
256 * Gets the expression matcher for the message contents, caching if needed
258 * @return Expression matcher to compare with message contents
260 inline ExpressionMatch contentsMatcher() const {
262 determineExpressions();
264 return _contentsMatch;
268 * Gets the expression matcher for the scope, caching if needed
270 * @return Expression matcher to compare with scope
272 inline ExpressionMatch scopeRuleMatcher() const {
274 determineExpressions();
276 return _scopeRuleMatch;
280 * Gets the expression matcher for the message contents, caching if needed
282 * @return Expression matcher to compare with message contents
284 inline ExpressionMatch senderCTCPMatcher() const {
286 determineExpressions();
288 return _ctcpSenderMatch;
291 bool operator!=(const IgnoreListItem &other) const;
295 * Update internal cache of expression matching if needed
297 void determineExpressions() const;
299 IgnoreType _type = {};
300 QString _contents = {};
301 bool _isRegEx = false;
302 StrictnessType _strictness = {};
303 ScopeType _scope = {};
304 QString _scopeRule = {};
305 bool _isEnabled = true;
307 QString _cacheCtcpSender = {}; ///< For CTCP rules, precalculate sender
308 QStringList _cacheCtcpTypes = {}; ///< For CTCP rules, precalculate types
310 // These represent internal cache and should be safe to mutate in 'const' functions
311 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
312 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
313 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message
314 mutable ExpressionMatch _scopeRuleMatch = {}; ///< Expression match cache for scope rule
315 mutable ExpressionMatch _ctcpSenderMatch = {}; ///< Expression match cache for CTCP nick
318 typedef QList<IgnoreListItem> IgnoreList;
320 int indexOf(const QString &ignore) const;
321 inline bool contains(const QString &ignore) const { return indexOf(ignore) != -1; }
322 inline bool isEmpty() const { return _ignoreList.isEmpty(); }
323 inline int count() const { return _ignoreList.count(); }
324 inline void removeAt(int index) { _ignoreList.removeAt(index); }
325 inline IgnoreListItem &operator[](int i) { return _ignoreList[i]; }
326 inline const IgnoreListItem &operator[](int i) const { return _ignoreList.at(i); }
327 inline const IgnoreList &ignoreList() const { return _ignoreList; }
329 //! Check if a message matches the IgnoreRule
330 /** This method checks if a message matches the users ignorelist.
331 * \param msg The Message that should be checked
332 * \param network The networkname the message belongs to
333 * \return UnmatchedStrictness, HardStrictness or SoftStrictness representing the match type
335 inline StrictnessType match(const Message &msg, const QString &network = QString()) { return _match(msg.contents(), msg.sender(), msg.type(), network, msg.bufferInfo().bufferName()); }
337 bool ctcpMatch(const QString sender, const QString &network, const QString &type = QString());
339 // virtual void addIgnoreListItem(const IgnoreListItem &item);
342 virtual QVariantMap initIgnoreList() const;
343 virtual void initSetIgnoreList(const QVariantMap &ignoreList);
345 //! Request removal of an ignore rule based on the rule itself.
346 /** Use this method if you want to remove a single ignore rule
347 * and get that synced with the core immediately.
348 * \param ignoreRule A valid ignore rule
350 virtual inline void requestRemoveIgnoreListItem(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
351 virtual void removeIgnoreListItem(const QString &ignoreRule);
353 //! Request toggling of "isActive" flag of a given ignore rule.
354 /** Use this method if you want to toggle the "isActive" flag of a single ignore rule
355 * and get that synced with the core immediately.
356 * \param ignoreRule A valid ignore rule
358 virtual inline void requestToggleIgnoreRule(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
359 virtual void toggleIgnoreRule(const QString &ignoreRule);
361 //! Request an IgnoreListItem to be added to the ignore list
362 /** Items added to the list with this method, get immediately synced with the core
363 * \param type The IgnoreType of the new rule
364 * \param ignoreRule The rule itself
365 * \param isRegEx Signals if the rule should be interpreted as a regular expression
366 * \param strictness Th StrictnessType that should be applied
367 * \param scope The ScopeType that should be set
368 * \param scopeRule A string of semi-colon separated network- or channelnames
369 * \param isActive Signals if the rule is enabled or not
371 virtual inline void requestAddIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
372 int scope, const QString &scopeRule, bool isActive)
374 REQUEST(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
378 virtual void addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
379 int scope, const QString &scopeRule, bool isActive);
382 void setIgnoreList(const QList<IgnoreListItem> &ignoreList) { _ignoreList = ignoreList; }
384 StrictnessType _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName);
387 void ignoreAdded(IgnoreType type, const QString &ignoreRule, bool isRegex, StrictnessType strictness, ScopeType scope, const QVariant &scopeRule, bool isActive);
390 IgnoreList _ignoreList;
394 #endif // IGNORELISTMANAGER_H