_except.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Exceptions in L{twisted.mail}.
  5. """
  6. from __future__ import absolute_import, division
  7. from twisted.python.compat import _PY3, unicode
  8. class IMAP4Exception(Exception):
  9. pass
  10. class IllegalClientResponse(IMAP4Exception):
  11. pass
  12. class IllegalOperation(IMAP4Exception):
  13. pass
  14. class IllegalMailboxEncoding(IMAP4Exception):
  15. pass
  16. class MailboxException(IMAP4Exception):
  17. pass
  18. class MailboxCollision(MailboxException):
  19. def __str__(self):
  20. return 'Mailbox named %s already exists' % self.args
  21. class NoSuchMailbox(MailboxException):
  22. def __str__(self):
  23. return 'No mailbox named %s exists' % self.args
  24. class ReadOnlyMailbox(MailboxException):
  25. def __str__(self):
  26. return 'Mailbox open in read-only state'
  27. class UnhandledResponse(IMAP4Exception):
  28. pass
  29. class NegativeResponse(IMAP4Exception):
  30. pass
  31. class NoSupportedAuthentication(IMAP4Exception):
  32. def __init__(self, serverSupports, clientSupports):
  33. IMAP4Exception.__init__(
  34. self, 'No supported authentication schemes available')
  35. self.serverSupports = serverSupports
  36. self.clientSupports = clientSupports
  37. def __str__(self):
  38. return (IMAP4Exception.__str__(self)
  39. + ': Server supports %r, client supports %r'
  40. % (self.serverSupports, self.clientSupports))
  41. class IllegalServerResponse(IMAP4Exception):
  42. pass
  43. class IllegalIdentifierError(IMAP4Exception):
  44. pass
  45. class IllegalQueryError(IMAP4Exception):
  46. pass
  47. class MismatchedNesting(IMAP4Exception):
  48. pass
  49. class MismatchedQuoting(IMAP4Exception):
  50. pass
  51. class SMTPError(Exception):
  52. pass
  53. class SMTPClientError(SMTPError):
  54. """
  55. Base class for SMTP client errors.
  56. """
  57. def __init__(self, code, resp, log=None, addresses=None, isFatal=False,
  58. retry=False):
  59. """
  60. @param code: The SMTP response code associated with this error.
  61. @param resp: The string response associated with this error.
  62. @param log: A string log of the exchange leading up to and including
  63. the error.
  64. @type log: L{bytes}
  65. @param isFatal: A boolean indicating whether this connection can
  66. proceed or not. If True, the connection will be dropped.
  67. @param retry: A boolean indicating whether the delivery should be
  68. retried. If True and the factory indicates further retries are
  69. desirable, they will be attempted, otherwise the delivery will be
  70. failed.
  71. """
  72. self.code = code
  73. self.resp = resp
  74. self.log = log
  75. self.addresses = addresses
  76. self.isFatal = isFatal
  77. self.retry = retry
  78. def __str__(self):
  79. if _PY3:
  80. return self.__bytes__().decode("utf-8")
  81. else:
  82. return self.__bytes__()
  83. def __bytes__(self):
  84. if self.code > 0:
  85. res = [u"{:03d} {}".format(self.code, self.resp)]
  86. else:
  87. res = [self.resp]
  88. if self.log:
  89. res.append(self.log)
  90. res.append(b'')
  91. for (i, r) in enumerate(res):
  92. if isinstance(r, unicode):
  93. res[i] = r.encode('utf-8')
  94. return b'\n'.join(res)
  95. class ESMTPClientError(SMTPClientError):
  96. """
  97. Base class for ESMTP client errors.
  98. """
  99. class EHLORequiredError(ESMTPClientError):
  100. """
  101. The server does not support EHLO.
  102. This is considered a non-fatal error (the connection will not be dropped).
  103. """
  104. class AUTHRequiredError(ESMTPClientError):
  105. """
  106. Authentication was required but the server does not support it.
  107. This is considered a non-fatal error (the connection will not be dropped).
  108. """
  109. class TLSRequiredError(ESMTPClientError):
  110. """
  111. Transport security was required but the server does not support it.
  112. This is considered a non-fatal error (the connection will not be dropped).
  113. """
  114. class AUTHDeclinedError(ESMTPClientError):
  115. """
  116. The server rejected our credentials.
  117. Either the username, password, or challenge response
  118. given to the server was rejected.
  119. This is considered a non-fatal error (the connection will not be
  120. dropped).
  121. """
  122. class AuthenticationError(ESMTPClientError):
  123. """
  124. An error occurred while authenticating.
  125. Either the server rejected our request for authentication or the
  126. challenge received was malformed.
  127. This is considered a non-fatal error (the connection will not be
  128. dropped).
  129. """
  130. class SMTPTLSError(ESMTPClientError):
  131. """
  132. An error occurred while negiotiating for transport security.
  133. This is considered a non-fatal error (the connection will not be dropped).
  134. """
  135. class SMTPConnectError(SMTPClientError):
  136. """
  137. Failed to connect to the mail exchange host.
  138. This is considered a fatal error. A retry will be made.
  139. """
  140. def __init__(self, code, resp, log=None, addresses=None, isFatal=True,
  141. retry=True):
  142. SMTPClientError.__init__(self, code, resp, log, addresses, isFatal,
  143. retry)
  144. class SMTPTimeoutError(SMTPClientError):
  145. """
  146. Failed to receive a response from the server in the expected time period.
  147. This is considered a fatal error. A retry will be made.
  148. """
  149. def __init__(self, code, resp, log=None, addresses=None, isFatal=True,
  150. retry=True):
  151. SMTPClientError.__init__(self, code, resp, log, addresses, isFatal,
  152. retry)
  153. class SMTPProtocolError(SMTPClientError):
  154. """
  155. The server sent a mangled response.
  156. This is considered a fatal error. A retry will not be made.
  157. """
  158. def __init__(self, code, resp, log=None, addresses=None, isFatal=True,
  159. retry=False):
  160. SMTPClientError.__init__(self, code, resp, log, addresses, isFatal,
  161. retry)
  162. class SMTPDeliveryError(SMTPClientError):
  163. """
  164. Indicates that a delivery attempt has had an error.
  165. """
  166. class SMTPServerError(SMTPError):
  167. def __init__(self, code, resp):
  168. self.code = code
  169. self.resp = resp
  170. def __str__(self):
  171. return "%.3d %s" % (self.code, self.resp)
  172. class SMTPAddressError(SMTPServerError):
  173. def __init__(self, addr, code, resp):
  174. from twisted.mail.smtp import Address
  175. SMTPServerError.__init__(self, code, resp)
  176. self.addr = Address(addr)
  177. def __str__(self):
  178. return "%.3d <%s>... %s" % (self.code, self.addr, self.resp)
  179. class SMTPBadRcpt(SMTPAddressError):
  180. def __init__(self, addr, code=550,
  181. resp='Cannot receive for specified address'):
  182. SMTPAddressError.__init__(self, addr, code, resp)
  183. class SMTPBadSender(SMTPAddressError):
  184. def __init__(self, addr, code=550, resp='Sender not acceptable'):
  185. SMTPAddressError.__init__(self, addr, code, resp)
  186. class AddressError(SMTPError):
  187. """
  188. Parse error in address
  189. """
  190. class POP3Error(Exception):
  191. """
  192. The base class for POP3 errors.
  193. """
  194. pass
  195. class _POP3MessageDeleted(Exception):
  196. """
  197. An internal control-flow error which indicates that a deleted message was
  198. requested.
  199. """
  200. class POP3ClientError(Exception):
  201. """
  202. The base class for all exceptions raised by POP3Client.
  203. """
  204. class InsecureAuthenticationDisallowed(POP3ClientError):
  205. """
  206. An error indicating secure authentication was required but no mechanism
  207. could be found.
  208. """
  209. class TLSError(POP3ClientError):
  210. """
  211. An error indicating secure authentication was required but either the
  212. transport does not support TLS or no TLS context factory was supplied.
  213. """
  214. class TLSNotSupportedError(POP3ClientError):
  215. """
  216. An error indicating secure authentication was required but the server does
  217. not support TLS.
  218. """
  219. class ServerErrorResponse(POP3ClientError):
  220. """
  221. An error indicating that the server returned an error response to a
  222. request.
  223. @ivar consumer: See L{__init__}
  224. """
  225. def __init__(self, reason, consumer=None):
  226. """
  227. @type reason: L{bytes}
  228. @param reason: The server response minus the status indicator.
  229. @type consumer: callable that takes L{object}
  230. @param consumer: The function meant to handle the values for a
  231. multi-line response.
  232. """
  233. POP3ClientError.__init__(self, reason)
  234. self.consumer = consumer
  235. class LineTooLong(POP3ClientError):
  236. """
  237. An error indicating that the server sent a line which exceeded the
  238. maximum line length (L{LineOnlyReceiver.MAX_LENGTH}).
  239. """