Fix link parsing in IRC (#3334)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "providers/irc/IrcMessageBuilder.hpp"
|
||||
|
||||
#include "Application.hpp"
|
||||
#include "common/IrcColors.hpp"
|
||||
#include "controllers/accounts/AccountController.hpp"
|
||||
#include "controllers/ignores/IgnoreController.hpp"
|
||||
#include "controllers/ignores/IgnorePhrase.hpp"
|
||||
@@ -15,6 +16,14 @@
|
||||
#include "util/IrcHelpers.hpp"
|
||||
#include "widgets/Window.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
QRegularExpression IRC_COLOR_PARSE_REGEX(
|
||||
"(\u0003(\\d{1,2})?(,(\\d{1,2}))?|\u000f)",
|
||||
QRegularExpression::UseUnicodePropertiesOption);
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
IrcMessageBuilder::IrcMessageBuilder(
|
||||
@@ -68,7 +77,107 @@ MessagePtr IrcMessageBuilder::build()
|
||||
|
||||
void IrcMessageBuilder::addWords(const QStringList &words)
|
||||
{
|
||||
this->emplace<IrcTextElement>(words.join(' '), MessageElementFlag::Text);
|
||||
MessageColor defaultColorType = MessageColor::Text;
|
||||
auto defaultColor = defaultColorType.getColor(*getApp()->themes);
|
||||
QColor textColor = defaultColor;
|
||||
int fg = -1;
|
||||
int bg = -1;
|
||||
|
||||
for (auto word : words)
|
||||
{
|
||||
if (word.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto string = QString(word);
|
||||
|
||||
// Actually just text
|
||||
auto linkString = this->matchLink(string);
|
||||
auto link = Link();
|
||||
|
||||
if (!linkString.isEmpty())
|
||||
{
|
||||
this->addLink(string, linkString);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Does the word contain a color changer? If so, split on it.
|
||||
// Add color indicators, then combine into the same word with the color being changed
|
||||
|
||||
auto i = IRC_COLOR_PARSE_REGEX.globalMatch(string);
|
||||
|
||||
if (!i.hasNext())
|
||||
{
|
||||
this->emplace<TextElement>(string, MessageElementFlag::Text,
|
||||
textColor);
|
||||
continue;
|
||||
}
|
||||
|
||||
int pos = 0;
|
||||
int lastPos = 0;
|
||||
|
||||
while (i.hasNext())
|
||||
{
|
||||
auto match = i.next();
|
||||
|
||||
if (lastPos != match.capturedStart() && match.capturedStart() != 0)
|
||||
{
|
||||
if (fg >= 0 && fg <= 98)
|
||||
{
|
||||
textColor = IRC_COLORS[fg];
|
||||
getApp()->themes->normalizeColor(textColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
textColor = defaultColor;
|
||||
}
|
||||
this->emplace<TextElement>(
|
||||
string.mid(lastPos, match.capturedStart() - lastPos),
|
||||
MessageElementFlag::Text, textColor)
|
||||
->setTrailingSpace(false);
|
||||
lastPos = match.capturedStart() + match.capturedLength();
|
||||
}
|
||||
if (!match.captured(1).isEmpty())
|
||||
{
|
||||
fg = -1;
|
||||
bg = -1;
|
||||
}
|
||||
|
||||
if (!match.captured(2).isEmpty())
|
||||
{
|
||||
fg = match.captured(2).toInt(nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
fg = -1;
|
||||
}
|
||||
if (!match.captured(4).isEmpty())
|
||||
{
|
||||
bg = match.captured(4).toInt(nullptr);
|
||||
}
|
||||
else if (fg == -1)
|
||||
{
|
||||
bg = -1;
|
||||
}
|
||||
|
||||
lastPos = match.capturedStart() + match.capturedLength();
|
||||
}
|
||||
|
||||
if (fg >= 0 && fg <= 98)
|
||||
{
|
||||
textColor = IRC_COLORS[fg];
|
||||
getApp()->themes->normalizeColor(textColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
textColor = defaultColor;
|
||||
}
|
||||
this->emplace<TextElement>(string.mid(lastPos),
|
||||
MessageElementFlag::Text, textColor);
|
||||
}
|
||||
|
||||
this->message().elements.back()->setTrailingSpace(false);
|
||||
}
|
||||
|
||||
void IrcMessageBuilder::appendUsername()
|
||||
|
||||
Reference in New Issue
Block a user