From 16f22647e6890d3eb8c3e94f7a0700e12fa29e44 Mon Sep 17 00:00:00 2001 From: Janne Koschinski Date: Fri, 1 Sep 2017 03:11:39 +0200 Subject: [PATCH] Implement core-side highlights --- src/common/CMakeLists.txt | 1 + src/common/highlightrulemanager.cpp | 184 ++++++++++++++++++++++++++ src/common/highlightrulemanager.h | 146 ++++++++++++++++++++ src/common/quassel.h | 3 +- src/core/CMakeLists.txt | 1 + src/core/corehighlightrulemanager.cpp | 57 ++++++++ src/core/corehighlightrulemanager.h | 55 ++++++++ src/core/coresession.cpp | 7 +- src/core/coresession.h | 3 + src/qtui/qtuimessageprocessor.cpp | 6 +- 10 files changed, 459 insertions(+), 4 deletions(-) create mode 100644 src/common/highlightrulemanager.cpp create mode 100644 src/common/highlightrulemanager.h create mode 100644 src/core/corehighlightrulemanager.cpp create mode 100644 src/core/corehighlightrulemanager.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index a0227561..1deac520 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -14,6 +14,7 @@ set(SOURCES dccconfig.cpp event.cpp eventmanager.cpp + highlightrulemanager.cpp identity.cpp ignorelistmanager.cpp internalpeer.cpp diff --git a/src/common/highlightrulemanager.cpp b/src/common/highlightrulemanager.cpp new file mode 100644 index 00000000..9f780f51 --- /dev/null +++ b/src/common/highlightrulemanager.cpp @@ -0,0 +1,184 @@ +/*************************************************************************** + * Copyright (C) 2005-2016 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "highlightrulemanager.h" +#include "util.h" + +#include +#include +#include + +INIT_SYNCABLE_OBJECT(HighlightRuleManager) +HighlightRuleManager &HighlightRuleManager::operator=(const HighlightRuleManager &other) +{ + if (this == &other) + return *this; + + SyncableObject::operator=(other); + _highlightRuleList = other._highlightRuleList; + return *this; +} + + +int HighlightRuleManager::indexOf(const QString &name) const +{ + for (int i = 0; i < _highlightRuleList.count(); i++) { + if (_highlightRuleList[i].name == name) + return i; + } + return -1; +} + + +QVariantMap HighlightRuleManager::initHighlightRuleList() const +{ + QVariantMap highlightRuleListMap; + QStringList name; + QVariantList isRegEx; + QVariantList isCaseSensitive; + QVariantList isActive; + QStringList channel; + + for (int i = 0; i < _highlightRuleList.count(); i++) { + name << _highlightRuleList[i].name; + isRegEx << _highlightRuleList[i].isRegEx; + isCaseSensitive << _highlightRuleList[i].isCaseSensitive; + isActive << _highlightRuleList[i].isEnabled; + channel << _highlightRuleList[i].chanName; + } + + highlightRuleListMap["name"] = name; + highlightRuleListMap["isRegEx"] = isRegEx; + highlightRuleListMap["isCaseSensitive"] = isCaseSensitive; + highlightRuleListMap["isEnabled"] = isActive; + highlightRuleListMap["channel"] = channel; + return highlightRuleListMap; +} + + +void HighlightRuleManager::initSetHighlightRuleList(const QVariantMap &highlightRuleList) +{ + QStringList name = highlightRuleList["name"].toStringList(); + QVariantList isRegEx = highlightRuleList["isRegEx"].toList(); + QVariantList isCaseSensitive = highlightRuleList["isCaseSensitive"].toList(); + QVariantList isActive = highlightRuleList["isEnabled"].toList(); + QStringList channel = highlightRuleList["channel"].toStringList(); + + int count = name.count(); + if (count != isRegEx.count() || count != isCaseSensitive.count() || + count != isActive.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(), + isActive[i].toBool(), channel[i]); + } +} + +void HighlightRuleManager::addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isActive, + const QString &channel) +{ + if (contains(name)) { + return; + } + + HighlightRule newItem = HighlightRule(name, isRegEx, isCaseSensitive, isActive, channel); + _highlightRuleList << newItem; + + SYNC(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isActive), 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) +{ + if (!((msgType & (Message::Plain | Message::Notice | Message::Action)) && !(msgFlags & Message::Self))) { + return false; + } + + if (!currentNick.isEmpty()) { + QStringList nickList; + if (_highlightNick == CurrentNick) { + nickList << currentNick; + } + else if (_highlightNick == AllNicks) { + nickList = identityNicks; + 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) { + return true; + } + } + } + + return false; +} + +void HighlightRuleManager::removeHighlightRule(const QString &highlightRule) +{ + removeAt(indexOf(highlightRule)); + SYNC(ARG(highlightRule)) +} + + +void HighlightRuleManager::toggleHighlightRule(const QString &highlightRule) +{ + int idx = indexOf(highlightRule); + if (idx == -1) + return; + _highlightRuleList[idx].isEnabled = !_highlightRuleList[idx].isEnabled; + SYNC(ARG(highlightRule)) +} diff --git a/src/common/highlightrulemanager.h b/src/common/highlightrulemanager.h new file mode 100644 index 00000000..02b1a223 --- /dev/null +++ b/src/common/highlightrulemanager.h @@ -0,0 +1,146 @@ +/*************************************************************************** + * Copyright (C) 2005-2016 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef HIGHLIGHTRULELISTMANAGER_H +#define HIGHLIGHTRULELISTMANAGER_H + +#include +#include + +#include "message.h" +#include "syncableobject.h" + +class HighlightRuleManager : public SyncableObject +{ + SYNCABLE_OBJECT + Q_OBJECT +public: + enum HighlightNickType { + NoNick = 0x00, + CurrentNick = 0x01, + AllNicks = 0x02 + }; + + inline HighlightRuleManager(QObject *parent = nullptr) : SyncableObject(parent) { setAllowClientUpdates(true); } + HighlightRuleManager &operator=(const HighlightRuleManager &other); + + struct HighlightRule { + QString name; + bool isRegEx = false; + bool isCaseSensitive = false; + bool isEnabled = true; + QString chanName; + HighlightRule() {} + HighlightRule(const QString &name_, bool isRegEx_, bool isCaseSensitive_, + bool isEnabled_, const QString &chanName_) + : name(name_), isRegEx(isRegEx_), isCaseSensitive(isCaseSensitive_), isEnabled(isEnabled_), chanName(chanName_) { + } + bool operator!=(const HighlightRule &other) + { + return (name != other.name || + isRegEx != other.isRegEx || + isCaseSensitive != other.isCaseSensitive || + isEnabled != other.isEnabled || + chanName != other.chanName); + } + }; + typedef QList HighlightRuleList; + + int indexOf(const QString &rule) const; + inline bool contains(const QString &rule) const { return indexOf(rule) != -1; } + inline bool isEmpty() const { return _highlightRuleList.isEmpty(); } + inline int count() const { return _highlightRuleList.count(); } + inline void removeAt(int index) { _highlightRuleList.removeAt(index); } + inline HighlightRule &operator[](int i) { return _highlightRuleList[i]; } + inline const HighlightRule &operator[](int i) const { return _highlightRuleList.at(i); } + inline const HighlightRuleList &highlightRuleList() const { return _highlightRuleList; } + + //! Check if a message matches the HighlightRule + /** This method checks if a message matches the users highlight rules. + * \param msg The Message that should be checked + */ + inline bool 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); } + +public slots: + virtual QVariantMap initHighlightRuleList() const; + virtual void initSetHighlightRuleList(const QVariantMap &HighlightRuleList); + + //! Request removal of an ignore rule based on the rule itself. + /** Use this method if you want to remove a single ignore rule + * and get that synced with the core immediately. + * \param highlightRule A valid ignore rule + */ + virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) } + virtual void removeHighlightRule(const QString &highlightRule); + + //! Request toggling of "isEnabled" flag of a given ignore rule. + /** Use this method if you want to toggle the "isEnabled" flag of a single ignore rule + * and get that synced with the core immediately. + * \param highlightRule A valid ignore rule + */ + virtual inline void requestToggleHighlightRule(const QString &highlightRule) { REQUEST(ARG(highlightRule)) } + virtual void toggleHighlightRule(const QString &highlightRule); + + //! Request an HighlightRule to be added to the ignore list + /** Items added to the list with this method, get immediately synced with the core + * \param name The rule + * \param isRegEx If the rule should be interpreted as a nickname, or a regex + * \param isCaseSensitive If the rule should be interpreted as case-sensitive + * \param isEnabled If the rule is active + * @param chanName The channel in which the rule should apply + */ + virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, bool isEnabled, + const QString &chanName) + { + REQUEST(ARG(name), ARG(isRegEx), ARG(isCaseSensitive), ARG(isEnabled), ARG(chanName)) + } + + + virtual void addHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, + bool isEnabled, const QString &chanName); + + virtual inline void requestSetHighlightNick(HighlightNickType highlightNick) + { + REQUEST(ARG(highlightNick)) + } + inline void setHighlightNick(HighlightNickType highlightNick) { _highlightNick = highlightNick; } + + virtual inline void requestSetNicksCaseSensitive(bool nicksCaseSensitive) + { + REQUEST(ARG(nicksCaseSensitive)) + } + inline void setNicksCaseSensitive(bool nicksCaseSensitive) { _nicksCaseSensitive = nicksCaseSensitive; } + +protected: + void setHighlightRuleList(const QList &HighlightRuleList) { _highlightRuleList = HighlightRuleList; } + + bool _match(const QString &msgContents, const QString &msgSender, Message::Type msgType, Message::Flags msgFlags, const QString &bufferName, const QString ¤tNick, const QStringList identityNicks); + +signals: + void ruleAdded(QString name, bool isRegEx, bool isCaseSensitive, bool isEnabled, QString chanName); + +private: + HighlightRuleList _highlightRuleList; + HighlightNickType _highlightNick = HighlightNickType::CurrentNick; + bool _nicksCaseSensitive = false; +}; + + +#endif // HIGHLIGHTRULELISTMANAGER_H diff --git a/src/common/quassel.h b/src/common/quassel.h index 21aaec77..d20fdc23 100644 --- a/src/common/quassel.h +++ b/src/common/quassel.h @@ -78,8 +78,9 @@ public: AwayFormatTimestamp = 0x0200, /// Timestamp formatting in away (e.g. %%hh:mm%%) Authenticators = 0x0400, /// Whether or not the core supports auth backends. BufferActivitySync = 0x0800, /// Sync buffer activity status + CoreSideHighlights = 0x1000, /// Core-Side highlight configuration and matching - NumFeatures = 0x0800 + NumFeatures = 0x1000 }; Q_DECLARE_FLAGS(Features, Feature) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6e980379..1ec6797d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -14,6 +14,7 @@ set(SOURCES corebufferviewmanager.cpp corecoreinfo.cpp coredccconfig.cpp + corehighlightrulemanager.cpp coreidentity.cpp coreignorelistmanager.cpp coreircchannel.cpp diff --git a/src/core/corehighlightrulemanager.cpp b/src/core/corehighlightrulemanager.cpp new file mode 100644 index 00000000..c35ca508 --- /dev/null +++ b/src/core/corehighlightrulemanager.cpp @@ -0,0 +1,57 @@ +/*************************************************************************** + * Copyright (C) 2005-2016 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "corehighlightrulemanager.h" + +#include "core.h" +#include "coresession.h" + +INIT_SYNCABLE_OBJECT(CoreHighlightRuleManager) +CoreHighlightRuleManager::CoreHighlightRuleManager(CoreSession *parent) + : HighlightRuleManager(parent) +{ + CoreSession *session = qobject_cast(parent); + if (!session) { + qWarning() << "CoreHighlightRuleManager: unable to load HighlightRuleList. Parent is not a Coresession!"; + //loadDefaults(); + return; + } + + initSetHighlightRuleList(Core::getUserSetting(session->user(), "HighlightRuleList").toMap()); + + // we store our settings whenever they change + connect(this, SIGNAL(updatedRemotely()), SLOT(save())); +} + +void CoreHighlightRuleManager::save() const +{ + CoreSession *session = qobject_cast(parent()); + if (!session) { + qWarning() << "CoreHighlightRuleManager: unable to save HighlightRuleList. Parent is not a Coresession!"; + return; + } + + Core::setUserSetting(session->user(), "HighlightRuleList", initHighlightRuleList()); +} + +bool CoreHighlightRuleManager::match(const RawMessage &msg, const QString ¤tNick, const QStringList &identityNicks) +{ + return _match(msg.text, msg.sender, msg.type, msg.flags, msg.target, currentNick, identityNicks); +} diff --git a/src/core/corehighlightrulemanager.h b/src/core/corehighlightrulemanager.h new file mode 100644 index 00000000..c8fb4508 --- /dev/null +++ b/src/core/corehighlightrulemanager.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (C) 2005-2016 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef COREHIGHLIGHTRULEMANAHER_H +#define COREHIGHLIGHTRULEMANAHER_H + +#include "highlightrulemanager.h" + +class CoreSession; +struct RawMessage; + +class CoreHighlightRuleManager : public HighlightRuleManager +{ + SYNCABLE_OBJECT + Q_OBJECT + +public: + explicit CoreHighlightRuleManager(CoreSession *parent); + + inline virtual const QMetaObject *syncMetaObject() const { return &HighlightRuleManager::staticMetaObject; } + + bool match(const RawMessage &msg, const QString ¤tNick, const QStringList &identityNicks); +public slots: + virtual inline void requestToggleHighlightRule(const QString &highlightRule) { toggleHighlightRule(highlightRule); } + virtual inline void requestRemoveHighlightRule(const QString &highlightRule) { removeHighlightRule(highlightRule); } + virtual inline void requestAddHighlightRule(const QString &name, bool isRegEx, bool isCaseSensitive, + bool isEnabled, const QString &chanName) + { + addHighlightRule(name, isRegEx, isCaseSensitive, isEnabled, chanName); + } + + +private slots: + void save() const; +}; + + +#endif //COREHIGHLIGHTRULEMANAHER_H diff --git a/src/core/coresession.cpp b/src/core/coresession.cpp index c97c4bf4..a2583e0c 100644 --- a/src/core/coresession.cpp +++ b/src/core/coresession.cpp @@ -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); @@ -131,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) @@ -317,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; diff --git a/src/core/coresession.h b/src/core/coresession.h index 61b7d22e..3bc7cd0a 100644 --- a/src/core/coresession.h +++ b/src/core/coresession.h @@ -31,6 +31,7 @@ #include "protocol.h" #include "message.h" #include "storage.h" +#include "corehighlightrulemanager.h" class CoreBacklogManager; class CoreBufferSyncer; @@ -87,6 +88,7 @@ public: inline CoreIrcListHelper *ircListHelper() const { return _ircListHelper; } inline CoreIgnoreListManager *ignoreListManager() { return &_ignoreListManager; } + inline HighlightRuleManager *highlightRuleManager() { return &_highlightRuleManager; } inline CoreTransferManager *transferManager() const { return _transferManager; } inline CoreDccConfig *dccConfig() const { return _dccConfig; } @@ -238,6 +240,7 @@ private: QList _messageQueue; bool _processMessages; CoreIgnoreListManager _ignoreListManager; + CoreHighlightRuleManager _highlightRuleManager; }; diff --git a/src/qtui/qtuimessageprocessor.cpp b/src/qtui/qtuimessageprocessor.cpp index eee90aab..de35336a 100644 --- a/src/qtui/qtuimessageprocessor.cpp +++ b/src/qtui/qtuimessageprocessor.cpp @@ -57,7 +57,8 @@ void QtUiMessageProcessor::reset() void QtUiMessageProcessor::process(Message &msg) { - checkForHighlight(msg); + if (!Client::coreFeatures().testFlag(Quassel::Feature::CoreSideHighlights)) + checkForHighlight(msg); preProcess(msg); Client::messageModel()->insertMessage(msg); } @@ -68,7 +69,8 @@ void QtUiMessageProcessor::process(QList &msgs) QList::iterator msgIter = msgs.begin(); QList::iterator msgIterEnd = msgs.end(); while (msgIter != msgIterEnd) { - checkForHighlight(*msgIter); + if (!Client::coreFeatures().testFlag(Quassel::Feature::CoreSideHighlights)) + checkForHighlight(*msgIter); preProcess(*msgIter); ++msgIter; } -- 2.20.1