common: Put scopeMatch in util, use in highlights
[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 <QString>
25 #include <QRegExp>
26
27 #include "message.h"
28 #include "syncableobject.h"
29 // Scope matching
30 #include "util.h"
31
32 class IgnoreListManager : public SyncableObject
33 {
34     SYNCABLE_OBJECT
35         Q_OBJECT
36 public:
37     inline IgnoreListManager(QObject *parent = 0) : SyncableObject(parent) { setAllowClientUpdates(true); }
38     IgnoreListManager &operator=(const IgnoreListManager &other);
39
40     enum IgnoreType {
41         SenderIgnore,
42         MessageIgnore,
43         CtcpIgnore
44     };
45
46     enum StrictnessType {
47         UnmatchedStrictness = 0,
48         SoftStrictness = 1,
49         HardStrictness = 2
50     };
51
52     enum ScopeType {
53         GlobalScope,
54         NetworkScope,
55         ChannelScope,
56     };
57
58     struct IgnoreListItem {
59         IgnoreType type;
60         QString ignoreRule;
61         bool isRegEx;
62         StrictnessType strictness;
63         ScopeType scope;
64         QString scopeRule;
65         bool isActive;
66         QRegExp regEx;
67         IgnoreListItem() {}
68         IgnoreListItem(IgnoreType type_, const QString &ignoreRule_, bool isRegEx_, StrictnessType strictness_,
69             ScopeType scope_, const QString &scopeRule_, bool isActive_)
70             : type(type_), ignoreRule(ignoreRule_), isRegEx(isRegEx_), strictness(strictness_), scope(scope_), scopeRule(scopeRule_), isActive(isActive_), regEx(ignoreRule_) {
71             regEx.setCaseSensitivity(Qt::CaseInsensitive);
72             if (!isRegEx_) {
73                 regEx.setPatternSyntax(QRegExp::Wildcard);
74             }
75         }
76         bool operator!=(const IgnoreListItem &other)
77         {
78             return (type != other.type ||
79                     ignoreRule != other.ignoreRule ||
80                     isRegEx != other.isRegEx ||
81                     strictness != other.strictness ||
82                     scope != other.scope ||
83                     scopeRule != other.scopeRule ||
84                     isActive != other.isActive);
85         }
86     };
87     typedef QList<IgnoreListItem> IgnoreList;
88
89     int indexOf(const QString &ignore) const;
90     inline bool contains(const QString &ignore) const { return indexOf(ignore) != -1; }
91     inline bool isEmpty() const { return _ignoreList.isEmpty(); }
92     inline int count() const { return _ignoreList.count(); }
93     inline void removeAt(int index) { _ignoreList.removeAt(index); }
94     inline IgnoreListItem &operator[](int i) { return _ignoreList[i]; }
95     inline const IgnoreListItem &operator[](int i) const { return _ignoreList.at(i); }
96     inline const IgnoreList &ignoreList() const { return _ignoreList; }
97
98     //! Check if a message matches the IgnoreRule
99     /** This method checks if a message matches the users ignorelist.
100       * \param msg The Message that should be checked
101       * \param network The networkname the message belongs to
102       * \return UnmatchedStrictness, HardStrictness or SoftStrictness representing the match type
103       */
104     inline StrictnessType match(const Message &msg, const QString &network = QString()) { return _match(msg.contents(), msg.sender(), msg.type(), network, msg.bufferInfo().bufferName()); }
105
106     bool ctcpMatch(const QString sender, const QString &network, const QString &type = QString());
107
108 //  virtual void addIgnoreListItem(const IgnoreListItem &item);
109
110 public slots:
111     virtual QVariantMap initIgnoreList() const;
112     virtual void initSetIgnoreList(const QVariantMap &ignoreList);
113
114     //! Request removal of an ignore rule based on the rule itself.
115     /** Use this method if you want to remove a single ignore rule
116       * and get that synced with the core immediately.
117       * \param ignoreRule A valid ignore rule
118       */
119     virtual inline void requestRemoveIgnoreListItem(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
120     virtual void removeIgnoreListItem(const QString &ignoreRule);
121
122     //! Request toggling of "isActive" flag of a given ignore rule.
123     /** Use this method if you want to toggle the "isActive" flag of a single ignore rule
124       * and get that synced with the core immediately.
125       * \param ignoreRule A valid ignore rule
126       */
127     virtual inline void requestToggleIgnoreRule(const QString &ignoreRule) { REQUEST(ARG(ignoreRule)) }
128     virtual void toggleIgnoreRule(const QString &ignoreRule);
129
130     //! Request an IgnoreListItem to be added to the ignore list
131     /** Items added to the list with this method, get immediately synced with the core
132       * \param type The IgnoreType of the new rule
133       * \param ignoreRule The rule itself
134       * \param isRegEx Signals if the rule should be interpreted as a regular expression
135       * \param strictness Th StrictnessType that should be applied
136       * \param scope The ScopeType that should be set
137       * \param scopeRule A string of semi-colon separated network- or channelnames
138       * \param isActive Signals if the rule is enabled or not
139       */
140     virtual inline void requestAddIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
141         int scope, const QString &scopeRule, bool isActive)
142     {
143         REQUEST(ARG(type), ARG(ignoreRule), ARG(isRegEx), ARG(strictness), ARG(scope), ARG(scopeRule), ARG(isActive))
144     }
145
146
147     virtual void addIgnoreListItem(int type, const QString &ignoreRule, bool isRegEx, int strictness,
148         int scope, const QString &scopeRule, bool isActive);
149
150 protected:
151     void setIgnoreList(const QList<IgnoreListItem> &ignoreList) { _ignoreList = ignoreList; }
152
153     StrictnessType _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, const QString &network, const QString &bufferName);
154
155 signals:
156     void ignoreAdded(IgnoreType type, const QString &ignoreRule, bool isRegex, StrictnessType strictness, ScopeType scope, const QVariant &scopeRule, bool isActive);
157
158 private:
159     IgnoreList _ignoreList;
160 };
161
162
163 #endif // IGNORELISTMANAGER_H