mirror-chatterino2/src/messages/MessageBuilder.cpp

102 lines
2.5 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "MessageBuilder.hpp"
#include "singletons/EmoteManager.hpp"
#include "singletons/ResourceManager.hpp"
#include "singletons/ThemeManager.hpp"
2017-04-12 17:46:44 +02:00
#include <QDateTime>
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-04-12 17:46:44 +02:00
MessageBuilder::MessageBuilder()
2017-09-12 19:06:16 +02:00
: message(new Message)
2017-04-12 17:46:44 +02:00
{
}
MessagePtr MessageBuilder::getMessage()
2017-04-12 17:46:44 +02:00
{
2017-09-12 19:06:16 +02:00
return this->message;
2017-04-12 17:46:44 +02:00
}
2018-01-28 03:48:15 +01:00
void MessageBuilder::append(MessageElement *element)
2017-04-12 17:46:44 +02:00
{
this->message->addElement(element);
2017-04-12 17:46:44 +02:00
}
void MessageBuilder::appendTimestamp()
{
this->appendTimestamp(QTime::currentTime());
2017-04-12 17:46:44 +02:00
}
2017-09-24 18:43:24 +02:00
void MessageBuilder::setHighlight(bool value)
2017-07-31 00:57:42 +02:00
{
if (value) {
2018-01-28 03:29:42 +01:00
this->message->flags |= Message::Highlighted;
} else {
2018-01-28 03:29:42 +01:00
this->message->flags &= ~Message::Highlighted;
}
}
void MessageBuilder::appendTimestamp(const QTime &time)
2017-04-12 17:46:44 +02:00
{
2018-01-28 03:48:15 +01:00
this->append(new TimestampElement(time));
2017-04-12 17:46:44 +02:00
}
QString MessageBuilder::matchLink(const QString &string)
{
2018-06-27 19:45:54 +02:00
QFile tldFile(":/tlds.txt");
tldFile.open(QFile::ReadOnly);
QTextStream t1(&tldFile);
t1.setCodec("UTF-8");
QString tldData = t1.readAll();
tldData.replace("\n", "|");
const QString urlRegExp = "^"
// protocol identifier
"(?:(?:https?|ftps?)://)?"
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?"
"(?:"
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"
"|"
// host name
"(?:(?:[_a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)"
// domain name
"(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*"
// TLD identifier
//"(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,}))"
"(?:[\\.](?:" + tldData + "))"
"\\.?"
")"
// port number
"(?::\\d{2,5})?"
// resource path
"(?:[/?#]\\S*)?"
"$";
static QRegularExpression linkRegex(urlRegExp, QRegularExpression::CaseInsensitiveOption);
2017-09-12 19:06:16 +02:00
static QRegularExpression httpRegex("\\bhttps?://");
2018-06-27 19:45:54 +02:00
static QRegularExpression ftpRegex("\\bftps?://");
2017-08-05 18:44:14 +02:00
auto match = linkRegex.match(string);
2017-08-12 12:09:26 +02:00
if (!match.hasMatch()) {
return QString();
}
2017-08-05 18:44:14 +02:00
QString captured = match.captured();
2017-09-12 19:06:16 +02:00
if (!captured.contains(httpRegex)) {
2018-06-27 19:45:54 +02:00
if (!captured.contains(ftpRegex)) {
captured.insert(0, "http://");
}
}
2017-09-24 18:43:24 +02:00
return captured;
2017-04-12 17:46:44 +02:00
}
} // namespace chatterino