Welcome to mirror list, hosted at ThFree Co, Russian Federation.

store.py « backend « pgp - dev.gajim.org/gajim/gajim-plugins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dea85684ac37b9e18939dbd94d1763d5b2015306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of the PGP Gajim Plugin.
#
# PGP Gajim Plugin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# PGP Gajim Plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.

import json
from pathlib import Path

from gajim.common import app
from gajim.common import configpaths
from gajim.common.helpers import delay_execution


class KeyStore:
    def __init__(self, account, own_jid, log):
        self._log = log
        self._account = account
        self._store = {
            'own_key_data': None,
            'contact_key_data': {},
        }

        own_bare_jid = own_jid.getBare()
        path = Path(configpaths.get('PLUGINS_DATA')) / 'pgplegacy' / own_bare_jid
        if not path.exists():
            path.mkdir(parents=True)

        self._store_path = path / 'store'
        if self._store_path.exists():
            with self._store_path.open('r') as file:
                try:
                    self._store = json.load(file)
                except Exception:
                    log.exception('Could not load config')

        if not self._store['contact_key_data']:
            self._migrate()

    def _migrate(self):
        keys = {}
        attached_keys = app.config.get_per(
            'accounts', self._account, 'attached_gpg_keys')
        if not attached_keys:
            return
        attached_keys = attached_keys.split()

        for i in range(len(attached_keys) // 2):
            keys[attached_keys[2 * i]] = attached_keys[2 * i + 1]

        for jid, key_id in keys.items():
            self.set_contact_key_data(jid, (key_id, ''))

        own_key_id = app.config.get_per('accounts', self._account, 'keyid')
        own_key_user = app.config.get_per('accounts', self._account, 'keyname')
        if own_key_id:
            self.set_own_key_data((own_key_id, own_key_user))

        attached_keys = app.config.set_per(
            'accounts', self._account, 'attached_gpg_keys', '')
        self._log.info('Migration successful')

    @delay_execution(500)
    def _save_store(self):
        with self._store_path.open('w') as file:
            json.dump(self._store, file)

    def _get_dict_key(self, jid):
        return '%s-%s' % (self._account, jid)

    def set_own_key_data(self, key_data):
        if key_data is None:
            self._store['own_key_data'] = None
        else:
            self._store['own_key_data'] = {
                'key_id': key_data[0],
                'key_user': key_data[1]
            }
        self._save_store()

    def get_own_key_data(self):
        return self._store['own_key_data']

    def get_contact_key_data(self, jid):
        key_ids = self._store['contact_key_data']
        dict_key = self._get_dict_key(jid)
        return key_ids.get(dict_key)

    def set_contact_key_data(self, jid, key_data):
        key_ids = self._store['contact_key_data']
        dict_key = self._get_dict_key(jid)
        if key_data is None:
            self._store['contact_key_data'][dict_key] = None
        else:
            key_ids[dict_key] = {
                'key_id': key_data[0],
                'key_user': key_data[1]
            }
        self._save_store()