{-# LINE 1 "src/Network/Email/Check.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LINE 2 "src/Network/Email/Check.hsc" #-}

module Network.Email.Check where

import Foreign.C.Types
import Text.Email.Validate
import Foreign.C


{-# LINE 10 "src/Network/Email/Check.hsc" #-}

foreign import ccall "check-mx.h check_mx" checkMx
    :: CString -> IO CInt

-- | Check to see whether an email is (1) RFC-valid and (2) has an
-- existant MX record.
check :: String -> IO (Either String EmailAddress)
check email = do
  case validate email of
    Left e -> return $ Left $ show e
    Right e -> do exists <- withCString (domainPart e) checkMx 
                  if exists /= 0
                     then return $ Right e
                     else return $ Left $ "no MX record exists for domain "
                                          ++ (domainPart e)