common: Make SyncableObject non-copyable
[quassel.git] / src / common / highlightrulemanager.cpp
index c9c4cfc..5a62f9e 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2018 by the Quassel Project                        *
+ *   Copyright (C) 2005-2020 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include "expressionmatch.h"
 #include "util.h"
 
-INIT_SYNCABLE_OBJECT(HighlightRuleManager)
-
-HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
-{
-    if (this == &other)
-        return *this;
-
-    SyncableObject::operator=(other);
-    _highlightRuleList = other._highlightRuleList;
-    _nicksCaseSensitive = other._nicksCaseSensitive;
-    _highlightNick = other._highlightNick;
-    return *this;
-}
-
-
 int HighlightRuleManager::indexOf(int id) const
 {
     for (int i = 0; i < _highlightRuleList.count(); i++) {
@@ -49,7 +34,6 @@ int HighlightRuleManager::indexOf(int id) const
     return -1;
 }
 
-
 int HighlightRuleManager::nextId()
 {
     int max = 0;
@@ -62,7 +46,6 @@ int HighlightRuleManager::nextId()
     return max + 1;
 }
 
-
 QVariantMap HighlightRuleManager::initHighlightRuleList() const
 {
     QVariantList id;
@@ -97,8 +80,7 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const
     return highlightRuleListMap;
 }
 
-
-void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
+void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap& highlightRuleList)
 {
     QVariantList id = highlightRuleList["id"].toList();
     QStringList name = highlightRuleList["name"].toStringList();
@@ -110,24 +92,27 @@ void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlight
     QStringList channel = highlightRuleList["channel"].toStringList();
 
     int count = id.count();
-    if (count != name.count() || count != isRegEx.count() || count != isCaseSensitive.count() ||
-        count != isActive.count() || count != isInverse.count() || count != sender.count() ||
-        count != channel.count()) {
+    if (count != name.count() || 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;
     }
 
     _highlightRuleList.clear();
     for (int i = 0; i < name.count(); i++) {
-        _highlightRuleList << HighlightRule(id[i].toInt(), name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
-                                            isActive[i].toBool(), isInverse[i].toBool(), sender[i], channel[i]);
+        _highlightRuleList << HighlightRule(id[i].toInt(),
+                                            name[i],
+                                            isRegEx[i].toBool(),
+                                            isCaseSensitive[i].toBool(),
+                                            isActive[i].toBool(),
+                                            isInverse[i].toBool(),
+                                            sender[i],
+                                            channel[i]);
     }
 }
 
-
-void HighlightRuleManager::addHighlightRule(int id, const QString &name, bool isRegEx, bool isCaseSensitive,
-                                            bool isActive, bool isInverse, const QString &sender,
-                                            const QString &channel)
+void HighlightRuleManager::addHighlightRule(
+    int id, const QString& name, bool isRegEx, bool isCaseSensitive, bool isActive, bool isInverse, const QString& sender, const QString& channel)
 {
     if (contains(id)) {
         return;
@@ -136,27 +121,26 @@ void HighlightRuleManager::addHighlightRule(int id, const QString &name, bool is
     HighlightRule newItem = HighlightRule(id, name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
     _highlightRuleList << newItem;
 
-    SYNC(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender),
-         ARG(channel))
+    SYNC(ARG(id), ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), ARG(isInverse), ARG(sender), ARG(channel))
 }
 
-
-bool HighlightRuleManager::match(const QString &msgContents,
-                                 const QString &msgSender,
+bool HighlightRuleManager::match(const NetworkId& netId,
+                                 const QString& msgContents,
+                                 const QStringmsgSender,
                                  Message::Type msgType,
                                  Message::Flags msgFlags,
-                                 const QString &bufferName,
-                                 const QString &currentNick,
-                                 const QStringList identityNicks)
+                                 const QStringbufferName,
+                                 const QStringcurrentNick,
+                                 const QStringList& identityNicks)
 {
     if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) {
-       return false;
+        return false;
     }
 
     bool matches = false;
 
     for (int i = 0; i < _highlightRuleList.count(); i++) {
-        auto &rule = _highlightRuleList.at(i);
+        autorule = _highlightRuleList.at(i);
         if (!rule.isEnabled())
             continue;
 
@@ -194,13 +178,9 @@ bool HighlightRuleManager::match(const QString &msgContents,
 
     // Check nicknames
     if (_highlightNick != HighlightNickType::NoNick && !currentNick.isEmpty()) {
-        // Update cache if needed
-        determineNickExpressions(currentNick, identityNicks);
-
-        // Check for a match
-        if (_cachedNickMatcher.isValid()
-                && _cachedNickMatcher.match(stripFormatCodes(msgContents))) {
-            // Nick matcher is valid and match found
+        // 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;
         }
     }
@@ -208,14 +188,12 @@ bool HighlightRuleManager::match(const QString &msgContents,
     return false;
 }
 
-
 void HighlightRuleManager::removeHighlightRule(int highlightRule)
 {
     removeAt(indexOf(highlightRule));
     SYNC(ARG(highlightRule))
 }
 
-
 void HighlightRuleManager::toggleHighlightRule(int highlightRule)
 {
     int idx = indexOf(highlightRule);
@@ -225,67 +203,28 @@ void HighlightRuleManager::toggleHighlightRule(int highlightRule)
     SYNC(ARG(highlightRule))
 }
 
-
-bool HighlightRuleManager::match(const Message &msg, const QString &currentNick, const QStringList &identityNicks)
-{
-    return match(msg.contents(), msg.sender(), msg.type(), msg.flags(), msg.bufferInfo().bufferName(), currentNick, identityNicks);
-}
-
-
-void HighlightRuleManager::determineNickExpressions(const QString &currentNick,
-                                                    const QStringList identityNicks) const
+bool HighlightRuleManager::match(const Message& msg, const QString& currentNick, const QStringList& identityNicks)
 {
-    // Don't do anything for no nicknames
-    if (_highlightNick == HighlightNickType::NoNick) {
-        return;
-    }
-
-    // Only update if needed (check nickname config, current nick, identity nicks for change)
-    if (!_cacheNickConfigInvalid
-          && _cachedNickCurrent == currentNick
-          && _cachedIdentityNicks == identityNicks) {
-        return;
-    }
-
-    // Add all nicknames
-    QStringList nickList;
-    if (_highlightNick == CurrentNick) {
-        nickList << currentNick;
-    }
-    else if (_highlightNick == AllNicks) {
-        nickList = identityNicks;
-        if (!nickList.contains(currentNick))
-            nickList.prepend(currentNick);
-    }
-
-    // Set up phrase matcher, joining with newlines
-    _cachedNickMatcher = ExpressionMatch(nickList.join("\n"),
-                                        ExpressionMatch::MatchMode::MatchMultiPhrase,
-                                        _nicksCaseSensitive);
-
-    _cacheNickConfigInvalid = false;
-    _cachedNickCurrent = currentNick;
-    _cachedIdentityNicks = 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
+bool HighlightRuleManager::HighlightRule::operator!=(const HighlightRuleother) 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);
+    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
@@ -295,13 +234,9 @@ void HighlightRuleManager::HighlightRule::determineExpressions() const
 
     // Set up matching rules
     // Message is either phrase or regex
-    ExpressionMatch::MatchMode contentsMode =
-            _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx :
-                       ExpressionMatch::MatchMode::MatchPhrase;
+    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;
+    ExpressionMatch::MatchMode scopeMode = _isRegEx ? ExpressionMatch::MatchMode::MatchRegEx : ExpressionMatch::MatchMode::MatchMultiWildcard;
 
     _contentsMatch = ExpressionMatch(_contents, contentsMode, _isCaseSensitive);
     _senderMatch = ExpressionMatch(_sender, scopeMode, _isCaseSensitive);