1 /***************************************************************************
2 * Copyright (C) 2005-2018 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>()
37 << 39 /* ISO-2022-JP */;
39 QString nickFromMask(const QString &mask)
41 return mask.left(mask.indexOf('!'));
45 QString userFromMask(const QString &mask)
47 const int offset = mask.indexOf('!') + 1;
50 const int length = mask.indexOf('@', offset) - offset;
51 return mask.mid(offset, length >= 0 ? length : -1);
55 QString hostFromMask(const QString &mask)
57 const int excl = mask.indexOf('!');
60 const int offset = mask.indexOf('@', excl + 1) + 1;
61 return offset > 0 && offset < mask.size() ? mask.mid(offset) : QString{};
65 bool isChannelName(const QString &str)
69 static constexpr std::array<quint8, 4> prefixes{{'#', '&', '!', '+'}};
70 return std::any_of(prefixes.cbegin(), prefixes.cend(), [&str](quint8 c) { return c == str[0]; });
74 QString stripFormatCodes(QString message)
76 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]"};
77 return message.remove(regEx);
81 QString stripAcceleratorMarkers(const QString &label_)
83 QString label = label_;
86 p = label.indexOf('&', p);
87 if (p < 0 || p + 1 >= label.length())
90 if (label.at(p + 1).isLetterOrNumber() || label.at(p + 1) == '&')
99 QString decodeString(const QByteArray &input, QTextCodec *codec)
101 if (codec && utf8DetectionBlacklist.contains(codec->mibEnum()))
102 return codec->toUnicode(input);
104 // First, we check if it's utf8. It is very improbable to encounter a string that looks like
105 // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
106 // is safe to assume that it is.
107 // Q_ASSERT(sizeof(const char) == sizeof(quint8)); // In God we trust...
110 for (int i = 0; i < input.size(); i++) {
112 // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
113 if ((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
117 if ((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
118 if ((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; } // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
119 if ((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; } // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
120 if ((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; } // 2-byte char 110xxxxx 10yyyyyy
121 isUtf8 = false; break; // 8 bit char, but not utf8!
123 if (isUtf8 && cnt == 0) {
124 QString s = QString::fromUtf8(input);
125 //qDebug() << "Detected utf8:" << s;
128 //QTextCodec *codec = QTextCodec::codecForName(encoding.toLatin1());
129 if (!codec) return QString::fromLatin1(input);
130 return codec->toUnicode(input);
134 uint editingDistance(const QString &s1, const QString &s2)
136 uint n = s1.size()+1;
137 uint m = s2.size()+1;
138 QVector<QVector<uint> > matrix(n, QVector<uint>(m, 0));
140 for (uint i = 0; i < n; i++)
143 for (uint i = 0; i < m; i++)
147 for (uint i = 1; i < n; i++) {
148 for (uint j = 1; j < m; j++) {
149 uint deleteChar = matrix[i-1][j] + 1;
150 uint insertChar = matrix[i][j-1] + 1;
152 if (deleteChar < insertChar)
157 if (s1[i-1] == s2[j-1]) {
158 uint inheritChar = matrix[i-1][j-1];
159 if (inheritChar < min)
166 return matrix[n-1][m-1];
170 QString secondsToString(int timeInSeconds)
172 static QVector<std::pair<int, QString>> timeUnit {
173 std::make_pair(365*24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "year")),
174 std::make_pair(24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "day")),
175 std::make_pair(60*60, QCoreApplication::translate("Quassel::secondsToString()", "h")),
176 std::make_pair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")),
177 std::make_pair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec"))
180 if (timeInSeconds != 0) {
181 QStringList returnString;
182 for (int i = 0; i < timeUnit.size(); i++) {
183 int n = timeInSeconds / timeUnit[i].first;
185 returnString += QString("%1 %2").arg(QString::number(n), timeUnit[i].second);
187 timeInSeconds = timeInSeconds % timeUnit[i].first;
189 return returnString.join(", ");
192 return QString("%1 %2").arg(QString::number(timeInSeconds), timeUnit.last().second);
197 QByteArray prettyDigest(const QByteArray &digest)
199 QByteArray hexDigest = digest.toHex().toUpper();
200 QByteArray prettyDigest;
201 prettyDigest.fill(':', hexDigest.count() + (hexDigest.count() / 2) - 1);
203 for (int i = 0; i * 2 < hexDigest.count(); i++) {
204 prettyDigest.replace(i * 3, 2, hexDigest.mid(i * 2, 2));
210 QString formatCurrentDateTimeInString(const QString &formatStr)
212 // Work on a copy of the string to avoid modifying the input string
213 QString formattedStr = QString(formatStr);
215 // Exit early if there's nothing to format
216 if (formattedStr.isEmpty())
219 // Find %%<text>%% in string. Replace inside text formatted to QDateTime with the current
220 // timestamp, using %%%% as an escape for multiple %% signs.
222 // Simple: "All Quassel clients vanished from the face of the earth... %%hh:mm:ss%%"
223 // > Result: "All Quassel clients vanished from the face of the earth... 23:20:34"
224 // Complex: "Away since %%hh:mm%% on %%dd.MM%% - %%%% not here %%%%"
225 // > Result: "Away since 23:20 on 21.05 - %% not here %%"
227 // Match groups of double % signs - Some text %%inside here%%, and even %%%%:
229 // (...) marks a capturing group
230 // .* matches zero or more characters, not including newlines
231 // Note that '\' must be escaped as '\\'
232 // Helpful interactive website for debugging and explaining: https://regex101.com/
233 QRegExp regExpMatchTime("%%(.*)%%");
235 // Preserve the smallest groups possible to allow for multiple %%blocks%%
236 regExpMatchTime.setMinimal(true);
238 // NOTE: Move regExpMatchTime to a static regular expression if used anywhere that performance
241 // Don't allow a runaway regular expression to loop for too long. This might not happen.. but
242 // when dealing with user input, better to be safe..?
243 int numIterations = 0;
245 // Find each group of %%text here%% starting from the beginning
246 int index = regExpMatchTime.indexIn(formattedStr);
248 QString matchedFormat;
249 while (index >= 0 && numIterations < 512) {
250 // Get the total length of the matched expression
251 matchLength = regExpMatchTime.cap(0).length();
252 // Get the format string, e.g. "this text here" from "%%this text here%%"
253 matchedFormat = regExpMatchTime.cap(1);
254 // Check that there's actual characters inside. A quadruple % (%%%%) represents two %%
256 if (matchedFormat.length() > 0) {
257 // Format the string according to the current date and time. Invalid time format
258 // strings are ignored.
259 formattedStr.replace(index, matchLength,
260 QDateTime::currentDateTime().toString(matchedFormat));
261 // Subtract the length of the removed % signs
262 // E.g. "%%h:mm ap%%" turns into "h:mm ap", removing four % signs, thus -4. This is
263 // used below to determine how far to advance when looking for the next formatting code.
265 } else if (matchLength == 4) {
266 // Remove two of the four percent signs, so '%%%%' escapes to '%%'
267 formattedStr.remove(index, 2);
268 // Subtract the length of the removed % signs, this time removing two % signs, thus -2.
271 // If neither of these match, something went wrong. Don't modify it to be safe.
272 qDebug() << "Unexpected time format when parsing string, no matchedFormat, matchLength "
273 "should be 4, actually is" << matchLength;
276 // Find the next group of %%text here%% starting from where the last group ended
277 index = regExpMatchTime.indexIn(formattedStr, index + matchLength);
285 bool scopeMatch(const QString &string, const QString &scopeRule, const bool &isRegEx,
286 const bool &isCaseSensitive)
288 // When isRegEx is false:
289 // A match happens when the string does NOT match ANY inverted rules and matches AT LEAST one
290 // normal rule, unless no normal rules exist (implicit wildcard match). This gives inverted
291 // rules higher priority regardless of ordering.
293 // When isRegEx is true:
294 // A match happens when the normal regular expression matches. If prefixed with '!', the match
295 // happens UNLESS the following regular expression matches.
297 // TODO: After switching to Qt 5, use of this should be split into two parts, one part that
298 // would generate compiled QRegularExpressions for match/inverted match, regenerating it on any
299 // rule changes, and another part that would check each message against these compiled rules.
301 // Cache case sensitivity
302 Qt::CaseSensitivity ruleExactCase = (isCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
305 // Regular expression tests
307 // Check if this is an inverted rule (starts with '!')
308 if (scopeRule.startsWith("!")) {
309 // Take the reminder of the string
310 QRegExp ruleRx(scopeRule.mid(1), ruleExactCase);
311 // Matching an inverted rule: matched (true) implies rule failure (false)
312 return !ruleRx.exactMatch(string);
314 QRegExp ruleRx(scopeRule, ruleExactCase);
315 // Matching a normal rule: matched (true) implies rule success (true)
316 return ruleRx.exactMatch(string);
319 // Wildcard expression tests
321 // Keep track if any matches are found
322 bool matches = false;
323 // Keep track if normal rules and inverted rules are found, allowing for implicit wildcard
324 bool normalRuleFound = false, invertedRuleFound = false;
326 // Split each scope rule by separator, ignoring empty parts
327 foreach(QString rule, scopeRule.split(";", QString::SkipEmptyParts)) {
328 // Trim whitespace from the start/end of the rule
329 rule = rule.trimmed();
330 // Ignore empty rules
334 // Check if this is an inverted rule (starts with '!')
335 if (rule.startsWith("!")) {
336 // Inverted rule found
337 invertedRuleFound = true;
339 // Take the reminder of the string
340 QRegExp ruleRx(rule.mid(1), ruleExactCase);
341 ruleRx.setPatternSyntax(QRegExp::Wildcard);
342 if (ruleRx.exactMatch(string)) {
343 // Matches an inverted rule, full rule cannot match
348 normalRuleFound = true;
350 QRegExp ruleRx(rule, ruleExactCase);
351 ruleRx.setPatternSyntax(QRegExp::Wildcard);
352 if (ruleRx.exactMatch(string)) {
353 // Matches a normal rule, full rule might match
355 // Continue checking in case other inverted rules negate this
359 // No inverted rules matched, okay to match normally
361 // ...we found a normal match
362 // ...implicit wildcard: we had inverted rules (that didn't match) and no normal rules
363 return matches || (invertedRuleFound && !normalRuleFound);
368 QString tryFormatUnixEpoch(const QString &possibleEpochDate)
370 // Does the string resemble a Unix epoch? Parse as 64-bit time
371 qint64 secsSinceEpoch = possibleEpochDate.toLongLong();
372 if (secsSinceEpoch == 0) {
373 // Parsing either failed, or '0' was sent. No need to distinguish; either way, it's not
375 // See https://doc.qt.io/qt-5/qstring.html#toLongLong
376 return possibleEpochDate;
379 // Time checks out, parse it
381 #if QT_VERSION >= 0x050800
382 date.setSecsSinceEpoch(secsSinceEpoch);
384 // toSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
385 // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch
386 date.setMSecsSinceEpoch(secsSinceEpoch * 1000);
389 // Return the localized date/time
390 return date.toString();