From babc22f82dce7f7bb2ea110d80c4f4fa776a8184 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Sat, 28 Feb 2015 15:47:51 +0100
Subject: [PATCH] punyencode only the host part of the URL. Fixes #7981

---
 src/common/helpers.py        | 9 +++++++++
 src/conversation_textview.py | 4 ++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/common/helpers.py b/src/common/helpers.py
index 72951c4016..9c46cc9641 100644
--- a/src/common/helpers.py
+++ b/src/common/helpers.py
@@ -36,6 +36,7 @@ import os
 import subprocess
 import urllib
 import urllib2
+from urlparse import urlparse
 import webbrowser
 import errno
 import select
@@ -136,6 +137,14 @@ def ascii_to_idn(host):
         converted_labels.append(idna.ToUnicode(label))
     return ".".join(converted_labels)
 
+def puny_encode_url(url):
+    _url = url
+    if '//' not in _url:
+        _url = '//' + _url
+    o = urlparse(_url)
+    p_loc = idn_to_ascii(o.netloc)
+    return url.replace(o.netloc, p_loc)
+
 def parse_resource(resource):
     """
     Perform stringprep on resource and return it
diff --git a/src/conversation_textview.py b/src/conversation_textview.py
index 683c047c57..5caef7977e 100644
--- a/src/conversation_textview.py
+++ b/src/conversation_textview.py
@@ -1203,8 +1203,8 @@ class ConversationTextview(gobject.GObject):
             all_tags = [(ttt.lookup(t) if isinstance(t, str) else t) for t in all_tags]
             buffer_.insert_with_tags(end_iter, special_text, *all_tags)
             if 'url' in tags:
-                puny_text = puny_encode(special_text)
-                if not puny_text.endswith('-'):
+                puny_text = helpers.puny_encode_url(special_text)
+                if puny_text != special_text:
                     puny_tags = []
                     if use_other_tags:
                         puny_tags += other_tags
-- 
GitLab