gotr: enable html messages

This commit is contained in:
Kjell Braden
2012-10-13 20:24:13 +02:00
parent 09898b73ab
commit 9e92cbda0b

View File

@@ -54,11 +54,12 @@ ended_tip = 'The private chat session to this contact has <i>ended</i>'
inactive_tip = 'Communication to this contact is currently ' \ inactive_tip = 'Communication to this contact is currently ' \
'<i>unencrypted</i>' '<i>unencrypted</i>'
import cgi
import logging
import os import os
import pickle import pickle
import time import time
import sys import sys
import logging
import common.xmpp import common.xmpp
from common import gajim from common import gajim
@@ -69,6 +70,9 @@ from message_control import TYPE_CHAT, MessageControl
from plugins.helpers import log_calls, log from plugins.helpers import log_calls, log
from plugins.plugin import GajimPluginException from plugins.plugin import GajimPluginException
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import ui import ui
sys.path.insert(0, os.path.dirname(ui.__file__)) sys.path.insert(0, os.path.dirname(ui.__file__))
@@ -570,14 +574,11 @@ class OtrPlugin(GajimPlugin):
if ctx is not None: if ctx is not None:
ctx.smpWindow.handle_tlv(tlvs) 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) event.stanza.setBody(event.msgtxt)
event.stanza.setXHTML(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)
return PASS return PASS
@@ -596,9 +597,11 @@ class OtrPlugin(GajimPlugin):
if event.resource: if event.resource:
fjid += '/' + event.resource fjid += '/' + event.resource
message = event.xhtml or cgi.escape(event.message)
try: try:
newmsg = self.us[event.account].getContext(fjid).sendMessage( 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}) appdata={'session':event.session})
except potr.context.NotEncryptedError, e: except potr.context.NotEncryptedError, e:
if e.args[0] == potr.context.EXC_FINISHED: if e.args[0] == potr.context.EXC_FINISHED:
@@ -608,9 +611,36 @@ class OtrPlugin(GajimPlugin):
return IGNORE return IGNORE
else: else:
raise e 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 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: ## TODO:
## - disconnect ctxs on disconnect ## - disconnect ctxs on disconnect