X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=blobdiff_plain;f=pdns-pipe-nmc.hs;h=9e4a72930691ab6b58e53e6220c4d6c495c5e4ce;hp=ba6289944877d8e464d70afbe55ab2ba6f65a8b6;hb=b90215dda1c086c7c288ac9d7e3d75e0516f0fd5;hpb=adf1eff6277a0f1fb69f505285a20554a9ecabd6 diff --git a/pdns-pipe-nmc.hs b/pdns-pipe-nmc.hs index ba62899..9e4a729 100644 --- a/pdns-pipe-nmc.hs +++ b/pdns-pipe-nmc.hs @@ -2,45 +2,23 @@ module Main where ---import Control.Applicative import Control.Monad import qualified Data.ByteString.Char8 as C (pack, unpack) import qualified Data.ByteString.Lazy.Char8 as L (pack, unpack) import Data.ByteString.Lazy as BS hiding (reverse, putStrLn) -import Data.ConfigFile -import Data.Either.Utils import Data.List.Split import Data.Aeson (encode, decode, Value(..)) import Network.HTTP.Types import Data.Conduit import Network.HTTP.Conduit -import Data.JsonRpcClient + +import JsonRpcClient +import Config import PowerDns import NmcJson confFile = "/etc/namecoin.conf" --- Config file handling - -data Config = Config { rpcuser :: String - , rpcpassword :: String - , rpchost :: String - , rpcport :: Int - } deriving (Show) - -readConfig :: String -> IO Config -readConfig f = do - cp <- return . forceEither =<< readfile emptyCP f - return (Config { rpcuser = getSetting cp "rpcuser" "" - , rpcpassword = getSetting cp "rpcpassword" "" - , rpchost = getSetting cp "rpchost" "localhost" - , rpcport = getSetting cp "rpcport" 8336 - }) - where - getSetting cp x dfl = case get cp "DEFAULT" x of - Left _ -> dfl - Right x -> x - -- HTTP/JsonRpc interface qReq :: Config -> ByteString -> ByteString -> Request m @@ -65,15 +43,18 @@ qRsp rsp = case parseJsonRpc (responseBody rsp) :: Either JsonRpcError NmcRes of Left jerr -> Left $ "Unparseable response: " ++ (show (responseBody rsp)) Right jrsp -> - case decode (resValue jrsp) :: Maybe NmcDom of - Nothing -> Left $ "Unparseable value: " ++ (show (resValue jrsp)) - Just dom -> Right dom + case resValue jrsp of + "" -> Right emptyNmcDom + vstr -> + case decode vstr :: Maybe NmcDom of + Nothing -> Left $ "Unparseable value: " ++ (show vstr) + Just dom -> Right dom -- NMC interface -queryNmc :: Manager -> Config -> String -> RRType -> String +queryNmc :: Manager -> Config -> String -> String -> IO (Either String NmcDom) -queryNmc mgr cfg fqdn qtype qid = do +queryNmc mgr cfg fqdn qid = do case reverse (splitOn "." fqdn) of "bit":dn:xs -> do rsp <- runResourceT $ @@ -105,18 +86,17 @@ main = do putStrLn $ "OK\tDnsNmc ready to serve, protocol v." ++ (show ver) mgr <- newManager def - - print $ qReq cfg "d/nosuchdomain" "query-nmc" - rsp <- runResourceT $ httpLbs (qReq cfg "d/nosuchdomain" "query-nmc") mgr - print $ (statusCode . responseStatus) rsp - putStrLn "===== complete response is:" - print rsp - let rbody = responseBody rsp - putStrLn "===== response body is:" - print rbody - let result = parseJsonRpc rbody :: Either JsonRpcError NmcRes - putStrLn "===== parsed response is:" - print result --- print $ parseJsonRpc (responseBody rsp) - - --forever $ getLine >>= (pdnsOut uri) . (pdnsParse ver) + forever $ do + l <- getLine + case pdnsParse ver l of + Left e -> putStrLn $ "ERROR\t" ++ e + Right preq -> do + case preq of + PdnsRequestQ qname qtype id _ _ _ -> do + ncres <- queryNmc mgr cfg qname id + case ncres of + Left e -> putStrLn $ "ERROR\t" ++ e + Right dom -> putStrLn $ pdnsOut qtype dom + PdnsRequestAXFR xfrreq -> + putStrLn ("ERROR\tNo support for AXFR " ++ xfrreq) + PdnsRequestPing -> putStrLn "OK"