ResponseCode.hs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. data ResponseCode = Unrecognized | InvalidHost | InvalidArguments {argumentError :: ByteString} |
  7. InvalidSetOfArguments |
  8. InvalidEmail {invalidEmailAddress :: ByteString} | Timeout | NotImplemented |
  9. BadSequence | MailboxUnavailable {unavailableMailbox :: ByteString} | TLSNotAvailable |
  10. TLSNoSecurity | AuthTypeNotSupported | RequiresTls | BadAuthCredentials |
  11. AuthRequired | Congestion | BadConnection | NoConversion | TempUndefined |
  12. InvalidCP deriving (Eq, Ord, Read, Show)
  13. errorMessage :: ResponseCode -> ByteString
  14. errorMessage = renderResponse . toResponse
  15. continueOnError :: ResponseCode -> Bool
  16. continueOnError Timeout = False
  17. continueOnError _ = True
  18. toResponse :: ResponseCode -> Response
  19. toResponse Unrecognized = Response PermanentError [] 500 (Just (5, 5, 1)) "Unrecognized command"
  20. toResponse InvalidHost = Response PermanentError [] 501 (Just (5, 5, 2)) "Invalid hostname"
  21. toResponse (InvalidArguments a) = Response PermanentError [] 502 (Just (5, 5, 4)) $ BS.concat ["Invalid argument: ", a, "."]
  22. toResponse InvalidSetOfArguments = Response PermanentError [] 502 (Just (5, 5, 4)) "Invalid argument sequence."
  23. toResponse (InvalidEmail a) = Response PermanentError [] 501 (Just (5, 1, 3)) $ BS.concat ["Invalid email address: ", a, "."]
  24. toResponse Timeout = Response TransientError [] 421 (Just (4, 2, 1)) "Connection timeout, closing transmission channel."
  25. toResponse NotImplemented = Response PermanentError [] 502 (Just (5, 5, 1)) "Command not implemented"
  26. toResponse BadSequence = Response PermanentError [] 503 (Just (5, 5, 1)) "Bad sequence of commands"
  27. toResponse (MailboxUnavailable a) = Response PermanentError [] 551 (Just (5, 5, 1)) $ BS.concat ["Nope, don't know ", a, "."]
  28. toResponse TLSNotAvailable = Response TransientError [] 454 (Just (4, 7, 0)) "TLS not available due to temporary reason."
  29. toResponse TLSNoSecurity = Response PermanentError [] 554 (Just (5, 7, 0)) "Get a non-broken TLS lib."
  30. toResponse AuthTypeNotSupported = Response PermanentError [] 504 (Just (5, 5, 4)) "Autentication mechanism is not supported."
  31. toResponse RequiresTls = Response PermanentError [] 538 (Just (5, 7, 11)) "StartTLS before this authentication."
  32. toResponse BadAuthCredentials = Response PermanentError [] 535 (Just (5, 7, 8)) "Bad credentials."
  33. toResponse AuthRequired = Response PermanentError [] 530 (Just (5, 7, 0)) "You must be authenticated."
  34. toResponse Congestion = Response TransientError [] 451 (Just (4, 4, 5)) "Mail system congestion."
  35. toResponse BadConnection = Response TransientError [] 421 (Just (4, 4, 2)) "Connection problems."
  36. toResponse NoConversion = Response PermanentError [] 554 (Just (5, 6, 1)) "Conversion is not supported."
  37. toResponse TempUndefined = Response TransientError [] 451 (Just (4, 2, 0)) "Undefined mailsystem error."
  38. toResponse InvalidCP = Response PermanentError [] 500 (Just (5, 7, 7)) "Capability corrupted or invalid."