mirror-chatterino2/src/util/IrcHelpers.hpp

100 lines
2.4 KiB
C++
Raw Normal View History

2017-12-17 00:01:42 +01:00
#pragma once
#include <IrcMessage>
2017-12-17 00:01:42 +01:00
#include <QString>
namespace chatterino {
2018-06-04 12:23:23 +02:00
inline QString parseTagString(const QString &input)
2017-12-17 00:01:42 +01:00
{
QString output = input;
2017-12-17 00:39:27 +01:00
output.detach();
2017-12-17 00:01:42 +01:00
2018-06-05 16:29:06 +02:00
auto length = output.length();
2017-12-17 00:01:42 +01:00
2018-10-21 13:43:02 +02:00
for (int i = 0; i < length - 1; i++)
{
if (output[i] == '\\')
{
2017-12-17 00:39:27 +01:00
QChar c = output[i + 1];
2018-10-21 13:43:02 +02:00
switch (c.cell())
{
2019-09-26 00:51:05 +02:00
case 'n': {
2018-06-05 16:29:06 +02:00
output.replace(i, 2, '\n');
2018-10-21 13:43:02 +02:00
}
break;
2017-12-17 00:56:33 +01:00
2019-09-26 00:51:05 +02:00
case 'r': {
2018-06-05 16:29:06 +02:00
output.replace(i, 2, '\r');
2018-10-21 13:43:02 +02:00
}
break;
2017-12-17 00:56:33 +01:00
2019-09-26 00:51:05 +02:00
case 's': {
2018-06-05 16:29:06 +02:00
output.replace(i, 2, ' ');
2018-10-21 13:43:02 +02:00
}
break;
2017-12-17 00:56:33 +01:00
2019-09-26 00:51:05 +02:00
case '\\': {
2018-06-05 16:29:06 +02:00
output.replace(i, 2, '\\');
2018-10-21 13:43:02 +02:00
}
break;
2017-12-17 00:56:33 +01:00
2019-09-26 00:51:05 +02:00
case ':': {
2018-06-05 16:29:06 +02:00
output.replace(i, 2, ';');
2018-10-21 13:43:02 +02:00
}
break;
2017-12-17 00:56:33 +01:00
2019-09-26 00:51:05 +02:00
default: {
2018-06-05 16:29:06 +02:00
output.remove(i, 1);
2018-10-21 13:43:02 +02:00
}
break;
2017-12-17 00:39:27 +01:00
}
2017-12-17 00:56:33 +01:00
2018-06-05 16:29:06 +02:00
length--;
2017-12-17 00:39:27 +01:00
}
}
return output;
2017-12-17 00:01:42 +01:00
}
inline QDateTime calculateMessageTime(const Communi::IrcMessage *message)
{
// Check if message is from recent-messages API
if (message->tags().contains("historical"))
{
bool customReceived = false;
auto ts =
message->tags().value("rm-received-ts").toLongLong(&customReceived);
if (!customReceived)
{
ts = message->tags().value("tmi-sent-ts").toLongLong();
}
return QDateTime::fromMSecsSinceEpoch(ts);
}
// If present, handle tmi-sent-ts tag and use it as timestamp
if (message->tags().contains("tmi-sent-ts"))
{
auto ts = message->tags().value("tmi-sent-ts").toLongLong();
return QDateTime::fromMSecsSinceEpoch(ts);
}
// Some IRC Servers might have server-time tag containing UTC date in ISO format, use it as timestamp
// See: https://ircv3.net/irc/#server-time
if (message->tags().contains("time"))
{
QString timedate = message->tags().value("time").toString();
auto date = QDateTime::fromString(timedate, Qt::ISODate);
date.setTimeSpec(Qt::TimeSpec::UTC);
return date.toLocalTime();
}
// Fallback to current time
return QDateTime::currentDateTime();
}
2017-12-17 00:01:42 +01:00
} // namespace chatterino