2018-11-18 11:00:25 +00:00
|
|
|
"""
|
|
|
|
Slixmpp: The Slick XMPP Library
|
|
|
|
|
2018-11-18 17:04:58 +00:00
|
|
|
Shamelessly inspired from Syndace's python-omemo examples.
|
2018-11-18 11:00:25 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import omemo
|
|
|
|
|
|
|
|
import os
|
|
|
|
import copy
|
|
|
|
import json
|
2018-11-19 10:31:49 +00:00
|
|
|
from typing import Any, Dict, Set, Union
|
2018-11-18 11:00:25 +00:00
|
|
|
|
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
class SyncFileStorage(omemo.Storage):
|
2018-11-19 10:31:49 +00:00
|
|
|
def __init__(self, storage_dir: str) -> None:
|
2018-11-18 11:00:25 +00:00
|
|
|
self.storage_dir = storage_dir
|
|
|
|
self.__state = None
|
2018-11-19 10:31:49 +00:00
|
|
|
self.__own_data = None # type: Union[None, Dict[str, str]]
|
|
|
|
self.__sessions = {} # type: Dict[str, Dict[int, Any]]
|
|
|
|
self.__devices = {} # type: Dict[str, Dict[str, Set[int]]]
|
2018-11-18 11:00:25 +00:00
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
return copy.deepcopy({
|
|
|
|
"state" : self.__state,
|
|
|
|
"sessions" : self.__sessions,
|
|
|
|
"devices" : self.__devices
|
|
|
|
})
|
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
def loadOwnData(self, _callback):
|
2018-11-18 11:00:25 +00:00
|
|
|
if self.__own_data is None:
|
2018-05-14 09:12:50 +00:00
|
|
|
try:
|
2018-11-18 11:00:25 +00:00
|
|
|
filepath = os.path.join(self.storage_dir, 'own_data.json')
|
|
|
|
with open(filepath, 'r') as f:
|
|
|
|
self.__own_data = json.load(f)
|
|
|
|
except OSError:
|
2018-11-19 00:37:07 +00:00
|
|
|
return None
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
return self.__own_data
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def storeOwnData(self, _callback, own_bare_jid: str, own_device_id: int) -> None:
|
2018-11-18 11:00:25 +00:00
|
|
|
self.__own_data = {
|
|
|
|
'own_bare_jid': own_bare_jid,
|
|
|
|
'own_device_id': own_device_id,
|
|
|
|
}
|
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
filepath = os.path.join(self.storage_dir, 'own_data.json')
|
|
|
|
with open(filepath, 'w') as f:
|
|
|
|
json.dump(self.__own_data, f)
|
|
|
|
|
2018-11-18 11:00:25 +00:00
|
|
|
def loadState(self, callback):
|
|
|
|
if self.__state is None:
|
|
|
|
try:
|
|
|
|
filepath = os.path.join(self.storage_dir, 'omemo.json')
|
|
|
|
with open(filepath, 'r') as f:
|
|
|
|
self.__state = json.load(f)
|
|
|
|
except OSError:
|
2018-11-19 00:37:07 +00:00
|
|
|
return None
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
return self.__state
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def storeState(self, _callback, state) -> None:
|
2018-11-18 11:00:25 +00:00
|
|
|
self.__state = state
|
2018-11-19 00:37:07 +00:00
|
|
|
filepath = os.path.join(self.storage_dir, 'omemo.json')
|
|
|
|
with open(filepath, 'w') as f:
|
|
|
|
json.dump(self.__state, f)
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def loadSession(self, _callback, bare_jid: str, device_id: int):
|
2018-11-19 00:37:07 +00:00
|
|
|
return self.__sessions.get(bare_jid, {}).get(device_id, None)
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def storeSession(self, callback, bare_jid: str, device_id: int, session) -> None:
|
2018-11-18 11:00:25 +00:00
|
|
|
self.__sessions[bare_jid] = self.__sessions.get(bare_jid, {})
|
|
|
|
self.__sessions[bare_jid][device_id] = session
|
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def loadActiveDevices(self, _callback, bare_jid: str) -> Union[None, Set[int]]:
|
2018-11-18 11:00:25 +00:00
|
|
|
if self.__devices is None:
|
|
|
|
try:
|
|
|
|
filepath = os.path.join(self.storage_dir, 'devices.json')
|
|
|
|
with open(filepath, 'r') as f:
|
|
|
|
self.__devices = json.load(f)
|
|
|
|
except OSError:
|
2018-11-19 00:37:07 +00:00
|
|
|
return None
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
return self.__devices.get(bare_jid, {}).get("active", [])
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def storeActiveDevices(self, _callback, bare_jid: str, devices: Set[int]) -> None:
|
2018-11-18 11:00:25 +00:00
|
|
|
self.__devices[bare_jid] = self.__devices.get(bare_jid, {})
|
2018-11-18 16:48:16 +00:00
|
|
|
self.__devices[bare_jid]["active"] = list(devices)
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
filepath = os.path.join(self.storage_dir, 'devices.json')
|
|
|
|
with open(filepath, 'w') as f:
|
|
|
|
json.dump(self.__devices, f)
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def loadInactiveDevices(self, _callback, bare_jid: str) -> Union[None, Set[int]]:
|
2018-11-18 16:48:16 +00:00
|
|
|
if self.__devices is None:
|
|
|
|
try:
|
|
|
|
filepath = os.path.join(self.storage_dir, 'devices.json')
|
|
|
|
with open(filepath, 'r') as f:
|
|
|
|
self.__devices = json.load(f)
|
|
|
|
except OSError:
|
2018-11-19 00:37:07 +00:00
|
|
|
return None
|
2018-11-18 16:48:16 +00:00
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
return self.__devices.get(bare_jid, {}).get("inactive", [])
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:49 +00:00
|
|
|
def storeInactiveDevices(self, _callback, bare_jid: str, devices: Set[int]) -> None:
|
2018-11-18 11:00:25 +00:00
|
|
|
self.__devices[bare_jid] = self.__devices.get(bare_jid, {})
|
2018-11-18 16:48:16 +00:00
|
|
|
self.__devices[bare_jid]["inactive"] = list(devices)
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 00:37:07 +00:00
|
|
|
filepath = os.path.join(self.storage_dir, 'devices.json')
|
|
|
|
with open(filepath, 'w') as f:
|
|
|
|
json.dump(self.__devices, f)
|
2018-11-18 11:00:25 +00:00
|
|
|
|
2018-11-19 10:31:05 +00:00
|
|
|
def trust(self, _trusted: str) -> None:
|
|
|
|
"""Set somebody as trusted"""
|
2018-05-14 09:12:50 +00:00
|
|
|
|
2018-11-19 10:31:05 +00:00
|
|
|
def isTrusted(self, callback, bare_jid: str, device: int) -> bool:
|
|
|
|
return True
|
2018-05-14 09:12:50 +00:00
|
|
|
|
2018-11-18 11:00:25 +00:00
|
|
|
@property
|
2018-11-19 10:31:49 +00:00
|
|
|
def is_async(self) -> bool:
|
2018-11-19 00:37:07 +00:00
|
|
|
return False
|