aboutsummaryrefslogtreecommitdiff
path: root/src/django_pgpmailman/forms.py
blob: 308327463806b758194e88ffa92a83140d802e57 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- 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 <http://www.gnu.org/licenses/>.
from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import validate_email

from django.utils.translation import ugettext_lazy as _
from pgpy import PGPKey
from pgpy.errors import PGPError


class NullBooleanRadioSelect(forms.RadioSelect):
    def value_from_datadict(self, data, files, name):
        value = data.get(name, None)
        return {'2': True,
                True: True,
                'True': True,
                '3': False,
                'False': False,
                False: False}.get(value, None)


boolean_choices = ((True, _('Yes')), (False, _('No')))
action_choices = (
    ('hold', _('Hold')),
    ('reject', _('Reject')),
    ('discard', _('Discard')),
    ('accept', _('Accept')),
    ('defer', _('Defer'))
)


class ListSignatureSettingsForm(forms.Form):
    sign_outgoing = forms.NullBooleanField(
            widget=NullBooleanRadioSelect(choices=boolean_choices),
            required=False,
            label=_('Sign outgoing messages'),
            help_text=_(
                    'Whether to sign the outgoing postings of a mailing list '
                    'by the list key.')
    )
    strip_original_sig = forms.NullBooleanField(
            widget=NullBooleanRadioSelect(choices=boolean_choices),
            required=False,
            label=_('Strip sender signature'),
            help_text=_('Whether to strip the original signature of a message'
                        '(if any).')
    )
    unsigned_msg_action = forms.ChoiceField(
            widget=forms.Select(),
            choices=action_choices,
            required=False,
            label=_('Unsigned message action'),
            help_text=_(
                    'An action to take on an unsigned message. `Defer` lets '
                    'the message pass through to the next check.')
    )
    inline_pgp_action = forms.ChoiceField(
            widget=forms.Select(),
            choices=action_choices,
            required=False,
            label=_('Inline PGP action'),
            help_text=_(
                    'An action to take on a message that is signed using '
                    'inline PGP. `Defer` lets the message pass through to the '
                    'next check.')
    )
    expired_sig_action = forms.ChoiceField(
            widget=forms.Select(),
            choices=action_choices,
            required=False,
            label=_('Expired signature action'),
            help_text=_('An action to take on a message that has an expired '
                        'signature. `Defer` lets the message pass through to '
                        'the next check.')
    )
    revoked_sig_action = forms.ChoiceField(
            widget=forms.Select(),
            choices=action_choices,
            required=False,
            label=_('Revoked signature action'),
            help_text=_('An action to take on a message that has a revoked '
                        'signature. `Defer` lets the message pass through to '
                        'the next check.')
    )
    invalid_sig_action = forms.ChoiceField(
            widget=forms.Select(),
            choices=action_choices,
            required=False,
            label=_('Invalid signature action'),
            help_text=_('An action to take on a message with an invalid '
                        'signature. `Defer` lets the message pass through to '
                        'the next check.')
    )
    duplicate_sig_action = forms.ChoiceField(
            widget=forms.Select(),
            choices=action_choices,
            required=False,
            label=_('Duplicate signature action'),
            help_text=_('An action to take on a message which contains a '
                        'signature that was already posted to a PGP enabled '
                        'mailing list before. `Defer` lets the message '
                        'pass through to the rest of the chain.')
    )


class ListEncryptionSettingsForm(forms.Form):
    encrypt_outgoing = forms.NullBooleanField(
            widget=NullBooleanRadioSelect(choices=boolean_choices),
            required=False,
            label=_('Encrypt outgoing messages'),
            help_text=_(
                    'Whether to encrypt the outgoing postings of a mailing '
                    'list to the subscribers keys.')
    )
    nonencrypted_msg_action = forms.ChoiceField(
            widget=forms.Select(),
            choices=action_choices,
            required=False,
            label=_('Nonencrypted message action'),
            help_text=_('An action to take on a nonencrypted message, is '
                        'done before executing the signature checks. `Defer` '
                        'lets the message pass through to signature checks.')
    )


