f23f6aae526c39bd5f35575528b1f9e9742a19f9
[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     _highlightNick = HighlightNickType(highlightRuleList["highlightNick"].toInt());
108     _nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].toBool();
109 }
110
111 void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
112                                             bool isInverse, const QString &sender, const QString &channel)
113 {
114     if (contains(name)) {
115         return;
116     }
117
118     HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
119     _highlightRuleList << newItem;
120
121     SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender), ARG(channel))
122 }
123
124
125 bool HighlightRuleManager::match(const QString &msgContents,
126                                  const QString &msgSender,
127                                  Message::Type msgType,
128                                  Message::Flags msgFlags,
129                                  const QString &bufferName,
130                                  const QString &currentNick,
131                                  const QStringList identityNicks)
132 {
133     if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
134        return false;
135     }
136
137     bool matches = false;
138
139     for (int i = 0; i < _highlightRuleList.count(); i++) {
140         const HighlightRule &rule = _highlightRuleList.at(i);
141         if (!rule.isEnabled)
142             continue;
143
144         if (!rule.chanName.isEmpty() && !scopeMatch(rule.chanName, bufferName)) {
145             // A channel name rule is specified and does NOT match the current buffer name, skip
146             // this rule
147             continue;
148         }
149
150         QRegExp rx;
151         if (rule.isRegEx) {
152             rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
153         } else {
154             rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
155         }
156         bool nameMatch = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
157
158         bool senderMatch;
159         if (rule.sender.isEmpty()) {
160             senderMatch = true;
161         } else {
162             if (rule.isRegEx) {
163                 rx = QRegExp(rule.sender, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
164             } else {
165                 rx = QRegExp(rule.sender, Qt::CaseInsensitive, QRegExp::Wildcard);
166             }
167             senderMatch = rx.exactMatch(msgSender);
168         }
169
170         if (nameMatch && senderMatch) {
171             // If an inverse rule matches, then we know that we never want to return a highlight.
172             if (rule.isInverse) {
173                 return false;
174             } else {
175                 matches = true;
176             }
177         }
178     }
179
180     if (matches)
181         return true;
182
183     if (!currentNick.isEmpty()) {
184         QStringList nickList;
185         if (_highlightNick == CurrentNick) {
186             nickList << currentNick;
187         }
188         else if (_highlightNick == AllNicks) {
189             nickList = identityNicks;
190             if (!nickList.contains(currentNick))
191                 nickList.prepend(currentNick);
192         }
193
194         for(const QString &nickname : nickList) {
195             QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
196             if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) {
197                 return true;
198             }
199         }
200     }
201
202     return false;
203 }
204
205 void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
206 {
207     removeAt(indexOf(highlightRule));
208     SYNC(ARG(highlightRule))
209 }
210
211
212 void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
213 {
214     int idx = indexOf(highlightRule);
215     if (idx == -1)
216         return;
217     _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
218     SYNC(ARG(highlightRule))
219 }
220
221 bool HighlightRuleManager::match(const Message &msg, const QString &currentNick, const QStringList &identityNicks)
222 {
223     return match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks);
224 }