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 ***************************************************************************/
21 #include "highlightrulemanager.h"
25 #include "expressionmatch.h"
28 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
30 HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
35 SyncableObject::operator=(other);
36 _highlightRuleList = other._highlightRuleList;
37 _nicksCaseSensitive = other._nicksCaseSensitive;
38 _highlightNick = other._highlightNick;
43 int HighlightRuleManager::indexOf(int id) const
45 for (int i = 0; i < _highlightRuleList.count(); i++) {
46 if (_highlightRuleList[i].id() == id)
53 int HighlightRuleManager::nextId()
56 for (int i = 0; i < _highlightRuleList.count(); i++) {
57 int id = _highlightRuleList[i].id();
66 QVariantMap HighlightRuleManager::initHighlightRuleList() const
69 QVariantMap highlightRuleListMap;
72 QVariantList isCaseSensitive;
73 QVariantList isActive;
74 QVariantList isInverse;
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();
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;
101 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
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();
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)";
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]);
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)
136 HighlightRule newItem = HighlightRule(id, name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
137 _highlightRuleList << newItem;
139 SYNC(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender),
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 ¤tNick,
151 const QStringList &identityNicks)
153 if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
157 bool matches = false;
159 for (int i = 0; i < _highlightRuleList.count(); i++) {
160 auto &rule = _highlightRuleList.at(i);
161 if (!rule.isEnabled())
164 // Skip if channel name doesn't match and channel rule is not empty
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
176 // Check message according to specified rule, allowing empty rules to match
177 bool contentsMatch = rule.contentsMatcher().match(stripFormatCodes(msgContents), true);
179 // Check sender according to specified rule, allowing empty rules to match
180 bool senderMatch = rule.senderMatcher().match(msgSender, true);
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()) {
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)) {
209 void HighlightRuleManager::removeHighlightRule(int highlightRule)
211 removeAt(indexOf(highlightRule));
212 SYNC(ARG(highlightRule))
216 void HighlightRuleManager::toggleHighlightRule(int highlightRule)
218 int idx = indexOf(highlightRule);
221 _highlightRuleList[idx].setIsEnabled(!_highlightRuleList[idx].isEnabled());
222 SYNC(ARG(highlightRule))
226 bool HighlightRuleManager::match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks)
228 return match(msg.bufferInfo().networkId(), msg.contents(), msg.sender(), msg.type(), msg.flags(),
229 msg.bufferInfo().bufferName(), currentNick, identityNicks);
233 /**************************************************************************
235 *************************************************************************/
236 bool HighlightRuleManager::HighlightRule::operator!=(const HighlightRule &other) const
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
250 void HighlightRuleManager::HighlightRule::determineExpressions() const
252 // Don't update if not needed
253 if (!_cacheInvalid) {
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;
267 _contentsMatch = ExpressionMatch(_contents, contentsMode, _isCaseSensitive);
268 _senderMatch = ExpressionMatch(_sender, scopeMode, _isCaseSensitive);
269 _chanNameMatch = ExpressionMatch(_chanName, scopeMode, _isCaseSensitive);
271 _cacheInvalid = false;