1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
27 #include <QCoreApplication>
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 */;
38 QString nickFromMask(const QString& mask)
40 return mask.left(mask.indexOf('!'));
43 QString userFromMask(const QString& mask)
45 const int offset = mask.indexOf('!') + 1;
48 const int length = mask.indexOf('@', offset) - offset;
49 return mask.mid(offset, length >= 0 ? length : -1);
52 QString hostFromMask(const QString& mask)
54 const int excl = mask.indexOf('!');
57 const int offset = mask.indexOf('@', excl + 1) + 1;
58 return offset > 0 && offset < mask.size() ? mask.mid(offset) : QString{};
61 bool isChannelName(const QString& str)
65 static constexpr std::array<quint8, 4> prefixes{{'#', '&', '!', '+'}};
66 return std::any_of(prefixes.cbegin(), prefixes.cend(), [&str](quint8 c) { return c == str[0]; });
69 QString stripFormatCodes(QString message)
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);
75 QString stripAcceleratorMarkers(const QString& label_)
77 QString label = label_;
81 p = label.indexOf('&', p);
82 if (p < 0 || p + 1 >= label.length())
85 if (label.at(p + 1).isLetterOrNumber() || label.at(p + 1) == '&')
93 QString decodeString(const QByteArray& input, QTextCodec* codec)
95 if (codec && utf8DetectionBlacklist.contains(codec->mibEnum()))
96 return codec->toUnicode(input);
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...
104 for (uchar c : input) {
106 // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
107 if ((c & 0xc0) != 0x80) {
114 if ((c & 0x80) == 0x00)
115 continue; // 7 bit is always ok
116 if ((c & 0xf8) == 0xf0) {
119 } // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
120 if ((c & 0xf0) == 0xe0) {
123 } // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
124 if ((c & 0xe0) == 0xc0) {
127 } // 2-byte char 110xxxxx 10yyyyyy
129 break; // 8 bit char, but not utf8!
131 if (isUtf8 && cnt == 0) {
132 QString s = QString::fromUtf8(input);
133 // qDebug() << "Detected utf8:" << s;
136 // QTextCodec *codec = QTextCodec::codecForName(encoding.toLatin1());
138 return QString::fromLatin1(input);
139 return codec->toUnicode(input);
142 uint editingDistance(const QString& s1, const QString& s2)
144 uint n = s1.size() + 1;
145 uint m = s2.size() + 1;
146 QVector<QVector<uint>> matrix(n, QVector<uint>(m, 0));
148 for (uint i = 0; i < n; i++)
151 for (uint i = 0; i < m; i++)
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;
160 if (deleteChar < insertChar)
165 if (s1[i - 1] == s2[j - 1]) {
166 uint inheritChar = matrix[i - 1][j - 1];
167 if (inheritChar < min)
174 return matrix[n - 1][m - 1];
177 QString secondsToString(int timeInSeconds)
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"))};
187 if (timeInSeconds != 0) {
188 QStringList returnString;
189 for (const auto& tu : timeUnit) {
190 int n = timeInSeconds / tu.first;
192 returnString += QString("%1 %2").arg(QString::number(n), tu.second);
194 timeInSeconds = timeInSeconds % tu.first;
196 return returnString.join(", ");
199 return QString("%1 %2").arg(QString::number(timeInSeconds), timeUnit.last().second);
202 QByteArray prettyDigest(const QByteArray& digest)
204 QByteArray hexDigest = digest.toHex().toUpper();
205 QByteArray prettyDigest;
206 prettyDigest.fill(':', hexDigest.count() + (hexDigest.count() / 2) - 1);
208 for (int i = 0; i * 2 < hexDigest.count(); i++) {
209 prettyDigest.replace(i * 3, 2, hexDigest.mid(i * 2, 2));
214 QString formatCurrentDateTimeInString(const QString& formatStr)
216 // Work on a copy of the string to avoid modifying the input string
217 QString formattedStr = QString(formatStr);
219 // Exit early if there's nothing to format
220 if (formattedStr.isEmpty())
223 // Find %%<text>%% in string. Replace inside text formatted to QDateTime with the current
224 // timestamp, using %%%% as an escape for multiple %% signs.
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 %%"
231 // Match groups of double % signs - Some text %%inside here%%, and even %%%%:
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("%%(.*)%%");
239 // Preserve the smallest groups possible to allow for multiple %%blocks%%
240 regExpMatchTime.setMinimal(true);
242 // NOTE: Move regExpMatchTime to a static regular expression if used anywhere that performance
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;
249 // Find each group of %%text here%% starting from the beginning
250 int index = regExpMatchTime.indexIn(formattedStr);
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 %%
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.
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.
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"
282 // Find the next group of %%text here%% starting from where the last group ended
283 index = regExpMatchTime.indexIn(formattedStr, index + matchLength);
290 QString tryFormatUnixEpoch(const QString& possibleEpochDate, Qt::DateFormat dateFormat, bool useUTC)
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
297 // See https://doc.qt.io/qt-5/qstring.html#toLongLong
298 return possibleEpochDate;
301 // Time checks out, parse it
303 #if QT_VERSION >= 0x050800
304 date.setSecsSinceEpoch(secsSinceEpoch);
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);
311 // Return the localized date/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.
319 // Before: 2018-06-21T21:35:52Z
320 // After: 2018-06-21 21:35:52Z
321 // ..........^ (10th character)
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, " ");
328 return date.toUTC().toString(dateFormat);
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);
338 return date.toString(dateFormat);
342 QString formatDateTimeToOffsetISO(const QDateTime& dateTime)
344 if (!dateTime.isValid()) {
345 // Don't try to do anything with invalid date/time
346 return "formatDateTimeToISO() invalid date/time";
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.
353 // Before: 2018-08-22T18:43:10-05:00
354 // After: 2018-08-22 18:43:10-05:00
355 // ..........^ (10th character)
357 // See https://en.wikipedia.org/wiki/ISO_8601#cite_note-32
358 // And https://www.ietf.org/rfc/rfc3339.txt
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, " ");
365 // Work around Qt bug that converts to UTC instead of including timezone information
366 // See https://bugreports.qt.io/browse/QTBUG-26161
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.
371 // Credit to "user362638" for the solution below, modified to fit Quassel's needs
372 // https://stackoverflow.com/questions/18750569/qdatetime-isodate-with-timezone
374 // Get the local and UTC time
375 QDateTime local = QDateTime(dateTime);
376 QDateTime utc = local.toUTC();
377 utc.setTimeSpec(Qt::LocalTime);
379 // Find the UTC offset
380 int utcOffset = utc.secsTo(local);
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, " ");