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 ***************************************************************************/
26 #include <QStringList>
27 #include <QVariantList>
28 #include <QVariantMap>
30 #include "expressionmatch.h"
32 #include "syncableobject.h"
34 class HighlightRuleManager : public SyncableObject
39 Q_PROPERTY(int highlightNick READ highlightNick WRITE setHighlightNick)
40 Q_PROPERTY(bool nicksCaseSensitive READ nicksCaseSensitive WRITE setNicksCaseSensitive)
43 enum HighlightNickType {
49 inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
50 HighlightRuleManager &operator=(const HighlightRuleManager &other);
53 * Individual highlight rule
59 * Construct an empty highlight rule
64 * Construct a highlight rule with the given parameters
66 * @param id Integer ID of the rule
67 * @param contents String representing a message contents expression to match
68 * @param isRegEx True if regular expression, otherwise false
69 * @param isCaseSensitive True if case sensitive, otherwise false
70 * @param isEnabled True if enabled, otherwise false
71 * @param isInverse True if rule is treated as highlight ignore, otherwise false
72 * @param sender String representing a message sender expression to match
73 * @param chanName String representing a channel name expression to match
75 HighlightRule(int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
76 bool isInverse, QString sender, QString chanName)
77 : _id(id), _contents(contents), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
78 _isEnabled(isEnabled), _isInverse(isInverse), _sender(sender), _chanName(chanName)
81 // Cache expression matches on construction
83 // This provides immediate feedback on errors when loading the rule. If profiling shows
84 // this as a performance bottleneck, this can be removed in deference to caching on
87 // Inversely, if needed for validity checks, caching can be done on every update below
88 // instead of on first use.
89 determineExpressions();
93 * Gets the unique ID of this rule
95 * @return Integer ID of the rule
97 inline int id() const {
101 * Sets the ID of this rule
103 * CAUTION: IDs should be kept unique!
105 * @param id Integer ID of the rule
107 inline void setId(int id) {
112 * Gets the message contents this rule matches
114 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
116 * @return String representing a phrase or expression to match
118 inline QString contents() const {
122 * Sets the message contents this rule matches
124 * @param contents String representing a phrase or expression to match
126 inline void setContents(const QString &contents) {
127 _contents = contents;
128 _cacheInvalid = true;
132 * Gets if this is a regular expression rule
134 * @return True if regular expression, otherwise false
136 inline bool isRegEx() const {
140 * Sets if this rule is a regular expression rule
142 * @param isRegEx True if regular expression, otherwise false
144 inline void setIsRegEx(bool isRegEx) {
146 _cacheInvalid = true;
150 * Gets if this rule is case sensitive
152 * @return True if case sensitive, otherwise false
154 inline bool isCaseSensitive() const {
155 return _isCaseSensitive;
158 * Sets if this rule is case sensitive
160 * @param isCaseSensitive True if case sensitive, otherwise false
162 inline void setIsCaseSensitive(bool isCaseSensitive) {
163 _isCaseSensitive = isCaseSensitive;
164 _cacheInvalid = true;
168 * Gets if this rule is enabled and active
170 * @return True if enabled, otherwise false
172 inline bool isEnabled() const {
176 * Sets if this rule is enabled and active
178 * @param isEnabled True if enabled, otherwise false
180 inline void setIsEnabled(bool isEnabled) {
181 _isEnabled = isEnabled;
185 * Gets if this rule is a highlight ignore rule
187 * @return True if rule is treated as highlight ignore, otherwise false
189 inline bool isInverse() const {
193 * Sets if this rule is a highlight ignore rule
195 * @param isInverse True if rule is treated as highlight ignore, otherwise false
197 inline void setIsInverse(bool isInverse) {
198 _isInverse = isInverse;
202 * Gets the message sender this rule matches
204 * NOTE: Use HighlightRule::senderMatcher() for performing matches
206 * @return String representing a phrase or expression to match
208 inline QString sender() const { return _sender; }
210 * Sets the message sender this rule matches
212 * @param sender String representing a phrase or expression to match
214 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) {
233 _chanName = chanName;
234 _cacheInvalid = true;
238 * Gets the expression matcher for the message contents, caching if needed
240 * @return Expression matcher to compare with message contents
242 inline ExpressionMatch contentsMatcher() const {
244 determineExpressions();
246 return _contentsMatch;
250 * Gets the expression matcher for the message sender, caching if needed
252 * @return Expression matcher to compare with message sender
254 inline ExpressionMatch senderMatcher() const {
256 determineExpressions();
262 * Gets the expression matcher for the channel name, caching if needed
264 * @return Expression matcher to compare with channel name
266 inline ExpressionMatch chanNameMatcher() const {
268 determineExpressions();
270 return _chanNameMatch;
273 bool operator!=(const HighlightRule &other) const;
277 * Update internal cache of expression matching if needed
279 void determineExpressions() const;
282 QString _contents = {};
283 bool _isRegEx = false;
284 bool _isCaseSensitive = false;
285 bool _isEnabled = true;
286 bool _isInverse = false;
287 QString _sender = {};
288 QString _chanName = {};
290 // These represent internal cache and should be safe to mutate in 'const' functions
291 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
292 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
293 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
294 mutable ExpressionMatch _senderMatch = {}; ///< Expression match cache for sender
295 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
298 using HighlightRuleList = QList<HighlightRule>;
300 int indexOf(int rule) const;
301 inline bool contains(int rule) const { return indexOf(rule) != -1; }
302 inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
303 inline int count() const { return _highlightRuleList.count(); }
304 inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
305 inline void clear() { _highlightRuleList.clear(); }
306 inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
307 inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
308 inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
312 inline int highlightNick() { return _highlightNick; }
313 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
315 //! Check if a message matches the HighlightRule
316 /** This method checks if a message matches the users highlight rules.
317 * \param msg The Message that should be checked
319 bool match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks);
322 virtual QVariantMap initHighlightRuleList() const;
323 virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
325 //! Request removal of an ignore rule based on the rule itself.
326 /** Use this method if you want to remove a single ignore rule
327 * and get that synced with the core immediately.
328 * \param highlightRule A valid ignore rule
330 virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
331 virtual void removeHighlightRule(int highlightRule);
333 //! Request toggling of "isEnabled" flag of a given ignore rule.
334 /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
335 * and get that synced with the core immediately.
336 * \param highlightRule A valid ignore rule
338 virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
339 virtual void toggleHighlightRule(int highlightRule);
341 //! Request an HighlightRule to be added to the ignore list
342 /** Items added to the list with this method, get immediately synced with the core
343 * \param name The rule
344 * \param isRegEx If the rule should be interpreted as a nickname, or a regex
345 * \param isCaseSensitive If the rule should be interpreted as case-sensitive
346 * \param isEnabled If the rule is active
347 * @param chanName The channel in which the rule should apply
349 virtual inline void requestAddHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
350 bool isInverse, const QString &sender, const QString &chanName)
352 REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender),
357 virtual void addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
358 bool isInverse, const QString &sender, const QString &chanName);
360 virtual inline void requestSetHighlightNick(int highlightNick)
362 REQUEST(ARG(highlightNick))
365 inline void setHighlightNick(int highlightNick) {
366 _highlightNick = static_cast<HighlightNickType>(highlightNick);
367 _cacheNickConfigInvalid = true;
370 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
372 REQUEST(ARG(nicksCaseSensitive))
375 inline void setNicksCaseSensitive(bool nicksCaseSensitive) {
376 _nicksCaseSensitive = nicksCaseSensitive;
377 _cacheNickConfigInvalid = true;
381 void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
383 bool match(const QString &msgContents,
384 const QString &msgSender,
385 Message::Type msgType,
386 Message::Flags msgFlags,
387 const QString &bufferName,
388 const QString ¤tNick,
389 const QStringList identityNicks);
392 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
396 * Update internal cache of expression matching if needed
398 void determineNickExpressions(const QString ¤tNick,
399 const QStringList identityNicks) const;
402 * Check if nickname matching cache is invalid
404 * @param identityNicks
407 bool cacheNickInvalid(const QString ¤tNick, const QStringList identityNicks) const {
408 if (_cacheNickConfigInvalid) return true;
409 if (_cachedNickCurrent != currentNick) return true;
410 if (_cachedIdentityNicks != identityNicks) return true;
413 HighlightRuleList _highlightRuleList;
414 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
415 bool _nicksCaseSensitive = false;
417 // These represent internal cache and should be safe to mutate in 'const' functions
418 mutable bool _cacheNickConfigInvalid = true; ///< If true, nick match cache needs redone
419 mutable QString _cachedNickCurrent = {}; ///< Last cached current nick
420 mutable QStringList _cachedIdentityNicks = {}; ///< Last cached identity nicks
421 mutable ExpressionMatch _cachedNickMatcher = {}; ///< Expression match cache for nicks