Add better support for IRC private messages (#4158)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
Mm2PL
2022-11-18 20:11:56 +01:00
committed by GitHub
parent 79a36e763d
commit 2f4272cc2a
7 changed files with 215 additions and 91 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ Outcome invokeIrcCommand(const QString &commandName, const QString &allParams,
if (cmd == "msg")
{
sendRaw("PRIVMSG " + params[0] + " :" + paramsAfter(0));
channel.server()->sendWhisper(params[0], paramsAfter(0));
}
else if (cmd == "away")
{
+36 -1
View File
@@ -6,6 +6,7 @@
#include "controllers/ignores/IgnoreController.hpp"
#include "controllers/ignores/IgnorePhrase.hpp"
#include "messages/Message.hpp"
#include "messages/MessageColor.hpp"
#include "providers/chatterino/ChatterinoBadges.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Resources.hpp"
@@ -41,6 +42,15 @@ IrcMessageBuilder::IrcMessageBuilder(
{
}
IrcMessageBuilder::IrcMessageBuilder(
const Communi::IrcPrivateMessage *_ircMessage,
const MessageParseArgs &_args)
: SharedMessageBuilder(Channel::getEmpty().get(), _ircMessage, _args,
_ircMessage->content(), false)
, whisperTarget_(_ircMessage->target())
{
}
MessagePtr IrcMessageBuilder::build()
{
// PARSE
@@ -93,7 +103,32 @@ void IrcMessageBuilder::appendUsername()
this->emplace<TextElement>("->", MessageElementFlag::Username,
MessageColor::System, FontStyle::ChatMedium);
this->emplace<TextElement>("you:", MessageElementFlag::Username);
if (this->whisperTarget_.isEmpty())
{
this->emplace<TextElement>("you:", MessageElementFlag::Username);
}
else
{
this->emplace<TextElement>(this->whisperTarget_ + ":",
MessageElementFlag::Username,
getRandomColor(this->whisperTarget_),
FontStyle::ChatMediumBold);
}
}
else if (this->args.isSentWhisper)
{
this->emplace<TextElement>(usernameText, MessageElementFlag::Username,
this->usernameColor_,
FontStyle::ChatMediumBold);
// Separator
this->emplace<TextElement>("->", MessageElementFlag::Username,
MessageColor::System, FontStyle::ChatMedium);
this->emplace<TextElement>(
this->whisperTarget_ + ":", MessageElementFlag::Username,
getRandomColor(this->whisperTarget_), FontStyle::ChatMediumBold)
->setLink({Link::UserWhisper, this->whisperTarget_});
}
else
{
+13
View File
@@ -36,10 +36,23 @@ public:
explicit IrcMessageBuilder(const Communi::IrcNoticeMessage *_ircMessage,
const MessageParseArgs &_args);
/**
* @brief used for whisper messages (i.e. PRIVMSG messages with our nick as the target)
**/
explicit IrcMessageBuilder(const Communi::IrcPrivateMessage *_ircMessage,
const MessageParseArgs &_args);
MessagePtr build() override;
private:
void appendUsername();
/**
* @brief holds the name of the target for the private/direct IRC message
*
* This might not be our nick
*/
QString whisperTarget_;
};
} // namespace chatterino
+64
View File
@@ -5,6 +5,8 @@
#include "common/QLogging.hpp"
#include "messages/Message.hpp"
#include "messages/MessageColor.hpp"
#include "messages/MessageElement.hpp"
#include "providers/irc/Irc2.hpp"
#include "providers/irc/IrcChannel2.hpp"
#include "providers/irc/IrcMessageBuilder.hpp"
@@ -190,6 +192,36 @@ void IrcServer::onReadConnected(IrcConnection *connection)
void IrcServer::privateMessageReceived(Communi::IrcPrivateMessage *message)
{
// Note: This doesn't use isPrivate() because it only applies to messages targeting our user,
// Servers or bouncers may send messages which have our user as the source
// (like with echo-message CAP), we need to take care of this.
if (!message->target().startsWith("#"))
{
MessageParseArgs args;
if (message->isOwn())
{
// The server sent us a whisper which has our user as the source
args.isSentWhisper = true;
}
else
{
args.isReceivedWhisper = true;
}
IrcMessageBuilder builder(message, args);
auto msg = builder.build();
for (auto &&weak : this->channels)
{
if (auto shared = weak.lock())
{
shared->addMessage(msg);
}
}
return;
}
auto target = message->target();
target = target.startsWith('#') ? target.mid(1) : target;
@@ -299,6 +331,38 @@ void IrcServer::readConnectionMessageReceived(Communi::IrcMessage *message)
}
}
void IrcServer::sendWhisper(const QString &target, const QString &message)
{
this->sendRawMessage(QString("PRIVMSG %1 :%2").arg(target, message));
if (this->hasEcho())
{
return;
}
MessageParseArgs args;
args.isSentWhisper = true;
MessageBuilder b;
b.emplace<TimestampElement>();
b.emplace<TextElement>(this->nick(), MessageElementFlag::Text,
MessageColor::Text, FontStyle::ChatMediumBold);
b.emplace<TextElement>("->", MessageElementFlag::Text,
MessageColor::System);
b.emplace<TextElement>(target + ":", MessageElementFlag::Text,
MessageColor::Text, FontStyle::ChatMediumBold);
b.emplace<TextElement>(message, MessageElementFlag::Text);
auto msg = b.release();
for (auto &&weak : this->channels)
{
if (auto shared = weak.lock())
{
shared->addMessage(msg);
}
}
}
bool IrcServer::hasEcho() const
{
return this->hasEcho_;
+4
View File
@@ -21,6 +21,10 @@ public:
const QString &userFriendlyIdentifier();
bool hasEcho() const;
/**
* @brief sends a whisper to the target user (PRIVMSG where a user is the target)
*/
void sendWhisper(const QString &target, const QString &message);
// AbstractIrcServer interface
protected: