common: Disable enum type stream operators for Qt >= 5.14
[quassel.git] / src / common / highlightrulemanager.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include "common-export.h"
24
25 #include <utility>
26
27 #include <QString>
28 #include <QStringList>
29 #include <QVariantList>
30 #include <QVariantMap>
31
32 #include "expressionmatch.h"
33 #include "message.h"
34 #include "nickhighlightmatcher.h"
35 #include "syncableobject.h"
36
37 class COMMON_EXPORT HighlightRuleManager : public SyncableObject
38 {
39     Q_OBJECT
40     SYNCABLE_OBJECT
41
42     Q_PROPERTY(int highlightNick READ highlightNick WRITE setHighlightNick)
43     Q_PROPERTY(bool nicksCaseSensitive READ nicksCaseSensitive WRITE setNicksCaseSensitive)
44
45 public:
46     enum HighlightNickType
47     {
48         NoNick = 0x00,
49         CurrentNick = 0x01,
50         AllNicks = 0x02
51     };
52
53     inline HighlightRuleManager(QObject* parent = nullptr)
54         : SyncableObject(parent)
55     {
56         setAllowClientUpdates(true);
57     }
58     HighlightRuleManager& operator=(const HighlightRuleManager& other);
59
60     /**
61      * Individual highlight rule
62      */
63     class COMMON_EXPORT HighlightRule
64     {
65     public:
66         /**
67          * Construct an empty highlight rule
68          */
69         HighlightRule() = default;
70
71         /**
72          * Construct a highlight rule with the given parameters
73          *
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
82          */
83         HighlightRule(
84             int id, QString contents, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName)
85             : _id(id)
86             , _contents(std::move(contents))
87             , _isRegEx(isRegEx)
88             , _isCaseSensitive(isCaseSensitive)
89             , _isEnabled(isEnabled)
90             , _isInverse(isInverse)
91             , _sender(std::move(sender))
92             , _chanName(std::move(chanName))
93         {
94             _cacheInvalid = true;
95             // Cache expression matches on construction
96             //
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
99             // first use.
100             //
101             // Inversely, if needed for validity checks, caching can be done on every update below
102             // instead of on first use.
103             determineExpressions();
104         }
105
106         /**
107          * Gets the unique ID of this rule
108          *
109          * @return Integer ID of the rule
110          */
111         inline int id() const { return _id; }
112         /**
113          * Sets the ID of this rule
114          *
115          * CAUTION: IDs should be kept unique!
116          *
117          * @param id Integer ID of the rule
118          */
119         inline void setId(int id) { _id = id; }
120
121         /**
122          * Gets the message contents this rule matches
123          *
124          * NOTE: Use HighlightRule::contentsMatcher() for performing matches
125          *
126          * @return String representing a phrase or expression to match
127          */
128         inline QString contents() const { return _contents; }
129         /**
130          * Sets the message contents this rule matches
131          *
132          * @param contents String representing a phrase or expression to match
133          */
134         inline void setContents(const QString& contents)
135         {
136             _contents = contents;
137             _cacheInvalid = true;
138         }
139
140         /**
141          * Gets if this is a regular expression rule
142          *
143          * @return True if regular expression, otherwise false
144          */
145         inline bool isRegEx() const { return _isRegEx; }
146         /**
147          * Sets if this rule is a regular expression rule
148          *
149          * @param isRegEx True if regular expression, otherwise false
150          */
151         inline void setIsRegEx(bool isRegEx)
152         {
153             _isRegEx = isRegEx;
154             _cacheInvalid = true;
155         }
156
157         /**
158          * Gets if this rule is case sensitive
159          *
160          * @return True if case sensitive, otherwise false
161          */
162         inline bool isCaseSensitive() const { return _isCaseSensitive; }
163         /**
164          * Sets if this rule is case sensitive
165          *
166          * @param isCaseSensitive True if case sensitive, otherwise false
167          */
168         inline void setIsCaseSensitive(bool isCaseSensitive)
169         {
170             _isCaseSensitive = isCaseSensitive;
171             _cacheInvalid = true;
172         }
173
174         /**
175          * Gets if this rule is enabled and active
176          *
177          * @return True if enabled, otherwise false
178          */
179         inline bool isEnabled() const { return _isEnabled; }
180         /**
181          * Sets if this rule is enabled and active
182          *
183          * @param isEnabled True if enabled, otherwise false
184          */
185         inline void setIsEnabled(bool isEnabled) { _isEnabled = isEnabled; }
186
187         /**
188          * Gets if this rule is a highlight ignore rule
189          *
190          * @return True if rule is treated as highlight ignore, otherwise false
191          */
192         inline bool isInverse() const { return _isInverse; }
193         /**
194          * Sets if this rule is a highlight ignore rule
195          *
196          * @param isInverse True if rule is treated as highlight ignore, otherwise false
197          */
198         inline void setIsInverse(bool isInverse) { _isInverse = isInverse; }
199
200         /**
201          * Gets the message sender this rule matches
202          *
203          * NOTE: Use HighlightRule::senderMatcher() for performing matches
204          *
205          * @return String representing a phrase or expression to match
206          */
207         inline QString sender() const { return _sender; }
208         /**
209          * Sets the message sender this rule matches
210          *
211          * @param sender String representing a phrase or expression to match
212          */
213         inline void setSender(const QString& sender)
214         {
215             _sender = sender;
216             _cacheInvalid = true;
217         }
218
219         /**
220          * Gets the channel name this rule matches
221          *
222          * NOTE: Use HighlightRule::chanNameMatcher() for performing matches
223          *
224          * @return String representing a phrase or expression to match
225          */
226         inline QString chanName() const { return _chanName; }
227         /**
228          * Sets the channel name this rule matches
229          *
230          * @param chanName String representing a phrase or expression to match
231          */
232         inline void setChanName(const QString& chanName)
233         {
234             _chanName = chanName;
235             _cacheInvalid = true;
236         }
237
238         /**
239          * Gets the expression matcher for the message contents, caching if needed
240          *
241          * @return Expression matcher to compare with message contents
242          */
243         inline ExpressionMatch contentsMatcher() const
244         {
245             if (_cacheInvalid) {
246                 determineExpressions();
247             }
248             return _contentsMatch;
249         }
250
251         /**
252          * Gets the expression matcher for the message sender, caching if needed
253          *
254          * @return Expression matcher to compare with message sender
255          */
256         inline ExpressionMatch senderMatcher() const
257         {
258             if (_cacheInvalid) {
259                 determineExpressions();
260             }
261             return _senderMatch;
262         }
263
264         /**
265          * Gets the expression matcher for the channel name, caching if needed
266          *
267          * @return Expression matcher to compare with channel name
268          */
269         inline ExpressionMatch chanNameMatcher() const
270         {
271             if (_cacheInvalid) {
272                 determineExpressions();
273             }
274             return _chanNameMatch;
275         }
276
277         bool operator!=(const HighlightRule& other) const;
278
279     private:
280         /**
281          * Update internal cache of expression matching if needed
282          */
283         void determineExpressions() const;
284
285         int _id = -1;
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 = {};
293
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
300     };
301
302     using HighlightRuleList = QList<HighlightRule>;
303
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; }
313
314     int nextId();
315
316     inline int highlightNick() { return _highlightNick; }
317     inline bool nicksCaseSensitive() { return _nicksCaseSensitive; }
318
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
322      */
323     bool match(const Message& msg, const QString& currentNick, const QStringList& identityNicks);
324
325 public slots:
326     virtual QVariantMap initHighlightRuleList() const;
327     virtual void initSetHighlightRuleList(const QVariantMap& HighlightRuleList);
328
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
333      */
334     virtual inline void requestRemoveHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
335     virtual void removeHighlightRule(int highlightRule);
336
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
341      */
342     virtual inline void requestToggleHighlightRule(int highlightRule) { REQUEST(ARG(highlightRule)) }
343     virtual void toggleHighlightRule(int highlightRule);
344
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
352      */
353     virtual inline void requestAddHighlightRule(int id,
354                                                 const QString& name,
355                                                 bool isRegEx,
356                                                 bool isCaseSensitive,
357                                                 bool isEnabled,
358                                                 bool isInverse,
359                                                 const QString& sender,
360                                                 const QString& chanName)
361     {
362         REQUEST(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(isInverse), ARG(sender), ARG(chanName))
363     }
364
365     virtual void addHighlightRule(int id,
366                                   const QString& name,
367                                   bool isRegEx,
368                                   bool isCaseSensitive,
369                                   bool isEnabled,
370                                   bool isInverse,
371                                   const QString& sender,
372                                   const QString& chanName);
373
374     virtual inline void requestSetHighlightNick(int highlightNick) { REQUEST(ARG(highlightNick)) }
375
376     inline void setHighlightNick(int highlightNick)
377     {
378         _highlightNick = static_cast<HighlightNickType>(highlightNick);
379         // Convert from HighlightRuleManager::HighlightNickType to
380         // NickHighlightMatcher::HighlightNickType
381         _nickMatcher.setHighlightMode(static_cast<NickHighlightMatcher::HighlightNickType>(_highlightNick));
382     }
383
384     virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive) { REQUEST(ARG(nicksCaseSensitive)) }
385
386     inline void setNicksCaseSensitive(bool nicksCaseSensitive)
387     {
388         _nicksCaseSensitive = nicksCaseSensitive;
389         // Update nickname matcher, too
390         _nickMatcher.setCaseSensitive(nicksCaseSensitive);
391     }
392
393     /**
394      * Network removed from system
395      *
396      * Handles cleaning up cache from stale networks.
397      *
398      * @param id Network ID of removed network
399      */
400     inline void networkRemoved(NetworkId id)
401     {
402         // Clean up nickname matching cache
403         _nickMatcher.removeNetwork(id);
404     }
405
406 protected:
407     void setHighlightRuleList(const QList<HighlightRule>& HighlightRuleList) { _highlightRuleList = HighlightRuleList; }
408
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);
417
418 signals:
419     void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, bool isInverse, QString sender, QString chanName);
420
421 private:
422     HighlightRuleList _highlightRuleList = {};  ///< Custom highlight rule list
423     NickHighlightMatcher _nickMatcher = {};     ///< Nickname highlight matcher
424
425     /// Nickname highlighting mode
426     HighlightNickType _highlightNick = HighlightNickType::CurrentNick;
427     bool _nicksCaseSensitive = false;  ///< If true, match nicknames with exact case
428 };