From 9e92cbda0b06e56ab060316cdd4505b8c2d20614 Mon Sep 17 00:00:00 2001 From: Kjell Braden Date: Sat, 13 Oct 2012 20:24:13 +0200 Subject: [PATCH] gotr: enable html messages --- gotr/otrmodule.py | 50 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/gotr/otrmodule.py b/gotr/otrmodule.py index e9fb77d..4d6d35a 100644 --- a/gotr/otrmodule.py +++ b/gotr/otrmodule.py @@ -54,11 +54,12 @@ ended_tip = 'The private chat session to this contact has ended' inactive_tip = 'Communication to this contact is currently ' \ 'unencrypted' +import cgi +import logging import os import pickle import time import sys -import logging import common.xmpp from common import gajim @@ -69,6 +70,9 @@ from message_control import TYPE_CHAT, MessageControl from plugins.helpers import log_calls, log from plugins.plugin import GajimPluginException +from HTMLParser import HTMLParser +from htmlentitydefs import name2codepoint + import ui sys.path.insert(0, os.path.dirname(ui.__file__)) @@ -570,14 +574,11 @@ class OtrPlugin(GajimPlugin): if ctx is not None: ctx.smpWindow.handle_tlv(tlvs) - event.msgtxt = unicode(msgtxt or '') + stripper = HTMLStripper() + stripper.feed(unicode(msgtxt or '')) + event.msgtxt = stripper.stripped_data event.stanza.setBody(event.msgtxt) - - # every message that went through OTR (ie. was OTR-related) gets - # stripped from html. I don't like html. - html_node = event.stanza.getTag('html') - if html_node: - event.stanza.delChild(html_node) + event.stanza.setXHTML(msgtxt) return PASS @@ -596,9 +597,11 @@ class OtrPlugin(GajimPlugin): if event.resource: fjid += '/' + event.resource + message = event.xhtml or cgi.escape(event.message) + try: newmsg = self.us[event.account].getContext(fjid).sendMessage( - potr.context.FRAGMENT_SEND_ALL_BUT_LAST, event.message, + potr.context.FRAGMENT_SEND_ALL_BUT_LAST, message, appdata={'session':event.session}) except potr.context.NotEncryptedError, e: if e.args[0] == potr.context.EXC_FINISHED: @@ -608,9 +611,36 @@ class OtrPlugin(GajimPlugin): return IGNORE else: raise e - event.message = newmsg + + if event.xhtml: # if we had html before, replace with new content + event.xhtml = newmsg + + stripper = HTMLStripper() + stripper.feed(unicode(newmsg or '')) + event.message = stripper.stripped_data return PASS + +class HTMLStripper(HTMLParser): + def reset(self): + self.stripped_data = '' + HTMLParser.reset(self) + + def handle_data(self, data): + self.stripped_data += data + def handle_entityref(self, name): + c = unichr(name2codepoint[name]) + self.stripped_data += c + def handle_charref(self, name): + if name.startswith('x'): + c = unichr(int(name[1:], 16)) + else: + c = unichr(int(name)) + self.stripped_data += c + def unknown_decl(self, data): + if data.startswith('CDATA['): + self.data += data[6:] + ## TODO: ## - disconnect ctxs on disconnect