Implement inverse highlight rules
[quassel.git] / src / common / highlightrulemanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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 #include "util.h"
23
24 #include <QtCore>
25 #include <QDebug>
26 #include <QStringList>
27
28 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
29 HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
30 {
31     if (this == &other)
32         return *this;
33
34     SyncableObject::operator=(other);
35     _highlightRuleList = other._highlightRuleList;
36     _nicksCaseSensitive = other._nicksCaseSensitive;
37     _highlightNick = other._highlightNick;
38     return *this;
39 }
40
41
42 int HighlightRuleManager::indexOf(const QString &name) const
43 {
44     for (int i = 0; i < _highlightRuleList.count(); i++) {
45         if (_highlightRuleList[i].name == name)
46             return i;
47     }
48     return -1;
49 }
50
51
52 QVariantMap HighlightRuleManager::initHighlightRuleList() const
53 {
54     QVariantMap highlightRuleListMap;
55     QStringList name;
56     QVariantList isRegEx;
57     QVariantList isCaseSensitive;
58     QVariantList isActive;
59     QVariantList isInverse;
60     QStringList channel;
61
62     for (int i = 0; i < _highlightRuleList.count(); i++) {
63         name << _highlightRuleList[i].name;
64         isRegEx << _highlightRuleList[i].isRegEx;
65         isCaseSensitive << _highlightRuleList[i].isCaseSensitive;
66         isActive << _highlightRuleList[i].isEnabled;
67         isInverse << _highlightRuleList[i].isInverse;
68         channel << _highlightRuleList[i].chanName;
69     }
70
71     highlightRuleListMap["name"] = name;
72     highlightRuleListMap["isRegEx"] = isRegEx;
73     highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
74     highlightRuleListMap["isEnabled"] = isActive;
75     highlightRuleListMap["isInverse"] = isInverse;
76     highlightRuleListMap["channel"] = channel;
77     highlightRuleListMap["highlightNick"] = _highlightNick;
78     highlightRuleListMap["nicksCaseSensitive"] = _nicksCaseSensitive;
79     return highlightRuleListMap;
80 }
81
82
83 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
84 {
85     QStringList name = highlightRuleList["name"].toStringList();
86     QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
87     QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
88     QVariantList isActive = highlightRuleList["isEnabled"].toList();
89     QVariantList isInverse = highlightRuleList["isInverse"].toList();
90     QStringList channel = highlightRuleList["channel"].toStringList();
91
92     int count = name.count();
93     if (count != isRegEx.count() || count != isCaseSensitive.count() ||
94         count != isActive.count() || count != channel.count()) {
95         qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
96         return;
97     }
98
99     _highlightRuleList.clear();
100     for (int i = 0; i < name.count(); i++) {
101         _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
102                                             isActive[i].toBool(), isInverse[i].toBool(), channel[i]);
103     }
104     _highlightNick = HighlightNickType(highlightRuleList["highlightNick"].toInt());
105     _nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].toBool();
106 }
107
108 void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
109                                             bool isInverse, const QString &channel)
110 {
111     if (contains(name)) {
112         return;
113     }
114
115     HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, isInverse, channel);
116     _highlightRuleList << newItem;
117
118     SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(channel))
119 }
120
121
122 bool HighlightRuleManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString &currentNick, const QStringList identityNicks)
123 {
124     if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
125        return false;
126     }
127
128     bool matches = false;
129
130     for (int i = 0; i < _highlightRuleList.count(); i++) {
131         const HighlightRule &rule = _highlightRuleList.at(i);
132         if (!rule.isEnabled)
133             continue;
134
135         if (rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) {
136             if (rule.chanName.startsWith("!")) {
137                 QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive);
138                 if (rx.exactMatch(bufferName))
139                     continue;
140             }
141             else {
142                 QRegExp rx(rule.chanName, Qt::CaseInsensitive);
143                 if (!rx.exactMatch(bufferName))
144                     continue;
145             }
146         }
147
148         QRegExp rx;
149         if (rule.isRegEx) {
150             rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
151         }
152         else {
153             rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
154         }
155         bool match = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
156         if (match) {
157             // If an inverse rule matches, then we know that we never want to return a highlight.
158             if (rule.isInverse) {
159                 return false;
160             } else {
161                 matches = true;
162             }
163         }
164     }
165
166     if (matches)
167         return true;
168
169     if (!currentNick.isEmpty()) {
170         QStringList nickList;
171         if (_highlightNick == CurrentNick) {
172             nickList << currentNick;
173         }
174         else if (_highlightNick == AllNicks) {
175             nickList = identityNicks;
176             if (!nickList.contains(currentNick))
177                 nickList.prepend(currentNick);
178         }
179
180         for(const QString &nickname : nickList) {
181             QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
182             if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) {
183                 return true;
184             }
185         }
186     }
187
188     return false;
189 }
190
191 void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
192 {
193     removeAt(indexOf(highlightRule));
194     SYNC(ARG(highlightRule))
195 }
196
197
198 void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
199 {
200     int idx = indexOf(highlightRule);
201     if (idx == -1)
202         return;
203     _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
204     SYNC(ARG(highlightRule))
205 }
206
207 bool HighlightRuleManager::match(const Message &msg, const QString &currentNick, const QStringList &identityNicks)
208 {
209     return _match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks);
210 }