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 ***************************************************************************/
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 {
52 inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); }
53 HighlightRuleManager &operator=(const HighlightRuleManager &other);
56 * Individual highlight rule
58 class COMMON_EXPORT HighlightRule
62 * Construct an empty highlight rule
64 HighlightRule() = default;
67 * Construct a highlight rule with the given parameters
69 * @param id Integer ID of the rule
70 * @param contents String representing a message contents expression to match
71 * @param isRegEx True if regular expression, otherwise false
72 * @param isCaseSensitive True if case sensitive, otherwise false
73 * @param isEnabled True if enabled, otherwise false
74 * @param isInverse True if rule is treated as highlight ignore, otherwise false
75 * @param sender String representing a message sender expression to match
76 * @param chanName String representing a channel name expression to match
78 HighlightRule(int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled,
79 bool isInverse, QString sender, QString chanName)
80 : _id(id), _contents(std::move(contents)), _isRegEx(isRegEx), _isCaseSensitive(isCaseSensitive),
81 _isEnabled(isEnabled), _isInverse(isInverse), _sender(std::move(sender)), _chanName(std::move(chanName))
84 // Cache expression matches on construction
86 // This provides immediate feedback on errors when loading the rule. If profiling shows
87 // this as a performance bottleneck, this can be removed in deference to caching on
90 // Inversely, if needed for validity checks, caching can be done on every update below
91 // instead of on first use.
92 determineExpressions();
96 * Gets the unique ID of this rule
98 * @return Integer ID of the rule
100 inline int id() const {
104 * Sets the ID of this rule
106 * CAUTION: IDs should be kept unique!
108 * @param id Integer ID of the rule
110 inline void setId(int id) {
115 * Gets the message contents this rule matches
117 * NOTE: Use HighlightRule::contentsMatcher() for performing matches
119 * @return String representing a phrase or expression to match
121 inline QString contents() const {
125 * Sets the message contents this rule matches
127 * @param contents String representing a phrase or expression to match
129 inline void setContents(const QString &contents) {
130 _contents = contents;
131 _cacheInvalid = true;
135 * Gets if this is a regular expression rule
137 * @return True if regular expression, otherwise false
139 inline bool isRegEx() const {
143 * Sets if this rule is a regular expression rule
145 * @param isRegEx True if regular expression, otherwise false
147 inline void setIsRegEx(bool isRegEx) {
149 _cacheInvalid = true;
153 * Gets if this rule is case sensitive
155 * @return True if case sensitive, otherwise false
157 inline bool isCaseSensitive() const {
158 return _isCaseSensitive;
161 * Sets if this rule is case sensitive
163 * @param isCaseSensitive True if case sensitive, otherwise false
165 inline void setIsCaseSensitive(bool isCaseSensitive) {
166 _isCaseSensitive = isCaseSensitive;
167 _cacheInvalid = true;
171 * Gets if this rule is enabled and active
173 * @return True if enabled, otherwise false
175 inline bool isEnabled() const {
179 * Sets if this rule is enabled and active
181 * @param isEnabled True if enabled, otherwise false
183 inline void setIsEnabled(bool isEnabled) {
184 _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 {
196 * Sets if this rule is a highlight ignore rule
198 * @param isInverse True if rule is treated as highlight ignore, otherwise false
200 inline void setIsInverse(bool isInverse) {
201 _isInverse = isInverse;
205 * Gets the message sender this rule matches
207 * NOTE: Use HighlightRule::senderMatcher() for performing matches
209 * @return String representing a phrase or expression to match
211 inline QString sender() const { return _sender; }
213 * Sets the message sender this rule matches
215 * @param sender String representing a phrase or expression to match
217 inline void setSender(const QString &sender) {
219 _cacheInvalid = true;
223 * Gets the channel name this rule matches
225 * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
227 * @return String representing a phrase or expression to match
229 inline QString chanName() const { return _chanName; }
231 * Sets the channel name this rule matches
233 * @param chanName String representing a phrase or expression to match
235 inline void setChanName(const QString &chanName) {
236 _chanName = chanName;
237 _cacheInvalid = true;
241 * Gets the expression matcher for the message contents, caching if needed
243 * @return Expression matcher to compare with message contents
245 inline ExpressionMatch contentsMatcher() const {
247 determineExpressions();
249 return _contentsMatch;
253 * Gets the expression matcher for the message sender, caching if needed
255 * @return Expression matcher to compare with message sender
257 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 {
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 ¤tNick, 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, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
353 bool isInverse, const QString &sender, const QString &chanName)
355 REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender),
360 virtual void addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled,
361 bool isInverse, const QString &sender, const QString &chanName);
363 virtual inline void requestSetHighlightNick(int highlightNick)
365 REQUEST(ARG(highlightNick))
368 inline void setHighlightNick(int highlightNick) {
369 _highlightNick = static_cast<HighlightNickType>(highlightNick);
370 // Convert from HighlightRuleManager::HighlightNickType to
371 // NickHighlightMatcher::HighlightNickType
372 _nickMatcher.setHighlightMode(
373 static_cast<NickHighlightMatcher::HighlightNickType>(_highlightNick));
376 virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive)
378 REQUEST(ARG(nicksCaseSensitive))
381 inline void setNicksCaseSensitive(bool nicksCaseSensitive) {
382 _nicksCaseSensitive = nicksCaseSensitive;
383 // Update nickname matcher, too
384 _nickMatcher.setCaseSensitive(nicksCaseSensitive);
388 * Network removed from system
390 * Handles cleaning up cache from stale networks.
392 * @param id Network ID of removed network
394 inline void networkRemoved(NetworkId id) {
395 // Clean up nickname matching cache
396 _nickMatcher.removeNetwork(id);
400 void setHighlightRuleList(const QList<HighlightRule> &HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
402 bool match(const NetworkId &netId,
403 const QString &msgContents,
404 const QString &msgSender,
405 Message::Type msgType,
406 Message::Flags msgFlags,
407 const QString &bufferName,
408 const QString ¤tNick,
409 const QStringList &identityNicks);
412 void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
415 HighlightRuleList _highlightRuleList = {}; ///< Custom highlight rule list
416 NickHighlightMatcher _nickMatcher = {}; ///< Nickname highlight matcher
418 /// Nickname highlighting mode
419 HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
420 bool _nicksCaseSensitive = false; ///< If true, match nicknames with exact case