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
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 qn qt id lip rip eip -> do
+ ncres <- queryNmc mgr cfg (qName preq) (qType preq) (iD preq)
+ case ncres of
+ Left e -> putStrLn $ "ERROR\t" ++ e
+ Right dom -> putStrLn $ pdnsOut dom
+ PdnsRequestAXFR xfrreq ->
+ putStrLn ("ERROR\t No support for AXFR " ++ xfrreq)
+ PdnsRequestPing -> putStrLn "OK"