aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/mail/_except.py
blob: ce01aedeb5200c00df4c9b938ec0f2f244d5c68e (plain) (blame)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Exceptions in L{twisted.mail}.
"""

from typing import Optional


class IMAP4Exception(Exception):
    pass


class IllegalClientResponse(IMAP4Exception):
    pass


class IllegalOperation(IMAP4Exception):
    pass


class IllegalMailboxEncoding(IMAP4Exception):
    pass


class MailboxException(IMAP4Exception):
    pass


class MailboxCollision(MailboxException):
    def __str__(self) -> str:
        return "Mailbox named %s already exists" % self.args


class NoSuchMailbox(MailboxException):
    def __str__(self) -> str:
        return "No mailbox named %s exists" % self.args


class ReadOnlyMailbox(MailboxException):
    def __str__(self) -> str:
        return "Mailbox open in read-only state"


class UnhandledResponse(IMAP4Exception):
    pass


class NegativeResponse(IMAP4Exception):
    pass


class NoSupportedAuthentication(IMAP4Exception):
    def __init__(self, serverSupports, clientSupports):
        IMAP4Exception.__init__(self, "No supported authentication schemes available")
        self.serverSupports = serverSupports
        self.clientSupports = clientSupports

    def __str__(self) -> str:
        return IMAP4Exception.__str__(
            self
        ) + ": Server supports {!r}, client supports {!r}".format(
            self.serverSupports,
            self.clientSupports,
        )


class IllegalServerResponse(IMAP4Exception):
    pass


class IllegalIdentifierError(IMAP4Exception):
    pass


class IllegalQueryError(IMAP4Exception):
    pass


class MismatchedNesting(IMAP4Exception):
    pass


class MismatchedQuoting(IMAP4Exception):
    pass


class SMTPError(Exception):
    pass


class SMTPClientError(SMTPError):
    """
    Base class for SMTP client errors.
    """

    def __init__(
        self,
        code: int,
        resp: bytes,
        log: Optional[bytes] = None,
        addresses: Optional[object] = None,
        isFatal: bool = False,
        retry: bool = False,
    ):
        """
        @param code: The SMTP response code associated with this error.
        @param resp: The string response associated with this error.
        @param log: A string log of the exchange leading up to and including
            the error.
        @param isFatal: A boolean indicating whether this connection can
            proceed or not. If True, the connection will be dropped.
        @param retry: A boolean indicating whether the delivery should be
            retried. If True and the factory indicates further retries are
            desirable, they will be attempted, otherwise the delivery will be
            failed.
        """
        if isinstance(resp, str):  # type: ignore[unreachable]
            resp = resp.encode("utf-8")  # type: ignore[unreachable]

        if isinstance(log, str):
            log = log.encode("utf-8")  # type: ignore[unreachable]

        self.code = code
        self.resp = resp
        self.log = log
        self.addresses = addresses
        self.isFatal = isFatal
        self.retry = retry

    def __str__(self) -> str:
        return self.__bytes__().decode("utf-8")

    def __bytes__(self) -> bytes:
        if self.code > 0:
            res = [f"{self.code:03d} ".encode() + self.resp]
        else:
            res = [self.resp]
        if self.log:
            res.append(self.log)
            res.append(b"")
        return b"\n".join(res)


class ESMTPClientError(SMTPClientError):
    """
    Base class for ESMTP client errors.
    """


class EHLORequiredError(ESMTPClientError):
    """
    The server does not support EHLO.

    This is considered a non-fatal error (the connection will not be dropped).
    """


class AUTHRequiredError(ESMTPClientError):
    """
    Authentication was required but the server does not support it.

    This is considered a non-fatal error (the connection will not be dropped).
    """


class TLSRequiredError(ESMTPClientError):
    """
    Transport security was required but the server does not support it.

    This is considered a non-fatal error (the connection will not be dropped).
    """


class AUTHDeclinedError(ESMTPClientError):
    """
    The server rejected our credentials.

    Either the username, password, or challenge response
    given to the server was rejected.

    This is considered a non-fatal error (the connection will not be
    dropped).
    """


class AuthenticationError(ESMTPClientError):
    """
    An error occurred while authenticating.

    Either the server rejected our request for authentication or the
    challenge received was malformed.

    This is considered a non-fatal error (the connection will not be
    dropped).
    """


class SMTPTLSError(ESMTPClientError):
    """
    An error occurred while negiotiating for transport security.

    This is considered a non-fatal error (the connection will not be dropped).
    """


class SMTPConnectError(SMTPClientError):
    """
    Failed to connect to the mail exchange host.

    This is considered a fatal error.  A retry will be made.
    """

    def __init__(self, code, resp, log=None, addresses=None, isFatal=True, retry=True):
        SMTPClientError.__init__(self, code, resp, log, addresses, isFatal, retry)


class SMTPTimeoutError(SMTPClientError):
    """
    Failed to receive a response from the server in the expected time period.

    This is considered a fatal error.  A retry will be made.
    """

    def __init__(self, code, resp, log=None, addresses=None, isFatal=True, retry=True):
        SMTPClientError.__init__(self, code, resp, log, addresses, isFatal, retry)


class SMTPProtocolError(SMTPClientError):
    """
    The server sent a mangled response.

    This is considered a fatal error.  A retry will not be made.
    """

    def __init__(self, code, resp, log=None, addresses=None, isFatal=True, retry=False):
        SMTPClientError.__init__(self, code, resp, log, addresses, isFatal, retry)


class SMTPDeliveryError(SMTPClientError):
    """
    Indicates that a delivery attempt has had an error.
    """


class SMTPServerError(SMTPError):
    def __init__(self, code, resp):
        self.code = code
        self.resp = resp

    def __str__(self) -> str:
        return "%.3d %s" % (self.code, self.resp)


class SMTPAddressError(SMTPServerError):
    def __init__(self, addr, code, resp):
        from twisted.mail.smtp import Address

        SMTPServerError.__init__(self, code, resp)
        self.addr = Address(addr)

    def __str__(self) -> str:
        return "%.3d <%s>... %s" % (self.code, self.addr, self.resp)


class SMTPBadRcpt(SMTPAddressError):
    def __init__(self, addr, code=550, resp="Cannot receive for specified address"):
        SMTPAddressError.__init__(self, addr, code, resp)


class SMTPBadSender(SMTPAddressError):
    def __init__(self, addr, code=550, resp="Sender not acceptable"):
        SMTPAddressError.__init__(self, addr, code, resp)


class AddressError(SMTPError):
    """
    Parse error in address
    """


class POP3Error(Exception):
    """
    The base class for POP3 errors.
    """

    pass


class _POP3MessageDeleted(Exception):
    """
    An internal control-flow error which indicates that a deleted message was
    requested.
    """


class POP3ClientError(Exception):
    """
    The base class for all exceptions raised by POP3Client.
    """


class InsecureAuthenticationDisallowed(POP3ClientError):
    """
    An error indicating secure authentication was required but no mechanism
    could be found.
    """


class TLSError(POP3ClientError):
    """
    An error indicating secure authentication was required but either the
    transport does not support TLS or no TLS context factory was supplied.
    """


class TLSNotSupportedError(POP3ClientError):
    """
    An error indicating secure authentication was required but the server does
    not support TLS.
    """


class ServerErrorResponse(POP3ClientError):
    """
    An error indicating that the server returned an error response to a
    request.

    @ivar consumer: See L{__init__}
    """

    def __init__(self, reason, consumer=None):
        """
        @type reason: L{bytes}
        @param reason: The server response minus the status indicator.

        @type consumer: callable that takes L{object}
        @param consumer: The function meant to handle the values for a
            multi-line response.
        """
        POP3ClientError.__init__(self, reason)
        self.consumer = consumer


class LineTooLong(POP3ClientError):
    """
    An error indicating that the server sent a line which exceeded the
    maximum line length (L{LineOnlyReceiver.MAX_LENGTH}).
    """