ResponseCode.hs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SMTP.ResponseCode where
  3. import Data.ByteString (ByteString)
  4. import qualified Data.ByteString as BS
  5. import Data.SMTP.Response
  6. import Text.StringConvert
  7. data ResponseCode =
  8. Unrecognized -- ^ Generic unrecognized command error
  9. | InvalidHost -- ^ Invalid host at HELO or EHLO argument
  10. | InvalidArguments ByteString -- ^ Generic invalid argument error
  11. | InvalidSetOfArguments -- ^ Invalid argument sequence error
  12. | InvalidEmail ByteString -- ^ @InvalidEmail m@ means @m@ is not in the correct format for an email address
  13. | Timeout -- ^ Timeout at server or client
  14. | NotImplemented -- ^ Command not implemented
  15. | BadSequence -- ^ Bad sequences of commands
  16. | MailboxUnavailable String -- ^ @MailbxUnavailable m@ means m has the correct format, but does not exist
  17. | TLSNotAvailable -- ^ Server can not do TLS
  18. | TLSNoSecurity -- ^ The agreed TLS parameters are not good enough
  19. | AuthTypeNotSupported -- ^ Server does not support the given authentication method
  20. | RequiresTls -- ^ This feature is only avilable in TLS connections
  21. | BadAuthCredentials -- ^ Bad authentication credentials
  22. | AuthRequired -- ^ This feature is only available for authenticated users
  23. | Congestion -- ^ Mailsystem congestion error
  24. | BadConnection -- ^ There were connection problems
  25. | NoConversion -- ^ This conversion of email encoding is not supported
  26. | TempUndefined -- ^ Generic undefined temporary error
  27. | InvalidCP -- ^ Capability has an invalid format
  28. deriving (Eq, Ord, Read, Show)
  29. errorMessage :: ResponseCode -> ByteString
  30. errorMessage = renderResponse . toResponse
  31. continueOnError :: ResponseCode -> Bool
  32. continueOnError Timeout = False
  33. continueOnError _ = True
  34. toResponse :: ResponseCode -> Response
  35. toResponse Unrecognized = Response PermanentError [] 500 (Just (5, 5, 1)) "Unrecognized command"
  36. toResponse InvalidHost = Response PermanentError [] 501 (Just (5, 5, 2)) "Invalid hostname"
  37. toResponse (InvalidArguments a) = Response PermanentError [] 502 (Just (5, 5, 4)) $ BS.concat ["Invalid argument: ", a, "."]
  38. toResponse InvalidSetOfArguments = Response PermanentError [] 502 (Just (5, 5, 4)) "Invalid argument sequence."
  39. toResponse (InvalidEmail a) = Response PermanentError [] 501 (Just (5, 1, 3)) $ BS.concat ["Invalid email address: ", a, "."]
  40. toResponse Timeout = Response TransientError [] 421 (Just (4, 2, 1)) "Connection timeout, closing transmission channel."
  41. toResponse NotImplemented = Response PermanentError [] 502 (Just (5, 5, 1)) "Command not implemented"
  42. toResponse BadSequence = Response PermanentError [] 503 (Just (5, 5, 1)) "Bad sequence of commands"
  43. toResponse (MailboxUnavailable a) = Response PermanentError [] 551 (Just (5, 5, 1)) $ BS.concat ["Nope, don't know ", s a, "."]
  44. toResponse TLSNotAvailable = Response TransientError [] 454 (Just (4, 7, 0)) "TLS not available due to temporary reason."
  45. toResponse TLSNoSecurity = Response PermanentError [] 554 (Just (5, 7, 0)) "Get a non-broken TLS lib."
  46. toResponse AuthTypeNotSupported = Response PermanentError [] 504 (Just (5, 5, 4)) "Autentication mechanism is not supported."
  47. toResponse RequiresTls = Response PermanentError [] 538 (Just (5, 7, 11)) "StartTLS before this authentication."
  48. toResponse BadAuthCredentials = Response PermanentError [] 535 (Just (5, 7, 8)) "Bad credentials."
  49. toResponse AuthRequired = Response PermanentError [] 530 (Just (5, 7, 0)) "You must be authenticated."
  50. toResponse Congestion = Response TransientError [] 451 (Just (4, 4, 5)) "Mail system congestion."
  51. toResponse BadConnection = Response TransientError [] 421 (Just (4, 4, 2)) "Connection problems."
  52. toResponse NoConversion = Response PermanentError [] 554 (Just (5, 6, 1)) "Conversion is not supported."
  53. toResponse TempUndefined = Response TransientError [] 451 (Just (4, 2, 0)) "Undefined mailsystem error."
  54. toResponse InvalidCP = Response PermanentError [] 500 (Just (5, 7, 7)) "Capability corrupted or invalid."