~sakuag333/mailman/lmtp-duplicate-id

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
# Copyright (C) 2010-2012 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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.
#
# GNU Mailman 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
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Mailing list configuration via REST API."""

from __future__ import absolute_import, print_function, unicode_literals

__metaclass__ = type
__all__ = [
    'ListConfiguration',
    ]


from lazr.config import as_boolean, as_timedelta
from restish import http, resource

from mailman.config import config
from mailman.core.errors import (
    ReadOnlyPATCHRequestError, UnknownPATCHRequestError)
from mailman.interfaces.action import Action
from mailman.interfaces.archiver import ArchivePolicy
from mailman.interfaces.autorespond import ResponseAction
from mailman.interfaces.mailinglist import IAcceptableAliasSet, ReplyToMunging
from mailman.rest.helpers import GetterSetter, PATCH, etag, no_content
from mailman.rest.validator import PatchValidator, Validator, enum_validator



class AcceptableAliases(GetterSetter):
    """Resource for the acceptable aliases of a mailing list."""

    def get(self, mlist, attribute):
        """Return the mailing list's acceptable aliases."""
        assert attribute == 'acceptable_aliases', (
            'Unexpected attribute: {0}'.format(attribute))
        aliases = IAcceptableAliasSet(mlist)
        return sorted(aliases.aliases)

    def put(self, mlist, attribute, value):
        """Change the acceptable aliases.

        Because this is a PUT operation, all previous aliases are cleared
        first.  Thus, this is an overwrite.  The keys in the request are
        ignored.
        """
        assert attribute == 'acceptable_aliases', (
            'Unexpected attribute: {0}'.format(attribute))
        alias_set = IAcceptableAliasSet(mlist)
        alias_set.clear()
        for alias in value:
            alias_set.add(unicode(alias))



# Additional validators for converting from web request strings to internal
# data types.  See below for details.

def pipeline_validator(pipeline_name):
    """Convert the pipeline name to a string, but only if it's known."""
    if pipeline_name in config.pipelines:
        return unicode(pipeline_name)
    raise ValueError('Unknown pipeline: {0}'.format(pipeline_name))


def list_of_unicode(values):
    """Turn a list of things into a list of unicodes."""
    return [unicode(value) for value in values]



# This is the list of IMailingList attributes that are exposed through the
# REST API.  The values of the keys are the GetterSetter instance holding the
# decoder used to convert the web request string to an internally valid value.
# The instance also contains the get() and put() methods used to retrieve and
# set the attribute values.  Its .decoder attribute will be None for read-only
# attributes.
#
# The decoder must either return the internal value or raise a ValueError if
# the conversion failed (e.g. trying to turn 'Nope' into a boolean).
#
# Many internal value types can be automatically JSON encoded, but see
# mailman.rest.helpers.ExtendedEncoder for specializations of certain types
# (e.g. datetimes, timedeltas, enums).

