pgpplugin.py 6.54 KB
Newer Older
Philipp Hörist's avatar
Philipp Hörist committed
1
# Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
Philipp Hörist's avatar
Philipp Hörist committed
2
#
Philipp Hörist's avatar
Philipp Hörist committed
3
# This file is part of the OpenPGP Gajim Plugin.
Philipp Hörist's avatar
Philipp Hörist committed
4
#
Philipp Hörist's avatar
Philipp Hörist committed
5
# OpenPGP Gajim Plugin is free software; you can redistribute it and/or modify
Philipp Hörist's avatar
Philipp Hörist committed
6
7
8
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
Philipp Hörist's avatar
Philipp Hörist committed
9
# OpenPGP Gajim Plugin is distributed in the hope that it will be useful,
Philipp Hörist's avatar
Philipp Hörist committed
10
11
12
13
14
# 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
Philipp Hörist's avatar
Philipp Hörist committed
15
# along with OpenPGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
Philipp Hörist's avatar
Philipp Hörist committed
16
17
18
19
20
21
22

import logging
import os
from pathlib import Path

from gi.repository import Gtk
from gi.repository import Gdk
Philipp Hörist's avatar
Philipp Hörist committed
23
24
import nbxmpp
from nbxmpp import JID
Philipp Hörist's avatar
Philipp Hörist committed
25
26
27
28
29
30

from gajim.common import app
from gajim.common import ged
from gajim.common import configpaths
from gajim.common import helpers
from gajim.common.const import CSSPriority
Philipp Hörist's avatar
Philipp Hörist committed
31

Philipp Hörist's avatar
Philipp Hörist committed
32
from gajim.gtk.dialogs import ErrorDialog
Philipp Hörist's avatar
Philipp Hörist committed
33
34

from gajim.plugins import GajimPlugin
Andriy Kushnir's avatar
Andriy Kushnir committed
35
from gajim.plugins.plugins_i18n import _
Philipp Hörist's avatar
Philipp Hörist committed
36

37
from openpgp.modules.util import ENCRYPTION_NAME
Philipp Hörist's avatar
Philipp Hörist committed
38
39
40
41
42
43
44
try:
    from openpgp.modules import openpgp
except ImportError as e:
    ERROR_MSG = str(e)
else:
    ERROR_MSG = None

Philipp Hörist's avatar
Philipp Hörist committed
45
log = logging.getLogger('gajim.p.openpgp')
Philipp Hörist's avatar
Philipp Hörist committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59


class OpenPGPPlugin(GajimPlugin):
    def init(self):
        if ERROR_MSG:
            self.activatable = False
            self.available_text = ERROR_MSG
            self.config_dialog = None
            return

        self.events_handlers = {
            'signed-in': (ged.PRECORE, self.signed_in),
            }

Philipp Hörist's avatar
Philipp Hörist committed
60
        self.modules = [openpgp]
Philipp Hörist's avatar
Philipp Hörist committed
61

62
        self.encryption_name = ENCRYPTION_NAME
Philipp Hörist's avatar
Philipp Hörist committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        self.config_dialog = None
        self.gui_extension_points = {
            'encrypt' + self.encryption_name: (self._encrypt_message, None),
            'send_message' + self.encryption_name: (
                self._before_sendmessage, None),
            'encryption_dialog' + self.encryption_name: (
                self.on_encryption_button_clicked, None),
            'encryption_state' + self.encryption_name: (
                self.encryption_state, None),
            'update_caps': (self._update_caps, None),
            }

        self.connections = {}

        self.plugin = self
        self.announced = []
        self.own_key = None
        self.pgp_instances = {}
        self._create_paths()
        self._load_css()

    def _load_css(self):
        path = Path(__file__).parent / 'gtk' / 'style.css'
        try:
87
            with path.open('r') as f:
