X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=blobdiff_plain;f=NmcTransform.hs;h=c461274acdae54ab4eb091f2c3cc9f35bd8ee737;hp=3cd37dfe314e46cbdb09918c12fb5f646bf6ef71;hb=444411783cdc992b99a6d9fe0e0a5922686d1931;hpb=aa91db8940672a57169b1bb400fa3c1a1e9dd335 diff --git a/NmcTransform.hs b/NmcTransform.hs index 3cd37df..c461274 100644 --- a/NmcTransform.hs +++ b/NmcTransform.hs @@ -4,7 +4,6 @@ module NmcTransform ( seedNmcDom import Prelude hiding (lookup) import Data.ByteString.Lazy (ByteString) -import Data.Text.Lazy (splitOn, pack, unpack) import Data.Map.Lazy (empty, lookup, delete, size, singleton , foldrWithKey, insert, insertWith) import Control.Monad (foldM) @@ -39,7 +38,7 @@ mergeIncl :: -> IO (Either String NmcDom) -- ^ result with merged import mergeIncl queryOp depth base = do let - mbase = (expandSrv . splitSubdoms . mergeSelf) base + mbase = mergeSelf base base' = mbase {domDelegate = Nothing, domImport = Nothing} -- print base if depth <= 0 then return $ Left "Nesting of imports is too deep" @@ -53,15 +52,15 @@ mergeIncl queryOp depth base = do sub <- queryNmcDom queryOp key case sub of Left err -> return $ Left err - Right sub' -> mergeIncl queryOp (depth - 1) $ sub' `mergeNmcDom` acc + Right sub' -> mergeIncl queryOp (depth - 1) $ sub' `merge` acc -- | If there is an element in the map with key "", merge the contents -- and remove this element. Do this recursively. mergeSelf :: NmcDom -> NmcDom mergeSelf base = let - map = domMap base - base' = base {domMap = removeSelf map} + map = domSubmap base + base' = base {domSubmap = removeSelf map} removeSelf Nothing = Nothing removeSelf (Just map) = if size map' == 0 then Nothing else Just map' where map' = delete "" map @@ -71,59 +70,12 @@ mergeSelf base = Just map' -> case lookup "" map' of Nothing -> base' - Just sub -> (mergeSelf sub) `mergeNmcDom` base' + Just sub -> (mergeSelf sub) `merge` base' -- recursion depth limited by the size of the record --- | replace Service with Srv down in the Map -expandSrv :: NmcDom -> NmcDom -expandSrv base = - let - base' = base { domService = Nothing } - in - case domService base of - Nothing -> base' - Just sl -> foldr addSrvMx base' sl - where - addSrvMx sr acc = sub1 `mergeNmcDom` acc - where - sub1 = def { domMap = Just (singleton proto sub2) - , domMx = maybemx} - sub2 = def { domMap = Just (singleton srvid sub3) } - sub3 = def { domSrv = Just [srvStr] } - proto = "_" ++ (srvProto sr) - srvid = "_" ++ (srvName sr) - srvStr = (show (srvPrio sr)) ++ "\t" - ++ (show (srvWeight sr)) ++ " " - ++ (show (srvPort sr)) ++ " " - ++ (srvHost sr) - maybemx = - if srvName sr == "smtp" - && srvProto sr == "tcp" - && srvPort sr == 25 - then Just [(show (srvPrio sr)) ++ "\t" ++ (srvHost sr)] - else Nothing - --- | Convert map elements of the form "subN...sub2.sub1.dom.bit" --- into nested map and merge it -splitSubdoms :: NmcDom -> NmcDom -splitSubdoms base = - let - base' = base { domMap = Nothing } - in - case domMap base of - Nothing -> base' - Just sdmap -> (def { domMap = Just sdmap' }) `mergeNmcDom` base' - where - sdmap' = foldrWithKey stow empty sdmap - stow fqdn sdom acc = insertWith mergeNmcDom fqdn' sdom' acc - where - (fqdn', sdom') = - nest (filter (/= "") (splitOnDots fqdn), sdom) - splitOnDots s = map unpack (splitOn (pack ".") (pack s)) - nest ([], v) = (fqdn, v) -- can split result be empty? - nest ([k], v) = (k, v) - nest (k:ks, v) = - nest (ks, def { domMap = Just (singleton k v) }) +-- | transfer some elements of `base` into `sub`, notably TLSA +propagate :: NmcDom -> NmcDom -> NmcDom +propagate base sub = sub -- FIXME implement it -- | Presence of some elements require removal of some others normalizeDom :: NmcDom -> NmcDom @@ -136,7 +88,7 @@ normalizeDom dom = foldr id dom [ translateNormalizer Just ns -> def { domNs = domNs dom, domEmail = domEmail dom } translateNormalizer dom = case domTranslate dom of Nothing -> dom - Just tr -> dom { domMap = Nothing } + Just tr -> dom { domSubmap = Nothing } -- | Merge imports and Selfs and follow the maps tree to get dom descendNmcDom :: @@ -152,12 +104,12 @@ descendNmcDom queryOp subdom base = do case base' of Left err -> return base' Right base'' -> - case domMap base'' of + case domSubmap base'' of Nothing -> return $ Right def Just map -> case lookup d map of Nothing -> return $ Right def - Just sub -> descendNmcDom queryOp ds sub + Just sub -> descendNmcDom queryOp ds $ propagate base'' sub -- | Initial NmcDom populated with "import" only, suitable for "descend" seedNmcDom ::