From 6835a97b15f642829ffe168464aeb1eae8f7b6a9 Mon Sep 17 00:00:00 2001
From: Konstantinos Sideris <sideris.konstantin@gmail.com>
Date: Sun, 24 Dec 2017 13:13:07 +0200
Subject: [PATCH] Use a better regex to identify URLs

---
 include/Config.h             |  9 +++++++++
 include/TopRoomBar.h         |  3 ---
 src/TopRoomBar.cc            |  2 +-
 src/timeline/TimelineItem.cc | 12 ++++--------
 4 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/include/Config.h b/include/Config.h
index 781e910c4..805e7c023 100644
--- a/include/Config.h
+++ b/include/Config.h
@@ -1,5 +1,8 @@
 #pragma once
 
+#include <QRegExp>
+#include <QString>
+
 // Non-theme app configuration. Layouts, fonts spacing etc.
 //
 // Font sizes are in pixels.
@@ -16,6 +19,12 @@ namespace dialogs {
 static constexpr int labelSize = 15;
 }
 
+namespace strings {
+static const QString url_html = "<a href=\"\\1\">\\1</a>";
+static const QRegExp url_regex(
+  "((www\\.(?!\\.)|[a-z][a-z0-9+.-]*://)[^\\s<>'\"]+[^!,\\.\\s<>'\"\\]\\)\\:])");
+}
+
 // Window geometry.
 namespace window {
 static constexpr int height = 600;
diff --git a/include/TopRoomBar.h b/include/TopRoomBar.h
index 471662a40..13eaaa38e 100644
--- a/include/TopRoomBar.h
+++ b/include/TopRoomBar.h
@@ -36,9 +36,6 @@ class Menu;
 class OverlayModal;
 class RoomSettings;
 
-static const QString URL_HTML = "<a href=\"\\1\">\\1</a>";
-static const QRegExp URL_REGEX("((?:https?|ftp)://\\S+)");
-
 class TopRoomBar : public QWidget
 {
         Q_OBJECT
diff --git a/src/TopRoomBar.cc b/src/TopRoomBar.cc
index 9ad300641..381702e01 100644
--- a/src/TopRoomBar.cc
+++ b/src/TopRoomBar.cc
@@ -216,7 +216,7 @@ TopRoomBar::paintEvent(QPaintEvent *event)
                 elidedText =
                   QFontMetrics(topicLabel_->font())
                     .elidedText(roomTopic_, Qt::ElideRight, topicLabel_->width() - perFrameResize);
-        elidedText.replace(URL_REGEX, URL_HTML);
+        elidedText.replace(conf::strings::url_regex, conf::strings::url_html);
         topicLabel_->setText(elidedText);
 }
 
diff --git a/src/timeline/TimelineItem.cc b/src/timeline/TimelineItem.cc
index f986a0a65..3ebbbee0e 100644
--- a/src/timeline/TimelineItem.cc
+++ b/src/timeline/TimelineItem.cc
@@ -16,7 +16,6 @@
  */
 
 #include <QFontDatabase>
-#include <QRegExp>
 #include <QTextEdit>
 
 #include "Avatar.h"
@@ -28,9 +27,6 @@
 #include "timeline/widgets/ImageItem.h"
 #include "timeline/widgets/VideoItem.h"
 
-static const QRegExp URL_REGEX("((?:https?|ftp)://\\S+)");
-static const QString URL_HTML = "<a href=\"\\1\">\\1</a>";
-
 void
 TimelineItem::init()
 {
@@ -84,7 +80,7 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
         }
 
         body = body.toHtmlEscaped();
-        body.replace(URL_REGEX, URL_HTML);
+        body.replace(conf::strings::url_regex, conf::strings::url_html);
         body.replace("\n", "<br/>");
         generateTimestamp(timestamp);
 
@@ -206,7 +202,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
 
         generateTimestamp(timestamp);
 
-        body.replace(URL_REGEX, URL_HTML);
+        body.replace(conf::strings::url_regex, conf::strings::url_html);
         body.replace("\n", "<br/>");
         body = "<i>" + body + "</i>";
 
@@ -252,7 +248,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
 
         generateTimestamp(timestamp);
         emoteMsg = emoteMsg.toHtmlEscaped();
-        emoteMsg.replace(URL_REGEX, URL_HTML);
+        emoteMsg.replace(conf::strings::url_regex, conf::strings::url_html);
         emoteMsg.replace("\n", "<br/>");
 
         if (with_sender) {
@@ -295,7 +291,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
         generateTimestamp(timestamp);
 
         body = body.toHtmlEscaped();
-        body.replace(URL_REGEX, URL_HTML);
+        body.replace(conf::strings::url_regex, conf::strings::url_html);
         body.replace("\n", "<br/>");
 
         if (with_sender) {
-- 
GitLab