Philipp Hörist's avatar
Philipp Hörist committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
                css = f.read()
        except Exception as exc:
            log.error('Error loading css: %s', exc)
            return

        try:
            provider = Gtk.CssProvider()
            provider.load_from_data(bytes(css.encode('utf-8')))
            Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
                                                     provider,
                                                     CSSPriority.DEFAULT_THEME)
        except Exception:
            log.exception('Error loading application css')

    def _create_paths(self):
        keyring_path = os.path.join(configpaths.get('MY_DATA'), 'openpgp')
        if not os.path.exists(keyring_path):
            os.makedirs(keyring_path)

    def signed_in(self, event):
        account = event.conn.name
        con = app.connections[account]
        if con.get_module('OpenPGP').secret_key_available:
            log.info('%s => Publish keylist and public key after sign in',
                     account)
Philipp Hörist's avatar
Philipp Hörist committed
113
114
            con.get_module('OpenPGP').request_keylist()
            con.get_module('OpenPGP').set_public_key()
Philipp Hörist's avatar
Philipp Hörist committed
115
116
117
118
119
120
121
122
123
124
125
126

    def activate(self):
        for account in app.connections:
            if app.caps_hash[account] != '':
                # Gajim has already a caps hash calculated, update it
                helpers.update_optional_features(account)

            con = app.connections[account]
            if app.account_is_connected(account):
                if con.get_module('OpenPGP').secret_key_available:
                    log.info('%s => Publish keylist and public key '
                             'after plugin activation', account)
Philipp Hörist's avatar
Philipp Hörist committed
127
128
                    con.get_module('OpenPGP').request_keylist()
                    con.get_module('OpenPGP').set_public_key()
Philipp Hörist's avatar
Philipp Hörist committed
129
130
131
132
133
134

    def deactivate(self):
        pass

    @staticmethod
    def _update_caps(account):
Philipp Hörist's avatar
Philipp Hörist committed
135
136
137
        namespace = nbxmpp.NS_OPENPGP_PK + '+notify'
        if namespace not in app.gajim_optional_features[account]:
            app.gajim_optional_features[account].append(namespace)
Philipp Hörist's avatar
Philipp Hörist committed
138
139
140
141
142
143
144
145
146

    def activate_encryption(self, chat_control):
        account = chat_control.account
        jid = chat_control.contact.jid
        con = app.connections[account]
        if con.get_module('OpenPGP').secret_key_available:
            keys = app.connections[account].get_module('OpenPGP').get_keys(
                jid, only_trusted=False)
            if not keys:
Philipp Hörist's avatar
Philipp Hörist committed
147
                con.get_module('OpenPGP').request_keylist(JID(jid))
Philipp Hörist's avatar
Philipp Hörist committed
148
149
150
151
152
153
                ErrorDialog(
                    _('No OpenPGP key'),
                    _('We didnt receive a OpenPGP key from this contact.'))
                return
            return True

Philipp Hörist's avatar
Philipp Hörist committed
154
155
156
157
158
159
        from openpgp.gtk.wizard import KeyWizard
        KeyWizard(self, account, chat_control)
        return False

    @staticmethod
    def encryption_state(_chat_control, state):
Philipp Hörist's avatar
Philipp Hörist committed
160
161
162
        state['authenticated'] = True
        state['visible'] = True

Philipp Hörist's avatar
Philipp Hörist committed
163
164
    @staticmethod
    def on_encryption_button_clicked(chat_control):
Philipp Hörist's avatar
Philipp Hörist committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        account = chat_control.account
        jid = chat_control.contact.jid
        transient = chat_control.parent_win.window

        from openpgp.gtk.key import KeyDialog
        KeyDialog(account, jid, transient)

    def _before_sendmessage(self, chat_control):
        account = chat_control.account
        jid = chat_control.contact.jid
        con = app.connections[account]

        if not con.get_module('OpenPGP').secret_key_available:
            from openpgp.gtk.wizard import KeyWizard
            KeyWizard(self, account, chat_control)
            return

        keys = con.get_module('OpenPGP').get_keys(jid)
        if not keys:
            ErrorDialog(
                _('Not Trusted'),
                _('There was no trusted and active key found'))
            chat_control.sendmessage = False

Philipp Hörist's avatar
Philipp Hörist committed
189
190
    @staticmethod
    def _encrypt_message(con, obj, callback):
Philipp Hörist's avatar
Philipp Hörist committed
191
192
193
        if not con.get_module('OpenPGP').secret_key_available:
            return
        con.get_module('OpenPGP').encrypt_message(obj, callback)