X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcommon%2Fhighlightrulemanager.cpp;h=207dd2194879786cbfff216f894f9934d8efcc9f;hb=f65431d2916386b93bf3c991578997203dfbc64d;hp=d18d8d633b92b8573c65ddf038191635336c890a;hpb=af39ec163d703852a8fa49dbf79c0ef694aa2e76;p=quassel.git diff --git a/src/common/highlightrulemanager.cpp b/src/common/highlightrulemanager.cpp index d18d8d63..207dd219 100644 --- a/src/common/highlightrulemanager.cpp +++ b/src/common/highlightrulemanager.cpp @@ -19,11 +19,10 @@ ***************************************************************************/ #include "highlightrulemanager.h" + #include "util.h" -#include #include -#include INIT_SYNCABLE_OBJECT(HighlightRuleManager) HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other) @@ -56,6 +55,8 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const QVariantList isRegEx; QVariantList isCaseSensitive; QVariantList isActive; + QVariantList isInverse; + QStringList sender; QStringList channel; for (int i = 0; i < _highlightRuleList.count(); i++) { @@ -63,6 +64,8 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const isRegEx << _highlightRuleList[i].isRegEx; isCaseSensitive << _highlightRuleList[i].isCaseSensitive; isActive << _highlightRuleList[i].isEnabled; + isInverse << _highlightRuleList[i].isInverse; + sender << _highlightRuleList[i].sender; channel << _highlightRuleList[i].chanName; } @@ -70,6 +73,8 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const highlightRuleListMap["isRegEx"] = isRegEx; highlightRuleListMap["isCaseSensitive"] = isCaseSensitive; highlightRuleListMap["isEnabled"] = isActive; + highlightRuleListMap["isInverse"] = isInverse; + highlightRuleListMap["sender"] = sender; highlightRuleListMap["channel"] = channel; highlightRuleListMap["highlightNick"] = _highlightNick; highlightRuleListMap["nicksCaseSensitive"] = _nicksCaseSensitive; @@ -83,11 +88,13 @@ void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlight QVariantList isRegEx = highlightRuleList["isRegEx"].toList(); QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList(); QVariantList isActive = highlightRuleList["isEnabled"].toList(); + QVariantList isInverse = highlightRuleList["isInverse"].toList(); + QStringList sender = highlightRuleList["sender"].toStringList(); QStringList channel = highlightRuleList["channel"].toStringList(); int count = name.count(); - if (count != isRegEx.count() || count != isCaseSensitive.count() || - count != isActive.count() || count != channel.count()) { + if (count != isRegEx.count() || count != isCaseSensitive.count() || count != isActive.count() || + count != isInverse.count() || count != sender.count() || count != channel.count()) { qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)"; return; } @@ -95,32 +102,91 @@ void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlight _highlightRuleList.clear(); for (int i = 0; i < name.count(); i++) { _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(), - isActive[i].toBool(), channel[i]); + isActive[i].toBool(), isInverse[i].toBool(), sender[i], channel[i]); } _highlightNick = HighlightNickType(highlightRuleList["highlightNick"].toInt()); _nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].toBool(); } void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive, - const QString &channel) + bool isInverse, const QString &sender, const QString &channel) { if (contains(name)) { return; } - HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, channel); + HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel); _highlightRuleList << newItem; - SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(channel)) + SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender), ARG(channel)) } -bool HighlightRuleManager::_match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString ¤tNick, const QStringList identityNicks) +bool HighlightRuleManager::match(const QString &msgContents, + const QString &msgSender, + Message::Type msgType, + Message::Flags msgFlags, + const QString &bufferName, + const QString ¤tNick, + const QStringList identityNicks) { if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) { return false; } + bool matches = false; + + for (int i = 0; i < _highlightRuleList.count(); i++) { + const HighlightRule &rule = _highlightRuleList.at(i); + if (!rule.isEnabled) + continue; + + if (rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) { + if (rule.chanName.startsWith("!")) { + QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive); + if (rx.exactMatch(bufferName)) + continue; + } + else { + QRegExp rx(rule.chanName, Qt::CaseInsensitive); + if (!rx.exactMatch(bufferName)) + continue; + } + } + + QRegExp rx; + if (rule.isRegEx) { + rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + } else { + rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + } + bool nameMatch = (rx.indexIn(stripFormatCodes(msgContents)) >= 0); + + bool senderMatch; + if (rule.sender.isEmpty()) { + senderMatch = true; + } else { + if (rule.isRegEx) { + rx = QRegExp(rule.sender, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + } else { + rx = QRegExp(rule.sender, Qt::CaseInsensitive, QRegExp::Wildcard); + } + senderMatch = rx.exactMatch(msgSender); + } + + if (nameMatch && senderMatch) { + // If an inverse rule matches, then we know that we never want to return a highlight. + if (rule.isInverse) { + return false; + } else { + matches = true; + } + } + } + + if (matches) + return true; + if (!currentNick.isEmpty()) { QStringList nickList; if (_highlightNick == CurrentNick) { @@ -131,40 +197,10 @@ bool HighlightRuleManager::_match(const QString &msgContents, const QString &msg if (!nickList.contains(currentNick)) nickList.prepend(currentNick); } - foreach(QString nickname, nickList) { - QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); - if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) { - return true; - } - } - - for (int i = 0; i < _highlightRuleList.count(); i++) { - const HighlightRule &rule = _highlightRuleList.at(i); - if (!rule.isEnabled) - continue; - - if (rule.chanName.size() > 0 && rule.chanName.compare(".*") != 0) { - if (rule.chanName.startsWith("!")) { - QRegExp rx(rule.chanName.mid(1), Qt::CaseInsensitive); - if (rx.exactMatch(bufferName)) - continue; - } - else { - QRegExp rx(rule.chanName, Qt::CaseInsensitive); - if (!rx.exactMatch(bufferName)) - continue; - } - } - QRegExp rx; - if (rule.isRegEx) { - rx = QRegExp(rule.name, rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); - } - else { - rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); - } - bool match = (rx.indexIn(stripFormatCodes(msgContents)) >= 0); - if (match) { + for(const QString &nickname : nickList) { + QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + if (nickRegExp.indexIn(stripFormatCodes(msgContents)) >= 0) { return true; } } @@ -188,3 +224,8 @@ void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule) _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled; SYNC(ARG(highlightRule)) } + +bool HighlightRuleManager::match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks) +{ + return match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks); +}