Gajim 1.9 support

This commit is contained in:
Bohdan Horbeshko 2024-06-14 06:38:21 +03:00
parent 12659472b0
commit 7f72c4bc60
3 changed files with 23 additions and 20 deletions

36
otr.py
View file

@ -21,10 +21,11 @@ import random
import itertools
import logging
from gajim.common import const, app, helpers, configpaths
from gajim.common.const import EncryptionData
from gajim.common.const import Trust
from gajim.common.structs import OutgoingMessage
from nbxmpp.protocol import Message, JID
from nbxmpp.simplexml import Node
from nbxmpp.structs import EncryptionData
import pathlib
import sys
@ -110,14 +111,9 @@ class OTR(context.Account):
# 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)
text=message)
# load my private key
def loadPrivkey(self):
@ -152,30 +148,36 @@ class OTR(context.Account):
else:
channel.resource = sFrom.resource
stanza.setBody(text and text.decode() or "")
properties.encrypted = EncryptionData({'name': OTR.ENCRYPTION_NAME})
properties.encrypted = EncryptionData(protocol=OTR.ENCRYPTION_NAME, key='Unknown', trust=Trust.UNDECIDED)
finally:
if channel.mayRetransmit and channel.state and ctl: channel.mayRetransmit = ctl.client.send_message(self.makeOutgoingMessage(channel.lastMessage.decode(), ctl, peer))
# encrypt message
def encrypt(self,event,callback):
peer = event.msg_iq.getTo().new_as_bare()
peer = event.contact.jid
channel, ctl = self.getContext(peer), self.getControl(peer)
stanza = event.get_stanza()
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''
message = (encrypted != self.getDefaultQueryMessage(OTR.DEFAULT_POLICY.get)) and event.message or ""
encrypted = channel.sendMessage(context.FRAGMENT_SEND_ALL_BUT_LAST,event.get_text().encode(),appdata=stanza.getThread()) or b''
message = (encrypted != self.getDefaultQueryMessage(OTR.DEFAULT_POLICY.get)) and event.get_text() or ""
except context.NotEncryptedError as e:
self.log.error("** got exception while encrypting message: %s" % e)
channel.printl(peer,OTR.STATUS[e.__class__])
else:
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
stanza.setBody(encrypted.decode()) # encrypted data goes here
event._text = message # message that will be displayed in our chat goes here
event.set_encryption(
EncryptionData(
protocol=OTR.ENCRYPTION_NAME,
key='Unknown',
trust=Trust.UNDECIDED,
)
) # 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
stanza.addChild('no-copy', namespace='urn:xmpp:hints') # don't send carbons
stanza.addChild('no-store', namespace='urn:xmpp:hints') # don't store in MAM
callback(event)

View file

@ -14,8 +14,8 @@
"win32"
],
"requirements": [
"gajim>=1.6,<1.9"
"gajim>=1.9,<1.10"
],
"short_name": "otrplugin",
"version": "0.5.3"
"version": "0.6"
}

View file

@ -22,6 +22,7 @@ ERROR = None
import logging
from gajim.plugins import GajimPlugin
from gajim.common import app
from gajim.common.storage.archive.const import MessageType
from gajim.gtk.util import make_menu_item
from gi.repository import Gtk, Gio
@ -93,7 +94,7 @@ class OTRPlugin(GajimPlugin):
# encrypt message #
def _encrypt_message(self,con,event,callback):
if not event.message or event.type_ != 'chat': return # drop empty and non-chat messages
if not event.has_text() or event.type != MessageType.CHAT: return # drop empty and non-chat messages
otr = self.get_otr(event.account)
otr.otr.encrypt(event,callback)