common: Port remote nicks to NickHighlightMatcher
[quassel.git] / src / common / highlightrulemanager.cpp
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 #include "highlightrulemanager.h"
22
23 #include <QDebug>
24
25 #include "expressionmatch.h"
26 #include "util.h"
27
28 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
29
30 HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
31 {
32     if (this == &other)
33         return *this;
34
35     SyncableObject::operator=(other);
36     _highlightRuleList = other._highlightRuleList;
37     _nicksCaseSensitive = other._nicksCaseSensitive;
38     _highlightNick = other._highlightNick;
39     return *this;
40 }
41
42
43 int HighlightRuleManager::indexOf(int id) const
44 {
45     for (int i = 0; i < _highlightRuleList.count(); i++) {
46         if (_highlightRuleList[i].id() == id)
47             return i;
48     }
49     return -1;
50 }
51
52
53 int HighlightRuleManager::nextId()
54 {
55     int max = 0;
56     for (int i = 0; i < _highlightRuleList.count(); i++) {
57         int id = _highlightRuleList[i].id();
58         if (id > max) {
59             max = id;
60         }
61     }
62     return max + 1;
63 }
64
65
66 QVariantMap HighlightRuleManager::initHighlightRuleList() const
67 {
68     QVariantList id;
69     QVariantMap highlightRuleListMap;
70     QStringList name;
71     QVariantList isRegEx;
72     QVariantList isCaseSensitive;
73     QVariantList isActive;
74     QVariantList isInverse;
75     QStringList sender;
76     QStringList channel;
77
78     for (int i = 0; i < _highlightRuleList.count(); i++) {
79         id << _highlightRuleList[i].id();
80         name << _highlightRuleList[i].contents();
81         isRegEx << _highlightRuleList[i].isRegEx();
82         isCaseSensitive << _highlightRuleList[i].isCaseSensitive();
83         isActive << _highlightRuleList[i].isEnabled();
84         isInverse << _highlightRuleList[i].isInverse();
85         sender << _highlightRuleList[i].sender();
86         channel << _highlightRuleList[i].chanName();
87     }
88
89     highlightRuleListMap["id"] = id;
90     highlightRuleListMap["name"] = name;
91     highlightRuleListMap["isRegEx"] = isRegEx;
92     highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
93     highlightRuleListMap["isEnabled"] = isActive;
94     highlightRuleListMap["isInverse"] = isInverse;
95     highlightRuleListMap["sender"] = sender;
96     highlightRuleListMap["channel"] = channel;
97     return highlightRuleListMap;
98 }
99
100
101 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
102 {
103     QVariantList id = highlightRuleList["id"].toList();
104     QStringList name = highlightRuleList["name"].toStringList();
105     QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
106     QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
107     QVariantList isActive = highlightRuleList["isEnabled"].toList();
108     QVariantList isInverse = highlightRuleList["isInverse"].toList();
109     QStringList sender = highlightRuleList["sender"].toStringList();
110     QStringList channel = highlightRuleList["channel"].toStringList();
111
112     int count = id.count();
113     if (count != name.count() || count != isRegEx.count() || count != isCaseSensitive.count() ||
114         count != isActive.count() || count != isInverse.count() || count != sender.count() ||
115         count != channel.count()) {
116         qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
117         return;
118     }
119
120     _highlightRuleList.clear();
121     for (int i = 0; i < name.count(); i++) {
122         _highlightRuleList << HighlightRule(id[i].toInt(), name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
123                                             isActive[i].toBool(), isInverse[i].toBool(), sender[i], channel[i]);
124     }
125 }
126
127
128 void HighlightRuleManager::addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive,
129                                             bool isActive, bool isInverse, const QString &sender,
130                                             const QString &channel)
131 {
132     if (contains(id)) {
133         return;
134     }
135
136     HighlightRule newItem = HighlightRule(id, name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
137     _highlightRuleList << newItem;
138
139     SYNC(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender),
140          ARG(channel))
141 }
142
143
144 bool HighlightRuleManager::match(const NetworkId &netId,
145                                  const QString &msgContents,
146                                  const QString &msgSender,
147                                  Message::Type msgType,
148                                  Message::Flags msgFlags,
149                                  const QString &bufferName,
150                                  const QString &currentNick,
151                                  const QStringList &identityNicks)
152 {
153     if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
154        return false;
155     }
156
157     bool matches = false;
158
159     for (int i = 0; i < _highlightRuleList.count(); i++) {
160         auto &rule = _highlightRuleList.at(i);
161         if (!rule.isEnabled())
162             continue;
163
164         // Skip if channel name doesn't match and channel rule is not empty
165         //
166         // Match succeeds if...
167         //   Channel name matches a defined rule
168         //   Defined rule is empty
169         // And take the inverse of the above
170         if (!rule.chanNameMatcher().match(bufferName, true)) {
171             // A channel name rule is specified and does NOT match the current buffer name, skip
172             // this rule
173             continue;
174         }
175
176         // Check message according to specified rule, allowing empty rules to match
177         bool contentsMatch = rule.contentsMatcher().match(stripFormatCodes(msgContents), true);
178
179         // Check sender according to specified rule, allowing empty rules to match
180         bool senderMatch = rule.senderMatcher().match(msgSender, true);
181
182         if (contentsMatch && senderMatch) {
183             // If an inverse rule matches, then we know that we never want to return a highlight.
184             if (rule.isInverse()) {
185                 return false;
186             }
187             else {
188                 matches = true;
189             }
190         }
191     }
192
193     if (matches)
194         return true;
195
196     // Check nicknames
197     if (_highlightNick != HighlightNickType::NoNick && !currentNick.isEmpty()) {
198         // Nickname matching allowed and current nickname is known
199         // Run the nickname matcher on the unformatted string
200         if (_nickMatcher.match(stripFormatCodes(msgContents), netId, currentNick, identityNicks)) {
201             return true;
202         }
203     }
204
205     return false;
206 }
207
208
209 void HighlightRuleManager::removeHighlightRule(int highlightRule)
210 {
211     removeAt(indexOf(highlightRule));
212     SYNC(ARG(highlightRule))
213 }
214
215
216 void HighlightRuleManager::toggleHighlightRule(int highlightRule)
217 {
218     int idx = indexOf(highlightRule);
219     if (idx == -1)
220         return;
221     _highlightRuleList[idx].setIsEnabled(!_highlightRuleList[idx].isEnabled());
222     SYNC(ARG(highlightRule))
223 }
224
225
226 bool HighlightRuleManager::match(const Message &msg, const QString &currentNick, const QStringList &identityNicks)
227 {
228     return match(msg.bufferInfo().networkId(), msg.contents(), msg.sender(), msg.type(), msg.flags(),
229                  msg.bufferInfo().bufferName(), currentNick, identityNicks);
230 }
231
232
233 /**************************************************************************
234  * HighlightRule
235  *************************************************************************/
236 bool HighlightRuleManager::HighlightRule::operator!=(const HighlightRule &other) const
237 {
238     return (_id != other._id ||
239             _contents != other._contents ||
240             _isRegEx != other._isRegEx ||
241             _isCaseSensitive != other._isCaseSensitive ||
242             _isEnabled != other._isEnabled ||
243             _isInverse != other._isInverse ||
244             _sender != other._sender ||
245             _chanName != other._chanName);
246     // Don't compare ExpressionMatch objects as they are created as needed from the above
247 }
248
249
250 void HighlightRuleManager::HighlightRule::determineExpressions() const
251 {
252     // Don't update if not needed
253     if (!_cacheInvalid) {
254         return;
255     }
256
257     // Set up matching rules
258     // Message is either phrase or regex
259     ExpressionMatch::MatchMode contentsMode =
260             _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx :
261                        ExpressionMatch::MatchMode::MatchPhrase;
262     // Sender and channel are either multiple wildcard entries or regex
263     ExpressionMatch::MatchMode scopeMode =
264             _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx :
265                        ExpressionMatch::MatchMode::MatchMultiWildcard;
266
267     _contentsMatch = ExpressionMatch(_contents, contentsMode, _isCaseSensitive);
268     _senderMatch = ExpressionMatch(_sender, scopeMode, _isCaseSensitive);
269     _chanNameMatch = ExpressionMatch(_chanName, scopeMode, _isCaseSensitive);
270
271     _cacheInvalid = false;
272 }