mirror-chatterino2/messages/message.cpp

417 lines
15 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "messages/message.h"
2017-01-05 16:07:20 +01:00
#include "colorscheme.h"
2017-01-07 20:43:55 +01:00
#include "emojis.h"
2017-01-11 18:52:09 +01:00
#include "emotes.h"
2017-01-11 01:08:20 +01:00
#include "fonts.h"
2017-01-11 18:52:09 +01:00
#include "ircmanager.h"
2017-01-18 21:30:23 +01:00
#include "messages/link.h"
2017-01-11 18:52:09 +01:00
#include "qcolor.h"
2017-01-13 18:59:11 +01:00
#include "resources.h"
2017-01-23 16:38:06 +01:00
#include "settings.h"
2017-01-05 16:07:20 +01:00
2017-01-11 18:52:09 +01:00
#include <QStringList>
2017-01-05 16:43:01 +01:00
#include <ctime>
2017-01-11 01:08:20 +01:00
#include <list>
2017-01-11 18:52:09 +01:00
#include <tuple>
2017-01-05 16:43:01 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace messages {
2017-01-11 18:52:09 +01:00
QRegularExpression *Message::cheerRegex =
new QRegularExpression("cheer[1-9][0-9]*");
2017-01-07 20:43:55 +01:00
2017-01-05 16:07:20 +01:00
Message::Message(const QString &text)
{
2017-01-24 20:15:12 +01:00
words.push_back(Word(text, Word::Text,
ColorScheme::getInstance().SystemMessageColor, text,
QString()));
2017-01-05 16:07:20 +01:00
}
2017-01-26 17:48:14 +01:00
Message::Message(const IrcPrivateMessage &ircMessage, Channel &channel,
2017-01-11 18:52:09 +01:00
bool enablePingSound, bool isReceivedWhisper,
bool isSentWhisper, bool includeChannel)
2017-01-05 16:07:20 +01:00
{
2017-01-18 04:33:30 +01:00
this->parseTime = std::chrono::system_clock::now();
2017-01-05 16:07:20 +01:00
2017-01-11 01:08:20 +01:00
auto words = std::vector<Word>();
auto tags = ircMessage.tags();
2017-01-05 16:07:20 +01:00
2017-01-11 01:08:20 +01:00
auto iterator = tags.find("id");
2017-01-05 20:49:33 +01:00
2017-01-11 18:52:09 +01:00
if (iterator != tags.end()) {
2017-01-18 04:33:30 +01:00
this->id = iterator.value().toString();
2017-01-05 20:49:33 +01:00
}
2017-01-05 16:07:20 +01:00
// timestamps
2017-01-11 01:08:20 +01:00
iterator = tags.find("tmi-sent-ts");
2017-01-13 18:59:11 +01:00
2017-01-05 16:43:01 +01:00
std::time_t time = std::time(NULL);
2017-01-05 16:07:20 +01:00
2017-01-13 18:59:11 +01:00
// if (iterator != tags.end()) {
// time = strtoll(iterator.value().toString().toStdString().c_str(),
// NULL,
// 10);
// }
2017-01-05 16:07:20 +01:00
char timeStampBuffer[69];
strftime(timeStampBuffer, 69, "%H:%M", localtime(&time));
QString timestamp = QString(timeStampBuffer);
strftime(timeStampBuffer, 69, "%H:%M:%S", localtime(&time));
QString timestampWithSeconds = QString(timeStampBuffer);
2017-01-11 18:52:09 +01:00
words.push_back(Word(timestamp, Word::TimestampNoSeconds,
2017-01-24 20:15:12 +01:00
ColorScheme::getInstance().SystemMessageColor,
QString(), QString()));
2017-01-11 18:52:09 +01:00
words.push_back(Word(timestampWithSeconds, Word::TimestampWithSeconds,
2017-01-24 20:15:12 +01:00
ColorScheme::getInstance().SystemMessageColor,
QString(), QString()));
2017-01-06 23:28:48 +01:00
2017-01-15 16:38:30 +01:00
// mod buttons
static QString buttonBanTooltip("Ban user");
static QString buttonTimeoutTooltip("Timeout user");
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getButtonBan(), Word::ButtonBan, QString(),
2017-01-15 16:38:30 +01:00
buttonBanTooltip,
Link(Link::UserBan, ircMessage.account())));
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getButtonTimeout(), Word::ButtonTimeout,
2017-01-15 16:38:30 +01:00
QString(), buttonTimeoutTooltip,
Link(Link::UserTimeout, ircMessage.account())));
2017-01-06 23:28:48 +01:00
// badges
2017-01-11 01:08:20 +01:00
iterator = tags.find("badges");
2017-01-06 23:28:48 +01:00
2017-01-11 18:52:09 +01:00
if (iterator != tags.end()) {
2017-01-06 23:28:48 +01:00
auto badges = iterator.value().toString().split(',');
2017-01-11 18:52:09 +01:00
for (QString badge : badges) {
if (badge.startsWith("bits/")) {
long long int cheer =
strtoll(badge.mid(5).toStdString().c_str(), NULL, 10);
words.push_back(Word(
Emotes::getCheerBadge(cheer), Word::BadgeCheer, QString(),
QString("Twitch Cheer" + QString::number(cheer))));
} else if (badge == "staff/1") {
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getBadgeStaff(),
Word::BadgeStaff, QString(),
QString("Twitch Staff")));
2017-01-11 18:52:09 +01:00
} else if (badge == "admin/1") {
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getBadgeAdmin(),
Word::BadgeAdmin, QString(),
QString("Twitch Admin")));
2017-01-11 18:52:09 +01:00
} else if (badge == "global_mod/1") {
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getBadgeGlobalmod(),
2017-01-13 18:59:11 +01:00
Word::BadgeGlobalMod, QString(),
QString("Global Moderator")));
2017-01-11 18:52:09 +01:00
} else if (badge == "moderator/1") {
// TODO: implement this xD
2017-01-13 18:59:11 +01:00
words.push_back(Word(
2017-01-18 04:33:30 +01:00
Resources::getBadgeTurbo(), Word::BadgeModerator, QString(),
2017-01-13 18:59:11 +01:00
QString("Channel Moderator"))); // custom badge
2017-01-11 18:52:09 +01:00
} else if (badge == "turbo/1") {
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getBadgeStaff(),
Word::BadgeTurbo, QString(),
QString("Turbo Subscriber")));
2017-01-11 18:52:09 +01:00
} else if (badge == "broadcaster/1") {
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getBadgeBroadcaster(),
2017-01-13 18:59:11 +01:00
Word::BadgeBroadcaster, QString(),
2017-01-11 18:52:09 +01:00
QString("Channel Broadcaster")));
} else if (badge == "premium/1") {
2017-01-18 04:33:30 +01:00
words.push_back(Word(Resources::getBadgePremium(),
2017-01-13 18:59:11 +01:00
Word::BadgePremium, QString(),
QString("Twitch Prime")));
2017-01-06 23:28:48 +01:00
}
}
}
2017-01-05 20:49:33 +01:00
// color
2017-01-24 20:15:12 +01:00
QColor usernameColor = ColorScheme::getInstance().SystemMessageColor;
2017-01-05 20:49:33 +01:00
2017-01-11 01:08:20 +01:00
iterator = tags.find("color");
2017-01-11 18:52:09 +01:00
if (iterator != tags.end()) {
usernameColor = QColor(iterator.value().toString());
2017-01-05 20:49:33 +01:00
}
2017-01-05 16:22:55 +01:00
2017-01-05 20:49:33 +01:00
// channel name
2017-01-11 18:52:09 +01:00
if (includeChannel) {
2017-01-18 04:33:30 +01:00
QString channelName("#" + channel.getName());
words.push_back(Word(
2017-01-24 20:15:12 +01:00
channelName, Word::Misc,
ColorScheme::getInstance().SystemMessageColor, QString(channelName),
QString(), Link(Link::Url, channel.getName() + "\n" + this->id)));
2017-01-05 20:49:33 +01:00
}
2017-01-05 16:07:20 +01:00
// username
2017-01-18 04:33:30 +01:00
this->userName = ircMessage.account();
2017-01-05 16:07:20 +01:00
2017-01-18 04:33:30 +01:00
if (this->userName.isEmpty()) {
2017-01-11 01:08:20 +01:00
auto iterator = tags.find("login");
2017-01-05 16:07:20 +01:00
2017-01-11 18:52:09 +01:00
if (iterator != tags.end()) {
2017-01-18 04:33:30 +01:00
this->userName = iterator.value().toString();
2017-01-05 16:07:20 +01:00
}
}
QString displayName;
2017-01-11 01:08:20 +01:00
iterator = tags.find("display-name");
if (iterator == tags.end()) {
2017-01-05 20:49:33 +01:00
displayName = ircMessage.account();
2017-01-11 18:52:09 +01:00
} else {
2017-01-05 16:07:20 +01:00
displayName = iterator.value().toString();
}
2017-01-11 18:52:09 +01:00
bool hasLocalizedName =
QString::compare(displayName, ircMessage.account()) == 0;
QString userDisplayString =
displayName +
(hasLocalizedName ? (" (" + ircMessage.account() + ")") : QString());
2017-01-05 16:07:20 +01:00
2017-01-11 18:52:09 +01:00
if (isSentWhisper) {
2017-01-18 04:33:30 +01:00
userDisplayString = IrcManager::account->getUsername() + " -> ";
2017-01-07 20:43:55 +01:00
}
2017-01-11 18:52:09 +01:00
if (isReceivedWhisper) {
2017-01-18 04:33:30 +01:00
userDisplayString += " -> " + IrcManager::account->getUsername();
2017-01-07 20:43:55 +01:00
}
2017-01-11 18:52:09 +01:00
if (!ircMessage.isAction()) {
2017-01-07 20:43:55 +01:00
userDisplayString += ": ";
}
2017-01-11 18:52:09 +01:00
words.push_back(Word(userDisplayString, Word::Username, usernameColor,
userDisplayString, QString()));
2017-01-05 16:07:20 +01:00
2017-01-18 04:33:30 +01:00
// highlights
// TODO: implement this xD
2017-01-05 16:07:20 +01:00
// bits
QString bits = "";
2017-01-11 01:08:20 +01:00
iterator = tags.find("bits");
2017-01-11 18:52:09 +01:00
if (iterator != tags.end()) {
bits = iterator.value().toString();
2017-01-05 16:07:20 +01:00
}
2017-01-06 23:28:48 +01:00
// twitch emotes
2017-01-11 18:52:09 +01:00
std::vector<std::pair<long int, LazyLoadedImage *>> twitchEmotes;
2017-01-06 23:28:48 +01:00
2017-01-11 01:08:20 +01:00
iterator = tags.find("emotes");
2017-01-05 16:07:20 +01:00
2017-01-11 18:52:09 +01:00
if (iterator != tags.end()) {
2017-01-06 23:28:48 +01:00
auto emotes = iterator.value().toString().split('/');
2017-01-05 16:07:20 +01:00
2017-01-11 18:52:09 +01:00
for (QString emote : emotes) {
if (!emote.contains(':'))
continue;
2017-01-06 23:28:48 +01:00
QStringList parameters = emote.split(':');
2017-01-11 18:52:09 +01:00
if (parameters.length() < 2)
continue;
2017-01-06 23:28:48 +01:00
long int id = std::stol(parameters.at(0).toStdString(), NULL, 10);
QStringList occurences = parameters.at(1).split(',');
2017-01-11 18:52:09 +01:00
for (QString occurence : occurences) {
2017-01-06 23:28:48 +01:00
QStringList coords = occurence.split('-');
2017-01-11 18:52:09 +01:00
if (coords.length() < 2)
continue;
2017-01-06 23:28:48 +01:00
2017-01-11 18:52:09 +01:00
long int start =
std::stol(coords.at(0).toStdString(), NULL, 10);
2017-01-06 23:28:48 +01:00
long int end = std::stol(coords.at(1).toStdString(), NULL, 10);
2017-01-11 18:52:09 +01:00
if (start >= end || start < 0 ||
end > ircMessage.content().length())
continue;
2017-01-06 23:28:48 +01:00
QString name = ircMessage.content().mid(start, end - start);
2017-01-11 18:52:09 +01:00
twitchEmotes.push_back(std::pair<long int, LazyLoadedImage *>(
start, Emotes::getTwitchEmoteById(name, id)));
2017-01-05 16:07:20 +01:00
}
}
2017-01-06 23:28:48 +01:00
std::sort(twitchEmotes.begin(), twitchEmotes.end(), sortTwitchEmotes);
2017-01-05 16:07:20 +01:00
}
2017-01-05 20:49:33 +01:00
2017-01-06 23:28:48 +01:00
auto currentTwitchEmote = twitchEmotes.begin();
// words
2017-01-11 18:52:09 +01:00
QColor textColor =
2017-01-24 20:15:12 +01:00
ircMessage.isAction() ? usernameColor : ColorScheme::getInstance().Text;
2017-01-05 20:49:33 +01:00
2017-01-06 23:28:48 +01:00
QStringList splits = ircMessage.content().split(' ');
long int i = 0;
2017-01-11 18:52:09 +01:00
for (QString split : splits) {
2017-01-06 23:28:48 +01:00
// twitch emote
2017-01-15 16:38:30 +01:00
if (currentTwitchEmote != twitchEmotes.end() &&
currentTwitchEmote->first == i) {
2017-01-11 18:52:09 +01:00
words.push_back(Word(currentTwitchEmote->second,
Word::TwitchEmoteImage,
2017-01-18 04:33:30 +01:00
currentTwitchEmote->second->getName(),
currentTwitchEmote->second->getName() +
2017-01-11 18:52:09 +01:00
QString("\nTwitch Emote")));
2017-01-18 04:33:30 +01:00
words.push_back(Word(currentTwitchEmote->second->getName(),
2017-01-11 18:52:09 +01:00
Word::TwitchEmoteText, textColor,
2017-01-18 04:33:30 +01:00
currentTwitchEmote->second->getName(),
currentTwitchEmote->second->getName() +
2017-01-11 18:52:09 +01:00
QString("\nTwitch Emote")));
2017-01-06 23:28:48 +01:00
i += split.length() + 1;
currentTwitchEmote = std::next(currentTwitchEmote);
continue;
}
// split words
2017-01-11 18:52:09 +01:00
std::vector<std::tuple<LazyLoadedImage *, QString>> parsed;
2017-01-07 20:43:55 +01:00
Emojis::parseEmojis(parsed, split);
2017-01-11 18:52:09 +01:00
for (const std::tuple<LazyLoadedImage *, QString> &tuple : parsed) {
LazyLoadedImage *image = std::get<0>(tuple);
2017-01-15 16:38:30 +01:00
if (image == NULL) { // is text
2017-01-07 20:43:55 +01:00
QString string = std::get<1>(tuple);
// cheers
2017-01-11 18:52:09 +01:00
if (!bits.isEmpty() && string.length() >= 6 &&
cheerRegex->match(string).isValid()) {
2017-01-07 20:43:55 +01:00
auto cheer = string.mid(5).toInt();
QString color;
QColor bitsColor;
2017-01-11 18:52:09 +01:00
if (cheer >= 10000) {
2017-01-07 20:43:55 +01:00
color = "red";
bitsColor = QColor::fromHslF(0, 1, 0.5);
2017-01-11 18:52:09 +01:00
} else if (cheer >= 5000) {
2017-01-07 20:43:55 +01:00
color = "blue";
bitsColor = QColor::fromHslF(0.61, 1, 0.4);
2017-01-11 18:52:09 +01:00
} else if (cheer >= 1000) {
2017-01-07 20:43:55 +01:00
color = "green";
bitsColor = QColor::fromHslF(0.5, 1, 0.5);
2017-01-11 18:52:09 +01:00
} else if (cheer >= 100) {
2017-01-07 20:43:55 +01:00
color = "purple";
bitsColor = QColor::fromHslF(0.8, 1, 0.5);
2017-01-11 18:52:09 +01:00
} else {
2017-01-07 20:43:55 +01:00
color = "gray";
bitsColor = QColor::fromHslF(0.5f, 0.5f, 0.5f);
}
2017-01-11 18:52:09 +01:00
QString bitsLinkAnimated = QString(
"http://static-cdn.jtvnw.net/bits/dark/animated/" +
color + "/1");
QString bitsLink = QString(
"http://static-cdn.jtvnw.net/bits/dark/static/" +
color + "/1");
LazyLoadedImage *imageAnimated =
2017-01-18 04:33:30 +01:00
Emotes::getMiscImageFromCache().getOrAdd(
2017-01-11 18:52:09 +01:00
bitsLinkAnimated, [&bitsLinkAnimated] {
return new LazyLoadedImage(bitsLinkAnimated);
});
LazyLoadedImage *image =
2017-01-18 04:33:30 +01:00
Emotes::getMiscImageFromCache().getOrAdd(
2017-01-11 18:52:09 +01:00
bitsLink, [&bitsLink] {
return new LazyLoadedImage(bitsLink);
});
words.push_back(
Word(imageAnimated, Word::BitsAnimated,
QString("cheer"), QString("Twitch Cheer"),
Link(Link::Url,
QString("https://blog.twitch.tv/"
"introducing-cheering-celebrate-"
"together-da62af41fac6"))));
words.push_back(
2017-01-22 12:46:35 +01:00
Word(image, Word::BitsStatic, QString("cheer"),
2017-01-11 18:52:09 +01:00
QString("Twitch Cheer"),
Link(Link::Url,
QString("https://blog.twitch.tv/"
"introducing-cheering-celebrate-"
"together-da62af41fac6"))));
words.push_back(
Word(QString("x" + string.mid(5)), Word::BitsAmount,
bitsColor, QString(string.mid(5)),
QString("Twitch Cheer"),
Link(Link::Url,
QString("https://blog.twitch.tv/"
"introducing-cheering-celebrate-"
"together-da62af41fac6"))));
2017-01-07 20:43:55 +01:00
2017-01-11 18:52:09 +01:00
continue;
2017-01-11 01:08:20 +01:00
}
2017-01-07 20:43:55 +01:00
// bttv / ffz emotes
2017-01-11 18:52:09 +01:00
LazyLoadedImage *bttvEmote;
// TODO: Implement this (ignored emotes)
2017-01-18 04:33:30 +01:00
if (Emotes::getBttvEmotes().tryGet(string, bttvEmote) ||
channel.getBttvChannelEmotes().tryGet(string, bttvEmote) ||
Emotes::getFfzEmotes().tryGet(string, bttvEmote) ||
channel.getFfzChannelEmotes().tryGet(string, bttvEmote) ||
Emotes::getChatterinoEmotes().tryGet(string, bttvEmote)) {
2017-01-11 18:52:09 +01:00
words.push_back(Word(bttvEmote, Word::BttvEmoteImage,
2017-01-18 04:33:30 +01:00
bttvEmote->getName(),
bttvEmote->getTooltip(),
Link(Link::Url, bttvEmote->getUrl())));
2017-01-07 20:43:55 +01:00
continue;
}
// actually just a word
QString link = matchLink(string);
2017-01-11 18:52:09 +01:00
words.push_back(
Word(string, Word::Text, textColor, string, QString(),
link.isEmpty() ? Link() : Link(Link::Url, link)));
2017-01-15 16:38:30 +01:00
} else { // is emoji
static QString emojiTooltip("Emoji");
2017-01-18 04:33:30 +01:00
words.push_back(Word(image, Word::EmojiImage, image->getName(),
emojiTooltip));
Word(image->getName(), Word::EmojiText, textColor,
image->getName(), emojiTooltip);
2017-01-07 20:43:55 +01:00
}
}
i += split.length() + 1;
2017-01-06 23:28:48 +01:00
}
2017-01-07 20:43:55 +01:00
2017-01-18 04:33:30 +01:00
this->words = words;
2017-01-07 20:43:55 +01:00
// TODO: Implement this xD
2017-01-11 18:52:09 +01:00
// if (!isReceivedWhisper &&
// AppSettings.HighlightIgnoredUsers.ContainsKey(Username))
// {
// HighlightTab = false;
// }
2017-01-06 23:28:48 +01:00
}
2017-01-11 18:52:09 +01:00
QString
Message::matchLink(const QString &string)
2017-01-07 20:43:55 +01:00
{
// TODO: Implement this xD
2017-01-07 20:43:55 +01:00
return QString();
}
2017-01-28 20:29:02 +01:00
bool
Message::sortTwitchEmotes(const std::pair<long int, LazyLoadedImage *> &a,
const std::pair<long int, LazyLoadedImage *> &b)
{
return a.first < b.first;
2017-01-18 21:30:23 +01:00
}
2017-01-28 20:29:02 +01:00
} // namespace messages
} // namespace chatterino