]> www.average.org Git - pdns-pipe-nmc.git/blobdiff - NmcJson.hs
add .gitignore
[pdns-pipe-nmc.git] / NmcJson.hs
index c92e363ce4b6a6f5d92e10f25806c8e58e45adf3..69e693ff000e7ea5aafb986e5c45f017a1c60d05 100644 (file)
@@ -7,6 +7,9 @@ module NmcJson  ( NmcRes(..)
                 ) where
 
 import Data.ByteString.Lazy (ByteString)
+import Data.Text as T (unpack)
+import Data.List.Split
+import Data.Char
 import Data.Map as M (Map, lookup)
 import Control.Applicative ((<$>), (<*>), empty)
 import Data.Aeson
@@ -65,6 +68,19 @@ data NmcDom = NmcDom    { domService     :: Maybe [[String]] -- [NmcRRService]
                         } deriving (Show, Eq)
 
 instance FromJSON NmcDom where
+        -- Wherever we expect a domain object, there may be a string
+        -- containing IPv4 address. Interpret it as such.
+        -- Question: shall we try to recognize IPv6 addresses too?
+        parseJSON (String s) =
+                 return $ if isIPv4 s'
+                            then emptyNmcDom { domIp = Just [s'] }
+                            else emptyNmcDom
+                          where
+                            s' = T.unpack s
+                            isIPv4 x = all isNibble $ splitOn "." x
+                            isNibble x =
+                              if all isDigit x then (read x :: Int) < 256
+                              else False
         parseJSON (Object o) = NmcDom
                 <$> o .:? "service"
                 <*> o .:? "ip"