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"
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);
60 * Individual highlight rule
62 class COMMON_EXPORT HighlightRule
66 * Construct an empty highlight rule
68 HighlightRule() = default;
71 * Construct a highlight rule with the given parameters
73 * @param id Integer ID of the rule
74 * @param contents String representing a message contents expression to match
75 * @param isRegEx True if regular expression, otherwise false
76 * @param isCaseSensitive True if case sensitive, otherwise false
77 * @param isEnabled True if enabled, otherwise false
78 * @param isInverse True if rule is treated as highlight ignore, otherwise false
79 * @param sender String representing a message sender expression to match
80 * @param chanName String representing a channel name expression to match
83 int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName)
85 , _contents(std::move(contents))
87 , _isCaseSensitive(isCaseSensitive)
88 , _isEnabled(isEnabled)
89 , _isInverse(isInverse)
90 , _sender(std::move(sender))
91 , _chanName(std::move(chanName))
94 // Cache expression matches on construction
96 // This provides immediate feedback on errors when loading the rule. If profiling shows
97 // this as a performance bottleneck, this can be removed in deference to caching on
100 // Inversely, if needed for validity checks, caching can be done on every update below
101 // instead of on first use.
102 determineExpressions();
106 * Gets the unique ID of this rule
108 * @return Integer ID of the rule
110 inline int id() const { return _id; }
112 * Sets the ID of this rule
114 * CAUTION: IDs should be kept unique!
116 * @param id Integer ID of the rule
118 inline void setId(int id) { _id = id; }
121 * Gets the message contents this rule matches
123 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
125 * @return String representing a phrase or expression to match
127 inline QString contents() const { return _contents; }
129 * Sets the message contents this rule matches
131 * @param contents String representing a phrase or expression to match
133 inline void setContents(const QString& contents)
135 _contents = contents;
136 _cacheInvalid = true;
140 * Gets if this is a regular expression rule
142 * @return True if regular expression, otherwise false
144 inline bool isRegEx() const { return _isRegEx; }
146 * Sets if this rule is a regular expression rule
148 * @param isRegEx True if regular expression, otherwise false
150 inline void setIsRegEx(bool isRegEx)
153 _cacheInvalid = true;
157 * Gets if this rule is case sensitive
159 * @return True if case sensitive, otherwise false
161 inline bool isCaseSensitive() const { return _isCaseSensitive; }
163 * Sets if this rule is case sensitive
165 * @param isCaseSensitive True if case sensitive, otherwise false
167 inline void setIsCaseSensitive(bool isCaseSensitive)
169 _isCaseSensitive = isCaseSensitive;
170 _cacheInvalid = true;
174 * Gets if this rule is enabled and active
176 * @return True if enabled, otherwise false
178 inline bool isEnabled() const { return _isEnabled; }
180 * Sets if this rule is enabled and active
182 * @param isEnabled True if enabled, otherwise false
184 inline void setIsEnabled(bool isEnabled) { _isEnabled = isEnabled; }
187 * Gets if this rule is a highlight ignore rule
189 * @return True if rule is treated as highlight ignore, otherwise false
191 inline bool isInverse() const { return _isInverse; }
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) { _isInverse = isInverse; }
200 * Gets the message sender this rule matches
202 * NOTE: Use HighlightRule::senderMatcher() for performing matches
204 * @return String representing a phrase or expression to match
206 inline QString sender() const { return _sender; }
208 * Sets the message sender this rule matches
210 * @param sender String representing a phrase or expression to match
212 inline void setSender(const QString& sender)
215 _cacheInvalid = true;
219 * Gets the channel name this rule matches
221 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
223 * @return String representing a phrase or expression to match
225 inline QString chanName() const { return _chanName; }
227 * Sets the channel name this rule matches
229 * @param chanName String representing a phrase or expression to match
231 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
245 determineExpressions();
247 return _contentsMatch;
251 * Gets the expression matcher for the message sender, caching if needed
253 * @return Expression matcher to compare with message sender
255 inline ExpressionMatch senderMatcher() const
258 determineExpressions();
264 * Gets the expression matcher for the channel name, caching if needed
266 * @return Expression matcher to compare with channel name
268 inline ExpressionMatch chanNameMatcher() const
271 determineExpressions();
273 return _chanNameMatch;
276 bool operator!=(const HighlightRule& other) const;
280 * Update internal cache of expression matching if needed
282 void determineExpressions() const;
285 QString _contents = {};
286 bool _isRegEx = false;
287 bool _isCaseSensitive = false;
288 bool _isEnabled = true;
289 bool _isInverse = false;
290 QString _sender = {};
291 QString _chanName = {};
293 // These represent internal cache and should be safe to mutate in 'const' functions
294 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
295 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
296 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
297 mutable ExpressionMatch _senderMatch = {}; ///< Expression match cache for sender
298 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
301 using HighlightRuleList = QList<HighlightRule>;
303 int indexOf(int rule) const;
304 inline bool contains(int rule) const { return indexOf(rule) != -1; }
305 inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
306 inline int count() const { return _highlightRuleList.count(); }
307 inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
308 inline void clear() { _highlightRuleList.clear(); }
309 inline HighlightRule& operator[](int i) { return _highlightRuleList[i]; }
310 inline const HighlightRule& operator[](int i) const { return _highlightRuleList.at(i); }
311 inline const HighlightRuleList& highlightRuleList() const { return _highlightRuleList; }
315 inline int highlightNick() { return _highlightNick; }
316 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
318 //! Check if a message matches the HighlightRule
319 /** This method checks if a message matches the users highlight rules.
320 * \param msg The Message that should be checked
322 bool match(const Message& msg, const QString& currentNick, const QStringList& identityNicks);
325 virtual QVariantMap initHighlightRuleList() const;
326 virtual void initSetHighlightRuleList(const QVariantMap& HighlightRuleList);
328 //! Request removal of an ignore rule based on the rule itself.
329 /** Use this method if you want to remove a single ignore rule
330 * and get that synced with the core immediately.
331 * \param highlightRule A valid ignore rule
333 virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
334 virtual void removeHighlightRule(int highlightRule);
336 //! Request toggling of "isEnabled" flag of a given ignore rule.
337 /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
338 * and get that synced with the core immediately.
339 * \param highlightRule A valid ignore rule
341 virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
342 virtual void toggleHighlightRule(int highlightRule);
344 //! Request an HighlightRule to be added to the ignore list
345 /** Items added to the list with this method, get immediately synced with the core
346 * \param name The rule
347 * \param isRegEx If the rule should be interpreted as a nickname, or a regex
348 * \param isCaseSensitive If the rule should be interpreted as case-sensitive
349 * \param isEnabled If the rule is active
350 * @param chanName The channel in which the rule should apply
352 virtual inline void requestAddHighlightRule(int id,
355 bool isCaseSensitive,
358 const QString& sender,
359 const QString& chanName)
361 REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender), ARG(chanName))
364 virtual void addHighlightRule(int id,
367 bool isCaseSensitive,
370 const QString& sender,
371 const QString& chanName);
373 virtual inline void requestSetHighlightNick(int highlightNick) { REQUEST(ARG(highlightNick)) }
375 inline void setHighlightNick(int highlightNick)
377 _highlightNick = static_cast<HighlightNickType>(highlightNick);
378 // Convert from HighlightRuleManager::HighlightNickType to
379 // NickHighlightMatcher::HighlightNickType
380 _nickMatcher.setHighlightMode(static_cast<NickHighlightMatcher::HighlightNickType>(_highlightNick));
383 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive) { REQUEST(ARG(nicksCaseSensitive)) }
385 inline void setNicksCaseSensitive(bool nicksCaseSensitive)
387 _nicksCaseSensitive = nicksCaseSensitive;
388 // Update nickname matcher, too
389 _nickMatcher.setCaseSensitive(nicksCaseSensitive);
393 * Network removed from system
395 * Handles cleaning up cache from stale networks.
397 * @param id Network ID of removed network
399 inline void networkRemoved(NetworkId id)
401 // Clean up nickname matching cache
402 _nickMatcher.removeNetwork(id);
406 void setHighlightRuleList(const QList<HighlightRule>& HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
408 bool match(const NetworkId& netId,
409 const QString& msgContents,
410 const QString& msgSender,
411 Message::Type msgType,
412 Message::Flags msgFlags,
413 const QString& bufferName,
414 const QString& currentNick,
415 const QStringList& identityNicks);
418 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
421 HighlightRuleList _highlightRuleList = {}; ///< Custom highlight rule list
422 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
424 /// Nickname highlighting mode
425 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
426 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case