Refactor encrypting PGP Message

- Add a _encrypt_message() and _finished_encrypt() method
- Handle Fallback body text in _on_continue_message() where we build the Stanza
This commit is contained in:
Philipp Hörist 2017-04-04 21:28:47 +02:00
parent d344c24a2a
commit 28a3a58ea9
1 changed files with 47 additions and 45 deletions

View File

@ -296,6 +296,12 @@ class CommonConnection:
return return
if obj.keyID and self.USE_GPG: if obj.keyID and self.USE_GPG:
self._encrypt_message(obj, callback)
return
self._on_continue_message(obj, callback)
def _encrypt_message(self, obj, callback):
obj.xhtml = None obj.xhtml = None
if obj.keyID == 'UNKNOWN': if obj.keyID == 'UNKNOWN':
error = _('Neither the remote presence is signed, nor a key was ' error = _('Neither the remote presence is signed, nor a key was '
@ -305,54 +311,50 @@ class CommonConnection:
'in Gajim.' % obj.keyID[:8]) 'in Gajim.' % obj.keyID[:8])
else: else:
myKeyID = gajim.config.get_per('accounts', self.name, 'keyid') myKeyID = gajim.config.get_per('accounts', self.name, 'keyid')
def encrypt_thread(msg, keyID, always_trust=False): key_list = [obj.keyID, myKeyID]
# encrypt message. This function returns (msgenc, error)
return self.gpg.encrypt(msg, [keyID, myKeyID],
always_trust)
def _on_encrypted(output): def _on_encrypted(output):
msgenc, error = output msgenc, error = output
if error.startswith('NOT_TRUSTED'): if error.startswith('NOT_TRUSTED'):
def _on_always_trust(answer): def _on_always_trust(answer):
if answer: if answer:
gajim.thread_interface(encrypt_thread, [obj.message, obj.keyID, gajim.thread_interface(
True], _on_encrypted, []) self.gpg.encrypt, [obj.message, key_list, True],
_on_encrypted, [])
else: else:
self._message_encrypted_cb(output, obj, callback) self._finished_encrypt(
obj, callback, msgenc=msgenc, error=error)
gajim.nec.push_incoming_event(GPGTrustKeyEvent(None, gajim.nec.push_incoming_event(GPGTrustKeyEvent(None,
conn=self, keyID=error.split(' ')[-1], conn=self, keyID=error.split(' ')[-1],
callback=_on_always_trust)) callback=_on_always_trust))
else: else:
self._message_encrypted_cb(output, obj, callback) self._finished_encrypt(
gajim.thread_interface(encrypt_thread, [obj.message, obj.keyID, False], obj, callback, msgenc=msgenc, error=error)
gajim.thread_interface(
self.gpg.encrypt, [obj.message, key_list, False],
_on_encrypted, []) _on_encrypted, [])
return return
self._finished_encrypt(obj, callback, error=error)
self._message_encrypted_cb((None, error), obj, callback) def _finished_encrypt(self, obj, callback, msgenc=None, error=None):
if error:
gajim.nec.push_incoming_event(
MessageNotSentEvent(
None, conn=self, jid=obj.jid, message=obj.message,
error=error, time_=time.time(), session=obj.session))
return return
self._on_continue_message(obj, callback, msgenc)
self._on_continue_message(obj, callback) def _on_continue_message(self, obj, callback, msgenc=None):
if msgenc:
def _message_encrypted_cb(self, output, obj, callback):
msgenc, error = output
if msgenc and not error:
msgtxt = '[This message is *encrypted* (See :XEP:`27`]' msgtxt = '[This message is *encrypted* (See :XEP:`27`]'
lang = os.getenv('LANG') lang = os.getenv('LANG')
if lang is not None and not lang.startswith('en'): if lang is not None and not lang.startswith('en'):
# we're not english: one in locale and one en # we're not english: one in locale and one en
msgtxt = _('[This message is *encrypted* (See :XEP:`27`]') + \ msgtxt = _('[This message is *encrypted* (See :XEP:`27`]') + \
' (' + msgtxt + ')' ' (' + msgtxt + ')'
self._on_continue_message(obj, callback, msgtxt=msgtxt, msgenc=msgenc) else:
return
# Encryption failed, do not send message
tim = time.localtime()
gajim.nec.push_incoming_event(MessageNotSentEvent(None, conn=self,
jid=obj.jid, message=msgtxt, error=error, time_=tim, session=obj.session))
def _on_continue_message(self, obj, callback, msgtxt=None, msgenc=None):
if not msgtxt:
msgtxt = obj.message msgtxt = obj.message
fjid = obj.get_full_jid() fjid = obj.get_full_jid()
if obj.correction_msg: if obj.correction_msg: