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"
27 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
28 HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other) {
32 SyncableObject::operator=(other);
33 _highlightRuleList = other._highlightRuleList;
34 _nicksCaseSensitive = other._nicksCaseSensitive;
35 _highlightNick = other._highlightNick;
40 int HighlightRuleManager::indexOf(int id) const {
41 for (int i = 0; i < _highlightRuleList.count(); i++) {
42 if (_highlightRuleList[i].id == id)
48 int HighlightRuleManager::nextId() {
50 for (int i = 0; i < _highlightRuleList.count(); i++) {
51 int id = _highlightRuleList[i].id;
60 QVariantMap HighlightRuleManager::initHighlightRuleList() const {
62 QVariantMap highlightRuleListMap;
65 QVariantList isCaseSensitive;
66 QVariantList isActive;
67 QVariantList isInverse;
71 for (int i = 0; i < _highlightRuleList.count(); i++) {
72 id << _highlightRuleList[i].id;
73 name << _highlightRuleList[i].name;
74 isRegEx << _highlightRuleList[i].isRegEx;
75 isCaseSensitive << _highlightRuleList[i].isCaseSensitive;
76 isActive << _highlightRuleList[i].isEnabled;
77 isInverse << _highlightRuleList[i].isInverse;
78 sender << _highlightRuleList[i].sender;
79 channel << _highlightRuleList[i].chanName;
82 highlightRuleListMap["id"] = id;
83 highlightRuleListMap["name"] = name;
84 highlightRuleListMap["isRegEx"] = isRegEx;
85 highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
86 highlightRuleListMap["isEnabled"] = isActive;
87 highlightRuleListMap["isInverse"] = isInverse;
88 highlightRuleListMap["sender"] = sender;
89 highlightRuleListMap["channel"] = channel;
90 return highlightRuleListMap;
94 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList) {
95 QVariantList id = highlightRuleList["id"].toList();
96 QStringList name = highlightRuleList["name"].toStringList();
97 QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
98 QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
99 QVariantList isActive = highlightRuleList["isEnabled"].toList();
100 QVariantList isInverse = highlightRuleList["isInverse"].toList();
101 QStringList sender = highlightRuleList["sender"].toStringList();
102 QStringList channel = highlightRuleList["channel"].toStringList();
104 int count = id.count();
105 if (count != name.count() || count != isRegEx.count() || count != isCaseSensitive.count() ||
106 count != isActive.count() || count != isInverse.count() || count != sender.count() ||
107 count != channel.count()) {
108 qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
112 _highlightRuleList.clear();
113 for (int i = 0; i < name.count(); i++) {
114 _highlightRuleList << HighlightRule(id[i].toInt(), name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
115 isActive[i].toBool(), isInverse[i].toBool(), sender[i], channel[i]);
119 void HighlightRuleManager::addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive,
120 bool isActive, bool isInverse, const QString &sender,
121 const QString &channel) {
126 HighlightRule newItem = HighlightRule(id, name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
127 _highlightRuleList << newItem;
129 SYNC(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender),
134 bool HighlightRuleManager::match(const QString &msgContents,
135 const QString &msgSender,
136 Message::Type msgType,
137 Message::Flags msgFlags,
138 const QString &bufferName,
139 const QString ¤tNick,
140 const QStringList identityNicks) {
141 if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
145 bool matches = false;
147 for (int i = 0; i < _highlightRuleList.count(); i++) {
148 const HighlightRule &rule = _highlightRuleList.at(i);
152 if (!rule.chanName.isEmpty()
153 && !scopeMatch(bufferName, rule.chanName, rule.isRegEx, rule.isCaseSensitive)) {
154 // A channel name rule is specified and does NOT match the current buffer name, skip
161 rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
163 rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
165 bool nameMatch = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
168 if (rule.sender.isEmpty()) {
171 // A sender name rule is specified, match according to scope rules.
172 senderMatch = scopeMatch(msgSender, rule.sender, rule.isRegEx, rule.isCaseSensitive);
175 if (nameMatch && senderMatch) {
176 // If an inverse rule matches, then we know that we never want to return a highlight.
177 if (rule.isInverse) {
188 if (!currentNick.isEmpty()) {
189 QStringList nickList;
190 if (_highlightNick == CurrentNick) {
191 nickList << currentNick;
193 else if (_highlightNick == AllNicks) {
194 nickList = identityNicks;
195 if (!nickList.contains(currentNick))
196 nickList.prepend(currentNick);
199 for(const QString &nickname : nickList) {
200 QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
201 if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) {
210 void HighlightRuleManager::removeHighlightRule(int highlightRule) {
211 removeAt(indexOf(highlightRule));
212 SYNC(ARG(highlightRule))
216 void HighlightRuleManager::toggleHighlightRule(int highlightRule) {
217 int idx = indexOf(highlightRule);
220 _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled;
221 SYNC(ARG(highlightRule))
224 bool HighlightRuleManager::match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks) {
225 return match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks);