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 "nickhighlightmatcher.h"
33 #include "syncableobject.h"
35 class HighlightRuleManager : public SyncableObject
40 Q_PROPERTY(int highlightNick READ highlightNick WRITE setHighlightNick)
41 Q_PROPERTY(bool nicksCaseSensitive READ nicksCaseSensitive WRITE setNicksCaseSensitive)
44 enum HighlightNickType {
50 inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
51 HighlightRuleManager &operator=(const HighlightRuleManager &other);
54 * Individual highlight rule
60 * Construct an empty highlight rule
65 * Construct a highlight rule with the given parameters
67 * @param id Integer ID of the rule
68 * @param contents String representing a message contents expression to match
69 * @param isRegEx True if regular expression, otherwise false
70 * @param isCaseSensitive True if case sensitive, otherwise false
71 * @param isEnabled True if enabled, otherwise false
72 * @param isInverse True if rule is treated as highlight ignore, otherwise false
73 * @param sender String representing a message sender expression to match
74 * @param chanName String representing a channel name expression to match
76 HighlightRule(int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
77 bool isInverse, QString sender, QString chanName)
78 : _id(id), _contents(contents), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
79 _isEnabled(isEnabled), _isInverse(isInverse), _sender(sender), _chanName(chanName)
82 // Cache expression matches on construction
84 // This provides immediate feedback on errors when loading the rule. If profiling shows
85 // this as a performance bottleneck, this can be removed in deference to caching on
88 // Inversely, if needed for validity checks, caching can be done on every update below
89 // instead of on first use.
90 determineExpressions();
94 * Gets the unique ID of this rule
96 * @return Integer ID of the rule
98 inline int id() const {
102 * Sets the ID of this rule
104 * CAUTION: IDs should be kept unique!
106 * @param id Integer ID of the rule
108 inline void setId(int id) {
113 * Gets the message contents this rule matches
115 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
117 * @return String representing a phrase or expression to match
119 inline QString contents() const {
123 * Sets the message contents this rule matches
125 * @param contents String representing a phrase or expression to match
127 inline void setContents(const QString &contents) {
128 _contents = contents;
129 _cacheInvalid = true;
133 * Gets if this is a regular expression rule
135 * @return True if regular expression, otherwise false
137 inline bool isRegEx() const {
141 * Sets if this rule is a regular expression rule
143 * @param isRegEx True if regular expression, otherwise false
145 inline void setIsRegEx(bool isRegEx) {
147 _cacheInvalid = true;
151 * Gets if this rule is case sensitive
153 * @return True if case sensitive, otherwise false
155 inline bool isCaseSensitive() const {
156 return _isCaseSensitive;
159 * Sets if this rule is case sensitive
161 * @param isCaseSensitive True if case sensitive, otherwise false
163 inline void setIsCaseSensitive(bool isCaseSensitive) {
164 _isCaseSensitive = isCaseSensitive;
165 _cacheInvalid = true;
169 * Gets if this rule is enabled and active
171 * @return True if enabled, otherwise false
173 inline bool isEnabled() const {
177 * Sets if this rule is enabled and active
179 * @param isEnabled True if enabled, otherwise false
181 inline void setIsEnabled(bool isEnabled) {
182 _isEnabled = isEnabled;
186 * Gets if this rule is a highlight ignore rule
188 * @return True if rule is treated as highlight ignore, otherwise false
190 inline bool isInverse() const {
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) {
199 _isInverse = isInverse;
203 * Gets the message sender this rule matches
205 * NOTE: Use HighlightRule::senderMatcher() for performing matches
207 * @return String representing a phrase or expression to match
209 inline QString sender() const { return _sender; }
211 * Sets the message sender this rule matches
213 * @param sender String representing a phrase or expression to match
215 inline void setSender(const QString &sender) {
217 _cacheInvalid = true;
221 * Gets the channel name this rule matches
223 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
225 * @return String representing a phrase or expression to match
227 inline QString chanName() const { return _chanName; }
229 * Sets the channel name this rule matches
231 * @param chanName String representing a phrase or expression to match
233 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 {
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 {
257 determineExpressions();
263 * Gets the expression matcher for the channel name, caching if needed
265 * @return Expression matcher to compare with channel name
267 inline ExpressionMatch chanNameMatcher() const {
269 determineExpressions();
271 return _chanNameMatch;
274 bool operator!=(const HighlightRule &other) const;
278 * Update internal cache of expression matching if needed
280 void determineExpressions() const;
283 QString _contents = {};
284 bool _isRegEx = false;
285 bool _isCaseSensitive = false;
286 bool _isEnabled = true;
287 bool _isInverse = false;
288 QString _sender = {};
289 QString _chanName = {};
291 // These represent internal cache and should be safe to mutate in 'const' functions
292 // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
293 mutable bool _cacheInvalid = true; ///< If true, match cache needs redone
294 mutable ExpressionMatch _contentsMatch = {}; ///< Expression match cache for message content
295 mutable ExpressionMatch _senderMatch = {}; ///< Expression match cache for sender
296 mutable ExpressionMatch _chanNameMatch = {}; ///< Expression match cache for channel name
299 using HighlightRuleList = QList<HighlightRule>;
301 int indexOf(int rule) const;
302 inline bool contains(int rule) const { return indexOf(rule) != -1; }
303 inline bool isEmpty() const { return _highlightRuleList.isEmpty(); }
304 inline int count() const { return _highlightRuleList.count(); }
305 inline void removeAt(int index) { _highlightRuleList.removeAt(index); }
306 inline void clear() { _highlightRuleList.clear(); }
307 inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; }
308 inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); }
309 inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; }
313 inline int highlightNick() { return _highlightNick; }
314 inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
316 //! Check if a message matches the HighlightRule
317 /** This method checks if a message matches the users highlight rules.
318 * \param msg The Message that should be checked
320 bool match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks);
323 virtual QVariantMap initHighlightRuleList() const;
324 virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList);
326 //! Request removal of an ignore rule based on the rule itself.
327 /** Use this method if you want to remove a single ignore rule
328 * and get that synced with the core immediately.
329 * \param highlightRule A valid ignore rule
331 virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
332 virtual void removeHighlightRule(int highlightRule);
334 //! Request toggling of "isEnabled" flag of a given ignore rule.
335 /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule
336 * and get that synced with the core immediately.
337 * \param highlightRule A valid ignore rule
339 virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
340 virtual void toggleHighlightRule(int highlightRule);
342 //! Request an HighlightRule to be added to the ignore list
343 /** Items added to the list with this method, get immediately synced with the core
344 * \param name The rule
345 * \param isRegEx If the rule should be interpreted as a nickname, or a regex
346 * \param isCaseSensitive If the rule should be interpreted as case-sensitive
347 * \param isEnabled If the rule is active
348 * @param chanName The channel in which the rule should apply
350 virtual inline void requestAddHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
351 bool isInverse, const QString &sender, const QString &chanName)
353 REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender),
358 virtual void addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
359 bool isInverse, const QString &sender, const QString &chanName);
361 virtual inline void requestSetHighlightNick(int highlightNick)
363 REQUEST(ARG(highlightNick))
366 inline void setHighlightNick(int highlightNick) {
367 _highlightNick = static_cast<HighlightNickType>(highlightNick);
368 // Convert from HighlightRuleManager::HighlightNickType to
369 // NickHighlightMatcher::HighlightNickType
370 _nickMatcher.setHighlightMode(
371 static_cast<NickHighlightMatcher::HighlightNickType>(_highlightNick));
374 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
376 REQUEST(ARG(nicksCaseSensitive))
379 inline void setNicksCaseSensitive(bool nicksCaseSensitive) {
380 _nicksCaseSensitive = nicksCaseSensitive;
381 // Update nickname matcher, too
382 _nickMatcher.setCaseSensitive(nicksCaseSensitive);
386 * Network removed from system
388 * Handles cleaning up cache from stale networks.
390 * @param id Network ID of removed network
392 inline void networkRemoved(NetworkId id) {
393 // Clean up nickname matching cache
394 _nickMatcher.removeNetwork(id);
398 void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
400 bool match(const NetworkId &netId,
401 const QString &msgContents,
402 const QString &msgSender,
403 Message::Type msgType,
404 Message::Flags msgFlags,
405 const QString &bufferName,
406 const QString ¤tNick,
407 const QStringList &identityNicks);
410 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
413 HighlightRuleList _highlightRuleList = {}; ///< Custom highlight rule list
414 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
416 /// Nickname highlighting mode
417 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
418 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case