7bf4bab3a273e31c06c06b5aabb2e4f9016a7e7a
[quassel.git] / src / common / ignorelistmanager.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 #ifndef IGNORELISTMANAGER_H
22 #define IGNORELISTMANAGER_H
23
24 #include <QDebug>
25 #include <QString>
26 #include <QStringList>
27 #include <QRegExp>
28
29 #include "expressionmatch.h"
30 #include "message.h"
31 #include "syncableobject.h"
32
33 class IgnoreListManager : public SyncableObject
34 {
35     SYNCABLE_OBJECT
36         Q_OBJECT
37 public:
38     inline IgnoreListManager(QObject *parent = 0) : SyncableObject(parent) { setAllowClientUpdates(true); }
39     IgnoreListManager &operator=(const IgnoreListManager &other);
40
41     enum IgnoreType {
42         SenderIgnore,
43         MessageIgnore,
44         CtcpIgnore
45     };
46
47     enum StrictnessType {
48         UnmatchedStrictness = 0,
49         SoftStrictness = 1,
50         HardStrictness = 2
51     };
52
53     enum ScopeType {
54         GlobalScope,
55         NetworkScope,
56         ChannelScope,
57     };
58
59     /**
60      * Individual ignore list rule
61      */
62     class IgnoreListItem {
63     public:
64         /**
65          * Construct an empty ignore rule
66          */
67         IgnoreListItem() {}
68
69         /**
70          * Construct an ignore rule with the given parameters
71          *
72          * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
73          * Duplicate entries are not allowed.
74          *
75          * @param type             Type of ignore rule
76          * @param contents         String representing a message contents expression to match
77          * @param isRegEx          True if regular expression, otherwise false
78          * @param strictness       Strictness of ignore rule
79          * @param scope            What to match scope rule against
80          * @param scopeRule        String representing a scope rule expression to match
81          * @param isEnabled        True if enabled, otherwise false
82          */
83         IgnoreListItem(IgnoreType type, const QString &contents, bool isRegEx,
84                        StrictnessType strictness, ScopeType scope, const QString &scopeRule,
85                        bool isEnabled)
86             : _contents(contents), _isRegEx(isRegEx), _strictness(strictness),
87               _scope(scope), _scopeRule(scopeRule), _isEnabled(isEnabled)
88         {
89             // Allow passing empty "contents" as they can happen when editing an ignore rule
90
91             // Handle CTCP ignores
92             setType(type);
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 type of this ignore rule
108          *
109          * @return IgnoreType of the rule
110          */
111         inline IgnoreType type() const {
112             return _type;
113         }
114         /**
115          * Sets the type of this ignore rule
116          *
117          * @param type IgnoreType of the rule
118          */
119         inline void setType(IgnoreType type) {
120             // Handle CTCP ignores
121             if (type == CtcpIgnore) {
122                 // This is not performance-intensive; sticking with QRegExp for Qt 4 is fine
123                 // Split based on whitespace characters
124                 QStringList split(contents().split(QRegExp("\\s+"), QString::SkipEmptyParts));
125                 // Match on the first item
126                 _cacheCtcpSender = split.takeFirst();
127                 // Track the rest as CTCP types to ignore
128                 _cacheCtcpTypes = split;
129             }
130             _type = type;
131         }
132
133         /**
134          * Gets the message contents this rule matches
135          *
136          * NOTE: Use IgnoreListItem::contentsMatcher() for performing matches
137          *
138          * CAUTION: For legacy reasons, "contents" doubles as the identifier for the ignore rule.
139          * Duplicate entries are not allowed.
140          *
141          * @return String representing a phrase or expression to match
142          */
143         inline QString contents() const {
144             return _contents;
145         }
146         /**
147          * Sets the message contents this rule matches
148          *
149          * @param contents String representing a phrase or expression to match
150          */
151         inline void setContents(const QString &contents) {
152             // Allow passing empty "contents" as they can happen when editing an ignore rule
153             _contents = contents;
154             _cacheInvalid = true;
155         }
156
157         /**
158          * Gets if this is a regular expression rule
159          *
160          * @return True if regular expression, otherwise false
161          */
162         inline bool isRegEx() const {
163             return _isRegEx;
164         }
165         /**
166          * Sets if this rule is a regular expression rule
167          *
168          * @param isRegEx True if regular expression, otherwise false
169          */
170         inline void setIsRegEx(bool isRegEx) {
171             _isRegEx = isRegEx;
172             _cacheInvalid = true;
173         }
174
175         /**
176          * Gets the strictness of this ignore rule
177          *
178          * @return StrictnessType of the rule
179          */
180         inline StrictnessType strictness() const {
181             return _strictness;
182         }
183         /**
184          * Sets the strictness of this ignore rule
185          *
186          * @param strictness StrictnessType of the rule
187          */
188         inline void setStrictness(StrictnessType strictness) {
189             _strictness = strictness;
190         }
191
192         /**
193          * Gets what to match scope rule against
194          *
195          * @return ScopeType of the rule
196          */
197         inline ScopeType scope() const {
198             return _scope;
199         }
200         /**
201          * Sets what to match scope rule against
202          *
203          * @param type ScopeType of the rule
204          */
205         inline void setScope(ScopeType scope) {
206             _scope = scope;
207         }
208
209         /**
210          * Gets the scope rule this rule matches
211          *
212          * NOTE: Use IgnoreListItem::scopeRuleMatcher() for performing matches
213          *
214          * @return String representing a phrase or expression to match
215          */
216         inline QString scopeRule() const {
217             return _scopeRule;
218         }
219         /**
220          * Sets the scope rule this rule matches
221          *
222          * @param scopeRule String representing a phrase or expression to match
223          */
224         inline void setScopeRule(const QString &scopeRule) {
225             _scopeRule = scopeRule;
226             _cacheInvalid = true;
227         }
228
229         /**
230          * Gets if this rule is enabled and active
231          *
232          * @return True if enabled, otherwise false
233          */
234         inline bool isEnabled() const {
235             return _isEnabled;
236         }
237         /**
238          * Sets if this rule is enabled and active
239          *
240          * @param isEnabled True if enabled, otherwise false
241          */
242         inline void setIsEnabled(bool isEnabled) {
243             _isEnabled = isEnabled;
244         }
245
246         /**
247          * Gets the ignored CTCP types for CTCP ignores
248          *
249          * @return List of CTCP types to ignore, or empty for all
250          */
251         inline QStringList ctcpTypes() const {
252             return _cacheCtcpTypes;
253         }
254
255         /**
256          * Gets the expression matcher for the message contents, caching if needed
257          *
258          * @return Expression matcher to compare with message contents
259          */
260         inline ExpressionMatch contentsMatcher() const {
261             if (_cacheInvalid) {
262                 determineExpressions();
263             }
264             return _contentsMatch;
265         }
266
267         /**
268          * Gets the expression matcher for the scope, caching if needed
269          *
270          * @return Expression matcher to compare with scope
271          */
272         inline ExpressionMatch scopeRuleMatcher() const {
273             if (_cacheInvalid) {
274                 determineExpressions();
275             }
276             return _scopeRuleMatch;
277         }
278
279         /**
280          * Gets the expression matcher for the message contents, caching if needed
281          *
282          * @return Expression matcher to compare with message contents
283          */
284         inline ExpressionMatch senderCTCPMatcher() const {
285             if (_cacheInvalid) {
286                 determineExpressions();
287             }
288             return _ctcpSenderMatch;
289         }
290
291         bool operator!=(const IgnoreListItem &other) const;
292
293     private:
294         /**
295          * Update internal cache of expression matching if needed
296          */
297         void determineExpressions() const;
298
299         IgnoreType _type = {};
300         QString _contents = {};
301         bool _isRegEx = false;
302         StrictnessType _strictness = {};
303         ScopeType _scope = {};
304         QString _scopeRule = {};
305         bool _isEnabled = true;
306
307         QString _cacheCtcpSender = {};                    ///< For CTCP rules, precalculate sender
308         QStringList _cacheCtcpTypes = {};                 ///< For CTCP rules, precalculate types
309
310         // These represent internal cache and should be safe to mutate in 'const' functions
311         // See https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration
312         mutable bool _cacheInvalid = true;                ///< If true, match cache needs redone
313         mutable ExpressionMatch _contentsMatch = {};      ///< Expression match cache for message
314         mutable ExpressionMatch _scopeRuleMatch = {};     ///< Expression match cache for scope rule
315         mutable ExpressionMatch _ctcpSenderMatch = {};    ///< Expression match cache for CTCP nick
316     };
317
318     typedef QList<IgnoreListItem> IgnoreList;
319
320     int indexOf(const QString &ignore) const;
321     inline bool contains(const QString &ignore) const { return indexOf(ignore) != -1; }
322     inline bool isEmpty() const { return _ignoreList.isEmpty(); }
323     inline int count() const { return _ignoreList.count(); }
324     inline void removeAt(int index) { _ignoreList.removeAt(index); }
325     inline IgnoreListItem &operator[](int i) { return _ignoreList[i]; }
326     inline const IgnoreListItem &operator[](int i) const { return _ignoreList.at(i); }
327     inline const IgnoreList &ignoreList() const { return _ignoreList; }
328
329     //! Check if a message matches the IgnoreRule
330     /** This method checks if a message matches the users ignorelist.
331       * \param msg The Message that should be checked
332       * \param network The networkname the message belongs to
333       * \return UnmatchedStrictness, HardStrictness or SoftStrictness representing the match type
334       */
335     inline StrictnessType match(const Message &msg, const QString &network = QString()) { return _match(msg.contents(), msg.sender(), msg.type(), network, msg.bufferInfo().bufferName()); }
336
337     bool ctcpMatch(const QString sender, const QString &network, const QString &type = QString());
338
339 //  virtual void addIgnoreListItem(const IgnoreListItem &item);
340
341 public slots:
342     virtual QVariantMap initIgnoreList() const;
343     virtual void initSetIgnoreList(const QVariantMap &ignoreList);
344
345     //! Request removal of an ignore rule based on the rule itself.
346     /** Use this method if you want to remove a single ignore rule
347       * and get that synced with the core immediately.
348       * \param ignoreRule A valid ignore rule
349       */
350     virtual inline void requestRemoveIgnoreListItem(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
351     virtual void removeIgnoreListItem(const QString &ignoreRule);
352
353     //! Request toggling of "isActive" flag of a given ignore rule.
354     /** Use this method if you want to toggle the "isActive" flag of a single ignore rule
355       * and get that synced with the core immediately.
356       * \param ignoreRule A valid ignore rule
357       */
358     virtual inline void requestToggleIgnoreRule(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
359     virtual void toggleIgnoreRule(const QString &ignoreRule);
360
361     //! Request an IgnoreListItem to be added to the ignore list
362     /** Items added to the list with this method, get immediately synced with the core
363       * \param type The IgnoreType of the new rule
364       * \param ignoreRule The rule itself
365       * \param isRegEx Signals if the rule should be interpreted as a regular expression
366       * \param strictness Th StrictnessType that should be applied
367       * \param scope The ScopeType that should be set
368       * \param scopeRule A string of semi-colon separated network- or channelnames
369       * \param isActive Signals if the rule is enabled or not
370       */
371     virtual inline void requestAddIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
372         int scope, const QString &scopeRule, bool isActive)
373     {
374         REQUEST(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
375     }
376
377
378     virtual void addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
379         int scope, const QString &scopeRule, bool isActive);
380
381 protected:
382     void setIgnoreList(const QList<IgnoreListItem> &ignoreList) { _ignoreList = ignoreList; }
383
384     StrictnessType _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName);
385
386 signals:
387     void ignoreAdded(IgnoreType type, const QString &ignoreRule, bool isRegex, StrictnessType strictness, ScopeType scope, const QVariant &scopeRule, bool isActive);
388
389 private:
390     IgnoreList _ignoreList;
391 };
392
393
394 #endif // IGNORELISTMANAGER_H