+import Control.Concurrent.ParallelIO.Global (
+ parallelInterleaved,
+ stopGlobalPool )
+import Control.Monad (unless)
+import qualified Data.ByteString.Char8 as BS (
+ getContents,
+ pack,
+ words )
+import Data.List ( (\\), intercalate, sort )
+import Network.DNS (
+ DNSError(NameError),
+ Domain,
+ FileOrNumericHost(RCHostName),
+ ResolvConf(resolvInfo, resolvTimeout),
+ defaultResolvConf,
+ makeResolvSeed,
+ normalize,
+ normalizeCase,
+ withResolver )
+import System.Exit (ExitCode(ExitFailure), exitWith)
+import System.IO (hPutStrLn, stderr)
+
+import CommandLine (
+ Args(NS,MX,delegates,no_append_root,server,timeout),
+ get_args)
+import DNS (
+ LookupResult,
+ lookupMX',
+ lookupNS',
+ resolve_address )
+import ExitCodes (exit_bad_server)
+import Timeout (Timeout(seconds))
+
+
+-- | Given a list of delegates, report results for this
+-- 'LookupResult'.
+--
+-- If there's an empty list in the second component, there were no
+-- query results, so we report that the domain was not delegated. If
+-- there were some results and there are leftovers (after removing
+-- the delegates), we report those as well.
+--
+-- Before processing, all names are normalized using the supplied
+-- function @normalize_function@. Ideally this should be the same
+-- function applied to the user-input names.
+--
+report :: [Domain] -- ^ The list of @delgts@
+ -> (Domain -> Domain) -- ^ Domain name normalization function,
+ -- @normalize_function@.
+ -> LookupResult
+ -> IO ()
+
+-- If the lookup succeeded but there were no results, report that the
+-- domain is not delegated. Note that the behavior of the DNS library
+-- changed with regard to this at some point: we used to get back
+-- a "success," but with an empty list of results. Now a NameError
+-- (which is not actually an error!) is returned.
+report _ _ (d, Left NameError) =
+ putStrLn $ "Domain " ++ (show d) ++ " not delegated."
+
+-- If the lookup resulted in some other DNS error, we just ignore the
+-- whole thing.
+report _ _ (_, Left _) = return ()
+
+-- Otherwise, subtract our delegates from the list of results and
+-- report the leftovers.
+report delgts normalize_function (d, Right raw_hosts) = do
+ let nrml_hosts = map normalize_function raw_hosts
+ -- Sort the leftovers so that we can test the expected output.
+ let leftovers = sort (nrml_hosts \\ delgts)
+ unless (null leftovers) $
+ putStrLn $ "Domain " ++
+ (show d) ++
+ " delegates somewhere else: " ++
+ (join " " (map show leftovers))
+ where
+ -- Create one big string by joining together a list of smaller
+ -- strings and placing a delimiter between them.
+ join :: String -> [String] -> String
+ join delimiter strings = intercalate delimiter strings
+
+