|
|
|
@ -20,11 +20,10 @@ import string
|
|
|
|
|
import random
|
|
|
|
|
import itertools
|
|
|
|
|
import logging
|
|
|
|
|
from inspect import signature
|
|
|
|
|
from gajim.common import const, app, helpers, configpaths
|
|
|
|
|
from gajim.common.const import EncryptionData
|
|
|
|
|
from gajim.common.structs import OutgoingMessage
|
|
|
|
|
from nbxmpp.protocol import Message, JID
|
|
|
|
|
from nbxmpp.simplexml import Node
|
|
|
|
|
|
|
|
|
|
import pathlib
|
|
|
|
|
import sys
|
|
|
|
@ -52,8 +51,8 @@ class OTRChannel(context.Context):
|
|
|
|
|
|
|
|
|
|
# print some text to chat window
|
|
|
|
|
def printl(self,line):
|
|
|
|
|
control = app.window.get_control()
|
|
|
|
|
control and control.add_info_message("OTR: "+line)
|
|
|
|
|
println = self.user.getControl(self.peer) and self.user.getControl(self.peer).conv_textview.print_conversation_line
|
|
|
|
|
println and println("OTR: "+line,kind='status',name='',tim='',**('jid' in signature(println).parameters and {'jid':None} or {}))
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def getPolicy(policy): return OTR.DEFAULT_POLICY.get(policy)
|
|
|
|
@ -99,7 +98,8 @@ class OTR(context.Account):
|
|
|
|
|
|
|
|
|
|
# get chat control
|
|
|
|
|
def getControl(self,peer):
|
|
|
|
|
return app.window.get_control()
|
|
|
|
|
ctl = app.interface.msg_win_mgr.get_control(peer.getStripped(),self.account)
|
|
|
|
|
return ctl
|
|
|
|
|
|
|
|
|
|
# get OTR context (encrypted dialog between Alice and Bob)
|
|
|
|
|
def getContext(self,peer):
|
|
|
|
@ -107,18 +107,6 @@ class OTR(context.Account):
|
|
|
|
|
self.ctxs[peer] = self.ctxs.get(peer) or OTRChannel(self,peer)
|
|
|
|
|
return self.ctxs[peer]
|
|
|
|
|
|
|
|
|
|
# factory for Gajim 1.4+
|
|
|
|
|
def makeOutgoingMessage(self,message,control,peer):
|
|
|
|
|
contact = control.client.get_module('Contacts').get_contact(peer, groupchat=False)
|
|
|
|
|
chatstate = control.client.get_module('Chatstate').get_active_chatstate(contact)
|
|
|
|
|
return OutgoingMessage(account=self.account,
|
|
|
|
|
contact=contact,
|
|
|
|
|
message=message,
|
|
|
|
|
type_='chat',
|
|
|
|
|
chatstate=chatstate,
|
|
|
|
|
label=None,
|
|
|
|
|
correct_id=None)
|
|
|
|
|
|
|
|
|
|
# load my private key
|
|
|
|
|
def loadPrivkey(self):
|
|
|
|
|
my = self.keystore.load(jid=str(self.jid))
|
|
|
|
@ -139,8 +127,7 @@ class OTR(context.Account):
|
|
|
|
|
|
|
|
|
|
# decrypt message
|
|
|
|
|
def decrypt(self,stanza,properties):
|
|
|
|
|
sFrom = stanza.getFrom()
|
|
|
|
|
peer = sFrom.new_as_bare()
|
|
|
|
|
peer = stanza.getFrom().new_as_bare()
|
|
|
|
|
msgtxt = stanza.getBody()
|
|
|
|
|
channel, ctl = self.getContext(peer), self.getControl(peer)
|
|
|
|
|
try:
|
|
|
|
@ -149,21 +136,16 @@ class OTR(context.Account):
|
|
|
|
|
self.log.error("** got exception while decrypting message: %s" % e)
|
|
|
|
|
channel.printl(OTR.STATUS[e].format(msg=msgtxt,err=e.args[0].error))
|
|
|
|
|
else:
|
|
|
|
|
channel.resource = sFrom.resource
|
|
|
|
|
stanza.setBody(text and text.decode() or "")
|
|
|
|
|
properties.encrypted = EncryptionData({'name': OTR.ENCRYPTION_NAME})
|
|
|
|
|
finally:
|
|
|
|
|
if channel.mayRetransmit and channel.state and ctl: channel.mayRetransmit = ctl.client.send_message(self.makeOutgoingMessage(channel.lastMessage.decode(), ctl, peer))
|
|
|
|
|
if channel.mayRetransmit and channel.state and ctl: channel.mayRetransmit = ctl.send_message(channel.lastMessage.decode())
|
|
|
|
|
|
|
|
|
|
# encrypt message
|
|
|
|
|
def encrypt(self,event,callback):
|
|
|
|
|
peer = event.msg_iq.getTo().new_as_bare()
|
|
|
|
|
channel, ctl = self.getContext(peer), self.getControl(peer)
|
|
|
|
|
if not hasattr(channel, 'resource'):
|
|
|
|
|
channel.resource = ""
|
|
|
|
|
if channel.resource:
|
|
|
|
|
peer = peer.new_with(resource=channel.resource)
|
|
|
|
|
if event.xhtml: return ctl.client.send_message(self.makeOutgoingMessage(event.message, ctl, peer)) # skip xhtml messages
|
|
|
|
|
channel, ctl = self.getContext(peer), event.control
|
|
|
|
|
if event.xhtml: return ctl.send_message(event.message) # skip xhtml messages
|
|
|
|
|
try:
|
|
|
|
|
encrypted = channel.sendMessage(context.FRAGMENT_SEND_ALL_BUT_LAST,event.message.encode(),appdata=event.msg_iq.getThread()) or b''
|
|
|
|
|
message = (encrypted != self.getDefaultQueryMessage(OTR.DEFAULT_POLICY.get)) and event.message or ""
|
|
|
|
@ -174,7 +156,4 @@ class OTR(context.Account):
|
|
|
|
|
event.msg_iq.setBody(encrypted.decode()) # encrypted data goes here
|
|
|
|
|
event.message = message # message that will be displayed in our chat goes here
|
|
|
|
|
event.encrypted, event.additional_data["encrypted"] = OTR.ENCRYPTION_NAME, {"name":OTR.ENCRYPTION_NAME} # some mandatory encryption flags
|
|
|
|
|
if channel.resource:
|
|
|
|
|
event.stanza.addChild('no-copy', namespace='urn:xmpp:hints') # don't send carbons
|
|
|
|
|
event.stanza.addChild('no-store', namespace='urn:xmpp:hints') # don't store in MAM
|
|
|
|
|
callback(event)
|
|
|
|
|