X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fhighlightrulemanager.cpp;h=0d0e7be4af5b46c87f1d1a3c2a539bdb6d8509bb;hp=deaf42ad122aa490fc8ffb165377d48c96be7edf;hb=ed5b2ff32158ae72c011eb1228f373cec05cbfeb;hpb=17c39210b1bce04795046657642de66292518fe6 diff --git a/src/common/highlightrulemanager.cpp b/src/common/highlightrulemanager.cpp index deaf42ad..0d0e7be4 100644 --- a/src/common/highlightrulemanager.cpp +++ b/src/common/highlightrulemanager.cpp @@ -20,12 +20,13 @@ #include "highlightrulemanager.h" -#include "util.h" - #include -INIT_SYNCABLE_OBJECT(HighlightRuleManager) -HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other) { +#include "expressionmatch.h" +#include "util.h" + +HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other) +{ if (this == &other) return *this; @@ -37,27 +38,31 @@ HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager } -int HighlightRuleManager::indexOf(int id) const { +int HighlightRuleManager::indexOf(int id) const +{ for (int i = 0; i < _highlightRuleList.count(); i++) { - if (_highlightRuleList[i].id == id) + if (_highlightRuleList[i].id() == id) return i; } return -1; } -int HighlightRuleManager::nextId() { + +int HighlightRuleManager::nextId() +{ int max = 0; for (int i = 0; i < _highlightRuleList.count(); i++) { - int id = _highlightRuleList[i].id; + int id = _highlightRuleList[i].id(); if (id > max) { max = id; } } - return max+1; + return max + 1; } -QVariantMap HighlightRuleManager::initHighlightRuleList() const { +QVariantMap HighlightRuleManager::initHighlightRuleList() const +{ QVariantList id; QVariantMap highlightRuleListMap; QStringList name; @@ -69,14 +74,14 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const { QStringList channel; for (int i = 0; i < _highlightRuleList.count(); i++) { - id << _highlightRuleList[i].id; - name << _highlightRuleList[i].name; - isRegEx << _highlightRuleList[i].isRegEx; - isCaseSensitive << _highlightRuleList[i].isCaseSensitive; - isActive << _highlightRuleList[i].isEnabled; - isInverse << _highlightRuleList[i].isInverse; - sender << _highlightRuleList[i].sender; - channel << _highlightRuleList[i].chanName; + id << _highlightRuleList[i].id(); + name << _highlightRuleList[i].contents(); + isRegEx << _highlightRuleList[i].isRegEx(); + isCaseSensitive << _highlightRuleList[i].isCaseSensitive(); + isActive << _highlightRuleList[i].isEnabled(); + isInverse << _highlightRuleList[i].isInverse(); + sender << _highlightRuleList[i].sender(); + channel << _highlightRuleList[i].chanName(); } highlightRuleListMap["id"] = id; @@ -91,7 +96,8 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const { } -void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList) { +void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList) +{ QVariantList id = highlightRuleList["id"].toList(); QStringList name = highlightRuleList["name"].toStringList(); QVariantList isRegEx = highlightRuleList["isRegEx"].toList(); @@ -116,9 +122,11 @@ void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlight } } + void HighlightRuleManager::addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive, bool isInverse, const QString &sender, - const QString &channel) { + const QString &channel) +{ if (contains(id)) { return; } @@ -131,13 +139,15 @@ void HighlightRuleManager::addHighlightRule(int id, const QString &name, bool is } -bool HighlightRuleManager::match(const QString &msgContents, +bool HighlightRuleManager::match(const NetworkId &netId, + const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString ¤tNick, - const QStringList identityNicks) { + const QStringList &identityNicks) +{ if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) { return false; } @@ -145,38 +155,34 @@ bool HighlightRuleManager::match(const QString &msgContents, bool matches = false; for (int i = 0; i < _highlightRuleList.count(); i++) { - const HighlightRule &rule = _highlightRuleList.at(i); - if (!rule.isEnabled) + auto &rule = _highlightRuleList.at(i); + if (!rule.isEnabled()) continue; - if (!rule.chanName.isEmpty() - && !scopeMatch(bufferName, rule.chanName, rule.isRegEx, rule.isCaseSensitive)) { + // Skip if channel name doesn't match and channel rule is not empty + // + // Match succeeds if... + // Channel name matches a defined rule + // Defined rule is empty + // And take the inverse of the above + if (!rule.chanNameMatcher().match(bufferName, true)) { // A channel name rule is specified and does NOT match the current buffer name, skip // this rule 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 { - // A sender name rule is specified, match according to scope rules. - senderMatch = scopeMatch(msgSender, rule.sender, rule.isRegEx, rule.isCaseSensitive); - } + // Check message according to specified rule, allowing empty rules to match + bool contentsMatch = rule.contentsMatcher().match(stripFormatCodes(msgContents), true); + + // Check sender according to specified rule, allowing empty rules to match + bool senderMatch = rule.senderMatcher().match(msgSender, true); - if (nameMatch && senderMatch) { + if (contentsMatch && senderMatch) { // If an inverse rule matches, then we know that we never want to return a highlight. - if (rule.isInverse) { + if (rule.isInverse()) { return false; - } else { + } + else { matches = true; } } @@ -185,42 +191,80 @@ bool HighlightRuleManager::match(const QString &msgContents, if (matches) return true; - if (!currentNick.isEmpty()) { - QStringList nickList; - if (_highlightNick == CurrentNick) { - nickList << currentNick; - } - else if (_highlightNick == AllNicks) { - nickList = identityNicks; - if (!nickList.contains(currentNick)) - nickList.prepend(currentNick); - } - - 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; - } + // Check nicknames + if (_highlightNick != HighlightNickType::NoNick && !currentNick.isEmpty()) { + // Nickname matching allowed and current nickname is known + // Run the nickname matcher on the unformatted string + if (_nickMatcher.match(stripFormatCodes(msgContents), netId, currentNick, identityNicks)) { + return true; } } return false; } -void HighlightRuleManager::removeHighlightRule(int highlightRule) { + +void HighlightRuleManager::removeHighlightRule(int highlightRule) +{ removeAt(indexOf(highlightRule)); SYNC(ARG(highlightRule)) } -void HighlightRuleManager::toggleHighlightRule(int highlightRule) { +void HighlightRuleManager::toggleHighlightRule(int highlightRule) +{ int idx = indexOf(highlightRule); if (idx == -1) return; - _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled; + _highlightRuleList[idx].setIsEnabled(!_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); + +bool HighlightRuleManager::match(const Message &msg, const QString ¤tNick, const QStringList &identityNicks) +{ + return match(msg.bufferInfo().networkId(), msg.contents(), msg.sender(), msg.type(), msg.flags(), + msg.bufferInfo().bufferName(), currentNick, identityNicks); +} + + +/************************************************************************** + * HighlightRule + *************************************************************************/ +bool HighlightRuleManager::HighlightRule::operator!=(const HighlightRule &other) const +{ + return (_id != other._id || + _contents != other._contents || + _isRegEx != other._isRegEx || + _isCaseSensitive != other._isCaseSensitive || + _isEnabled != other._isEnabled || + _isInverse != other._isInverse || + _sender != other._sender || + _chanName != other._chanName); + // Don't compare ExpressionMatch objects as they are created as needed from the above +} + + +void HighlightRuleManager::HighlightRule::determineExpressions() const +{ + // Don't update if not needed + if (!_cacheInvalid) { + return; + } + + // Set up matching rules + // Message is either phrase or regex + ExpressionMatch::MatchMode contentsMode = + _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : + ExpressionMatch::MatchMode::MatchPhrase; + // Sender and channel are either multiple wildcard entries or regex + ExpressionMatch::MatchMode scopeMode = + _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : + ExpressionMatch::MatchMode::MatchMultiWildcard; + + _contentsMatch = ExpressionMatch(_contents, contentsMode, _isCaseSensitive); + _senderMatch = ExpressionMatch(_sender, scopeMode, _isCaseSensitive); + _chanNameMatch = ExpressionMatch(_chanName, scopeMode, _isCaseSensitive); + + _cacheInvalid = false; }