common: Empty highlight name matches all messages
[quassel.git] / src / common / highlightrulemanager.cpp
index aca87e6..ff60df6 100644 (file)
 
 #include "highlightrulemanager.h"
 
-#include "util.h"
-
 #include <QDebug>
 
+#include "util.h"
+
 INIT_SYNCABLE_OBJECT(HighlightRuleManager)
+
 HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other)
 {
     if (this == &other)
@@ -38,18 +39,32 @@ HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager
 }
 
 
-int HighlightRuleManager::indexOf(const QString &name) const
+int HighlightRuleManager::indexOf(int id) const
 {
     for (int i = 0; i < _highlightRuleList.count(); i++) {
-        if (_highlightRuleList[i].name == name)
+        if (_highlightRuleList[i].id == id)
             return i;
     }
     return -1;
 }
 
 
+int HighlightRuleManager::nextId()
+{
+    int max = 0;
+    for (int i = 0; i < _highlightRuleList.count(); i++) {
+        int id = _highlightRuleList[i].id;
+        if (id > max) {
+            max = id;
+        }
+    }
+    return max + 1;
+}
+
+
 QVariantMap HighlightRuleManager::initHighlightRuleList() const
 {
+    QVariantList id;
     QVariantMap highlightRuleListMap;
     QStringList name;
     QVariantList isRegEx;
@@ -60,6 +75,7 @@ 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;
@@ -69,6 +85,7 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const
         channel << _highlightRuleList[i].chanName;
     }
 
+    highlightRuleListMap["id"] = id;
     highlightRuleListMap["name"] = name;
     highlightRuleListMap["isRegEx"] = isRegEx;
     highlightRuleListMap["isCaseSensitive"] = isCaseSensitive;
@@ -76,14 +93,13 @@ QVariantMap HighlightRuleManager::initHighlightRuleList() const
     highlightRuleListMap["isInverse"] = isInverse;
     highlightRuleListMap["sender"] = sender;
     highlightRuleListMap["channel"] = channel;
-    highlightRuleListMap["highlightNick"] = _highlightNick;
-    highlightRuleListMap["nicksCaseSensitive"] = _nicksCaseSensitive;
     return highlightRuleListMap;
 }
 
 
 void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList)
 {
+    QVariantList id = highlightRuleList["id"].toList();
     QStringList name = highlightRuleList["name"].toStringList();
     QVariantList isRegEx = highlightRuleList["isRegEx"].toList();
     QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList();
@@ -92,33 +108,35 @@ void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlight
     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 != isInverse.count() || count != sender.count() || count != channel.count()) {
+    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()) {
         qWarning() << "Corrupted HighlightRuleList settings! (Count mismatch)";
         return;
     }
 
     _highlightRuleList.clear();
     for (int i = 0; i < name.count(); i++) {
-        _highlightRuleList << HighlightRule(name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
+        _highlightRuleList << HighlightRule(id[i].toInt(), name[i], isRegEx[i].toBool(), isCaseSensitive[i].toBool(),
                                             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,
-                                            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(name)) {
+    if (contains(id)) {
         return;
     }
 
-    HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
+    HighlightRule newItem = HighlightRule(id, name, isRegEx, isCaseSensitive, isActive, isInverse, sender, channel);
     _highlightRuleList << newItem;
 
-    SYNC(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))
 }
 
 
@@ -141,37 +159,36 @@ bool HighlightRuleManager::match(const QString &msgContents,
         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;
-            }
+        if (!rule.chanName.isEmpty()
+                && !scopeMatch(bufferName, rule.chanName, rule.isRegEx, rule.isCaseSensitive)) {
+            // 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);
+        bool nameMatch = false;
+        if (rule.name.isEmpty()) {
+            // Empty rule, matches any message
+            nameMatch = true;
         } else {
-            rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
+            // Check according to specified rule
+            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);
+            }
+            nameMatch = (rx.indexIn(stripFormatCodes(msgContents)) >= 0);
         }
-        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);
+            // A sender name rule is specified, match according to scope rules.
+            senderMatch = scopeMatch(msgSender, rule.sender, rule.isRegEx, rule.isCaseSensitive);
         }
 
         if (nameMatch && senderMatch) {
@@ -209,14 +226,15 @@ bool HighlightRuleManager::match(const QString &msgContents,
     return false;
 }
 
-void HighlightRuleManager::removeHighlightRule(const QString &highlightRule)
+
+void HighlightRuleManager::removeHighlightRule(int highlightRule)
 {
     removeAt(indexOf(highlightRule));
     SYNC(ARG(highlightRule))
 }
 
 
-void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule)
+void HighlightRuleManager::toggleHighlightRule(int highlightRule)
 {
     int idx = indexOf(highlightRule);
     if (idx == -1)
@@ -225,6 +243,7 @@ void HighlightRuleManager::toggleHighlightRule(const QString &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);