From 874812298c1e29ff57a9929a91368914b2a0cf44 Mon Sep 17 00:00:00 2001 From: J08nY Date: Fri, 11 Aug 2017 16:08:23 +0200 Subject: Setup some abstractions above PGP enabled mailing lists. --- example_project/settings.py | 3 ++ setup.py | 56 +++++++++++++++++--------------- src/django_pgpmailman/models.py | 64 +++++++++++++++++++++++++++++++++++++ src/django_pgpmailman/plugin.py | 49 ++++++++++++++++++++++++++++ src/django_pgpmailman/views/list.py | 4 ++- 5 files changed, 149 insertions(+), 27 deletions(-) create mode 100644 src/django_pgpmailman/models.py create mode 100644 src/django_pgpmailman/plugin.py diff --git a/example_project/settings.py b/example_project/settings.py index 93140b7..9351a73 100644 --- a/example_project/settings.py +++ b/example_project/settings.py @@ -44,6 +44,9 @@ DEBUG = True ALLOWED_HOSTS = [] +# Set this to the name the mailman_pgp plugin has in your Mailman Core config. +# [plugin.] +MAILMAN_PGP_PLUGIN_NAME = 'pgp' # Application definition diff --git a/setup.py b/setup.py index 608d19d..b4a4462 100644 --- a/setup.py +++ b/setup.py @@ -1,30 +1,34 @@ from setuptools import find_packages, setup setup( - name='django_pgpmailman', - version='0.1', - description='A web interface extension for the GNU Mailman apps Postorius and HyperKitty.', - long_description="""""", - author='Jan Jancar', - author_email='johny@neuromancer.sk', - license='', - classifiers=[ - 'Development Status :: 1 - Planning', - 'Intended Audience :: System Administrators', - 'Operating System :: POSIX', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Topic :: Communications :: Email :: Mailing List Servers', - ], - keywords='email pgp', - packages=find_packages('src'), - package_dir={'': 'src'}, - include_package_data=True, - install_requires=[ - 'Django>=1.8', - 'Django<1.12', - 'django_mailman3', - 'mailmanclient', - ] + name='django_pgpmailman', + version='0.1', + description='A web interface extension for the GNU Mailman apps Postorius and HyperKitty.', + long_description="""""", + author='Jan Jancar', + author_email='johny@neuromancer.sk', + license='', + classifiers=[ + 'Development Status :: 1 - Planning', + 'Intended Audience :: System Administrators', + 'Operating System :: POSIX', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Topic :: Communications :: Email :: Mailing List Servers', + ], + keywords='email pgp', + packages=find_packages('src'), + package_dir={'': 'src'}, + include_package_data=True, + install_requires=[ + 'Django>=1.8', + 'Django<1.12', + 'django_mailman3', + 'mailmanclient', + 'PGPy>=0.4.2' + ], + dependency_links=[ + 'https://github.com/J08nY/PGPy/archive/dev.zip#egg=PGPy-0.4.2' + ] ) diff --git a/src/django_pgpmailman/models.py b/src/django_pgpmailman/models.py new file mode 100644 index 0000000..f396e73 --- /dev/null +++ b/src/django_pgpmailman/models.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2017 Jan Jancar +# +# This file is a part of the Django Mailman PGP plugin. +# +# This program 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, either version 3 of the License, or (at your option) +# any later version. +# +# This program 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 +# this program. If not, see . +from itertools import chain + +from mailmanclient._client import RESTObject, MailingList +from pgpy import PGPKey +from pgpy.errors import PGPError +from six.moves.urllib_error import HTTPError + + +class PGPMailingList(RESTObject): + _writable_properties = ( + 'unsigned_msg_action', 'inline_pgp_action', 'expired_sig_action', + 'revoked_sig_action', 'invalid_sig_action', 'duplicate_sig_action', + 'strip_original_sig', 'sign_outgoing', 'nonencrypted_msg_action', + 'encrypt_outgoing', 'key_change_workflow', 'key_signing_allowed') + _read_only_properties = ('self_link', 'list_id') + _properties = chain(_writable_properties, _read_only_properties) + + @property + def mlist(self): + return MailingList(self._connection, 'lists/{}'.format(self.list_id)) + + @property + def key(self): + try: + response, content = self._connection.call(self._url + '/key') + key, _ = PGPKey.from_blob(response['key']) + return key + except PGPError: + return None + + @key.setter + def key(self, value): + str_key = str(value) if value else '' + try: + self._connection.call(self._url + '/key', data=dict(key=str_key), + method='PUT') + except HTTPError: + pass + + @property + def pubkey(self): + try: + response, content = self._connection.call(self._url + '/pubkey') + key, _ = PGPKey.from_blob(response['key']) + return key + except PGPError: + return None diff --git a/src/django_pgpmailman/plugin.py b/src/django_pgpmailman/plugin.py new file mode 100644 index 0000000..77ed5c8 --- /dev/null +++ b/src/django_pgpmailman/plugin.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2017 Jan Jancar +# +# This file is a part of the Django Mailman PGP plugin. +# +# This program 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, either version 3 of the License, or (at your option) +# any later version. +# +# This program 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 +# this program. If not, see . +from operator import itemgetter + +from django.conf import settings +from django_mailman3.lib.mailman import get_mailman_client +from mailmanclient._client import Plugin + +from django_pgpmailman.models import PGPMailingList + + +class PGPPlugin(Plugin): + def __init__(self, base_plugin): + super(PGPPlugin, self).__init__(base_plugin.connection, + base_plugin.name, + base_plugin.rest_data) + self._base_plugin = base_plugin + + @property + def lists(self): + response, content = self.call('lists') + if 'entries' not in content: + return [] + return [PGPMailingList(self._connection, entry['self_link'], entry) for + entry in sorted(content['entries'], key=itemgetter('list_id'))] + + def get_list(self, list_identifier): + response, content = self.call('lists/{}'.format(list_identifier)) + return PGPMailingList(self._connection, content['self_link', content]) + + +def get_pgp_plugin(): + return PGPPlugin( + get_mailman_client().get_plugin(settings.MAILMAN_PGP_PLUGIN_NAME)) diff --git a/src/django_pgpmailman/views/list.py b/src/django_pgpmailman/views/list.py index 4bdfdff..0c0f367 100644 --- a/src/django_pgpmailman/views/list.py +++ b/src/django_pgpmailman/views/list.py @@ -20,7 +20,9 @@ from __future__ import absolute_import, unicode_literals from django.shortcuts import render +from django_pgpmailman.plugin import get_pgp_plugin + def pgp_list_index(request): return render(request, 'django_pgpmailman/index.html', - {'lists': []}) + {'lists': get_pgp_plugin().lists}) -- cgit v1.2.3-70-g09d2