diff --git a/.gitignore b/.gitignore index 9cf97601a332277f5b5fd81a9efeefcf676b2c2c..acc2ff714e86ea0166e8b1c783e1d6c6e4117897 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ syntax: glob *.pyc *.pyo __pycache__/ -*.*~ \ No newline at end of file +*.*~ +.mypy_cache + diff --git a/omemo/backend/liteaxolotlstore.py b/omemo/backend/liteaxolotlstore.py index 231969e780cc5ef988297cc7770f82580e2a27c3..31f6dfb1b5c34261f392707e937ad627b1869392 100644 --- a/omemo/backend/liteaxolotlstore.py +++ b/omemo/backend/liteaxolotlstore.py @@ -18,6 +18,7 @@ import time import sqlite3 from collections import namedtuple +from typing import List from axolotl.state.axolotlstore import AxolotlStore from axolotl.state.signedprekeyrecord import SignedPreKeyRecord @@ -229,7 +230,7 @@ class LiteAxolotlStore(AxolotlStore): CREATE TABLE IF NOT EXISTS secret ( device_id INTEGER, public_key BLOB, private_key BLOB); INSERT INTO secret (device_id, public_key, private_key) - SELECT registration_id + 1, public_key, private_key + SELECT registration_id + 1, public_key, private_key FROM identities WHERE recipient_id = -1; """ @@ -540,7 +541,7 @@ class LiteAxolotlStore(AxolotlStore): result = self._con.execute(query, (recipientId, public_key)).fetchone() return result.trust if result is not None else None - def getFingerprints(self, jid): + def getFingerprints(self, jid: str) -> list: query = '''SELECT recipient_id as "recipient_id [jid]", public_key as "public_key [pk]", trust, @@ -549,7 +550,7 @@ class LiteAxolotlStore(AxolotlStore): WHERE recipient_id = ? ORDER BY trust ASC''' return self._con.execute(query, (jid,)).fetchall() - def getMucFingerprints(self, jids): + def getMucFingerprints(self, jids: List[str]) -> list: query = ''' SELECT recipient_id as "recipient_id [jid]", public_key as "public_key [pk]", diff --git a/omemo/backend/util.py b/omemo/backend/util.py index f63801c30c761b2a0d98c4bba564aae3108d94b1..edc31482d7e3c18c811fc56c349c5013011dc906 100644 --- a/omemo/backend/util.py +++ b/omemo/backend/util.py @@ -34,6 +34,8 @@ class Trust(IntEnum): UNDECIDED = 2 +# This is not a method of IdentityKeyExtended, because we also want to use it +# for axolotl.identitykey.IdentityKey def get_fingerprint(identity_key, formatted=False): public_key = identity_key.getPublicKey().serialize() fingerprint = binascii.hexlify(public_key).decode()[2:] @@ -52,5 +54,5 @@ class IdentityKeyExtended(IdentityKey): def __hash__(self): return hash(self.publicKey.serialize()) - def get_fingerprint(self, formatted=False): + def get_fingerprint(self, formatted: bool = False) -> str: return get_fingerprint(self, formatted=formatted) diff --git a/omemo/plugin.py b/omemo/plugin.py index 40dc694a69f3a44c2638c217b40f5609687760ef..e37982a4d9a867e41358a533365b01c943311a84 100644 --- a/omemo/plugin.py +++ b/omemo/plugin.py @@ -21,6 +21,7 @@ import binascii import threading from enum import IntEnum, unique from pathlib import Path +from typing import Dict from gi.repository import GLib from gi.repository import Gtk @@ -28,6 +29,7 @@ from gi.repository import Gdk from gajim import dialogs from gajim.common import app, ged +from gajim.common.const import Trust as GajimTrust from gajim.plugins import GajimPlugin from gajim.plugins.plugins_i18n import _ from gajim.groupchat_control import GroupchatControl @@ -36,6 +38,7 @@ from omemo import file_crypto from omemo.gtk.key import KeyDialog from omemo.gtk.config import OMEMOConfigDialog from omemo.backend.aes import aes_encrypt_file +from omemo.backend.util import IdentityKeyExtended, Trust AXOLOTL_MISSING = 'You are missing Python3-Axolotl or use an outdated version' @@ -329,3 +332,21 @@ class OmemoPlugin(GajimPlugin): if msg is None: return chat_control.print_conversation_line(msg, 'status', '', None) + + def get_fingerprints(self, account: str, jid: str, + groupchat: bool = False) -> Dict[str, GajimTrust]: + omemo = self.get_omemo(account) + + if groupchat: + members = list(omemo.backend.get_muc_members(jid)) + fpr_list = omemo.backend.storage.getMucFingerprints(members) + else: + fpr_list = omemo.backend.storage.getFingerprints(jid) + + # every row is like: + # recipient_id: str, public_key: omemo.backend.util.IdentityKeyExtended, trust: int, timestamp: Optional[int] + # FIXME, that code is awful + return { + row.public_key.get_fingerprint(): GajimTrust[Trust(row.trust).name] + for row in fpr_list + }