def test_xhtml_to_poezio_colors(): start = b'<body xmlns="http://www.w3.org/1999/xhtml"><p>' end = b'</p></body>' xhtml = start + b'test' + end assert xhtml_to_poezio_colors(xhtml) == 'test' xhtml = start + b'<a href="http://perdu.com">salut</a>' + end assert xhtml_to_poezio_colors( xhtml) == '\x19usalut\x19o (http://perdu.com)' xhtml = start + b'<a href="http://perdu.com">http://perdu.com</a>' + end assert xhtml_to_poezio_colors(xhtml) == '\x19uhttp://perdu.com\x19o' xhtml = b'<div style="font-weight:bold">Allo <div style="color:red">test <div style="color: blue">test2</div></div></div>' assert xhtml_to_poezio_colors( xhtml, force=True) == '\x19bAllo \x19196}test \x1921}test2\x19o' xhtml = ( b'<div style="color:blue"><div style="color:yellow">' b'<div style="color:blue">Allo <div style="color:red">' b'test <div style="color: blue">test2</div></div></div></div></div>') assert xhtml_to_poezio_colors( xhtml, force=True) == '\x1921}Allo \x19196}test \x1921}test2\x19o' with pytest.raises(xml.sax._exceptions.SAXParseException): xhtml_to_poezio_colors(b'<p>Invalid xml')
def command_xhtml(self, xhtml_data): message = self.generate_xhtml_message(xhtml_data) if message: if self.remote_supports_receipts: message._add_receipt = True if self.remote_wants_chatstates: message['chat_sate'] = 'active' message.send() body = xhtml.xhtml_to_poezio_colors(xhtml_data, force=True) self._text_buffer.add_message(body, nickname=self.core.own_nick, identifier=message['id'],) self.refresh()
def command_xhtml(self, xhtml_data): message = self.generate_xhtml_message(xhtml_data) if message: if self.remote_supports_receipts: message._add_receipt = True if self.remote_wants_chatstates: message['chat_sate'] = 'active' message.send() body = xhtml.xhtml_to_poezio_colors(xhtml_data, force=True) self._text_buffer.add_message( body, nickname=self.core.own_nick, identifier=message['id'], ) self.refresh()
def generate_xhtml_message(self, arg): if not arg: return try: body = xhtml.clean_text(xhtml.xhtml_to_poezio_colors(arg)) ET.fromstring(arg) except: self.core.information('Could not send custom xhtml', 'Error') log.error('/xhtml: Unable to send custom xhtml', exc_info=True) return msg = self.core.xmpp.make_message(self.get_dest_jid()) msg['body'] = body msg.enable('html') msg['html']['body'] = arg return msg
def generate_xhtml_message(self, arg): if not arg: return try: body = xhtml.clean_text(xhtml.xhtml_to_poezio_colors(arg)) ET.fromstring(arg) except: self.core.information('Could not send custom xhtml', 'Error') log.error('/xhtml: Unable to send custom xhtml', exc_info=True) return msg = self.core.xmpp.make_message(self.get_dest_jid()) msg['body'] = body msg.enable('html') msg['html']['body'] = arg return msg
def encrypted_message_received(self, msg, ctx, tab, txt): """ A properly encrypted message was received, so we add it to the buffer, and try to format it according to the configuration. """ # remove xhtml del msg['html'] del msg['body'] if not txt: return if isinstance(tab, PrivateTab): user = tab.parent_muc.get_user_by_name(msg['from'].resource) nick_color = None else: user = None nick_color = get_theme().COLOR_REMOTE_USER body = txt.decode() decode_entities = self.config.get_by_tabname('decode_entities', msg['from'].bare, 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 Exception: if 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) hl(tab) self.core.refresh_window() del msg['body']
def encrypted_message_received(self, msg, ctx, tab, txt): """ A properly encrypted message was received, so we add it to the buffer, and try to format it according to the configuration. """ # remove xhtml del msg['html'] del msg['body'] if not txt: return if isinstance(tab, PrivateTab): user = tab.parent_muc.get_user_by_name(msg['from'].resource) nick_color = None else: user = None nick_color = get_theme().COLOR_REMOTE_USER body = txt.decode() decode_entities = self.config.get_by_tabname('decode_entities', msg['from'].bare, 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 Exception: if 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) hl(tab) self.core.refresh_window() del msg['body']
def incoming_stanza(self, stanza): """ We are receiving a new stanza, write it in the xml buffer if needed. """ if self.xml_tab: if PYGMENTS: xhtml_text = highlight('%s' % stanza, LEXER, FORMATTER) poezio_colored = xhtml.xhtml_to_poezio_colors(xhtml_text, force=True).rstrip('\x19o').strip() else: poezio_colored = '%s' % stanza self.add_message_to_text_buffer(self.xml_buffer, poezio_colored, nickname=get_theme().CHAR_XML_IN) try: if self.xml_tab.match_stanza(stanza): self.add_message_to_text_buffer(self.xml_tab.filtered_buffer, poezio_colored, nickname=get_theme().CHAR_XML_IN) except: log.debug('', exc_info=True) if isinstance(self.current_tab(), tabs.XMLTab): self.current_tab().refresh() self.doupdate()
def setoilette(self, msg, tab): """ split body, get html toilet result, remove title, convert to poezio style """ msgsplit = xhtml.clean_text(msg['body']).split() # check if ok if len(msgsplit) > 2 and self.nocommand(msgsplit[0]) and self.nocommand(msgsplit[1]) and self.nocommand(' '.join(msgsplit[2:])): xhtml_result = None try: # -f == --font && -E == --export <format> process = subprocess.Popen(['toilet', '-E', 'html', '-f', msgsplit[0], '--filter', msgsplit[1], ' '.join(msgsplit[2:])], stdout=subprocess.PIPE) xhtml_result = process.communicate()[0].decode('utf-8') except: xhtml_result = None if xhtml_result: # remove title titlefind = xhtml_result.find('<title>') if titlefind: xhtml_result = xhtml_result[:titlefind] + xhtml_result[xhtml_result.find('</title>')+8:] # convert to poezio color ## not work like wanted, color not show result = "\n" + xhtml.xhtml_to_poezio_colors(xhtml_result) msg['body'] = result else: msg['body'] = ' '.join(msgsplit[2:])
def test_xhtml_to_poezio_colors(): start = b'<body xmlns="http://www.w3.org/1999/xhtml"><p>' end = b'</p></body>' xhtml = start + b'test' + end assert xhtml_to_poezio_colors(xhtml) == 'test' xhtml = start + b'<a href="http://perdu.com">salut</a>' + end assert xhtml_to_poezio_colors(xhtml) == '\x19usalut\x19o (http://perdu.com)' xhtml = start + b'<a href="http://perdu.com">http://perdu.com</a>' + end assert xhtml_to_poezio_colors(xhtml) == '\x19uhttp://perdu.com\x19o' xhtml = b'<div style="font-weight:bold">Allo <div style="color:red">test <div style="color: blue">test2</div></div></div>' assert xhtml_to_poezio_colors(xhtml, force=True) == '\x19bAllo \x19196}test \x1921}test2\x19o' xhtml = (b'<div style="color:blue"><div style="color:yellow">' b'<div style="color:blue">Allo <div style="color:red">' b'test <div style="color: blue">test2</div></div></div></div></div>') assert xhtml_to_poezio_colors(xhtml, force=True) == '\x1921}Allo \x19196}test \x1921}test2\x19o' with pytest.raises(xml.sax._exceptions.SAXParseException): xhtml_to_poezio_colors(b'<p>Invalid xml')
def on_conversation_msg(self, msg, tab): color_jid = '\x19%s}' % dump_tuple(get_theme().COLOR_MUC_JID) color_info = '\x19%s}' % dump_tuple(get_theme().COLOR_INFORMATION_TEXT) try: ctx = self.get_context(msg['from']) txt, tlvs = ctx.receiveMessage(msg["body"].encode('utf-8')) except UnencryptedMessage as err: # received an unencrypted message inside an OTR session text = _('%(info)sThe following message from %(jid_c)s%(jid)s' '%(info)s was \x19bnot\x19o%(info)s encrypted:' '\x19o\n%(msg)s') % { 'info': color_info, 'jid_c': color_jid, 'jid': msg['from'], 'msg': err.args[0].decode('utf-8')} tab.add_message(text, jid=msg['from'], typ=0) del msg['body'] del msg['html'] hl(tab) self.core.refresh_window() return except ErrorReceived as err: # Received an OTR error text = _('%(info)sReceived the following error from ' '%(jid_c)s%(jid)s%(info)s:\x19o %(err)s') % { 'jid': msg['from'], 'err': err.args[0], 'info': color_info, 'jid_c': color_jid} tab.add_message(text, typ=0) del msg['body'] del msg['html'] hl(tab) self.core.refresh_window() return except NotOTRMessage as err: # ignore non-otr messages # if we expected an OTR message, we would have # got an UnencryptedMesssage # but do an additional check because of a bug with py3k if ctx.state != STATE_PLAINTEXT or ctx.getPolicy('REQUIRE_ENCRYPTION'): text = _('%(info)sThe following message from ' '%(jid_c)s%(jid)s%(info)s was \x19b' 'not\x19o%(info)s encrypted:\x19o\n%(msg)s') % { 'jid': msg['from'], 'msg': err.args[0].decode('utf-8'), 'info': color_info, 'jid_c': color_jid} tab.add_message(text, jid=msg['from'], typ=ctx.log) del msg['body'] del msg['html'] hl(tab) self.core.refresh_window() return return except NotEncryptedError as err: text = _('%(info)sAn encrypted message from %(jid_c)s%(jid)s' '%(info)s was received but is unreadable, as you are' ' not currently communicating privately.') % { 'info': color_info, 'jid_c': color_jid, 'jid': msg['from']} tab.add_message(text, jid=msg['from'], typ=0) hl(tab) del msg['body'] del msg['html'] self.core.refresh_window() return except crypt.InvalidParameterError: tab.add_message('%sThe message from %s%s%s could not be decrypted.' % (color_info, color_jid, msg['from'], color_info), jid=msg['from'], typ=0) hl(tab) del msg['body'] del msg['html'] self.core.refresh_window() return except: tab.add_message('%sAn unspecified error in the OTR plugin occured' % color_info, typ=0) log.error('Unspecified error in the OTR plugin', exc_info=True) return # remove xhtml del msg['html'] del msg['body'] if not txt: return if isinstance(tab, PrivateTab): user = tab.parent_muc.get_user_by_name(msg['from'].resource) nick_color = None else: user = None nick_color = get_theme().COLOR_REMOTE_USER body = txt.decode() decode_entities = self.config.get_by_tabname('decode_entities', msg['from'].bare, 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 Exception: if 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) hl(tab) self.core.refresh_window() del msg['body']
def on_conversation_msg(self, msg, tab): color_jid = '\x19%s}' % dump_tuple(get_theme().COLOR_MUC_JID) color_info = '\x19%s}' % dump_tuple(get_theme().COLOR_INFORMATION_TEXT) try: ctx = self.get_context(msg['from']) txt, tlvs = ctx.receiveMessage(msg["body"].encode('utf-8')) except UnencryptedMessage as err: # received an unencrypted message inside an OTR session text = _('%(info)sThe following message from %(jid_c)s%(jid)s' '%(info)s was \x19bnot\x19o%(info)s encrypted:' '\x19o\n%(msg)s') % { 'info': color_info, 'jid_c': color_jid, 'jid': msg['from'], 'msg': err.args[0].decode('utf-8') } tab.add_message(text, jid=msg['from'], typ=0) del msg['body'] del msg['html'] hl(tab) self.core.refresh_window() return except ErrorReceived as err: # Received an OTR error text = _('%(info)sReceived the following error from ' '%(jid_c)s%(jid)s%(info)s:\x19o %(err)s') % { 'jid': msg['from'], 'err': err.args[0], 'info': color_info, 'jid_c': color_jid } tab.add_message(text, typ=0) del msg['body'] del msg['html'] hl(tab) self.core.refresh_window() return except NotOTRMessage as err: # ignore non-otr messages # if we expected an OTR message, we would have # got an UnencryptedMesssage # but do an additional check because of a bug with py3k if ctx.state != STATE_PLAINTEXT or ctx.getPolicy( 'REQUIRE_ENCRYPTION'): text = _('%(info)sThe following message from ' '%(jid_c)s%(jid)s%(info)s was \x19b' 'not\x19o%(info)s encrypted:\x19o\n%(msg)s') % { 'jid': msg['from'], 'msg': err.args[0].decode('utf-8'), 'info': color_info, 'jid_c': color_jid } tab.add_message(text, jid=msg['from'], typ=ctx.log) del msg['body'] del msg['html'] hl(tab) self.core.refresh_window() return return except NotEncryptedError as err: text = _('%(info)sAn encrypted message from %(jid_c)s%(jid)s' '%(info)s was received but is unreadable, as you are' ' not currently communicating privately.') % { 'info': color_info, 'jid_c': color_jid, 'jid': msg['from'] } tab.add_message(text, jid=msg['from'], typ=0) hl(tab) del msg['body'] del msg['html'] self.core.refresh_window() return except crypt.InvalidParameterError: tab.add_message( '%sThe message from %s%s%s could not be decrypted.' % (color_info, color_jid, msg['from'], color_info), jid=msg['from'], typ=0) hl(tab) del msg['body'] del msg['html'] self.core.refresh_window() return except: tab.add_message( '%sAn unspecified error in the OTR plugin occured' % color_info, typ=0) log.error('Unspecified error in the OTR plugin', exc_info=True) return # remove xhtml del msg['html'] del msg['body'] if not txt: return if isinstance(tab, PrivateTab): user = tab.parent_muc.get_user_by_name(msg['from'].resource) nick_color = None else: user = None nick_color = get_theme().COLOR_REMOTE_USER body = txt.decode() decode_entities = self.config.get_by_tabname('decode_entities', msg['from'].bare, 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 Exception: if 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) hl(tab) self.core.refresh_window() del msg['body']