pip pycryptodome compatibility, Gajim ≤1.8, eliminate carbons

This commit is contained in:
Bohdan Horbeshko 2023-09-20 16:19:34 +03:00
parent 7c6d87080c
commit d6b7575c4c
4 changed files with 43 additions and 20 deletions

13
otr.py
View file

@ -24,6 +24,7 @@ from gajim.common import const, app, helpers, configpaths
from gajim.common.const import EncryptionData from gajim.common.const import EncryptionData
from gajim.common.structs import OutgoingMessage from gajim.common.structs import OutgoingMessage
from nbxmpp.protocol import Message, JID from nbxmpp.protocol import Message, JID
from nbxmpp.simplexml import Node
import pathlib import pathlib
import sys import sys
@ -52,7 +53,7 @@ class OTRChannel(context.Context):
# print some text to chat window # print some text to chat window
def printl(self,line): def printl(self,line):
control = app.window.get_control() control = app.window.get_control()
control and control.add_message("OTR: "+line,kind='status',tim=0.0) control and control.add_info_message("OTR: "+line)
@staticmethod @staticmethod
def getPolicy(policy): return OTR.DEFAULT_POLICY.get(policy) def getPolicy(policy): return OTR.DEFAULT_POLICY.get(policy)
@ -138,7 +139,8 @@ class OTR(context.Account):
# decrypt message # decrypt message
def decrypt(self,stanza,properties): def decrypt(self,stanza,properties):
peer = stanza.getFrom().new_as_bare() sFrom = stanza.getFrom()
peer = sFrom.new_as_bare()
msgtxt = stanza.getBody() msgtxt = stanza.getBody()
channel, ctl = self.getContext(peer), self.getControl(peer) channel, ctl = self.getContext(peer), self.getControl(peer)
try: try:
@ -147,6 +149,7 @@ class OTR(context.Account):
self.log.error("** got exception while decrypting message: %s" % e) self.log.error("** got exception while decrypting message: %s" % e)
channel.printl(OTR.STATUS[e].format(msg=msgtxt,err=e.args[0].error)) channel.printl(OTR.STATUS[e].format(msg=msgtxt,err=e.args[0].error))
else: else:
channel.resource = sFrom.resource
stanza.setBody(text and text.decode() or "") stanza.setBody(text and text.decode() or "")
properties.encrypted = EncryptionData({'name': OTR.ENCRYPTION_NAME}) properties.encrypted = EncryptionData({'name': OTR.ENCRYPTION_NAME})
finally: finally:
@ -156,6 +159,10 @@ class OTR(context.Account):
def encrypt(self,event,callback): def encrypt(self,event,callback):
peer = event.msg_iq.getTo().new_as_bare() peer = event.msg_iq.getTo().new_as_bare()
channel, ctl = self.getContext(peer), self.getControl(peer) 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 if event.xhtml: return ctl.client.send_message(self.makeOutgoingMessage(event.message, ctl, peer)) # skip xhtml messages
try: try:
encrypted = channel.sendMessage(context.FRAGMENT_SEND_ALL_BUT_LAST,event.message.encode(),appdata=event.msg_iq.getThread()) or b'' encrypted = channel.sendMessage(context.FRAGMENT_SEND_ALL_BUT_LAST,event.message.encode(),appdata=event.msg_iq.getThread()) or b''
@ -167,4 +174,6 @@ class OTR(context.Account):
event.msg_iq.setBody(encrypted.decode()) # encrypted data goes here event.msg_iq.setBody(encrypted.decode()) # encrypted data goes here
event.message = message # message that will be displayed in our chat 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 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
callback(event) callback(event)

View file

@ -14,8 +14,8 @@
"win32" "win32"
], ],
"requirements": [ "requirements": [
"gajim>=1.5,<1.6" "gajim>=1.5,<1.9"
], ],
"short_name": "otrplugin", "short_name": "otrplugin",
"version": "0.4" "version": "0.5"
} }

View file

@ -29,6 +29,8 @@ log = logging.getLogger('gajim.p.otr')
try: from Cryptodome import * try: from Cryptodome import *
except ImportError as e: except ImportError as e:
try: from Crypto import *
except ImportError as e:
log.error(e) log.error(e)
ERROR = 'Cryptodome is missing' ERROR = 'Cryptodome is missing'
@ -54,7 +56,7 @@ class OTRPlugin(GajimPlugin):
self.modules = [module] self.modules = [module]
self.gui_extension_points = { self.gui_extension_points = {
'encrypt' + self.encryption_name: (self._encrypt_message, None), 'encrypt' + self.encryption_name: (self._encrypt_message, None),
'message_actions_box': (self._message_actions_box_activate, self._message_actions_box_deactivate), # 'message_actions_box': (self._message_actions_box_activate, self._message_actions_box_deactivate),
} }
self._menuitem = None self._menuitem = None
@ -67,13 +69,15 @@ class OTRPlugin(GajimPlugin):
return grid._ui.encryption_menu_button.get_menu_model() return grid._ui.encryption_menu_button.get_menu_model()
def activate(self): def activate(self):
if app.window is not None: pass
grid = self._get_grid() # if app.window is not None:
self._actions_hack_activate(grid) # grid = self._get_grid()
# self._actions_hack_activate(grid)
def deactivate(self): def deactivate(self):
grid = self._get_grid() pass
self._actions_hack_deactivate(grid) # grid = self._get_grid()
# self._actions_hack_deactivate(grid)
@staticmethod @staticmethod
def get_otr(account): def get_otr(account):

View file

@ -17,14 +17,24 @@
# along with this library. If not, see <http://www.gnu.org/licenses/>. # along with this library. If not, see <http://www.gnu.org/licenses/>.
from Cryptodome import Cipher try:
from Cryptodome.Hash import HMAC as _HMAC from Cryptodome import Cipher
from Cryptodome.Hash import SHA256 as _SHA256 from Cryptodome.Hash import HMAC as _HMAC
from Cryptodome.Hash import SHA as _SHA1 from Cryptodome.Hash import SHA256 as _SHA256
from Cryptodome.PublicKey import DSA from Cryptodome.Hash import SHA as _SHA1
from Cryptodome.Random import random from Cryptodome.PublicKey import DSA
from Cryptodome.Signature import DSS from Cryptodome.Random import random
from Cryptodome.Util import Counter from Cryptodome.Signature import DSS
from Cryptodome.Util import Counter
except ImportError as e:
from Crypto import Cipher
from Crypto.Hash import HMAC as _HMAC
from Crypto.Hash import SHA256 as _SHA256
from Crypto.Hash import SHA as _SHA1
from Crypto.PublicKey import DSA
from Crypto.Random import random
from Crypto.Signature import DSS
from Crypto.Util import Counter
from numbers import Number from numbers import Number