aboutsummaryrefslogtreecommitdiff
path: root/src/mailman_pgp/workflows/mod_approval.py
blob: f610c4c848b551048214a6edccd327af9cd26c6a (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
# Copyright (C) 2017 Jan Jancar
#
# This file is a part of the 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/>.

""""""
import copy
from enum import Enum

from mailman.email.message import UserNotification
from mailman.interfaces.pending import IPendings
from mailman.interfaces.subscriptions import TokenOwner
from public import public
from zope.component import getUtility

from mailman_pgp.pgp.mime import MIMEWrapper
from mailman_pgp.utils.email import overwrite_message

SUBSCRIPTION_MOD_REQUEST = """\
----------
TODO: this is a pgp enabled list.
A user with address {address} requested subscription
His new key is attached to this message.

Fingerprint: {new_fpr}
----------
"""

KEY_CHANGE_MOD_REQUEST = """\
----------
TODO: this is a pgp enabled list.
A subscriber with address {address} requested a change of his key.
His new key is attached to this message.

Old key fingerprint: {old_fpr}
New key fingerprint: {new_fpr}
----------
"""


@public
class WhichApproval(Enum):
    subscription = 1
    key_change = 2


@public
class ModeratorApprovalMixin:
    def __init__(self, approval_type, pre_approved=False):
        self.approved = pre_approved
        self._approval_type = approval_type

    def _step_mod_approval(self):
        if not self.approved:
            self.push('get_approval')

    def _step_get_approval(self):
        self._pend(TokenOwner.moderator)
        self.push('receive_mod_confirmation')
        self.save()

        params = {'mlist': self.mlist.fqdn_listname,
                  'address': self.pgp_address.email}

        if self._approval_type is WhichApproval.subscription:
            name = 'subscription'
            body = SUBSCRIPTION_MOD_REQUEST
            params['old_fpr'] = params['new_fpr'] = self.pubkey.fingerprint
        else:
            name = 'key change'
            body = KEY_CHANGE_MOD_REQUEST
            params['old_fpr'] = self.pgp_address.key_fingerprint
            params['new_fpr'] = self.pubkey.fingerprint

        if self.mlist.admin_immed_notify:
            subject = 'New {} request from {}'.format(name,
                                                      self.pgp_address.email)
            text = body.format(**params)
            msg = UserNotification(
                    self.mlist.owner_address, self.mlist.owner_address,
                    subject, text, self.mlist.preferred_language)
            out = copy.deepcopy(msg)
            wrapped = MIMEWrapper(msg)
            msg = wrapped.attach_keys(self.pubkey)
            overwrite_message(msg, out)
            out.send(self.mlist)
        raise StopIteration

    def _step_receive_mod_confirmation(self):
        pendings = getUtility(IPendings)
        if self.token is not None:
            pendings.confirm(self.token)
            self.token = None
            self.token_owner = TokenOwner.no_one


@public
class ModeratorSubApprovalMixin(ModeratorApprovalMixin):
    def __init__(self, pre_approved=False):
        super().__init__(WhichApproval.subscription, pre_approved)


@public
class ModeratorKeyChangeApprovalMixin(ModeratorApprovalMixin):
    def __init__(self, pre_approved=False):
        super().__init__(WhichApproval.key_change, pre_approved)