Philipp Hörist pushed to branch omemo_integration at gajim / gajim
Commits:
-
14ae155c
by Philipp Hörist at 2022-10-22T14:40:29+02:00
3 changed files:
Changes:
| ... | ... | @@ -484,10 +484,7 @@ def send_message(self, message: OutgoingMessage) -> None: |
| 484 | 484 | |
| 485 | 485 | if method == 'OMEMO':
|
| 486 | 486 | try:
|
| 487 | - can_send = self.get_module('OMEMO').encrypt_message(message)
|
|
| 488 | - if not can_send:
|
|
| 489 | - return
|
|
| 490 | - |
|
| 487 | + self.get_module('OMEMO').encrypt_message(message)
|
|
| 491 | 488 | except Exception:
|
| 492 | 489 | app.ged.raise_event(
|
| 493 | 490 | MessageNotSent(client=self._client,
|
| ... | ... | @@ -180,8 +180,7 @@ def _get_backend(self) -> OmemoState: |
| 180 | 180 | db_path = data_dir / f'omemo_{self._own_jid}.db'
|
| 181 | 181 | return OmemoState(self._own_jid, db_path, self._account, self)
|
| 182 | 182 | |
| 183 | - def _can_send_message(self, event: OutgoingMessage) -> bool:
|
|
| 184 | - contact = event.contact
|
|
| 183 | + def check_send_preconditions(self, contact: types.ChatContactT) -> bool:
|
|
| 185 | 184 | jid = str(contact.jid)
|
| 186 | 185 | if contact.is_groupchat:
|
| 187 | 186 | if not self.is_omemo_groupchat(jid):
|
| ... | ... | @@ -223,35 +222,37 @@ def _can_send_message(self, event: OutgoingMessage) -> bool: |
| 223 | 222 | message=EncryptionInfoMsg.UNDECIDED_FINGERPRINTS))
|
| 224 | 223 | return False
|
| 225 | 224 | |
| 226 | - self._log.debug('%s => Sending Message to %s',
|
|
| 227 | - contact.account, jid)
|
|
| 225 | + if self._new_fingerprints_available(contact):
|
|
| 226 | + return False
|
|
| 227 | + |
|
| 228 | + self._log.debug('%s => Sending Message to %s',
|
|
| 229 | + contact.account, jid)
|
|
| 228 | 230 | |
| 229 | 231 | return True
|
| 230 | 232 | |
| 231 | - def _new_fingerprints_available(self, event: OutgoingMessage) -> bool:
|
|
| 232 | - contact = event.contact
|
|
| 233 | + def _new_fingerprints_available(self, contact: types.ChatContactT) -> bool:
|
|
| 234 | + fingerprints: list[int] = []
|
|
| 233 | 235 | if contact.is_groupchat:
|
| 234 | 236 | for member_jid in self.backend.get_muc_members(str(contact.jid),
|
| 235 | 237 | without_self=False):
|
| 236 | 238 | fingerprints = self.backend.storage.getNewFingerprints(
|
| 237 | 239 | member_jid)
|
| 238 | 240 | if fingerprints:
|
| 239 | - app.ged.raise_event(EncryptionAnnouncement(
|
|
| 240 | - account=contact.account,
|
|
| 241 | - jid=contact.jid,
|
|
| 242 | - message=EncryptionInfoMsg.UNDECIDED_FINGERPRINTS))
|
|
| 243 | - return True
|
|
| 241 | + break
|
|
| 242 | + |
|
| 244 | 243 | else:
|
| 245 | 244 | fingerprints = self.backend.storage.getNewFingerprints(
|
| 246 | 245 | str(contact.jid))
|
| 247 | - if fingerprints:
|
|
| 248 | - app.ged.raise_event(EncryptionAnnouncement(
|
|
| 249 | - account=contact.account,
|
|
| 250 | - jid=contact.jid,
|
|
| 251 | - message=EncryptionInfoMsg.UNDECIDED_FINGERPRINTS))
|
|
| 252 | - return True
|
|
| 253 | 246 | |
| 254 | - return False
|
|
| 247 | + if not fingerprints:
|
|
| 248 | + return False
|
|
| 249 | + |
|
| 250 | + app.ged.raise_event(EncryptionAnnouncement(
|
|
| 251 | + account=contact.account,
|
|
| 252 | + jid=contact.jid,
|
|
| 253 | + message=EncryptionInfoMsg.UNDECIDED_FINGERPRINTS))
|
|
| 254 | + |
|
| 255 | + return True
|
|
| 255 | 256 | |
| 256 | 257 | def is_omemo_groupchat(self, room_jid: str) -> bool:
|
| 257 | 258 | return room_jid in self._omemo_groupchats
|
| ... | ... | @@ -260,12 +261,6 @@ def encrypt_message(self, event: OutgoingMessage) -> bool: |
| 260 | 261 | if not event.message:
|
| 261 | 262 | return False
|
| 262 | 263 | |
| 263 | - if self._new_fingerprints_available(event):
|
|
| 264 | - return False
|
|
| 265 | - |
|
| 266 | - if not self._can_send_message(event):
|
|
| 267 | - return False
|
|
| 268 | - |
|
| 269 | 264 | omemo_message = self.backend.encrypt(str(event.jid), event.message)
|
| 270 | 265 | if omemo_message is None:
|
| 271 | 266 | raise Exception('Encryption error')
|
| ... | ... | @@ -717,8 +717,14 @@ def _on_send_message(self) -> None: |
| 717 | 717 | contact = self._current_contact
|
| 718 | 718 | assert contact is not None
|
| 719 | 719 | |
| 720 | + client = app.get_client(contact.account)
|
|
| 721 | + |
|
| 720 | 722 | encryption = contact.settings.get('encryption')
|
| 721 | - if encryption and encryption != 'OMEMO':
|
|
| 723 | + if encryption == 'OMEMO':
|
|
| 724 | + if not client.get_module('OMEMO').check_send_preconditions(contact):
|
|
| 725 | + return
|
|
| 726 | + |
|
| 727 | + elif encryption:
|
|
| 722 | 728 | if encryption not in app.plugin_manager.encryption_plugins:
|
| 723 | 729 | ErrorDialog(_('Encryption error'),
|
| 724 | 730 | _('Missing necessary encryption plugin'))
|
| ... | ... | @@ -731,7 +737,6 @@ def _on_send_message(self) -> None: |
| 731 | 737 | if not self._chat_control.sendmessage:
|
| 732 | 738 | return
|
| 733 | 739 | |
| 734 | - client = app.get_client(contact.account)
|
|
| 735 | 740 | |
| 736 | 741 | message = helpers.remove_invalid_xml_chars(message)
|
| 737 | 742 | if message in ('', None, '\n'):
|