From ef3beab02b0ac10e5b535bb4cea949d44a413fd9 Mon Sep 17 00:00:00 2001
From: mathieui <mathieui@mathieui.net>
Date: Tue, 16 Dec 2014 20:46:45 +0100
Subject: Add a decode_newlines option to the OTR plugin
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

I wish I didn’t have to do such ugly things to get a close-to-decent
interop with legacy OTR plugins that insert awful HTML4 stuff inside
messages that do not need it.
---
 plugins/otr.py | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/plugins/otr.py b/plugins/otr.py
index a632a165..7488ed46 100644
--- a/plugins/otr.py
+++ b/plugins/otr.py
@@ -129,12 +129,20 @@ Configuration
         Decode embedded XHTML.
 
     decode_entities
-        **Default:** ``false``
+        **Default:** ``true``
 
         Decode XML and HTML entities (like ``&amp;``) even when the
         document isn't valid (if it is valid, it will be decoded even
         without this option).
 
+    decode_newlines
+        **Default:** ``true``
+
+        Decode ``<br/>`` and ``<br>`` tags even when the document
+        isn't valid (if it is valid, it will be decoded even
+        without this option for ``<br/>``, and ``<br>`` will make
+        the document invalid anyway).
+
     keys_dir
         **Default:** ``$XDG_DATA_HOME/poezio/otr``
 
@@ -554,15 +562,23 @@ class Plugin(BasePlugin):
         body = txt.decode()
         decode_entities = self.config.get_by_tabname('decode_entities',
                                                      msg['from'].bare,
-                                                     default=False)
+                                                     default=True)
+        decode_newlines = self.config.get_by_tabname('decode_newlines',
+                                                     msg['from'].bare,
+                                                     default=True)
         if self.config.get_by_tabname('decode_xhtml', msg['from'].bare, default=True):
             try:
                 body = xhtml.xhtml_to_poezio_colors(body, force=True)
-            except:
+            except Exception:
                 if decode_entities:
                     body = html.unescape(body)
-        elif decode_entities:
-            body = html.unescape(body)
+                if decode_newlines:
+                    body = body.replace('<br/>', '\n').replace('<br>', '\n')
+        else:
+            if decode_entities:
+                body = html.unescape(body)
+            if decode_newlines:
+                body = body.replace('<br/>', '\n').replace('<br>', '\n')
         tab.add_message(body, nickname=tab.nick, jid=msg['from'],
                         forced_user=user, typ=ctx.log,
                         nick_color=nick_color)
-- 
cgit v1.2.3