Fix a potential crash
[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     QStringList channel;
60
61     for (int i = 0; i < _highlightRuleList.count(); i++) {
62         name << _highlightRuleList[i].name;
63         isRegEx << _highlightRuleList[i].isRegEx;
64         isCaseSensitive << _highlightRuleList[i].isCaseSensitive;
65         isActive << _highlightRuleList[i].isEnabled;
66         channel << _highlightRuleList[i].chanName;
67     }
68
69     highlightRuleListMap["name"] = name;
70     highlightRuleListMap["isRegEx"] = isRegEx;
71     highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
72     highlightRuleListMap["isEnabled"] = isActive;
73     highlightRuleListMap["channel"] = channel;
74     highlightRuleListMap["highlightNick"] = _highlightNick;
75     highlightRuleListMap["nicksCaseSensitive"] = _nicksCaseSensitive;
76     return highlightRuleListMap;
77 }
78
79
80 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
81 {
82     QStringList name = highlightRuleList["name"].toStringList();
83     QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
84     QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
85     QVariantList isActive = highlightRuleList["isEnabled"].toList();
86     QStringList channel = highlightRuleList["channel"].toStringList();
87
88     int count = name.count();
89     if (count != isRegEx.count() || count != isCaseSensitive.count() ||
90         count != isActive.count() || count != channel.count()) {
91         qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
92         return;
93     }
94
95     _highlightRuleList.clear();
96     for (int i = 0; i < name.count(); i++) {
97         _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
98                                             isActive[i].toBool(), channel[i]);
99     }
100     _highlightNick = HighlightNickType(highlightRuleList["highlightNick"].toInt());
101     _nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].toBool();
102 }
103
104 void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive,
105                                             const QString &channel)
106 {
107     if (contains(name)) {
108         return;
109     }
110
111     HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, channel);
112     _highlightRuleList << newItem;
113
114     SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(channel))
115 }
116
117
118 bool HighlightRuleManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString &currentNick, const QStringList identityNicks)
119 {
120     if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
121        return false;
122     }
123
124     if (!currentNick.isEmpty()) {
125         QStringList nickList;
126         if (_highlightNick == CurrentNick) {
127             nickList << currentNick;
128         }
129         else if (_highlightNick == AllNicks) {
130             nickList = identityNicks;
131             if (!nickList.contains(currentNick))
132                 nickList.prepend(currentNick);
133         }
134             foreach(QString nickname, nickList) {
135                 QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
136                 if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) {
137                     return true;
138                 }
139             }
140
141         for (int i = 0; i < _highlightRuleList.count(); i++) {
142             const HighlightRule &rule = _highlightRuleList.at(i);
143             if (!rule.isEnabled)
144                 continue;
145
146             if (rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) {
147                 if (rule.chanName.startsWith("!")) {
148                     QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive);
149                     if (rx.exactMatch(bufferName))
150                         continue;
151                 }
152                 else {
153                     QRegExp rx(rule.chanName, Qt::CaseInsensitive);
154                     if (!rx.exactMatch(bufferName))
155                         continue;
156                 }
157             }
158
159             QRegExp rx;
160             if (rule.isRegEx) {
161                 rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
162             }
163             else {
164                 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
165             }
166             bool match = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
167             if (match) {
168                 return true;
169             }
170         }
171     }
172
173     return false;
174 }
175
176 void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
177 {
178     removeAt(indexOf(highlightRule));
179     SYNC(ARG(highlightRule))
180 }
181
182
183 void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
184 {
185     int idx = indexOf(highlightRule);
186     if (idx == -1)
187         return;
188     _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
189     SYNC(ARG(highlightRule))
190 }