Implement core-side highlights
[quassel.git] / src / core / coresession.cpp
index 5c377c8..a2583e0 100644 (file)
@@ -77,7 +77,8 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent)
     _ircParser(new IrcParser(this)),
     scriptEngine(new QScriptEngine(this)),
     _processMessages(false),
-    _ignoreListManager(this)
+    _ignoreListManager(this),
+    _highlightRuleManager(this)
 {
     SignalProxy *p = signalProxy();
     p->setHeartBeatInterval(30);
@@ -105,6 +106,9 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent)
     p->attachSlot(SIGNAL(changePassword(PeerPtr,QString,QString,QString)), this, SLOT(changePassword(PeerPtr,QString,QString,QString)));
     p->attachSignal(this, SIGNAL(passwordChanged(PeerPtr,bool)));
 
+    p->attachSlot(SIGNAL(kickClient(int)), this, SLOT(kickClient(int)));
+    p->attachSignal(this, SIGNAL(disconnectFromCore()));
+
     loadSettings();
     initScriptEngine();
 
@@ -128,6 +132,7 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent)
     p->synchronize(networkConfig());
     p->synchronize(&_coreInfo);
     p->synchronize(&_ignoreListManager);
+    p->synchronize(&_highlightRuleManager);
     p->synchronize(transferManager());
     // Restore session state
     if (restoreState)
@@ -314,6 +319,9 @@ void CoreSession::recvMessageFromServer(NetworkId networkId, Message::Type type,
     if (_ignoreListManager.match(rawMsg, networkName) == IgnoreListManager::HardStrictness)
         return;
 
+    if (_highlightRuleManager.match(rawMsg, currentNetwork->myNick(), currentNetwork->identityPtr()->nicks()))
+        rawMsg.flags |= Message::Flag::Highlight;
+
     _messageQueue << rawMsg;
     if (!_processMessages) {
         _processMessages = true;
@@ -366,7 +374,8 @@ void CoreSession::processMessages()
             Q_ASSERT(!createBuffer);
             bufferInfo = Core::bufferInfo(user(), rawMsg.networkId, BufferInfo::StatusBuffer, "");
         }
-        Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender, rawMsg.flags);
+        Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender,
+                    senderPrefixes(rawMsg.sender, bufferInfo), rawMsg.flags);
         if(Core::storeMessage(msg))
             emit displayMsg(msg);
     }
@@ -390,7 +399,8 @@ void CoreSession::processMessages()
                 }
                 bufferInfoCache[rawMsg.networkId][rawMsg.target] = bufferInfo;
             }
-            Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender, rawMsg.flags);
+            Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender,
+                        senderPrefixes(rawMsg.sender, bufferInfo), rawMsg.flags);
             messages << msg;
         }
 
@@ -406,7 +416,8 @@ void CoreSession::processMessages()
                 // add the StatusBuffer to the Cache in case there are more Messages for the original target
                 bufferInfoCache[rawMsg.networkId][rawMsg.target] = bufferInfo;
             }
-            Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender, rawMsg.flags);
+            Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender,
+                        senderPrefixes(rawMsg.sender, bufferInfo), rawMsg.flags);
             messages << msg;
         }
 
@@ -421,6 +432,25 @@ void CoreSession::processMessages()
     _messageQueue.clear();
 }
 
+QString CoreSession::senderPrefixes(const QString &sender, const BufferInfo &bufferInfo) const
+{
+    CoreNetwork *currentNetwork = network(bufferInfo.networkId());
+    if (!currentNetwork) {
+        return {};
+    }
+
+    if (bufferInfo.type() != BufferInfo::ChannelBuffer) {
+        return {};
+    }
+
+    IrcChannel *currentChannel = currentNetwork->ircChannel(bufferInfo.bufferName());
+    if (!currentChannel) {
+        return {};
+    }
+
+    const QString modes = currentChannel->userModes(nickFromMask(sender).toLower());
+    return currentNetwork->modesToPrefixes(modes);
+}
 
 Protocol::SessionState CoreSession::sessionState() const
 {
@@ -664,13 +694,14 @@ void CoreSession::clientsDisconnected()
             if (!identity->detachAwayReason().isEmpty())
                 awayReason = identity->detachAwayReason();
             net->setAutoAwayActive(true);
+            // Allow handleAway() to format the current date/time in the string.
             net->userInputHandler()->handleAway(BufferInfo(), awayReason);
         }
     }
 }
 
 
-void CoreSession::globalAway(const QString &msg)
+void CoreSession::globalAway(const QString &msg, const bool skipFormatting)
 {
     QHash<NetworkId, CoreNetwork *>::iterator netIter = _networks.begin();
     CoreNetwork *net = 0;
@@ -681,16 +712,30 @@ void CoreSession::globalAway(const QString &msg)
         if (!net->isConnected())
             continue;
 
-        net->userInputHandler()->issueAway(msg, false /* no force away */);
+        net->userInputHandler()->issueAway(msg, false /* no force away */, skipFormatting);
     }
 }
 
-void CoreSession::changePassword(PeerPtr peer, const QString &userName, const QString &oldPassword, const QString &newPassword)
-{
+void CoreSession::changePassword(PeerPtr peer, const QString &userName, const QString &oldPassword, const QString &newPassword) {
+    Q_UNUSED(peer);
+
     bool success = false;
     UserId uid = Core::validateUser(userName, oldPassword);
     if (uid.isValid() && uid == user())
         success = Core::changeUserPassword(uid, newPassword);
 
-    emit passwordChanged(peer, success);
+    signalProxy()->restrictTargetPeers(signalProxy()->sourcePeer(), [&]{
+        emit passwordChanged(nullptr, success);
+    });
+}
+
+void CoreSession::kickClient(int peerId) {
+    auto peer = signalProxy()->peerById(peerId);
+    if (peer == nullptr) {
+        qWarning() << "Invalid peer Id: " << peerId;
+        return;
+    }
+    signalProxy()->restrictTargetPeers(peer, [&]{
+        emit disconnectFromCore();
+    });
 }