1 /***************************************************************************
2 * Copyright (C) 2005-2019 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"
28 #include <QStringList>
29 #include <QVariantList>
30 #include <QVariantMap>
32 #include "expressionmatch.h"
34 #include "nickhighlightmatcher.h"
35 #include "syncableobject.h"
37 class COMMON_EXPORT HighlightRuleManager : public SyncableObject
42 Q_PROPERTY(int highlightNick READ highlightNick WRITE setHighlightNick)
43 Q_PROPERTY(bool nicksCaseSensitive READ nicksCaseSensitive WRITE setNicksCaseSensitive)
46 enum HighlightNickType
53 inline HighlightRuleManager(QObject* parent = nullptr)
54 : SyncableObject(parent)
56 setAllowClientUpdates(true);
58 HighlightRuleManager& operator=(const HighlightRuleManager& other);
61 * Individual highlight rule
63 class COMMON_EXPORT HighlightRule
67 * Construct an empty highlight rule
69 HighlightRule() = default;
72 * Construct a highlight rule with the given parameters
74 * @param id Integer ID of the rule
75 * @param contents String representing a message contents expression to match
76 * @param isRegEx True if regular expression, otherwise false
77 * @param isCaseSensitive True if case sensitive, otherwise false
78 * @param isEnabled True if enabled, otherwise false
79 * @param isInverse True if rule is treated as highlight ignore, otherwise false
80 * @param sender String representing a message sender expression to match
81 * @param chanName String representing a channel name expression to match
84 int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName)
86 , _contents(std::move(contents))
88 , _isCaseSensitive(isCaseSensitive)
89 , _isEnabled(isEnabled)
90 , _isInverse(isInverse)
91 , _sender(std::move(sender))
92 , _chanName(std::move(chanName))
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 unique ID of this rule
109 * @return Integer ID of the rule
111 inline int id() const { return _id; }
113 * Sets the ID of this rule
115 * CAUTION: IDs should be kept unique!
117 * @param id Integer ID of the rule
119 inline void setId(int id) { _id = id; }
122 * Gets the message contents this rule matches
124 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
126 * @return String representing a phrase or expression to match
128 inline QString contents() const { return _contents; }
130 * Sets the message contents this rule matches
132 * @param contents String representing a phrase or expression to match
134 inline void setContents(const QString& contents)
136 _contents = contents;
137 _cacheInvalid = true;
141 * Gets if this is a regular expression rule
143 * @return True if regular expression, otherwise false
145 inline bool isRegEx() const { return _isRegEx; }
147 * Sets if this rule is a regular expression rule
149 * @param isRegEx True if regular expression, otherwise false
151 inline void setIsRegEx(bool isRegEx)
154 _cacheInvalid = true;
158 * Gets if this rule is case sensitive
160 * @return True if case sensitive, otherwise false
162 inline bool isCaseSensitive() const { return _isCaseSensitive; }
164 * Sets if this rule is case sensitive
166 * @param isCaseSensitive True if case sensitive, otherwise false
168 inline void setIsCaseSensitive(bool isCaseSensitive)
170 _isCaseSensitive = isCaseSensitive;
171 _cacheInvalid = true;
175 * Gets if this rule is enabled and active
177 * @return True if enabled, otherwise false
179 inline bool isEnabled() const { return _isEnabled; }
181 * Sets if this rule is enabled and active
183 * @param isEnabled True if enabled, otherwise false
185 inline void setIsEnabled(bool isEnabled) { _isEnabled = isEnabled; }
188 * Gets if this rule is a highlight ignore rule
190 * @return True if rule is treated as highlight ignore, otherwise false
192 inline bool isInverse() const { return _isInverse; }
194 * Sets if this rule is a highlight ignore rule
196 * @param isInverse True if rule is treated as highlight ignore, otherwise false
198 inline void setIsInverse(bool isInverse) { _isInverse = isInverse; }
201 * Gets the message sender this rule matches
203 * NOTE: Use HighlightRule::senderMatcher() for performing matches
205 * @return String representing a phrase or expression to match
207 inline QString sender() const { return _sender; }
209 * Sets the message sender this rule matches
211 * @param sender String representing a phrase or expression to match
213 inline void setSender(const QString& sender)
216 _cacheInvalid = true;
220 * Gets the channel name this rule matches
222 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
224 * @return String representing a phrase or expression to match
226 inline QString chanName() const { return _chanName; }
228 * Sets the channel name this rule matches
230 * @param chanName String representing a phrase or expression to match
232 inline void setChanName(const QString& chanName)
234 _chanName = chanName;
235 _cacheInvalid = true;
239 * Gets the expression matcher for the message contents, caching if needed
241 * @return Expression matcher to compare with message contents
243 inline ExpressionMatch contentsMatcher() const
246 determineExpressions();
248 return _contentsMatch;
252 * Gets the expression matcher for the message sender, caching if needed
254 * @return Expression matcher to compare with message sender
256 inline ExpressionMatch senderMatcher() const
259 determineExpressions();
265 * Gets the expression matcher for the channel name, caching if needed
267 * @return Expression matcher to compare with channel name
269 inline ExpressionMatch chanNameMatcher() const
272 determineExpressions();
274 return _chanNameMatch;
277 bool operator!=(const HighlightRule& other) const;
281 * Update internal cache of expression matching if needed
283 void determineExpressions() const;
286 QString _contents = {};
287 bool _isRegEx = false;
288 bool _isCaseSensitive = false;
289 bool _isEnabled = true;
290 bool _isInverse = false;
291 QString _sender = {};
292 QString _chanName = {};
294 // These represent internal cache and should be safe to mutate in 'const' functions
295 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
296 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
297 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
298 mutable ExpressionMatch _senderMatch = {}; ///< Expression match cache for sender
299 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
302 using HighlightRuleList = QList<HighlightRule>;
304 int indexOf(int rule) const;
305 inline bool contains(int rule) const { return indexOf(rule) != -1; }
306 inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
307 inline int count() const { return _highlightRuleList.count(); }
308 inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
309 inline void clear() { _highlightRuleList.clear(); }
310 inline HighlightRule& operator[](int i) { return _highlightRuleList[i]; }
311 inline const HighlightRule& operator[](int i) const { return _highlightRuleList.at(i); }
312 inline const HighlightRuleList& highlightRuleList() const { return _highlightRuleList; }
316 inline int highlightNick() { return _highlightNick; }
317 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
319 //! Check if a message matches the HighlightRule
320 /** This method checks if a message matches the users highlight rules.
321 * \param msg The Message that should be checked
323 bool match(const Message& msg, const QString& currentNick, const QStringList& identityNicks);
326 virtual QVariantMap initHighlightRuleList() const;
327 virtual void initSetHighlightRuleList(const QVariantMap& HighlightRuleList);
329 //! Request removal of an ignore rule based on the rule itself.
330 /** Use this method if you want to remove a single ignore rule
331 * and get that synced with the core immediately.
332 * \param highlightRule A valid ignore rule
334 virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
335 virtual void removeHighlightRule(int highlightRule);
337 //! Request toggling of "isEnabled" flag of a given ignore rule.
338 /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
339 * and get that synced with the core immediately.
340 * \param highlightRule A valid ignore rule
342 virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
343 virtual void toggleHighlightRule(int highlightRule);
345 //! Request an HighlightRule to be added to the ignore list
346 /** Items added to the list with this method, get immediately synced with the core
347 * \param name The rule
348 * \param isRegEx If the rule should be interpreted as a nickname, or a regex
349 * \param isCaseSensitive If the rule should be interpreted as case-sensitive
350 * \param isEnabled If the rule is active
351 * @param chanName The channel in which the rule should apply
353 virtual inline void requestAddHighlightRule(int id,
356 bool isCaseSensitive,
359 const QString& sender,
360 const QString& chanName)
362 REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender), ARG(chanName))
365 virtual void addHighlightRule(int id,
368 bool isCaseSensitive,
371 const QString& sender,
372 const QString& chanName);
374 virtual inline void requestSetHighlightNick(int highlightNick) { REQUEST(ARG(highlightNick)) }
376 inline void setHighlightNick(int highlightNick)
378 _highlightNick = static_cast<HighlightNickType>(highlightNick);
379 // Convert from HighlightRuleManager::HighlightNickType to
380 // NickHighlightMatcher::HighlightNickType
381 _nickMatcher.setHighlightMode(static_cast<NickHighlightMatcher::HighlightNickType>(_highlightNick));
384 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive) { REQUEST(ARG(nicksCaseSensitive)) }
386 inline void setNicksCaseSensitive(bool nicksCaseSensitive)
388 _nicksCaseSensitive = nicksCaseSensitive;
389 // Update nickname matcher, too
390 _nickMatcher.setCaseSensitive(nicksCaseSensitive);
394 * Network removed from system
396 * Handles cleaning up cache from stale networks.
398 * @param id Network ID of removed network
400 inline void networkRemoved(NetworkId id)
402 // Clean up nickname matching cache
403 _nickMatcher.removeNetwork(id);
407 void setHighlightRuleList(const QList<HighlightRule>& HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
409 bool match(const NetworkId& netId,
410 const QString& msgContents,
411 const QString& msgSender,
412 Message::Type msgType,
413 Message::Flags msgFlags,
414 const QString& bufferName,
415 const QString& currentNick,
416 const QStringList& identityNicks);
419 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
422 HighlightRuleList _highlightRuleList = {}; ///< Custom highlight rule list
423 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
425 /// Nickname highlighting mode
426 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
427 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case