ATTRIBUTES = dict(
    acceptable_aliases=AcceptableAliases(list_of_unicode),
    admin_immed_notify=GetterSetter(as_boolean),
    admin_notify_mchanges=GetterSetter(as_boolean),
    administrivia=GetterSetter(as_boolean),
    advertised=GetterSetter(as_boolean),
    anonymous_list=GetterSetter(as_boolean),
    autorespond_owner=GetterSetter(enum_validator(ResponseAction)),
    autorespond_postings=GetterSetter(enum_validator(ResponseAction)),
    autorespond_requests=GetterSetter(enum_validator(ResponseAction)),
    autoresponse_grace_period=GetterSetter(as_timedelta),
    autoresponse_owner_text=GetterSetter(unicode),
    autoresponse_postings_text=GetterSetter(unicode),
    autoresponse_request_text=GetterSetter(unicode),
    archive_policy=GetterSetter(enum_validator(ArchivePolicy)),
    bounces_address=GetterSetter(None),
    collapse_alternatives=GetterSetter(as_boolean),
    convert_html_to_plaintext=GetterSetter(as_boolean),
    created_at=GetterSetter(None),
    default_member_action=GetterSetter(enum_validator(Action)),
    default_nonmember_action=GetterSetter(enum_validator(Action)),
    description=GetterSetter(unicode),
    digest_last_sent_at=GetterSetter(None),
    digest_size_threshold=GetterSetter(float),
    filter_content=GetterSetter(as_boolean),
    fqdn_listname=GetterSetter(None),
    mail_host=GetterSetter(None),
    allow_list_posts=GetterSetter(as_boolean),
    include_rfc2369_headers=GetterSetter(as_boolean),
    join_address=GetterSetter(None),
    last_post_at=GetterSetter(None),
    leave_address=GetterSetter(None),
    list_name=GetterSetter(None),
    next_digest_number=GetterSetter(None),
    no_reply_address=GetterSetter(None),
    owner_address=GetterSetter(None),
    post_id=GetterSetter(None),
    posting_address=GetterSetter(None),
    posting_pipeline=GetterSetter(pipeline_validator),
    display_name=GetterSetter(unicode),
    reply_goes_to_list=GetterSetter(enum_validator(ReplyToMunging)),
    request_address=GetterSetter(None),
    scheme=GetterSetter(None),
    send_welcome_message=GetterSetter(as_boolean),
    subject_prefix=GetterSetter(unicode),
    volume=GetterSetter(None),
    web_host=GetterSetter(None),
    welcome_message_uri=GetterSetter(unicode),
    )


VALIDATORS = ATTRIBUTES.copy()
for attribute, gettersetter in VALIDATORS.items():
    if gettersetter.decoder is None:
        del VALIDATORS[attribute]



class ListConfiguration(resource.Resource):
    """A mailing list configuration resource."""

    def __init__(self, mailing_list, attribute):
        self._mlist = mailing_list
        self._attribute = attribute

    @resource.GET()
    def get_configuration(self, request):
        """Get a mailing list configuration."""
        resource = {}
        if self._attribute is None:
            # Return all readable attributes.
            for attribute in ATTRIBUTES:
                value = ATTRIBUTES[attribute].get(self._mlist, attribute)
                resource[attribute] = value
        elif self._attribute not in ATTRIBUTES:
            return http.bad_request(
                [], b'Unknown attribute: {0}'.format(self._attribute))
        else:
            attribute = self._attribute
            value = ATTRIBUTES[attribute].get(self._mlist, attribute)
            resource[attribute] = value
        return http.ok([], etag(resource))

    @resource.PUT()
    def put_configuration(self, request):
        """Set a mailing list configuration."""
        attribute = self._attribute
        if attribute is None:
            validator = Validator(**VALIDATORS)
            try:
                validator.update(self._mlist, request)
            except ValueError as error:
                return http.bad_request([], str(error))
        elif attribute not in ATTRIBUTES:
            return http.bad_request(
                [], b'Unknown attribute: {0}'.format(attribute))
        elif ATTRIBUTES[attribute].decoder is None:
            return http.bad_request(
                [], b'Read-only attribute: {0}'.format(attribute))
        else:
            validator = Validator(**{attribute: VALIDATORS[attribute]})
            try:
                validator.update(self._mlist, request)
            except ValueError as error:
                return http.bad_request([], str(error))
        return no_content()

    @PATCH()
    def patch_configuration(self, request):
        """Patch the configuration (i.e. partial update)."""
        try:
            validator = PatchValidator(request, ATTRIBUTES)
        except UnknownPATCHRequestError as error:
            return http.bad_request(
                [], b'Unknown attribute: {0}'.format(error.attribute))
        except ReadOnlyPATCHRequestError as error:
            return http.bad_request(
                [], b'Read-only attribute: {0}'.format(error.attribute))
        try:
            validator.update(self._mlist, request)
        except ValueError as error:
            return http.bad_request([], str(error))
        return no_content()