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