+
+
+-- | Takes a list of domain names represented as 'String's and
+-- constructs a map from domain names to sets of mail exchangers
+-- (for those domain names) also represented as 'String's.
+--
+-- During construction, we have to switch to the DNS internal
+-- representation of a 'Domain' which uses ByteStrings, but before
+-- we return the map to the client, we want everything to be in
+-- terms of standard 'String's for comparison purposes.
+--
+-- The list of domains is normalized and de-duped before lookups are
+-- performed to avoid doing lookups twice for identical domains.
+--
+mx_set_map :: [String] -> IO MxSetMap
+mx_set_map domains = do
+ -- Construct a list of pairs.
+ pairs <- mapM make_pair unique_domains
+
+ -- And make a map from the pairs.
+ return $ Map.fromList pairs
+
+ where
+ -- Convert, normalize, and de-dupe the @domains@.
+ unique_domains :: [Domain]
+ unique_domains = nub $ map (normalize . BS.pack) domains
+
+ -- | Convert a string domain name into a pair containing the
+ -- domain name in the first component and a set of its mail
+ -- exchangers (as strings) in the second component.
+ --
+ make_pair :: Domain -> IO (NormalDomain, Set NormalDomain)
+ make_pair domain = do
+ -- Lookup the @domain@'s MX records.
+ mx_list <- lookup_mxs domain
+
+ -- Now convert the MX records *back* to strings, and then to
+ -- NormalDomains
+ let normal_mx_list = map (normalize_string . BS.unpack) mx_list
+
+ -- Convert the list into a set...
+ let normal_mx_set = Set.fromList normal_mx_list
+
+ -- The lookup key.
+ let normal_domain = normalize_string $ BS.unpack domain
+
+ -- Finally, construct the pair and return it.
+ return (normal_domain, normal_mx_set)