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.structs import OutgoingMessage
from nbxmpp.protocol import Message, JID
from nbxmpp.simplexml import Node
import pathlib
import sys
@ -52,7 +53,7 @@ class OTRChannel(context.Context):
# print some text to chat window
def printl(self,line):
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
def getPolicy(policy): return OTR.DEFAULT_POLICY.get(policy)
@ -138,7 +139,8 @@ class OTR(context.Account):
# decrypt message
def decrypt(self,stanza,properties):
peer = stanza.getFrom().new_as_bare()
sFrom = stanza.getFrom()
peer = sFrom.new_as_bare()
msgtxt = stanza.getBody()
channel, ctl = self.getContext(peer), self.getControl(peer)
try:
@ -147,6 +149,7 @@ 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:
@ -156,6 +159,10 @@ class OTR(context.Account):
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
try:
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.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
callback(event)

View file

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

View file

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

View file

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