key_change_choices = (('pgp-key-change-workflow', _('Default')),
                      ('pgp-key-change-mod-workflow',
                       _('Default, then moderate')))
member_role_choices = (('member', _('Member')),
                       ('owner', _('Owner')),
                       ('moderator', _('Moderator')),
                       ('nonmember', _('Nonmember')))


class ListMiscSettingsForm(forms.Form):
    key_change_workflow = forms.ChoiceField(
            widget=forms.Select(),
            choices=key_change_choices,
            required=False,
            label=_('Key change workflow'),
            help_text=_('A workflow to use for the key change operation. The '
                        'default workflow does the key confirmation the same '
                        'way the subscription process does. With possible '
                        'additional moderation.')
    )
    key_signing_allowed = forms.MultipleChoiceField(
            widget=forms.SelectMultiple(),
            choices=member_role_choices,
            required=False,
            label=_('Key signing allowed'),
            help_text=_('A set of member roles that are allowed to sign the '
                        'lists public key via the `key sign` command.')
    )


class KeyFileField(forms.FileField):
    def to_python(self, data):
        try:
            return PGPKey.from_blob(data.read())[0]
        except PGPError as e:
            raise ValidationError(str(e), code='invalid')


class ListKeyManagementForm(forms.Form):
    key = KeyFileField(
            widget=forms.ClearableFileInput(),
            required=False,
            label=_('Upload a new private key'),
            help_text=_('Useful for uploading a complately different list key '
                        'than the one generated by mailman-pgp, when setting '
                        'up a new mailing list. The uploaded key must be a '
                        'private PGP key, not expired and must be usable for '
                        'encryption and signatures.')
    )
    pubkey = KeyFileField(
            widget=forms.ClearableFileInput(),
            required=False,
            label=_('Upload a public key'),
            help_text=_('New signatures from the uploaded key are merged with '
                        'the current list key, provided the uploaded key '
                        'has the same key material and contains the UID that '
                        'was signed.')
    )


class ListCreateForm(forms.Form):
    listname = forms.CharField(
            label=_('List Name'),
            required=True,
            error_messages={
                'required': _('Please enter a name for your list.'),
                'invalid': _('Please enter a valid list name.')})
    mail_host = forms.ChoiceField()
    list_owner = forms.EmailField(
            label=_('Inital list owner address'),
            error_messages={
                'required': _(
                    'Please enter the list owner\'s email address.')},
            required=True)
    advertised = forms.ChoiceField(
            widget=forms.RadioSelect(),
            label=_('Advertise this list?'),
            error_messages={
                'required': _('Please choose a list type.')},
            required=True,
            choices=(
                (True, _('Advertise this list in list index')),
                (False, _('Hide this list in list index'))))
    description = forms.CharField(
            label=_('Description'),
            required=False)
    list_style = forms.ChoiceField()

    def __init__(self, domain_choices=None, style_choices=None, *args, **kwargs):
        super(ListCreateForm, self).__init__(*args, **kwargs)
        self.fields['mail_host'] = forms.ChoiceField(
                widget=forms.Select(),
                label=_('Mail Host'),
                required=True,
                choices=domain_choices,
                error_messages={'required': _('Choose an existing Domain.'),
                                'invalid': _('Invalid mail host')})
        self.fields['list_style'] = forms.ChoiceField(
                widget=forms.Select(),
                label=_('List style'),
                required=True,
                choices=style_choices
        )
        if len(domain_choices) < 2:
            self.fields['mail_host'].help_text = _(
                    'Site admin has not created any domains')

    def clean_listname(self):
        try:
            validate_email(self.cleaned_data['listname'] + '@example.net')
        except:
            raise forms.ValidationError(_('Please enter a valid listname'))
        return self.cleaned_data['listname']