Chatterino
IrcHelpers.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <IrcMessage>
4 #include <QString>
5 
6 namespace chatterino {
7 
8 inline QString parseTagString(const QString &input)
9 {
10  QString output = input;
11  output.detach();
12 
13  auto length = output.length();
14 
15  for (int i = 0; i < length - 1; i++)
16  {
17  if (output[i] == '\\')
18  {
19  QChar c = output[i + 1];
20 
21  switch (c.cell())
22  {
23  case 'n': {
24  output.replace(i, 2, '\n');
25  }
26  break;
27 
28  case 'r': {
29  output.replace(i, 2, '\r');
30  }
31  break;
32 
33  case 's': {
34  output.replace(i, 2, ' ');
35  }
36  break;
37 
38  case '\\': {
39  output.replace(i, 2, '\\');
40  }
41  break;
42 
43  case ':': {
44  output.replace(i, 2, ';');
45  }
46  break;
47 
48  default: {
49  output.remove(i, 1);
50  }
51  break;
52  }
53 
54  length--;
55  }
56  }
57 
58  return output;
59 }
60 
61 inline QDateTime calculateMessageTime(const Communi::IrcMessage *message)
62 {
63  // Check if message is from recent-messages API
64  if (message->tags().contains("historical"))
65  {
66  bool customReceived = false;
67  auto ts =
68  message->tags().value("rm-received-ts").toLongLong(&customReceived);
69  if (!customReceived)
70  {
71  ts = message->tags().value("tmi-sent-ts").toLongLong();
72  }
73 
74  return QDateTime::fromMSecsSinceEpoch(ts);
75  }
76 
77  // If present, handle tmi-sent-ts tag and use it as timestamp
78  if (message->tags().contains("tmi-sent-ts"))
79  {
80  auto ts = message->tags().value("tmi-sent-ts").toLongLong();
81  return QDateTime::fromMSecsSinceEpoch(ts);
82  }
83 
84  // Some IRC Servers might have server-time tag containing UTC date in ISO format, use it as timestamp
85  // See: https://ircv3.net/irc/#server-time
86  if (message->tags().contains("time"))
87  {
88  QString timedate = message->tags().value("time").toString();
89 
90  auto date = QDateTime::fromString(timedate, Qt::ISODate);
91  date.setTimeSpec(Qt::TimeSpec::UTC);
92  return date.toLocalTime();
93  }
94 
95  // Fallback to current time
96  return QDateTime::currentDateTime();
97 }
98 
99 } // namespace chatterino
Definition: Application.cpp:48
QDateTime calculateMessageTime(const Communi::IrcMessage *message)
Definition: IrcHelpers.hpp:61
QString parseTagString(const QString &input)
Definition: IrcHelpers.hpp:8