[openpgp] Refactor Plugin

- Adapt to nbxmpp now supporting openpgp
This commit is contained in:
Philipp Hörist
2019-02-09 20:38:20 +01:00
parent 0189214c41
commit 2b9780a9f9
10 changed files with 484 additions and 769 deletions

View File

@@ -1,20 +1,18 @@
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
# Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of Gajim.
# This file is part of the OpenPGP Gajim Plugin.
#
# Gajim is free software; you can redistribute it and/or modify
# OpenPGP 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.
#
# Gajim is distributed in the hope that it will be useful,
# OpenPGP 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
# XEP-0373: OpenPGP for XMPP
# along with OpenPGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
@@ -22,19 +20,21 @@ from pathlib import Path
from gi.repository import Gtk
from gi.repository import Gdk
import nbxmpp
from nbxmpp import JID
from gajim.plugins import GajimPlugin
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
from gajim.gtk.dialogs import ErrorDialog
from gajim.plugins import GajimPlugin
from gajim.plugins.plugins_i18n import _
from openpgp.modules.util import NS_NOTIFY
from openpgp.modules.util import ENCRYPTION_NAME
from openpgp.modules import pgp_keylist
try:
from openpgp.modules import openpgp
except ImportError as e:
@@ -45,8 +45,6 @@ else:
log = logging.getLogger('gajim.plugin_system.openpgp')
#TODO: we cant encrypt "thread" right now, because its needed for Gajim to find ChatControls.
class OpenPGPPlugin(GajimPlugin):
def init(self):
if ERROR_MSG:
@@ -59,14 +57,12 @@ class OpenPGPPlugin(GajimPlugin):
'signed-in': (ged.PRECORE, self.signed_in),
}
self.modules = [pgp_keylist,
openpgp]
self.modules = [openpgp]
self.encryption_name = ENCRYPTION_NAME
self.config_dialog = None
self.gui_extension_points = {
'encrypt' + self.encryption_name: (self._encrypt_message, None),
'decrypt': (self._decrypt_message, None),
'send_message' + self.encryption_name: (
self._before_sendmessage, None),
'encryption_dialog' + self.encryption_name: (
@@ -114,8 +110,8 @@ class OpenPGPPlugin(GajimPlugin):
if con.get_module('OpenPGP').secret_key_available:
log.info('%s => Publish keylist and public key after sign in',
account)
con.get_module('OpenPGP').query_key_list()
con.get_module('OpenPGP').publish_key()
con.get_module('OpenPGP').request_keylist()
con.get_module('OpenPGP').set_public_key()
def activate(self):
for account in app.connections:
@@ -128,16 +124,17 @@ class OpenPGPPlugin(GajimPlugin):
if con.get_module('OpenPGP').secret_key_available:
log.info('%s => Publish keylist and public key '
'after plugin activation', account)
con.get_module('OpenPGP').query_key_list()
con.get_module('OpenPGP').publish_key()
con.get_module('OpenPGP').request_keylist()
con.get_module('OpenPGP').set_public_key()
def deactivate(self):
pass
@staticmethod
def _update_caps(account):
if NS_NOTIFY not in app.gajim_optional_features[account]:
app.gajim_optional_features[account].append(NS_NOTIFY)
namespace = nbxmpp.NS_OPENPGP_PK + '+notify'
if namespace not in app.gajim_optional_features[account]:
app.gajim_optional_features[account].append(namespace)
def activate_encryption(self, chat_control):
account = chat_control.account
@@ -147,21 +144,24 @@ class OpenPGPPlugin(GajimPlugin):
keys = app.connections[account].get_module('OpenPGP').get_keys(
jid, only_trusted=False)
if not keys:
con.get_module('OpenPGP').query_key_list(jid)
con.get_module('OpenPGP').request_keylist(JID(jid))
ErrorDialog(
_('No OpenPGP key'),
_('We didnt receive a OpenPGP key from this contact.'))
return
return True
else:
from openpgp.gtk.wizard import KeyWizard
KeyWizard(self, account, chat_control)
def encryption_state(self, chat_control, state):
from openpgp.gtk.wizard import KeyWizard
KeyWizard(self, account, chat_control)
return False
@staticmethod
def encryption_state(_chat_control, state):
state['authenticated'] = True
state['visible'] = True
def on_encryption_button_clicked(self, chat_control):
@staticmethod
def on_encryption_button_clicked(chat_control):
account = chat_control.account
jid = chat_control.contact.jid
transient = chat_control.parent_win.window
@@ -186,12 +186,8 @@ class OpenPGPPlugin(GajimPlugin):
_('There was no trusted and active key found'))
chat_control.sendmessage = False
def _encrypt_message(self, con, obj, callback):
@staticmethod
def _encrypt_message(con, obj, callback):
if not con.get_module('OpenPGP').secret_key_available:
return
con.get_module('OpenPGP').encrypt_message(obj, callback)
def _decrypt_message(self, con, obj, callback):
if not con.get_module('OpenPGP').secret_key_available:
return
con.get_module('OpenPGP').decrypt_message(obj, callback)