ResponseCode.hs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. | BadSeal -- ^ Seal data is invalid
  29. deriving (Eq, Ord, Read, Show)
  30. errorMessage :: ResponseCode -> ByteString
  31. errorMessage = renderResponse . toResponse
  32. continueOnError :: ResponseCode -> Bool
  33. continueOnError Timeout = False
  34. continueOnError _ = True
  35. toResponse :: ResponseCode -> Response
  36. toResponse Unrecognized = Response PermanentError [] 500 (Just (5, 5, 1)) "Unrecognized command"
  37. toResponse InvalidHost = Response PermanentError [] 501 (Just (5, 5, 2)) "Invalid hostname"
  38. toResponse (InvalidArguments a) = Response PermanentError [] 502 (Just (5, 5, 4)) $ BS.concat ["Invalid argument: ", a, "."]
  39. toResponse InvalidSetOfArguments = Response PermanentError [] 502 (Just (5, 5, 4)) "Invalid argument sequence."
  40. toResponse (InvalidEmail a) = Response PermanentError [] 501 (Just (5, 1, 3)) $ BS.concat ["Invalid email address: ", a, "."]
  41. toResponse Timeout = Response TransientError [] 421 (Just (4, 2, 1)) "Connection timeout, closing transmission channel."
  42. toResponse NotImplemented = Response PermanentError [] 502 (Just (5, 5, 1)) "Command not implemented"
  43. toResponse BadSequence = Response PermanentError [] 503 (Just (5, 5, 1)) "Bad sequence of commands"
  44. toResponse (MailboxUnavailable a) = Response PermanentError [] 551 (Just (5, 5, 1)) $ BS.concat ["Nope, don't know ", s a, "."]
  45. toResponse TLSNotAvailable = Response TransientError [] 454 (Just (4, 7, 0)) "TLS not available due to temporary reason."
  46. toResponse TLSNoSecurity = Response PermanentError [] 554 (Just (5, 7, 0)) "Get a non-broken TLS lib."
  47. toResponse AuthTypeNotSupported = Response PermanentError [] 504 (Just (5, 5, 4)) "Autentication mechanism is not supported."
  48. toResponse RequiresTls = Response PermanentError [] 538 (Just (5, 7, 11)) "StartTLS before this authentication."
  49. toResponse BadAuthCredentials = Response PermanentError [] 535 (Just (5, 7, 8)) "Bad credentials."
  50. toResponse AuthRequired = Response PermanentError [] 530 (Just (5, 7, 0)) "You must be authenticated."
  51. toResponse Congestion = Response TransientError [] 451 (Just (4, 4, 5)) "Mail system congestion."
  52. toResponse BadConnection = Response TransientError [] 421 (Just (4, 4, 2)) "Connection problems."
  53. toResponse NoConversion = Response PermanentError [] 554 (Just (5, 6, 1)) "Conversion is not supported."
  54. toResponse TempUndefined = Response TransientError [] 451 (Just (4, 2, 0)) "Undefined mailsystem error."
  55. toResponse InvalidCP = Response PermanentError [] 500 (Just (5, 7, 7)) "Capability corrupted or invalid."
  56. toResponse BadSeal = Response PermanentError [] 535 (Just (5, 7, 8)) "Seal data is invalid."