src: Yearly copyright bump
[quassel.git] / src / common / util.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "util.h"
22
23 #include <algorithm>
24 #include <array>
25 #include <utility>
26
27 #include <QCoreApplication>
28 #include <QDateTime>
29 #include <QDebug>
30 #include <QTextCodec>
31 #include <QVector>
32
33 #include "quassel.h"
34
35 // MIBenum values from http://www.iana.org/assignments/character-sets/character-sets.xml#table-character-sets-1
36 static QList<int> utf8DetectionBlacklist = QList<int>() << 39 /* ISO-2022-JP */;
37
38 QString nickFromMask(const QString& mask)
39 {
40     return mask.left(mask.indexOf('!'));
41 }
42
43 QString userFromMask(const QString& mask)
44 {
45     const int offset = mask.indexOf('!') + 1;
46     if (offset <= 0)
47         return {};
48     const int length = mask.indexOf('@', offset) - offset;
49     return mask.mid(offset, length >= 0 ? length : -1);
50 }
51
52 QString hostFromMask(const QString& mask)
53 {
54     const int excl = mask.indexOf('!');
55     if (excl < 0)
56         return {};
57     const int offset = mask.indexOf('@', excl + 1) + 1;
58     return offset > 0 && offset < mask.size() ? mask.mid(offset) : QString{};
59 }
60
61 bool isChannelName(const QString& str)
62 {
63     if (str.isEmpty())
64         return false;
65     static constexpr std::array<quint8, 4> prefixes{{'#', '&', '!', '+'}};
66     return std::any_of(prefixes.cbegin(), prefixes.cend(), [&str](quint8 c) { return c == str[0]; });
67 }
68
69 QString stripFormatCodes(QString message)
70 {
71     static QRegExp regEx{"\x03(\\d\\d?(,\\d\\d?)?)?|\x04([\\da-fA-F]{6}(,[\\da-fA-F]{6})?)?|[\x02\x0f\x11\x12\x16\x1d\x1e\x1f]"};
72     return message.remove(regEx);
73 }
74
75 QString stripAcceleratorMarkers(const QString& label_)
76 {
77     QString label = label_;
78     int p = 0;
79     forever
80     {
81         p = label.indexOf('&', p);
82         if (p < 0 || p + 1 >= label.length())
83             break;
84
85         if (label.at(p + 1).isLetterOrNumber() || label.at(p + 1) == '&')
86             label.remove(p, 1);
87
88         ++p;
89     }
90     return label;
91 }
92
93 QString decodeString(const QByteArray& input, QTextCodec* codec)
94 {
95     if (codec && utf8DetectionBlacklist.contains(codec->mibEnum()))
96         return codec->toUnicode(input);
97
98     // First, we check if it's utf8. It is very improbable to encounter a string that looks like
99     // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
100     // is safe to assume that it is.
101     // Q_ASSERT(sizeof(const char) == sizeof(quint8));  // In God we trust...
102     bool isUtf8 = true;
103     int cnt = 0;
104     for (uchar c : input) {
105         if (cnt) {
106             // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
107             if ((c & 0xc0) != 0x80) {
108                 isUtf8 = false;
109                 break;
110             }
111             cnt--;
112             continue;
113         }
114         if ((c & 0x80) == 0x00)
115             continue;  // 7 bit is always ok
116         if ((c & 0xf8) == 0xf0) {
117             cnt = 3;
118             continue;
119         }  // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
120         if ((c & 0xf0) == 0xe0) {
121             cnt = 2;
122             continue;
123         }  // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
124         if ((c & 0xe0) == 0xc0) {
125             cnt = 1;
126             continue;
127         }  // 2-byte char 110xxxxx 10yyyyyy
128         isUtf8 = false;
129         break;  // 8 bit char, but not utf8!
130     }
131     if (isUtf8 && cnt == 0) {
132         QString s = QString::fromUtf8(input);
133         // qDebug() << "Detected utf8:" << s;
134         return s;
135     }
136     // QTextCodec *codec = QTextCodec::codecForName(encoding.toLatin1());
137     if (!codec)
138         return QString::fromLatin1(input);
139     return codec->toUnicode(input);
140 }
141
142 uint editingDistance(const QString& s1, const QString& s2)
143 {
144     uint n = s1.size() + 1;
145     uint m = s2.size() + 1;
146     QVector<QVector<uint>> matrix(n, QVector<uint>(m, 0));
147
148     for (uint i = 0; i < n; i++)
149         matrix[i][0] = i;
150
151     for (uint i = 0; i < m; i++)
152         matrix[0][i] = i;
153
154     uint min;
155     for (uint i = 1; i < n; i++) {
156         for (uint j = 1; j < m; j++) {
157             uint deleteChar = matrix[i - 1][j] + 1;
158             uint insertChar = matrix[i][j - 1] + 1;
159
160             if (deleteChar < insertChar)
161                 min = deleteChar;
162             else
163                 min = insertChar;
164
165             if (s1[i - 1] == s2[j - 1]) {
166                 uint inheritChar = matrix[i - 1][j - 1];
167                 if (inheritChar < min)
168                     min = inheritChar;
169             }
170
171             matrix[i][j] = min;
172         }
173     }
174     return matrix[n - 1][m - 1];
175 }
176
177 QString secondsToString(int timeInSeconds)
178 {
179     static QVector<std::pair<int, QString>> timeUnit{std::make_pair(365 * 24 * 60 * 60,
180                                                                     QCoreApplication::translate("Quassel::secondsToString()", "year")),
181                                                      std::make_pair(24 * 60 * 60,
182                                                                     QCoreApplication::translate("Quassel::secondsToString()", "day")),
183                                                      std::make_pair(60 * 60, QCoreApplication::translate("Quassel::secondsToString()", "h")),
184                                                      std::make_pair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")),
185                                                      std::make_pair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec"))};
186
187     if (timeInSeconds != 0) {
188         QStringList returnString;
189         for (const auto& tu : timeUnit) {
190             int n = timeInSeconds / tu.first;
191             if (n > 0) {
192                 returnString += QString("%1 %2").arg(QString::number(n), tu.second);
193             }
194             timeInSeconds = timeInSeconds % tu.first;
195         }
196         return returnString.join(", ");
197     }
198
199     return QString("%1 %2").arg(QString::number(timeInSeconds), timeUnit.last().second);
200 }
201
202 QByteArray prettyDigest(const QByteArray& digest)
203 {
204     QByteArray hexDigest = digest.toHex().toUpper();
205     QByteArray prettyDigest;
206     prettyDigest.fill(':', hexDigest.count() + (hexDigest.count() / 2) - 1);
207
208     for (int i = 0; i * 2 < hexDigest.count(); i++) {
209         prettyDigest.replace(i * 3, 2, hexDigest.mid(i * 2, 2));
210     }
211     return prettyDigest;
212 }
213
214 QString formatCurrentDateTimeInString(const QString& formatStr)
215 {
216     // Work on a copy of the string to avoid modifying the input string
217     QString formattedStr = QString(formatStr);
218
219     // Exit early if there's nothing to format
220     if (formattedStr.isEmpty())
221         return formattedStr;
222
223     // Find %%<text>%% in string. Replace inside text formatted to QDateTime with the current
224     // timestamp, using %%%% as an escape for multiple %% signs.
225     // For example:
226     // Simple:   "All Quassel clients vanished from the face of the earth... %%hh:mm:ss%%"
227     // > Result:  "All Quassel clients vanished from the face of the earth... 23:20:34"
228     // Complex:  "Away since %%hh:mm%% on %%dd.MM%% - %%%% not here %%%%"
229     // > Result:  "Away since 23:20 on 21.05 - %% not here %%"
230     //
231     // Match groups of double % signs - Some text %%inside here%%, and even %%%%:
232     //   %%(.*)%%
233     //   (...)    marks a capturing group
234     //   .*       matches zero or more characters, not including newlines
235     // Note that '\' must be escaped as '\\'
236     // Helpful interactive website for debugging and explaining:  https://regex101.com/
237     QRegExp regExpMatchTime("%%(.*)%%");
238
239     // Preserve the smallest groups possible to allow for multiple %%blocks%%
240     regExpMatchTime.setMinimal(true);
241
242     // NOTE: Move regExpMatchTime to a static regular expression if used anywhere that performance
243     // matters.
244
245     // Don't allow a runaway regular expression to loop for too long.  This might not happen.. but
246     // when dealing with user input, better to be safe..?
247     int numIterations = 0;
248
249     // Find each group of %%text here%% starting from the beginning
250     int index = regExpMatchTime.indexIn(formattedStr);
251     int matchLength;
252     QString matchedFormat;
253     while (index >= 0 && numIterations < 512) {
254         // Get the total length of the matched expression
255         matchLength = regExpMatchTime.cap(0).length();
256         // Get the format string, e.g. "this text here" from "%%this text here%%"
257         matchedFormat = regExpMatchTime.cap(1);
258         // Check that there's actual characters inside.  A quadruple % (%%%%) represents two %%
259         // signs.
260         if (matchedFormat.length() > 0) {
261             // Format the string according to the current date and time.  Invalid time format
262             // strings are ignored.
263             formattedStr.replace(index, matchLength, QDateTime::currentDateTime().toString(matchedFormat));
264             // Subtract the length of the removed % signs
265             // E.g. "%%h:mm ap%%" turns into "h:mm ap", removing four % signs, thus -4.  This is
266             // used below to determine how far to advance when looking for the next formatting code.
267             matchLength -= 4;
268         }
269         else if (matchLength == 4) {
270             // Remove two of the four percent signs, so '%%%%' escapes to '%%'
271             formattedStr.remove(index, 2);
272             // Subtract the length of the removed % signs, this time removing two % signs, thus -2.
273             matchLength -= 2;
274         }
275         else {
276             // If neither of these match, something went wrong.  Don't modify it to be safe.
277             qDebug() << "Unexpected time format when parsing string, no matchedFormat, matchLength "
278                         "should be 4, actually is"
279                      << matchLength;
280         }
281
282         // Find the next group of %%text here%% starting from where the last group ended
283         index = regExpMatchTime.indexIn(formattedStr, index + matchLength);
284         numIterations++;
285     }
286
287     return formattedStr;
288 }
289
290 QString tryFormatUnixEpoch(const QString& possibleEpochDate, Qt::DateFormat dateFormat, bool useUTC)
291 {
292     // Does the string resemble a Unix epoch?  Parse as 64-bit time
293     qint64 secsSinceEpoch = possibleEpochDate.toLongLong();
294     if (secsSinceEpoch == 0) {
295         // Parsing either failed, or '0' was sent.  No need to distinguish; either way, it's not
296         // useful as epoch.
297         // See https://doc.qt.io/qt-5/qstring.html#toLongLong
298         return possibleEpochDate;
299     }
300
301     // Time checks out, parse it
302     QDateTime date;
303 #if QT_VERSION >= 0x050800
304     date.setSecsSinceEpoch(secsSinceEpoch);
305 #else
306     // toSecsSinceEpoch() was added in Qt 5.8.  Manually downconvert to seconds for now.
307     // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch
308     date.setMSecsSinceEpoch(secsSinceEpoch * 1000);
309 #endif
310
311     // Return the localized date/time
312     if (useUTC) {
313         // Return UTC time
314         if (dateFormat == Qt::DateFormat::ISODate) {
315             // Replace the "T" date/time separator with " " for readability.  This isn't quite the
316             // ISO 8601 spec (it specifies omitting the "T" entirely), but RFC 3339 allows this.
317             // Go with RFC 3339 for human readability that's still machine-parseable, too.
318             //
319             // Before: 2018-06-21T21:35:52Z
320             // After:  2018-06-21 21:35:52Z
321             //         ..........^ (10th character)
322             //
323             // See https://en.wikipedia.org/wiki/ISO_8601#cite_note-32
324             // And https://www.ietf.org/rfc/rfc3339.txt
325             return date.toUTC().toString(dateFormat).replace(10, 1, " ");
326         }
327         else {
328             return date.toUTC().toString(dateFormat);
329         }
330     }
331     else if (dateFormat == Qt::DateFormat::ISODate) {
332         // Add in ISO local timezone information via special handling below
333         // formatDateTimeToOffsetISO() handles converting "T" to " "
334         return formatDateTimeToOffsetISO(date);
335     }
336     else {
337         // Return local time
338         return date.toString(dateFormat);
339     }
340 }
341
342 QString formatDateTimeToOffsetISO(const QDateTime& dateTime)
343 {
344     if (!dateTime.isValid()) {
345         // Don't try to do anything with invalid date/time
346         return "formatDateTimeToISO() invalid date/time";
347     }
348
349     // Replace the "T" date/time separator with " " for readability.  This isn't quite the ISO 8601
350     // spec (it specifies omitting the "T" entirely), but RFC 3339 allows this.  Go with RFC 3339
351     // for human readability that's still machine-parseable, too.
352     //
353     // Before: 2018-08-22T18:43:10-05:00
354     // After:  2018-08-22 18:43:10-05:00
355     //         ..........^ (10th character)
356     //
357     // See https://en.wikipedia.org/wiki/ISO_8601#cite_note-32
358     // And https://www.ietf.org/rfc/rfc3339.txt
359
360 #if 0
361     // The expected way to get a UTC offset on ISO 8601 dates
362     // Remove the "T" date/time separator
363     return dateTime.toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate).replace(10, 1, " ");
364 #else
365     // Work around Qt bug that converts to UTC instead of including timezone information
366     // See https://bugreports.qt.io/browse/QTBUG-26161
367     //
368     // NOTE: Despite the bug report marking as fixed in Qt 5.2.0 (QT_VERSION >= 0x050200), this
369     // still appears broken in Qt 5.5.1.
370     //
371     // Credit to "user362638" for the solution below, modified to fit Quassel's needs
372     // https://stackoverflow.com/questions/18750569/qdatetime-isodate-with-timezone
373
374     // Get the local and UTC time
375     QDateTime local = QDateTime(dateTime);
376     QDateTime utc = local.toUTC();
377     utc.setTimeSpec(Qt::LocalTime);
378
379     // Find the UTC offset
380     int utcOffset = utc.secsTo(local);
381
382     // Force the local time to follow this offset
383     local.setUtcOffset(utcOffset);
384     // Now the output should be correct
385     // Remove the "T" date/time separator
386     return local.toString(Qt::ISODate).replace(10, 1, " ");
387 #endif
388 }